weixin_36434694 2024-11-04 12:33 采纳率: 0%
浏览 5

nodeMCU用https怎么连接到新版onenet

  • 写回答

2条回答 默认 最新

  • weixin_36434694 2024-11-04 12:35
    关注
    
    #include <Arduino.h>
    #include <ESP8266WiFi.h>
    #include <WiFiClientSecure.h>
    
    #ifndef STASSID
    #define STASSID "xxxx"
    #define STAPSK  "12345678"
    #endif
    
    const char* ssid = STASSID;
    const char* password = STAPSK;
    
    //把你需要调用的请求地址用浏览器访问,然后打开调试工具选择“安全”即可看到对应请求的fingerprint,复制到这个地方即可,类似于入网凭证一样
    
    const char fingerprint[] PROGMEM = "80 EA 87 52 BF 69 0C AB 0C D9 9F 68 0C 08 08 9C 11 EB 1D 9D";
    
    void setup() {
      Serial.begin(115200);
      Serial.println();
      Serial.print("connecting to ");
      wificonf();
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    }
    
    //配置上网
    void wificonf(){
      Serial.println(ssid);
      WiFi.mode(WIFI_STA); //设置sta
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) { //检查连接状态
        delay(500);
        Serial.print(".");
      }
    }
    
    //发送网络请求
    void httprequest() {
      
      const char* host = "open.iot.10086.cn";
      const int httpsPort = 443;
      
      WiFiClientSecure client;
      Serial.print("connecting to ");
      Serial.println(host);
    
      Serial.printf("Using fingerprint '%s'\n", fingerprint);
      client.setFingerprint(fingerprint);  //设置指纹码
    
      //链接服务器
      if (!client.connect(host, httpsPort)) {
        Serial.println("connection failed");
        return;
      }
    
      String url = "/repos/esp8266/Arduino/commits/master/status";
      Serial.print("requesting URL: ");
      Serial.println(url);
    
      //发送客户端请求信息
      // client.print(String("GET ") + url + " HTTP/1.1\r\n" +
      //              "Host: " + host + "\r\n" +
      //              "User-Agent: BuildFailureDetectorESP8266\r\n" +
      //              "Connection: close\r\n\r\n");
    
      Serial.println("request sent");
      //等待服务器响应信息
      while (client.connected()) {
        String line = client.readStringUntil('\n'); //获取返回值
        if (line == "\r") {
          Serial.println("headers received");
          break;
        }
      }
      String line = client.readStringUntil('\n');
      if (line.startsWith("{\"state\":\"success\"")) { //判断检测返回的字符串是否包含该内容
        Serial.println("esp8266/Arduino CI successfull!");
      } else {
        Serial.println("esp8266/Arduino CI has failed");
      }
      Serial.println("reply was:");
      Serial.println("==========");
      Serial.println(line);
      Serial.println("==========");
      client.stop();
      Serial.println("closing connection");
    }
    
    void loop() {
      // Use WiFiClientSecure class to create TLS connection
      //使用WiFiClientSecure类创建TLS连接
      httprequest();
      delay(5000);
    }
    
    

    12:19:14.845 -> connecting to open.iot.10086.cn
    12:19:14.845 -> Using fingerprint '80 EA 87 52 BF 69 0C AB 0C D9 9F 68 0C 08 08 9C 11 EB 1D 9D'
    12:19:15.679 -> requesting URL: /repos/esp8266/Arduino/commits/master/status
    12:19:15.761 -> request sent
    12:19:15.939 -> headers received
    12:19:15.939 -> esp8266/Arduino CI has failed
    12:19:15.939 -> reply was:
    12:19:15.939 -> ==========

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 11月4日

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境