The fledgling Xiao Li’s blog uses ESP8266 to obtain the fan data of his B station

How to get it

ESP8266 initiates HTTP request + parses json data

Get fans API:
https://api.bilibili.com/x/relation/stat?vmid=349513188

API browser test return results

{<!-- -->
    "code": 0,
    "message": "0",
    "ttl": 1,
    "data": {<!-- -->
        "mid": 349513188, //User's UID number
        "following": 1223, //The number of users' following
        "whisper": 0,
        "black": 0,
        "follower": 7951 //The number of followers of the user
    }
}

Actual results

Development method/hardware platform

Arduino IDE + ESP8266 NODEMCU board

Implementation steps

  1. Install ArduinoIED development environment
    This is very simple, just search the official website address and download it. https://www.arduino.cc/en/software

  2. Add development board

    link address:
    https://arduino.esp8266.com/stable/package_esp8266com_index.json

  3. Install the libraries required for the project

#include <ESP8266WiFi.h> // This program uses the ESP8266WiFi library
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <ArduinoJson.h>

Among them, the ArduinoJson library needs to be installed separately, and other development boards will be installed automatically after selecting it.

  1. CV Dafa
    This process is quite long, so I won’t record it one by one. Anyway, I encountered a lot of pitfalls and learned a lot of knowledge.
    Directly upload the final version code
#include <ESP8266WiFi.h> // This program uses the ESP8266WiFi library
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <ArduinoJson.h>

const char* ssid = "AQST"; //Connect WiFi name
const char* password = "123456789"; // Password for connecting to WiFi (123456789 is used as an example here)
const String host = "https://api.bilibili.com";
const String path = "/x/relation/stat?vmid=";
const String uid = "349513188";
const unsigned char fingerprint[] = {0x00,0xb3,0x57,0x0f,0xaa,0x95,0xc7,0x03,0xeb,0x78,0x30,0xd9,0xfc,0xd8,0x2b,
0x89,0xd8,0xce,0x06,0xa8,0x30,0x4e,0x7a,0x8d,0x3f,0x18,0x60,0xa5,0x90,0x74,0xf4,0xdc};
/*
  Access API instructions: https://api.bilibili.com/x/relation/stat?vmid=349513188
  The browser accesses the above connection to get the data:
  {
    "code": 0,
    "message": "0",
    "ttl": 1,
    "data": {
        "mid": 349513188,
        "following": 1223,
        "whisper": 0,
        "black": 0,
        "follower": 7951
    }
}
  Parameter Description:
  host:api.bilibili.com
  path:/x/relation/stat?vmid=
  uid:349513188
*/
/*
  Original link: https://blog.csdn.net/weixin_43794311/article/details/133140939
*/

void de_json(String input)
{
  //From here it is generated using auxiliary tools
  StaticJsonDocument<256> doc;
  DeserializationError error = deserializeJson(doc, input);
  if(error)
  {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.f_str());
    return;
  }

  int code = doc["code"]; // 0
  const char* message = doc["message"]; // "0"
  int ttl = doc["ttl"]; // 1

  JsonObject data = doc["data"];
  long data_mid = data["mid"]; // 231216575
  int data_following = data["following"]; // 154
  int data_whisper = data["whisper"]; // 0
  int data_black = data["black"]; // 0
  int data_follower = data["follower"]; // 1
  //The code obtained by the auxiliary tool ends here. The following is the content you want to display.

  Serial.println("=======================");
  //Serial.println(ttl);
  Serial.print("The USID of the fledgling Xiao Li is: "); // The serial monitor outputs the user ID
  Serial.println(data_mid);
  Serial.print("Number of fans of the fledgling Xiao Li B station: "); // Serial monitor outputs the number of fans
  Serial.println(data_follower);
  Serial.print("The number of followers of the fledgling Xiaoli B station: "); // The serial port monitor outputs the number of followers
  Serial.println(data_following);
  Serial.print("=======================");
  Serial.println();
}


void setup() {
  pinMode(2, OUTPUT); //Set GPIO2 to output mode
  digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
  Serial.begin(9600); // Start serial communication
  WiFi.begin(ssid, password); // Start network connection
  Serial.print("Connecting to "); // The serial monitor outputs network connection information
  Serial.print(ssid);
  Serial.println(" ..."); // Inform the user that NodeMCU is trying to connect to WiFi
  int i = 0; // This program statement is used to check whether the WiFi connection is successful
  while (WiFi.status() != WL_CONNECTED) //The return value of the WiFi.status() function is determined by the WiFi connection status of NodeMCU.
  {
    delay(1000); // If the WiFi connection is successful, the return value is WL_CONNECTED
    Serial.print(i + + ); Serial.print(' '); // Here, the While loop is used to let NodeMCU check the return value of the WiFi.status() function every second
                                               // At the same time, NodeMCU will output the connection duration count in seconds through the serial port monitor.
  }
  Serial.println(""); // After successful WiFi connection
  Serial.println("Connection successful!"); // NodeMCU will output "Connection successful" information through the serial port monitor.
  Serial.print("The IP address is: "); // The IP address of NodeMCU will also be output. This function is done by calling
  Serial.println(WiFi.localIP()); // Implemented by WiFi.localIP() function. The return value of this function is the IP address of NodeMCU.
}


