-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix potential code error and optimization
- Loading branch information
Showing
11 changed files
with
1,659 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
/** | ||
* This example shows how to connect to server via https and verify the root certificate using the SSL client. | ||
* | ||
* Email: [email protected] | ||
* | ||
* Github: https://github.com/mobizt/ESP_SSLSClient | ||
* | ||
* Copyright (c) 2023 mobizt | ||
* | ||
*/ | ||
|
||
#include <Arduino.h> | ||
#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ARDUINO_GIGA) | ||
#include <WiFi.h> | ||
#elif defined(ESP8266) | ||
#include <ESP8266WiFi.h> | ||
#elif __has_include(<WiFiNINA.h>) || defined(ARDUINO_NANO_RP2040_CONNECT) | ||
#include <WiFiNINA.h> | ||
#elif __has_include(<WiFi101.h>) | ||
#include <WiFi101.h> | ||
#elif __has_include(<WiFiS3.h>) || defined(ARDUINO_UNOWIFIR4) | ||
#include <WiFiS3.h> | ||
#elif __has_include(<WiFiC3.h>) || defined(ARDUINO_PORTENTA_C33) | ||
#include <WiFiC3.h> | ||
#elif __has_include(<WiFi.h>) | ||
#include <WiFi.h> | ||
#endif | ||
|
||
#include <ESP_SSLClient.h> | ||
|
||
#define WIFI_SSID "WIFI_AP" | ||
#define WIFI_PASSWORD "WIFI_PASSWORD" | ||
|
||
// Baltimore CyberTrust Root | ||
// Expired on Tue May 13 2025 | ||
const char rootCA[] PROGMEM = "-----BEGIN CERTIFICATE-----\n" | ||
"MIIDdzCCAl+gAwIBAgIEAgAAuTANBgkqhkiG9w0BAQUFADBaMQswCQYDVQQGEwJJ\n" | ||
"RTESMBAGA1UEChMJQmFsdGltb3JlMRMwE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5OCBRLbf1wbWsaY71k5h+3zvDyny67G7fyUIhz\n" | ||
"ksLi4xaNmjICq44Y3ekQEe5+NauQrz4wlHrQMz2nZQ/1/I6eYs9HRCwBXbsdtTLS\n" | ||
"R9I4LtD+gdwyah617jzV/OeBHRnDJELqYzmp\n" | ||
"-----END CERTIFICATE-----\n"; | ||
|
||
ESP_SSLClient ssl_client; | ||
|
||
// EthernetClient basic_client; | ||
// GSMClient basic_client; | ||
WiFiClient basic_client; | ||
|
||
#if defined(ARDUINO_RASPBERRY_PI_PICO_W) | ||
WiFiMulti multi; | ||
#endif | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
|
||
#if defined(ARDUINO_RASPBERRY_PI_PICO_W) | ||
multi.addAP(WIFI_SSID, WIFI_PASSWORD); | ||
multi.run(); | ||
#else | ||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | ||
#endif | ||
|
||
Serial.print("Connecting to Wi-Fi"); | ||
unsigned long ms = millis(); | ||
while (WiFi.status() != WL_CONNECTED) | ||
{ | ||
Serial.print("."); | ||
delay(300); | ||
#if defined(ARDUINO_RASPBERRY_PI_PICO_W) | ||
if (millis() - ms > 10000) | ||
break; | ||
#endif | ||
} | ||
Serial.println(); | ||
Serial.print("Connected with IP: "); | ||
Serial.println(WiFi.localIP()); | ||
Serial.println(); | ||
|
||
// The valid time is required for server certificate verification. | ||
#if defined(ESP8266) || defined(ESP32) && !defined(ARDUINO_NANO_RP2040_CONNECT) | ||
|
||
configTime(0, 0, "pool.ntp.org", "time.nist.gov"); | ||
while (time(nullptr) < ESP_SSLCLIENT_VALID_TIMESTAMP) | ||
{ | ||
delay(100); | ||
} | ||
|
||
// If verification time was not set via this function, the device system time will be used | ||
// ssl_client.setX509Time(time(nullptr)); | ||
|
||
#elif defined(ARDUINO_RASPBERRY_PI_PICO_W) | ||
|
||
configTime(10000, 0, "pool.ntp.org", "time.nist.gov"); | ||
while (time(nullptr) < ESP_SSLCLIENT_VALID_TIMESTAMP) | ||
{ | ||
delay(100); | ||
} | ||
|
||
// If verification time was not set via this function, the device system time will be used | ||
// ssl_client.setX509Time(time(nullptr)); | ||
|
||
#elif __has_include(<WiFiNINA.h>) || __has_include(<WiFi101.h>) | ||
time_t ts = 0; | ||
do | ||
{ | ||
ts = WiFi.getTime(); | ||
delay(100); | ||
} while (ts < ESP_SSLCLIENT_VALID_TIMESTAMP); | ||
|
||
// The verification time setting is required because the device system time i.e. time(nullptr) is not available in this case. | ||
ssl_client.setX509Time(ts); | ||
#endif | ||
|
||
// Set the server certificate, intermediate cerificate or root certificate | ||
ssl_client.setCACert(rootCA); | ||
|
||
// Set the receive and transmit buffers size in bytes for memory allocation (512 to 16384). | ||
ssl_client.setBufferSizes(1024 /* rx */, 512 /* tx */); | ||
|
||
/** Call setDebugLevel(level) to set the debug | ||
* esp_ssl_debug_none = 0 | ||
* esp_ssl_debug_error = 1 | ||
* esp_ssl_debug_warn = 2 | ||
* esp_ssl_debug_info = 3 | ||
* esp_ssl_debug_dump = 4 | ||
*/ | ||
ssl_client.setDebugLevel(1); | ||
|
||
// Assign the basic client | ||
// Due to the basic_client pointer is assigned, to avoid dangling pointer, basic_client should be existed | ||
// as long as it was used by ssl_client for transportation. | ||
ssl_client.setClient(&basic_client); | ||
} | ||
|
||
void loop() | ||
{ | ||
Serial.println("---------------------------------"); | ||
Serial.print("Connecting to server..."); | ||
|
||
String payload = "{\"title\":\"hello\"}"; | ||
|
||
if (ssl_client.connect("reqres.in", 443)) | ||
{ | ||
Serial.println(" ok"); | ||
Serial.println("Send POST request..."); | ||
ssl_client.print("POST /api/users HTTP/1.1\r\n"); | ||
ssl_client.print("Host: reqres.in\r\n"); | ||
ssl_client.print("Content-Type: application/json\r\n"); | ||
ssl_client.print("Content-Length: "); | ||
ssl_client.print(payload.length()); | ||
ssl_client.print("\r\n\r\n"); | ||
ssl_client.print(payload); | ||
|
||
Serial.print("Read response..."); | ||
|
||
unsigned long ms = millis(); | ||
while (!ssl_client.available() && millis() - ms < 3000) | ||
{ | ||
delay(0); | ||
} | ||
Serial.println(); | ||
while (ssl_client.available()) | ||
{ | ||
Serial.print((char)ssl_client.read()); | ||
} | ||
Serial.println(); | ||
} | ||
else | ||
Serial.println(" failed\n"); | ||
|
||
ssl_client.stop(); | ||
|
||
Serial.println(); | ||
|
||
delay(5000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/** | ||
* This example shows how to connect to server via custom secure port. | ||
* | ||
* The standard secure ports as listed here are supported by this library https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers. | ||
* | ||
* If the port you want to connect with SSL/TLS is not in the list, you can begin the connection in the plain text mode first and upgrade later. | ||
* | ||
* Email: [email protected] | ||
* | ||
* Github: https://github.com/mobizt/ESP_SSLSClient | ||
* | ||
* Copyright (c) 2023 mobizt | ||
* | ||
*/ | ||
#include <Arduino.h> | ||
#if defined(ESP32) || defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ARDUINO_GIGA) | ||
#include <WiFi.h> | ||
#elif defined(ESP8266) | ||
#include <ESP8266WiFi.h> | ||
#elif __has_include(<WiFiNINA.h>) || defined(ARDUINO_NANO_RP2040_CONNECT) | ||
#include <WiFiNINA.h> | ||
#elif __has_include(<WiFi101.h>) | ||
#include <WiFi101.h> | ||
#elif __has_include(<WiFiS3.h>) || defined(ARDUINO_UNOWIFIR4) | ||
#include <WiFiS3.h> | ||
#elif __has_include(<WiFiC3.h>) || defined(ARDUINO_PORTENTA_C33) | ||
#include <WiFiC3.h> | ||
#elif __has_include(<WiFi.h>) | ||
#include <WiFi.h> | ||
#endif | ||
|
||
#include <ESP_SSLClient.h> | ||
|
||
#define WIFI_SSID "WIFI_AP" | ||
#define WIFI_PASSWORD "WIFI_PASSWORD" | ||
|
||
ESP_SSLClient ssl_client; | ||
|
||
// EthernetClient basic_client; | ||
// GSMClient basic_client; | ||
WiFiClient basic_client; | ||
|
||
#if defined(ARDUINO_RASPBERRY_PI_PICO_W) | ||
WiFiMulti multi; | ||
#endif | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
|
||
#if defined(ARDUINO_RASPBERRY_PI_PICO_W) | ||
multi.addAP(WIFI_SSID, WIFI_PASSWORD); | ||
multi.run(); | ||
#else | ||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | ||
#endif | ||
|
||
Serial.print("Connecting to Wi-Fi"); | ||
unsigned long ms = millis(); | ||
while (WiFi.status() != WL_CONNECTED) | ||
{ | ||
Serial.print("."); | ||
delay(300); | ||
#if defined(ARDUINO_RASPBERRY_PI_PICO_W) | ||
if (millis() - ms > 10000) | ||
break; | ||
#endif | ||
} | ||
Serial.println(); | ||
Serial.print("Connected with IP: "); | ||
Serial.println(WiFi.localIP()); | ||
Serial.println(); | ||
|
||
// ignore server ssl certificate verification | ||
ssl_client.setInsecure(); | ||
|
||
// Set the receive and transmit buffers size in bytes for memory allocation (512 to 16384). | ||
ssl_client.setBufferSizes(1024 /* rx */, 512 /* tx */); | ||
|
||
/** Call setDebugLevel(level) to set the debug | ||
* esp_ssl_debug_none = 0 | ||
* esp_ssl_debug_error = 1 | ||
* esp_ssl_debug_warn = 2 | ||
* esp_ssl_debug_info = 3 | ||
* esp_ssl_debug_dump = 4 | ||
*/ | ||
ssl_client.setDebugLevel(1); | ||
|
||
// Assign the basic client to use in non-secure mode. | ||
ssl_client.setClient(&basic_client, false /* set enable SSL option to false */); | ||
} | ||
|
||
void loop() | ||
{ | ||
Serial.println("---------------------------------"); | ||
|
||
Serial.print("Connecting to server via HTTP..."); | ||
|
||
String payload = "{\"title\":\"hello\"}"; | ||
|
||
String server = "your_server.com"; // The server to connect. | ||
uint16_t port = 5443; // The port to connect. | ||
|
||
if (ssl_client.connect(server.c_str(), port)) | ||
{ | ||
Serial.println(" ok"); | ||
|
||
Serial.print("Upgrade to HTTPS..."); | ||
if (!ssl_client.connectSSL()) | ||
{ | ||
Serial.println(" failed\r\n"); | ||
return; | ||
} | ||
|
||
Serial.println(" ok"); | ||
|
||
Serial.println("Send GET request..."); | ||
ssl_client.print("GET / HTTP/1.1\r\n"); | ||
ssl_client.print("Host: "); | ||
ssl_client.print(server); | ||
ssl_client.print("\r\n"); | ||
ssl_client.print("Content-Type: application/json\r\n"); | ||
ssl_client.print("Content-Length: "); | ||
ssl_client.print(payload.length()); | ||
ssl_client.print("\r\n\r\n"); | ||
ssl_client.print(payload); | ||
|
||
Serial.print("Read response..."); | ||
unsigned long ms = millis(); | ||
while (!ssl_client.available() && millis() - ms < 3000) | ||
{ | ||
delay(0); | ||
} | ||
Serial.println(); | ||
while (ssl_client.available()) | ||
{ | ||
Serial.print((char)ssl_client.read()); | ||
} | ||
Serial.println(); | ||
} | ||
else | ||
Serial.println(" failed\n"); | ||
|
||
ssl_client.stop(); | ||
|
||
Serial.println(); | ||
|
||
delay(5000); | ||
} |
Oops, something went wrong.