说明
太久没更新博文了,生活和工作让我迷失了方向,一直都停滞不前。可能是过完年口袋里又没钱了,又想起来要努力了吧,从新开始更新博客内容。
这次给大家带来通过8266 + SSD1306显示屏来显示bilibili粉丝数量,这个其实网上都能找到一大把,但是我还是贡献一篇较为完整的内容吧。
接线图
我买的是下图这种四脚的SSD1306显示屏
接线方式如下:
1 2 3 4
| GND—G VCC—3V SCL—D1 SDA—D2
|
接线图如下:
代码
获取代码之前,需要先去获取B站用户的UID,这个点到UP主的主页,浏览器地址栏就可以看到。
如:https://space.bilibili.com/385237224
,其中【385237224】就是我的bilibili粉丝UID,将代码中的UID改成自己的即可。
注意:代码烧录过程中可能会报缺少库的错误,这些【ArduinoJson
,Adafruit_GFX
,Adafruit_SSD1306
】库是需要自己手动去搜索库文件安装一下就可以了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <ESP8266HTTPClient.h>
#include <WiFiClient.h> #include <ArduinoJson.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <WiFiClientSecureBearSSL.h>
#define SCREEN_WIDTH 128 // OLED显示器宽度(像素) #define SCREEN_HEIGHT 64 // OLED显示器高度(像素) // 初始化SSD1306 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const uint8_t fingerprint[20] = {0x40, 0xaf, 0x00, 0x6b, 0xec, 0x90, 0x22, 0x41, 0x8e, 0xa3, 0xad, 0xfa, 0x1a, 0xe8, 0x25, 0x41, 0x1d, 0x1a, 0x54, 0xb3};
// 这里设置B站的用户ID #define BILIBILI_UID 385237224
ESP8266WiFiMulti WiFiMulti;
void setup() { Serial.begin(115200); if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306初始化失败")); while (true) {}; } display.clearDisplay(); display.setTextColor(WHITE); // 开始连接wifi WiFi.mode(WIFI_STA); WiFiMulti.addAP("你家wifi名称,只支持2.4Gwifi", "你加wifi密码");
}
void loop() { // 等待WiFi连接 if ((WiFiMulti.run() == WL_CONNECTED)) { std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure); client->setFingerprint(fingerprint); client->setInsecure();
HTTPClient http; Serial.print("开始调用接口获取bilibili粉丝接口!\n"); http.begin(*client, "https://api.bilibili.com/x/relation/stat?vmid=" + BILIBILI_UID); int httpCode = http.GET(); // 出错时httpCode将为负数 if (httpCode > 0) { // HTTP标头已发送,服务器响应标头已处理 Serial.printf("[HTTP]code: %d\n", httpCode); if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { String json = http.getString(); Serial.println(json); DynamicJsonDocument doc(2048); deserializeJson(doc, json); // 获取bilibili粉丝数量 long follower = doc["data"]["follower"]; showBilibiliFollower(follower); } } else { Serial.printf("[HTTP] 调用获取bilibili粉丝接口失败,失败原因: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } delay(1000); }
/** 显示bilibili粉丝数量 */ void showBilibiliFollower(long follower) { // 显示 display.clearDisplay(); display.setTextSize(4); display.setCursor(0, 15); display.print(follower); //刷新显示 display.display(); }
|