使用Blinker及Json V6库获取B站粉丝数
本帖最后由 问题有点大 于 2021-7-2 11:11 编辑本人小白,看到网上的获取B站粉丝数教程基本都是用的Arduino Json V5的库,而V6相对于V5有一定的改动,故尝试用V6库获取 很棒,补一句
StaticJsonDocument<1024> doc;
单片机空间上寸土寸金,这句1024可以在 https://arduinojson.org/v6/assistant/ 上填入json生成,解这个json只要256,省点 #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(" 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(" GET... failed");
}
http.end();
} 这个是串口打印信息
GET... code: 200
Get OK
{"code":0,"message":"0","ttl":1,"data":{"mid":698018684,"following":337,"whisper":0,"black":0,"follower":11880}}
following: 337
followers: 11880 Cp0204 发表于 2021-8-25 15:17
很棒,补一句
StaticJsonDocument doc;
感谢补充
页:
[1]