帮我写一个用esp32获取B站粉丝数的代码
时间: 2023-08-28 13:14:05 浏览: 170
好的,以下是ESP32获取B站粉丝数的代码:
```c
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* host = "api.bilibili.com";
const char* endpoint = "/x/relation/stat?vmid=your_UID";
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
Serial.print("Connecting to host: ");
Serial.println(host);
http.begin(host, 80, endpoint);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(payload);
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
int followerCount = doc["data"]["follower"].as<int>();
Serial.print("Follower count: ");
Serial.println(followerCount);
} else {
Serial.print("HTTP request failed with code ");
Serial.println(httpCode);
}
http.end();
}
delay(60000); // 每分钟更新一次粉丝数
}
```
在这个代码中,我们使用WiFi库连接到WiFi网络,并使用HTTPClient库向B站API发出GET请求,以获取B站用户的粉丝数。我们将收到的JSON响应解析为JSON对象,并从中提取粉丝计数。最后,我们将粉丝计数打印到串口。此代码每分钟更新一次粉丝数。请将“your_SSID”,“your_PASSWORD”和“your_UID”替换为您的WiFi SSID,密码和B站用户ID。
阅读全文