void WiFi_Connect()
{
WiFi.begin("Wi-Fi", "1234567890");
while (WiFi.status() != WL_CONNECTED)
{
delay(300);
Serial.print(".");
}
}
- 然后是获取B站粉丝数
这里的B站粉丝数主要是利用HTTP get获取网页信息,但是此信息为json格式,所以要利用deserializeJson函数把json格式解析,从而进行截取打印输出。下面是代码:
void getBiliBiliFollower()
{
HTTPClient http;
http.begin(followerUrl);
int httpCode = http.GET();
if (httpCode > 0)
{
Serial.printf("HTTP Get Code: %d\r\n", httpCode);
if (httpCode == HTTP_CODE_OK)
{
String resBuff = http.getString();
Serial.println(resBuff);
deserializeJson(doc, resBuff);
follower = doc["data"]["follower"];
Serial.printf("Follers: %ld \r\n", follower);
}
}
else
{
Serial.printf("HTTP Get Error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include "../lib/ArduinoJson/ArduinoJson.h"
void WiFi_Connect()
{
WiFi.begin("Wi-Fi", "1234567890");
while (WiFi.status() != WL_CONNECTED)
{
delay(300);
Serial.print(".");
}
}
void setup()
{
Serial.begin(115200);
delay(100);
Serial.print("Connecting.. ");
WiFi_Connect();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
String UID = "***";
String followerUrl = "http://api.bilibili.com/x/relation/stat?vmid=" + UID;
long follower = 0;
DynamicJsonDocument doc(1024);
void getBiliBiliFollower()
{
HTTPClient http;
http.begin(followerUrl);
int httpCode = http.GET();
if (httpCode > 0)
{
Serial.printf("HTTP Get Code: %d\r\n", httpCode);
if (httpCode == HTTP_CODE_OK)
{
String resBuff = http.getString();
Serial.println(resBuff);
deserializeJson(doc, resBuff);
follower = doc["data"]["follower"];
Serial.printf("Follers: %ld \r\n", follower);
}
}
else
{
Serial.printf("HTTP Get Error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
void loop()
{
getBiliBiliFollower();
delay(5000);
delay(5000);
}
- 注意
注意自己的lib库中是否包含ArduinoJson文件 - 扩展
同理,我们还可以用此代码进行获取天气情况,与上面类似。
只要找到获取天气的API即可。