作了个b站的粉丝显示器,挺好好玩的,记录一下踩过的坑
具体可以参考菜鸟哥的教程,说些容易出错的地方
1.如果单片机是esp8266,0.96寸的应该在有如下东西
#define SDA 4
#define SCL 5
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
// All Boards without Reset of the Display
2.如果要用u8g2.print,记得在初始化的时候加上
u8g2.enableUTF8Print();
3.关于string转const *char
可以参考这篇博客
,我是这样用的。
String str= timeClient.getFormattedTime();
const char* p=str.c_str();
u8g2.drawStr(15,30,p);
有些up主,用时间戳显示时期/星期/时间,挺麻烦的,主要是我time库总是包错,应该是,,类型不对。也一直没有解决,就偷了个懒,只显示时间了啦!
附上测试时候的小代码
#include
// change next line to use with another board/shield
#include
//#include // for WiFi shield
//#include // for WiFi 101 shield or MKR1000
#include
const char *ssid = "@xxxxx";
const char *password = "xxxxxx";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com",60*60*8, 30*60*1000);
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
/* 最好注释调这里,要不然后面会出错
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}*/
timeClient.begin();
}
void loop() {
timeClient.update();
String a ;
a=timeClient.getFormattedTime();
Serial.println(timeClient.getFormattedTime());
Serial.println(a);
delay(1000);
}
至于日期就自己画上区了啦
可以用以下代码爬取b站的很多资料啦,具体可以在串口监视器里面看到,然后就是对这里的数据进行json解析,拿去出我们想要的信息啦
bool getJson()
{
bool r = false;
http.setTimeout(HTTP_TIMEOUT);
//followe
http.begin("http://api.bilibili.com/x/relation/stat?vmid=" + biliuid);
int httpCode = http.GET();
if (httpCode > 0){
if (httpCode == HTTP_CODE_OK){
response = http.getString();
//Serial.println(response);
r = true;
}
}else{
Serial.printf("[HTTP] GET JSON failed, error: %s\n", http.errorToString(httpCode).c_str());
r = false;
}
http.end();
//return r;
//view
http.begin("http://api.bilibili.com/x/space/upstat?mid=" + biliuid);
int httpCodeview = http.GET();
if (httpCodeview > 0){
if (httpCodeview == HTTP_CODE_OK){
responseview = http.getString();
Serial.println(responseview);
r = true;
}
}else{
Serial.printf("[HTTP] GET JSON failed, error: %s\n", http.errorToString(httpCode).c_str());
r = false;
}
http.end();
return r;
}
#include
#include
#include
#include
#include
#include
#include
const char *ssid = "@PHICOMM_68";
const char *password = "13467776878";
#define SDA 4
#define SCL 5
WiFiUDP ntpUDP;
HTTPClient http;
const unsigned long HTTP_TIMEOUT = 5000;
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com",60*60*8, 30*60*1000);
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
const unsigned char xing[] U8X8_PROGMEM = {
0x00, 0x00, 0xF8, 0x0F, 0x08, 0x08, 0xF8, 0x0F, 0x08, 0x08, 0xF8, 0x0F, 0x80, 0x00, 0x88, 0x00,
0xF8, 0x1F, 0x84, 0x00, 0x82, 0x00, 0xF8, 0x0F, 0x80, 0x00, 0x80, 0x00, 0xFE, 0x3F, 0x00, 0x00
}; /*星*/
const unsigned char liu[] U8X8_PROGMEM = {
0x40, 0x00, 0x80, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x00,
0x20, 0x02, 0x20, 0x04, 0x10, 0x08, 0x10, 0x10, 0x08, 0x10, 0x04, 0x20, 0x02, 0x20, 0x00, 0x00
}; /*六*/
//24*24小电视
const unsigned char bilibilitv_24u[] U8X8_PROGMEM = {0x00, 0x00, 0x02, 0x00, 0x00, 0x03, 0x30, 0x00, 0x01, 0xe0, 0x80, 0x01,
0x80, 0xc3, 0x00, 0x00, 0xef, 0x00, 0xff, 0xff, 0xff, 0x03, 0x00, 0xc0, 0xf9, 0xff, 0xdf, 0x09, 0x00, 0xd0, 0x09, 0x00, 0xd0, 0x89, 0xc1,
0xd1, 0xe9, 0x81, 0xd3, 0x69, 0x00, 0xd6, 0x09, 0x91, 0xd0, 0x09, 0xdb, 0xd0, 0x09, 0x7e, 0xd0, 0x0d, 0x00, 0xd0, 0x4d, 0x89, 0xdb, 0xfb,
0xff, 0xdf, 0x03, 0x00, 0xc0, 0xff, 0xff, 0xff, 0x78, 0x00, 0x1e, 0x30, 0x00, 0x0c};
String biliuid = "312309053";
String response;
String responseview;
int follower = 0;
int view = 0;
int times = 0;
void show_bili();
void setup(){
Serial.begin(115200);
u8g2.begin();
u8g2.enableUTF8Print();
WiFi.begin(ssid, password);
/*while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}*/
timeClient.begin();
}
void loop() {
timeClient.update();
if (times == 0){
show_bili();}
if (times==5)
if(getJson()){
if (parseJson(response)){
parseJson1(responseview);
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_unifont_t_chinese2);
u8g2.drawXBMP( 80 , 20 , 24 , 24 , bilibilitv_24u ); //根据你的图片尺寸修改
u8g2.setCursor(0, 10);
u8g2.print("ID:XXXXXX");
u8g2.setCursor(0, 40);
u8g2.print("fans:");
u8g2.setCursor(45, 40);
u8g2.println(follower);
u8g2.setCursor(0, 60);
u8g2.print("view:");
u8g2.setCursor(45, 60);
u8g2.println(view);
u8g2.sendBuffer();
}
}
delay(1000);
times += 1;
if(times >=10){
times=0;
}
}
void show_bili(){
u8g2.clearBuffer(); // 清除内部缓冲区
// choose a suitable font
u8g2.setFont(u8g2_font_wqy15_t_chinese2);
u8g2.setCursor(0, 15);
u8g2.print("2020年3月29日");
String str= timeClient.getFormattedTime();
const char* p=str.c_str();
u8g2.drawStr(15,30,p);
u8g2.setFont(u8g2_font_wqy15_t_chinese2);
u8g2.setCursor(50, 50);
u8g2.print("素衣");
u8g2.drawXBMP( 0, 40, 24 , 24 , bilibilitv_24u );
u8g2.sendBuffer(); // transfer internal memory to the display
delay(1000);
}
bool getJson()
{
bool r = false;
http.setTimeout(HTTP_TIMEOUT);
//followe
http.begin("http://api.bilibili.com/x/relation/stat?vmid=" + biliuid);
int httpCode = http.GET();
if (httpCode > 0){
if (httpCode == HTTP_CODE_OK){
response = http.getString();
//Serial.println(response);
r = true;
}
}else{
Serial.printf("[HTTP] GET JSON failed, error: %s\n", http.errorToString(httpCode).c_str());
r = false;
}
http.end();
//return r;
//view
http.begin("http://api.bilibili.com/x/space/upstat?mid=" + biliuid);
int httpCodeview = http.GET();
if (httpCodeview > 0){
if (httpCodeview == HTTP_CODE_OK){
responseview = http.getString();
Serial.println(responseview);
r = true;
}
}else{
Serial.printf("[HTTP] GET JSON failed, error: %s\n", http.errorToString(httpCode).c_str());
r = false;
}
http.end();
return r;
}
bool parseJson(String json)
{
const size_t capacity = JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + 70;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, json);
int code = doc["code"];
const char *message = doc["message"];
if (code != 0){
Serial.print("[API]Code:");
Serial.print(code);
Serial.print(" Message:");
Serial.println(message);
return false;
}
JsonObject data = doc["data"];
unsigned long data_mid = data["mid"];
int data_follower = data["follower"];
if (data_mid == 0){
Serial.println("[JSON] FORMAT ERROR");
return false;
}
Serial.print("UID: ");
Serial.print(data_mid);
Serial.print(" follower: ");
Serial.println(data_follower);
follower = data_follower;
return true;
}
void parseJson1(String json)
{
const size_t capacity = JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + 70;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, json);
int code = doc["code"];
const char *message = doc["message"];
JsonObject data = doc["data"];
JsonObject archive = data["archive"];
int data_view = archive["view"];
Serial.print("vive: ");
Serial.println(data_view);
view = data_view;
}
参考了很多的资料啦,总的效果图就有时间在放出来啦