From 9e1e8575032243d3bc29ceb1ab826e7806fb5c60 Mon Sep 17 00:00:00 2001 From: Moritz Ulmer Date: Sun, 16 Jun 2024 12:02:40 +0200 Subject: [PATCH] Fix example build --- .../esp32/WebSocketClientSSLBundle/.gitignore | 2 +- .../WebSocketClientSSLBundle/example.cpp | 108 ---------- .../WebSocketClientSSLBundle/include/main.hpp | 17 -- .../include/secrets.hpp.template | 2 - .../WebSocketClientSSLBundle/platformio.ini | 3 +- .../run_gen_script.py | 4 +- .../WebSocketClientSSLBundle/src/main.cpp | 192 +++++++++++------- 7 files changed, 120 insertions(+), 208 deletions(-) delete mode 100644 examples/esp32/WebSocketClientSSLBundle/example.cpp delete mode 100644 examples/esp32/WebSocketClientSSLBundle/include/main.hpp delete mode 100644 examples/esp32/WebSocketClientSSLBundle/include/secrets.hpp.template diff --git a/examples/esp32/WebSocketClientSSLBundle/.gitignore b/examples/esp32/WebSocketClientSSLBundle/.gitignore index 292d674..7599937 100644 --- a/examples/esp32/WebSocketClientSSLBundle/.gitignore +++ b/examples/esp32/WebSocketClientSSLBundle/.gitignore @@ -5,4 +5,4 @@ .vscode/ipch *secret* !*secrets.hpp.template -*x509_crt_bundle \ No newline at end of file +*x509_crt_bundle.bin \ No newline at end of file diff --git a/examples/esp32/WebSocketClientSSLBundle/example.cpp b/examples/esp32/WebSocketClientSSLBundle/example.cpp deleted file mode 100644 index ef4662f..0000000 --- a/examples/esp32/WebSocketClientSSLBundle/example.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/* - * WebSocketClient.ino - * - * Created on: 15.06.2024 - * - */ - -#include - -#include -#include -#include - -#include - -extern const uint8_t rootca_crt_bundle_start[] asm( - "_binary_data_cert_x509_crt_bundle_bin_start"); - -WiFiMulti WiFiMulti; -WebSocketsClient webSocket; - -#define USE_SERIAL Serial1 - -void hexdump(const void * mem, uint32_t len, uint8_t cols = 16) { - const uint8_t * src = (const uint8_t *)mem; - USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len); - for(uint32_t i = 0; i < len; i++) { - if(i % cols == 0) { - USE_SERIAL.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i); - } - USE_SERIAL.printf("%02X ", *src); - src++; - } - USE_SERIAL.printf("\n"); -} - -void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { - switch(type) { - case WStype_DISCONNECTED: - USE_SERIAL.printf("[WSc] Disconnected!\n"); - break; - case WStype_CONNECTED: - USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload); - - // send message to server when Connected - webSocket.sendTXT("Connected"); - break; - case WStype_TEXT: - USE_SERIAL.printf("[WSc] get text: %s\n", payload); - - // send message to server - // webSocket.sendTXT("message here"); - break; - case WStype_BIN: - USE_SERIAL.printf("[WSc] get binary length: %u\n", length); - hexdump(payload, length); - - // send data to server - // webSocket.sendBIN(payload, length); - break; - case WStype_ERROR: - case WStype_FRAGMENT_TEXT_START: - case WStype_FRAGMENT_BIN_START: - case WStype_FRAGMENT: - case WStype_FRAGMENT_FIN: - break; - } -} - -void setup() { - USE_SERIAL.begin(115200); - - USE_SERIAL.setDebugOutput(true); - - USE_SERIAL.println(); - USE_SERIAL.println(); - USE_SERIAL.println(); - - for(uint8_t t = 4; t > 0; t--) { - USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t); - USE_SERIAL.flush(); - delay(1000); - } - - WiFiMulti.addAP("SSID", "passpasspass"); - - // WiFi.disconnect(); - while(WiFiMulti.run() != WL_CONNECTED) { - delay(100); - } - - // server address, port and URL (https://piehost.com/websocket-tester) - webSocket.beginSslWithBundle( - "demo.piesocket.com", 443, "/v3/channel_123?api_key=XYZ", rootca_crt_bundle_start); - - // event handler - webSocket.onEvent(webSocketEvent); - - // use HTTP Basic Authorization this is optional remove if not needed - webSocket.setAuthorization("user", "Password"); - - // try ever 5000 again if connection has failed - webSocket.setReconnectInterval(5000); -} - -void loop() { - webSocket.loop(); -} diff --git a/examples/esp32/WebSocketClientSSLBundle/include/main.hpp b/examples/esp32/WebSocketClientSSLBundle/include/main.hpp deleted file mode 100644 index b27d0da..0000000 --- a/examples/esp32/WebSocketClientSSLBundle/include/main.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include -#include - -#include -#include "WiFiClientSecure.h" - -#include "secrets.hpp" - -#define SSL_TEST_URL "https://raw.githubusercontent.com/Duckle29/esp32-certBundle/main/readme.md" - -extern const uint8_t x509_crt_bundle_start[] asm("_binary_src_x509_crt_bundle_start"); -extern const uint8_t x509_crt_bundle_end[] asm("_binary_src_x509_crt_bundle_end"); - -WiFiMulti WiFiMulti; - -void setClock(void); diff --git a/examples/esp32/WebSocketClientSSLBundle/include/secrets.hpp.template b/examples/esp32/WebSocketClientSSLBundle/include/secrets.hpp.template deleted file mode 100644 index cda8d28..0000000 --- a/examples/esp32/WebSocketClientSSLBundle/include/secrets.hpp.template +++ /dev/null @@ -1,2 +0,0 @@ -#define SSID "WIFI SSID" -#define PASS "WIFI PASSWORD" diff --git a/examples/esp32/WebSocketClientSSLBundle/platformio.ini b/examples/esp32/WebSocketClientSSLBundle/platformio.ini index f76bae1..c4e94e4 100644 --- a/examples/esp32/WebSocketClientSSLBundle/platformio.ini +++ b/examples/esp32/WebSocketClientSSLBundle/platformio.ini @@ -16,9 +16,10 @@ monitor_speed = 115200 upload_speed = 921600 build_flags = -DCORE_DEBUG_LEVEL=5 +lib_deps = ../../../src extra_scripts = pre:run_gen_script.py board_build.embed_txtfiles = - x509_crt_bundle + data/cert/x509_crt_bundle.bin diff --git a/examples/esp32/WebSocketClientSSLBundle/run_gen_script.py b/examples/esp32/WebSocketClientSSLBundle/run_gen_script.py index be5ba41..88505db 100644 --- a/examples/esp32/WebSocketClientSSLBundle/run_gen_script.py +++ b/examples/esp32/WebSocketClientSSLBundle/run_gen_script.py @@ -1,4 +1,6 @@ Import("env") env.Execute("$PYTHONEXE -m pip install cryptography") -env.Execute("$PYTHONEXE gen_crt_bundle.py --input cacrt_all.pem") \ No newline at end of file +env.Execute("$PYTHONEXE gen_crt_bundle.py --input cacrt_all.pem") +env.Execute("mkdir -p data/cert") +env.Execute("mv -f x509_crt_bundle data/cert/x509_crt_bundle.bin") diff --git a/examples/esp32/WebSocketClientSSLBundle/src/main.cpp b/examples/esp32/WebSocketClientSSLBundle/src/main.cpp index bfa3197..1765614 100644 --- a/examples/esp32/WebSocketClientSSLBundle/src/main.cpp +++ b/examples/esp32/WebSocketClientSSLBundle/src/main.cpp @@ -1,91 +1,127 @@ -#include "main.hpp" +/* + * main.cpp + * + * Created on: 15.06.2024 + * + */ -void setup() -{ - Serial.begin(115200); - // Serial.setDebugOutput(true); +#include +#include +#include - Serial.println(); - Serial.println(); - Serial.println(); +#include - WiFi.mode(WIFI_STA); - WiFiMulti.addAP(SSID, PASS); +extern const uint8_t rootca_crt_bundle_start[] asm( + "_binary_data_cert_x509_crt_bundle_bin_start"); - // wait for WiFi connection - Serial.print("Waiting for WiFi to connect..."); - while ((WiFiMulti.run() != WL_CONNECTED)) { - Serial.print("."); - } - Serial.println(" connected"); +WiFiMulti wifiMulti; +WebSocketsClient webSocket; - setClock(); +#define USE_SERIAL Serial + +void setClock() { + configTime(0, 0, "pool.ntp.org", "time.nist.gov"); + + USE_SERIAL.print(F("Waiting for NTP time sync: ")); + time_t nowSecs = time(nullptr); + while(nowSecs < 8 * 3600 * 2) { + delay(500); + USE_SERIAL.print(F(".")); + yield(); + nowSecs = time(nullptr); + } + + USE_SERIAL.println(); + struct tm timeinfo; + gmtime_r(&nowSecs, &timeinfo); + USE_SERIAL.print(F("Current time: ")); + USE_SERIAL.print(asctime(&timeinfo)); } -void loop() -{ - WiFiClientSecure *client = new WiFiClientSecure; - if(client) - { - client -> setUseCertBundle(true); - { - // Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is - HTTPClient https; - - Serial.print("[HTTPS] begin...\n"); - if (https.begin(*client, SSL_TEST_URL)) { // HTTPS - Serial.print("[HTTPS] GET...\n"); - // start connection and send HTTP header - int httpCode = https.GET(); - - // httpCode will be negative on error - if (httpCode > 0) { - // HTTP header has been sent 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); - } - } else { - Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); +void hexdump(const void * mem, uint32_t len, uint8_t cols = 16) { + const uint8_t * src = (const uint8_t *)mem; + USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len); + for(uint32_t i = 0; i < len; i++) { + if(i % cols == 0) { + USE_SERIAL.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i); } - - https.end(); - } else { - Serial.printf("[HTTPS] Unable to connect\n"); - } + USE_SERIAL.printf("%02X ", *src); + src++; + } + USE_SERIAL.printf("\n"); +} - // End extra scoping block +void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { + switch(type) { + case WStype_DISCONNECTED: + USE_SERIAL.printf("[WSc] Disconnected!\n"); + break; + case WStype_CONNECTED: + USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload); + + // send message to server when Connected + webSocket.sendTXT("Connected"); + break; + case WStype_TEXT: + USE_SERIAL.printf("[WSc] get text: %s\n", payload); + + // send message to server + // webSocket.sendTXT("message here"); + break; + case WStype_BIN: + USE_SERIAL.printf("[WSc] get binary length: %u\n", length); + hexdump(payload, length); + + // send data to server + // webSocket.sendBIN(payload, length); + break; + case WStype_ERROR: + case WStype_FRAGMENT_TEXT_START: + case WStype_FRAGMENT_BIN_START: + case WStype_FRAGMENT: + case WStype_FRAGMENT_FIN: + break; } - - delete client; - } else { - Serial.println("Unable to create client"); - } - - Serial.println(); - Serial.println("Waiting 10s before the next round..."); - delay(60000); } -void setClock() -{ - configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - Serial.print(F("Waiting for NTP time sync: ")); - time_t nowSecs = time(nullptr); - while (nowSecs < 8 * 3600 * 2) { - delay(500); - Serial.print(F(".")); - yield(); - nowSecs = time(nullptr); - } - - Serial.println(); - struct tm timeinfo; - gmtime_r(&nowSecs, &timeinfo); - Serial.print(F("Current time: ")); - Serial.print(asctime(&timeinfo)); +void setup() { + USE_SERIAL.begin(115200); + + USE_SERIAL.setDebugOutput(true); + + USE_SERIAL.println(); + USE_SERIAL.println(); + USE_SERIAL.println(); + + for(uint8_t t = 4; t > 0; t--) { + USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t); + USE_SERIAL.flush(); + delay(1000); + } + + wifiMulti.addAP("SSID", "WIFI_PASSPHRASE"); + + // WiFi.disconnect(); + while(wifiMulti.run() != WL_CONNECTED) { + delay(100); + } + + setClock(); + + // server address, port and URL. This server can be flakey. + // Expected response: Request served by 0123456789abcdef + webSocket.beginSslWithBundle("echo.websocket.org", 443, "/", rootca_crt_bundle_start, ""); + + // event handler + webSocket.onEvent(webSocketEvent); + + // use HTTP Basic Authorization this is optional enable if needed + // webSocket.setAuthorization("user", "Password"); + + // try ever 5000 again if connection has failed + webSocket.setReconnectInterval(5000); +} + +void loop() { + webSocket.loop(); }