位置导航: ESP8266库 / WiFiClientSecure库 / 本页
说明
ESP8266通过HTTPS协议实现物联网通讯时,我们可以使用setSession函数将会话保存在设备内存中以便在有需要的时候恢复会话。
语法
httpsClient.setSession(session)
参数
Session对象
返回值
无
示例程序
以下程序将实现ESP8266建立与网站的HTTPS通讯,并且将网站服务器响应信息显示于串口监视器中。
请将以下示例程序中的Wifi联网信息进行调整并将程序上传给ESP8266。程序上传完毕后,ESP8266将会自动连接 WiFi并通过HTTPS协议与必应网站服务器进行通讯。服务器响应状态码以及响应头信息显示于串口监视器中以便我们查阅。
注意:出于安全原因,CA会定期更新根证书信息。因此本程序中的证书可能已经过期。请使用浏览器获取最新的网站根证书并复制粘贴到程序中相应位置。如需了解如何执行这一操作,请参考太极创客网站中
“查看网站根证书”页面。
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
/********************************************************************** 项目名称/Project : 零基础入门学用物联网 程序名称/Program name : ESP8266-WiFiClientSecure-setSessions 团队/Team : 太极创客团队 / Taichi-Maker (www.taichi-maker.com) 作者/Author : CYNO朔 日期/Date(YYYYMMDD) : 20200419 程序目的/Purpose : 本程序改编自ESP8266库中BearSSL_Sessions示例程序。以下是原示例程序注释信息 Example of using SSL sessions to speed up SSL connection initiation September 2018 by Earle F. Philhower, III Released to the public domain 本程序将使用setSessions函数将HTTPS对话保存在ESP8266的内存中,以便恢复使用。 ----------------------------------------------------------------------- 本示例程序为太极创客团队制作的《零基础入门学用物联网》中示例程序。 该教程为对物联网开发感兴趣的朋友所设计和制作。如需了解更多该教程的信息,请参考以下网页: http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/ ***********************************************************************/ #include <ESP8266WiFi.h> #include <time.h> const char *ssid = "taichimaker"; const char *pass = "12345678"; const char * host = "cn.bing.com"; const uint16_t port = 443; const char * path = "/"; void setup() { Serial.begin(9600); Serial.println(); Serial.println(); Serial.printf("Connecting to %s\n", ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected"); Serial.println("IP Address: "); Serial.println(WiFi.localIP()); // 设置时间 configTime(3 * 3600, 0, "ntp.ntsc.ac.cn","pool.ntp.org", "time.nist.gov"); Serial.print("Waiting for NTP time sync: "); time_t now = time(nullptr); while (now < 8 * 3600 * 2) { delay(500); Serial.print("."); now = time(nullptr); } Serial.println(""); struct tm timeinfo; gmtime_r(&now, &timeinfo); Serial.print("Current time: "); Serial.print(asctime(&timeinfo)); } // 连接网站服务器,通过串口监视器输出服务器响应状态码 void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_t port, const char *path) { if (!path) { path = "/"; } Serial.printf("Trying: %s:443...", host); client->connect(host, port); if (!client->connected()) { Serial.printf("*** Can't connect. ***\n-------\n"); return; } Serial.printf("Connected!\n-------\n"); client->write("GET "); client->write(path); client->write(" HTTP/1.0\r\nHost: "); client->write(host); client->write("\r\nUser-Agent: ESP8266\r\n"); client->write("\r\n"); uint32_t to = millis() + 5000; if (client->connected()) { do { char tmp[32]; memset(tmp, 0, 32); int rlen = client->read((uint8_t*)tmp, sizeof(tmp) - 1); yield(); if (rlen < 0) { break; } char *nl = strchr(tmp, '\r'); if (nl) { *nl = 0; Serial.print(tmp); break; } Serial.print(tmp); } while (millis() < to); } client->stop(); Serial.printf("\n-------\n\n"); } void loop() { static const char digicert[] PROGMEM = R"EOF(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f1wbWsaY71k5h+3zvDyny67G7fyUIhz ksLi4xaNmjICq44Y3ekQEe5+NauQrz4wlHrQMz2nZQ/1/I6eYs9HRCwBXbsdtTLS R9I4LtD+gdwyah617jzV/OeBHRnDJELqYzmp -----END CERTIFICATE----- )EOF"; uint32_t start, finish; BearSSL::WiFiClientSecure client; BearSSL::X509List cert(digicert); Serial.printf("Connecting without sessions..."); start = millis(); client.setTrustAnchors(&cert); fetchURL(&client, host, port, path); finish = millis(); Serial.printf("Total time: %dms\n", finish - start); BearSSL::Session session; client.setSession(&session); Serial.printf("Connecting with an unitialized session..."); start = millis(); client.setTrustAnchors(&cert); fetchURL(&client, host, port, path); finish = millis(); Serial.printf("Total time: %dms\n", finish - start); Serial.printf("Connecting with the just initialized session..."); start = millis(); client.setTrustAnchors(&cert); fetchURL(&client, host, port, path); finish = millis(); Serial.printf("Total time: %dms\n", finish - start); Serial.printf("Connecting again with the initialized session..."); start = millis(); client.setTrustAnchors(&cert); fetchURL(&client, host, port, path); finish = millis(); Serial.printf("Total time: %dms\n", finish - start); delay(10000); } |
位置导航: ESP8266库 / WiFiClientSecure库 / 本页