esp8266 发送 https 请求

代码:

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>

//Include the SSL client
#include <WiFiClientSecure.h>

char ssid[] = "HelloWifi";       // your network SSID (name)
char password[] = "123ab";  // your network key

//Add a SSL client
WiFiClientSecure client;

String subredditName = "arduino";

long checkRedditDueTime;
int checkRedditDelay = 60000; // 60 x 1000 (1 minute)

void setup() {

  Serial.begin(115200);

  // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);

}

String getTitleOfNewestPost(String sub) {
  String title = "";
  String headers = "";
  String body = "";
  bool finishedHeaders = false;
  bool currentLineIsBlank = true;
  bool gotResponse = false;
  long now;

  char host[] = "api.reddit.com";

  if (client.connect(host, 443)) {
    Serial.println("connected");

    String URL = "/r/" + sub + "/new?limit=1";

    Serial.println(URL);

    client.println("GET " + URL + " HTTP/1.1");
    client.print("Host: "); client.println(host);
    client.println("User-Agent: arduino/1.0");
    client.println("");

    now = millis();
    // checking the timeout
    while (millis() - now < 1500) {
      while (client.available()) {
        char c = client.read();
        //Serial.print(c);

        if (finishedHeaders) {
          body=body+c;
        } else {
          if (currentLineIsBlank && c == '\n') {
            finishedHeaders = true;
          }
          else {
            headers = headers + c;
          }
        }

        if (c == '\n') {
          currentLineIsBlank = true;
        }else if (c != '\r') {
          currentLineIsBlank = false;
        }

        //marking we got a response
        gotResponse = true;

      }
      if (gotResponse) {

        DynamicJsonBuffer jsonBuffer;
        JsonObject& root = jsonBuffer.parseObject(body);
        if (root.success()) {
          if (root.containsKey("data")) {
            JsonObject& post = root["data"]["children"][0];
            if (post.containsKey("data")) { 
              title = post["data"]["title"].as<String>();
            }
          } 
        } else {
          Serial.println("failed to parse JSON");
        }

        break;
      }
    }
  }

  //Serial.println(body);

  return title;
}

void loop() {
  long now = millis();
  if(now >= checkRedditDueTime) {
    Serial.println("---------");
    String title = getTitleOfNewestPost(subredditName);
    if(title != "") {
      Serial.println("Most recent post on /r/" + subredditName);
      Serial.println(title);
    } else {
     Serial.println("Error getting title."); 
    }
    Serial.println("---------");
    checkRedditDueTime = now + checkRedditDelay;
  }
}

log:

scandone
Connecting Wifi: HelloWifi
......scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1

connected with HelloWifi, channel 1
dhcp client start...
cnt 
........ip:192.168.0.103,mask:255.255.255.0,gw:192.168.0.1

WiFi connected
IP address: 
192.168.0.103
---------
connected
/r/arduino/new?limit=1
pm open,type:2 0
Most recent post on /r/arduino
350 mhz transmitter
---------
---------
connected
/r/arduino/new?limit=1
Most recent post on /r/arduino
350 mhz transmitter
---------
---------
connected
/r/arduino/new?limit=1
Most recent post on /r/arduino
350 mhz transmitter
---------

参考:
https://github.com/witnessmenow/HTTPS-directly-from-ESP8266/blob/master/implentation/implentation.ino

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值