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


    获取方式

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

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

    API浏览器测试返回结果

    {
        "code": 0,
        "message": "0",
        "ttl": 1,
        "data": {
            "mid": 349513188,   //用户的UID号
            "following": 1223,  //用户的关注数
            "whisper": 0,
            "black": 0,
            "follower": 7951    //用户的粉丝数
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    实际结果

    在这里插入图片描述

    开发方式/硬件平台

    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库
    #include 
    #include 
    #include 
    #include 
    
    • 1
    • 2
    • 3
    • 4
    • 5

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

    1. CV大法
      这个过程比较漫长就不一一记录了,反正遇到了挺多坑,也学到了很多知识
      直接上最终版本代码
    #include         // 本程序使用ESP8266WiFi库
    #include 
    #include 
    #include 
    #include 
    
    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); 
    }
    
    
    • 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
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142

    代码解释

    • 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   
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    上传代码

    在这里插入图片描述

    上传日志

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

    运行效果

    在这里插入图片描述

    [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
    =======================
    代码正在运行...
    
    • 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

    参考资料

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

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

  • 相关阅读:
    YOLOv5独家原创改进:最新原创WIoU_NMS改进点,改进有效可以直接当做自己的原创改进点来写,提升网络模型性能精度
    res_config_settings.py文件详解
    强化学习 - 策略梯度(Policy Gradient)
    【Java 进阶篇】Java XML快速入门:理解、解析和生成XML
    人工神经网络与卷积神经网络区别与联系
    TiUP 术语及核心概念
    Linux设备树插件
    【JAVA问题解决方案】02.Freemarker导出Excel超出上限分表解决方案
    【Flink】FlinkCDC自定义反序列化器
    Appl swc
  • 原文地址:https://blog.csdn.net/weixin_43176183/article/details/133976218