#define BLINKER_WIFI
#include <Blinker.h>
StaticJsonDocument<1024> doc;
char auth[] = "";//改成自己的
char ssid[] = "";
char pswd[] = "";
//B站API网址
String UID = ""; //改成自己的UID
String followerUrl = "http://api.bilibili.com/x/relation/stat?vmid=" + UID; // 粉丝数、关注数
int follower = 0; // 粉丝数
int following = 0; // 关注数
// 新建组件对象
BlinkerButton Button1("btn-abc");
BlinkerNumber Number1("num-abc");
// 按下按键即会执行该函数
void button1_callback(const String & state){
BLINKER_LOG("get button state: ", state);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data){
BLINKER_LOG("Blinker readString: ", data);
}
void setup(){
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
U8G2_begin();
// 初始化有LED的IO
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
}
void loop() {
Blinker.run();
getFollower(followerUrl);
Blinker.delay(10000);
}
void getFollower(String url){//获取 B 站粉丝数
HTTPClient http;
client.setTimeout(10000);
http.begin(url);
int httpCode = http.GET();
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode == 200)
{
Serial.println("Get OK");
String resBuff = http.getString();
Serial.println(resBuff);
DeserializationError error = deserializeJson(doc, resBuff);
// if (error)
// {
// Serial.println(F("deserializeJson() failed: "));
// return;
// }
following = doc["data"]["following"];
Serial.print("following: ");
Serial.println(following);
follower = doc["data"]["follower"];
Serial.print("followers: ");
Serial.println(follower);
}
else
{
Serial.println("[HTTP] GET... failed");
}
http.end();
} |