所选硬件
ESP8266串口wifi模块

MAX7219八位数码管

链接方法

第一步下载CH340G的驱动,下载地址
http://www.wch.cn/downloads/CH341SER_EXE.html
安装完驱动后,链接开发板,可在设备管理器中看到CH340(COM*)字样


第二步,安装Arduino IDE,地址
https://www.arduino.cc/en/Main/Software
安装一直下一步即可
打开Arduino IDE开发环境,点击“文件-首选项”
在“附加开发板管理器网址”中,添加
http://arduino.esp8266.com/stable/package_esp8266com_index.json

接下来点击 工具 - 开发板 - 开发板管理器,等待下载平台索引,
接着在搜索框中输入8266,点击安装最新版即可,如果下载速度过慢,自己用科学方法下载。

然后点击 项目 - 加载库 - 管理库,等待下载索引,索引完成后在搜索框输入“ArduinoJSON”,点击安装,等待下载安装完成。
至此开发环境完成了,接下来导入B站上下载的,B站粉丝计数器的代码。
//硬件连接说明:
//MAX7219 --- ESP8266
// VCC --- 3V(3.3V)
// GND --- G (GND)
// DIN --- D7(GPIO13)
// CS --- D1(GPIO5)
// CLK --- D5(GPIO14)
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <SPI.h>
//---------------修改此处""内的信息-----------------------
const char *ssid = "WIFI名称"; //WiFi名,只支持2.4Ghz,不支持5G
const char *password = "WiFi密码"; //WiFi密码
String biliuid = "B站个ID"; //bilibili UID
//-------------------------------------------------------
const unsigned long HTTP_TIMEOUT = 5000;
WiFiClient client;
HTTPClient http;
String response;
int follower = 0;
const int slaveSelect = 5;
const int scanLimit = 7;
void setup()
{
Serial.begin(9600);
while (!Serial)
continue;
Serial.println("bilibili fans monitor, version v1.2");
SPI.begin();
pinMode(slaveSelect, OUTPUT);
digitalWrite(slaveSelect, LOW);
sendCommand(12, 1); //Shutdown,open
sendCommand(15, 0); //DisplayTest,no
sendCommand(10, 15); //Intensity,15(max)
sendCommand(11, scanLimit); //ScanLimit,8-1=7
sendCommand(9, 255); //DecodeMode,Code B decode for digits 7-0
digitalWrite(slaveSelect, HIGH);
initdisplay();
Serial.println("LED Ready");
Serial.print("Connecting WiFi...");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
bool getJson()
{
bool r = false;
http.setTimeout(HTTP_TIMEOUT);
http.begin("http://api.bilibili.com/x/relation/stat?vmid=" + biliuid);
int httpCode = http.GET();
if (httpCode > 0){
if (httpCode == HTTP_CODE_OK){
response = http.getString();
//Serial.println(response);
r = true;
}
}else{
Serial.printf("[HTTP] GET JSON failed, error: %s\n", http.errorToString(httpCode).c_str());
errorCode(0x2);
r = false;
}
http.end();
return r;
}
bool parseJson(String json)
{
const size_t capacity = JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + 70;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, json);
int code = doc["code"];
const char *message = doc["message"];
if (code != 0){
Serial.print("[API]Code:");
Serial.print(code);
Serial.print(" Message:");
Serial.println(message);
errorCode(0x3);
return false;
}
JsonObject data = doc["data"];
unsigned long data_mid = data["mid"];
int data_follower = data["follower"];
if (data_mid == 0){
Serial.println("[JSON] FORMAT ERROR");
errorCode(0x4);
return false;
}
Serial.print("UID: ");
Serial.print(data_mid);
Serial.print(" follower: ");
Serial.println(data_follower);
follower = data_follower;
return true;
}
void sendCommand(int command, int value)
{
digitalWrite(slaveSelect, LOW);
SPI.transfer(command);
SPI.transfer(value);
digitalWrite(slaveSelect, HIGH);
}
void displayNumber(int number) //display number in the middle
{
if (number < 0 || number > 99999999)
return;
int x = 1;
int tmp = number;
for (x = 1; tmp /= 10; x++);
for (int i = 1; i < 9; i++)
{
if (i < (10 - x) / 2 || i >= (x / 2 + 5)){
sendCommand(i, 0xf);
}else{
int character = number % 10;
sendCommand(i, character);
number /= 10;
}
}
}
void initdisplay()
{
sendCommand(8, 0xa);
sendCommand(7, 0xa);
sendCommand(6, 0xa);
sendCommand(5, 0xa);
sendCommand(4, 0xa);
sendCommand(3, 0xa);
sendCommand(2, 0xa);
sendCommand(1, 0xa);
}
void loop()
{
if (WiFi.status() == WL_CONNECTED){
if (getJson()){
if (parseJson(response)){
displayNumber(follower);
}
}
}else{
Serial.println("[WiFi] Waiting to reconnect...");
errorCode(0x1);
}
delay(1000);
}
void errorCode(byte errorcode)
{
sendCommand(8, 0xa);
sendCommand(7, 0xa);
sendCommand(6, 0xa);
sendCommand(5, 0xb);
sendCommand(4, errorcode);
sendCommand(3, 0xa);
sendCommand(2, 0xa);
sendCommand(1, 0xa);
}
修改完无线名称、密码和b站ID之后,点击保存;开发板连接电脑,进行编译设置。
在IDE工具中点击 工具 - 开发板 - 选择“NodeMCU 1.0 (ESP-12E Module)”

再点击 工具 - 端口,选择和设备管理器中看到的CH340对应的COM口,比如我这是COM3
再点击工具,修改编程器

点击IDE工具栏上的√,进行代码验证;验证无错之后,点击→箭头进行代码烧录,直至100%


至此b站的粉丝计数器就完成了,然后想要其他功能,就可以学习Arduino开发来实现了


Comments | NOTHING