void loop() {
  digitalWrite(2, HIGH);
  delay(1000);
  digitalWrite(2, LOW);
  delay(1000);
  if(WiFi.status()==WL_CONNECTED)
  {
    std::unique_ptrclient(new BearSSL::WiFiClientSecure);
   // client->setFingerprint(fingerprint);
    client->setInsecure(); //You can also log in without using a fingerprint certificate
    HTTPClient https;
    Serial.print("[HTTPS] begin...\
");
    if(https.begin(*client,host + path + uid))
    { // HTTPS
      Serial.print("[HTTPS] GET...\
");
      // start connection and send HTTP header
      int httpCode = https.GET(); //Status code returned by accessing url
      // httpCode will be negative on error
      if (httpCode > 0)
      {
        // HTTP header has been sent and Server response header has been handled
        Serial.printf("[HTTPS] GET... code: %d\
", httpCode);
        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
        {
          String payload = https.getString();
          Serial.println(payload);
          de_json(payload); //Parse and display the requested json data
        }
      }
      else
      {
        Serial.printf("[HTTPS] GET... failed, error: %s\
", https.errorToString(httpCode).c_str());
      }
      https.end();
    }
    else
    {
      Serial.printf("[HTTPS] Unable to connect\
");
    }
  }
  Serial.println("The code is running...");

  delay(5000);
}

Code explanation

  • GPIO2 is an LED light that comes with the development board.
  • AQST is the name of the hotspot on my computer
  • 123456789 is the password for the hotspot on your computer
    The effect after connecting

Compile code

Compilation results

. Variables and constants in RAM (global, static), used 29316 / 80192 bytes (36%)
║ SEGMENT BYTES DESCRIPTION
╠══ DATA 1504 initialized variables
╠══ RODATA 1948 constants
╚══ BSS 25864 zeroed variables
. Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 60603 / 65536 bytes (92%)
║ SEGMENT BYTES DESCRIPTION
╠══ ICACHE 32768 reserved space for flash instruction cache
╚══ IRAM 27835 code in IRAM
. Code in flash (default, ICACHE_FLASH_ATTR), used 361136 / 1048576 bytes (34%)
║ SEGMENT BYTES DESCRIPTION
╚══ IROM 361136 code in flash

Upload code

Upload log

esptool.py v3.0
Serial port COM16
Connecting....
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: e8:db:84:e1:5a:a8
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 396576 bytes to 293196...
Writing at 0x00000000... (5 %)
Writing at 0x00004000... (11 %)
Writing at 0x00008000... (16 %)
Writing at 0x0000c000... (22%)
Writing at 0x00010000... (27 %)
Writing at 0x00014000... (33%)
Writing at 0x00018000... (38%)
Writing at 0x0001c000... (44%)
Writing at 0x00020000... (50 %)
Writing at 0x00024000... (55%)
Writing at 0x00028000... (61 %)
Writing at 0x0002c000... (66 %)
Writing at 0x00030000... (72%)
Writing at 0x00034000... (77 %)
Writing at 0x00038000... (83%)
Writing at 0x0003c000... (88 %)
Writing at 0x00040000... (94%)
Writing at 0x00044000... (100 %)
Wrote 396576 bytes (293196 compressed) at 0x00000000 in 26.0 seconds (effective 122.2 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

Operating effect

[HTTPS] GET...
[HTTPS] GET... code: 200
{<!-- -->"code":0,"message":"0","ttl":1,"data":{<!-- -->"mid":349513188,"following": 1223,"whisper":0,"black":0,"follower":7951}}
=======================
Xiao Li’s USID is: 349513188
The number of fans of Xiao Li’s B station: 7951
Number of followers on the fledgling Xiao Li B station: 1223
=======================
Code is running...
[HTTPS] begin...
[HTTPS] GET...
[HTTPS] GET... code: 200
{<!-- -->"code":0,"message":"0","ttl":1,"data":{<!-- -->"mid":349513188,"following": 1223,"whisper":0,"black":0,"follower":7951}}
=======================
Xiao Li’s USID is: 349513188
The number of fans of Xiao Li’s B station: 7951
Number of followers on the fledgling Xiao Li B station: 1223
=======================
Code is running...
[HTTPS] begin...
[HTTPS] GET...
[HTTPS] GET... code: 200
{<!-- -->"code":0,"message":"0","ttl":1,"data":{<!-- -->"mid":349513188,"following": 1223,"whisper":0,"black":0,"follower":7951}}
=======================
Xiao Li’s USID is: 349513188
The number of fans of Xiao Li’s B station: 7951
Number of followers on the fledgling Xiao Li B station: 1223
=======================
Code is running...

Reference materials

https://blog.csdn.net/weixin_43794311/article/details/133140939
https://blog.csdn.net/qq_55493007/article/details/128421685

Note: This blog is only shared as my own learning record, everyone is welcome to leave a message for discussion~