初出茅庐的小李博客之ESP8266获取自己B站粉丝数据

获取方式

ESP8266发起HTTP请求+解析json数据

获取粉丝API:
https://api.bilibili.com/x/relation/stat?vmid=349513188

API浏览器测试返回结果

{
    "code": 0,
    "message": "0",
    "ttl": 1,
    "data": {
        "mid": 349513188,   //用户的UID号
        "following": 1223,  //用户的关注数
        "whisper": 0,
        "black": 0,
        "follower": 7951    //用户的粉丝数
    }
}

实际结果

在这里插入图片描述

开发方式/硬件平台

Arduino IDE + ESP8266 NODEMCU板子

实现步骤

  1. 安装ArduinoIED 开发发环境
    这个非常简单,直接搜官网地址下载即可。https://www.arduino.cc/en/software
    在这里插入图片描述

  2. 添加开发板
    在这里插入图片描述
    链接地址:
    https://arduino.esp8266.com/stable/package_esp8266com_index.json

  3. 安装项目需要的库

#include <ESP8266WiFi.h>        // 本程序使用ESP8266WiFi库
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <ArduinoJson.h>

其中ArduinoJson 库需要单独安装,其他选好开发板就会自动安装

  1. CV大法
    这个过程比较漫长就不一一记录了,反正遇到了挺多坑,也学到了很多知识
    直接上最终版本代码
#include <ESP8266WiFi.h>        // 本程序使用ESP8266WiFi库
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <ArduinoJson.h>

const char* ssid     = "AQST";                // 连接WiFi名
const char* password = "123456789";           // 连接WiFi密码(此处使用123456789为示例)
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};
/*
  访问API说明:https://api.bilibili.com/x/relation/stat?vmid=349513188
  浏览器访问上面连接得到数据:
  {
    "code": 0,
    "message": "0",
    "ttl": 1,
    "data": {
        "mid": 349513188,
        "following": 1223,
        "whisper": 0,
        "black": 0,
        "follower": 7951
    }
}
  参数说明:
  host:api.bilibili.com
  path:/x/relation/stat?vmid=
  uid:349513188
*/
/*
  原文链接:https://blog.csdn.net/weixin_43794311/article/details/133140939
*/

void de_json(String input)
{
  //从这里开始是使用辅助工具生成的
  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  
  //辅助工具得到的代码到这里结束,下面是自己想要显示的内容

  Serial.println("=======================");
  //Serial.println(ttl); 
  Serial.print("初出茅庐的小李的USID是: ");              // 串口监视器输出用户ID
  Serial.println(data_mid);     
  Serial.print("初出茅庐的小李B站粉丝数: ");              // 串口监视器输出粉丝数量
  Serial.println(data_follower);
  Serial.print("初出茅庐的小李B站关注数: ");              // 串口监视器输出关注数量
  Serial.println(data_following);  
  Serial.print("=======================");
  Serial.println();
}


void setup() {
  pinMode(2, OUTPUT);                          // 设置GPIO2为输出模式
  digitalWrite(2, HIGH);                       // turn the LED on (HIGH is the voltage level)
  Serial.begin(9600);                          // 启动串口通讯
  WiFi.begin(ssid, password);                  // 启动网络连接
  Serial.print("Connecting to ");              // 串口监视器输出网络连接信息
  Serial.print(ssid);
  Serial.println(" ...");                      // 告知用户NodeMCU正在尝试WiFi连接
  int i = 0;                                   // 这一段程序语句用于检查WiFi是否连接成功
  while (WiFi.status() != WL_CONNECTED)        // WiFi.status()函数的返回值是由NodeMCU的WiFi连接状态所决定的。 
  {     
    delay(1000);                               // 如果WiFi连接成功则返回值为WL_CONNECTED                       
    Serial.print(i++); Serial.print(' ');      // 此处通过While循环让NodeMCU每隔一秒钟检查一次WiFi.status()函数返回值                                              
                                               // 同时NodeMCU将通过串口监视器输出连接时长读秒。
  }                                            
  Serial.println("");                          // WiFi连接成功后
  Serial.println("连接成功!");                  // NodeMCU将通过串口监视器输出"连接成功"信息。
  Serial.print("IP地址是:    ");                // 同时还将输出NodeMCU的IP地址。这一功能是通过调用
  Serial.println(WiFi.localIP());              // WiFi.localIP()函数来实现的。该函数的返回值即NodeMCU的IP地址。
}


void loop() {
  digitalWrite(2, HIGH);                     
  delay(1000);                     
  digitalWrite(2, LOW);  
  delay(1000);       
  if(WiFi.status()==WL_CONNECTED)
  {
    std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
   // client->setFingerprint(fingerprint);
    client->setInsecure();  //也可以不使用指纹证书登录
    HTTPClient https;
    Serial.print("[HTTPS] begin...\n");
    if(https.begin(*client,host+path+uid)) 
    {  // HTTPS
      Serial.print("[HTTPS] GET...\n");
      // start connection and send HTTP header
      int httpCode = https.GET();  //访问url返回的状态码
      // httpCode will be negative on error
      if (httpCode > 0)
      {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTPS] GET... code: %d\n", 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);  //将请求到的json数据进行解析显示
        }
      } 
      else 
      {
        Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
      }
      https.end();
    } 
    else 
    {
      Serial.printf("[HTTPS] Unable to connect\n");
    }
  }
  Serial.println("代码正在运行...");     

  delay(5000); 
}

代码解释

  • GPIO2是开发板自带的一颗LED灯
  • AQST是我电脑开的热点名字
  • 123456789是电脑开的热点的密码
    连上后效果

在这里插入图片描述

编译代码

在这里插入图片描述

编译结果

. 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   

上传代码

在这里插入图片描述

上传日志

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...

运行效果

在这里插入图片描述

[HTTPS] GET...
[HTTPS] GET... code: 200
{"code":0,"message":"0","ttl":1,"data":{"mid":349513188,"following":1223,"whisper":0,"black":0,"follower":7951}}
=======================
初出茅庐的小李的USID是: 349513188
初出茅庐的小李B站粉丝数: 7951
初出茅庐的小李B站关注数: 1223
=======================
代码正在运行...
[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}}
=======================
初出茅庐的小李的USID是: 349513188
初出茅庐的小李B站粉丝数: 7951
初出茅庐的小李B站关注数: 1223
=======================
代码正在运行...
[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}}
=======================
初出茅庐的小李的USID是: 349513188
初出茅庐的小李B站粉丝数: 7951
初出茅庐的小李B站关注数: 1223
=======================
代码正在运行...

参考资料

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

注意:本博客仅作为自己学习记录分享、欢迎大家留言讨论~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值