-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
120 additions
and
208 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 |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
.vscode/ipch | ||
*secret* | ||
!*secrets.hpp.template | ||
*x509_crt_bundle | ||
*x509_crt_bundle.bin |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
2 changes: 0 additions & 2 deletions
2
examples/esp32/WebSocketClientSSLBundle/include/secrets.hpp.template
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
Import("env") | ||
|
||
env.Execute("$PYTHONEXE -m pip install cryptography") | ||
env.Execute("$PYTHONEXE gen_crt_bundle.py --input cacrt_all.pem") | ||
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") |
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 |
---|---|---|
@@ -1,91 +1,127 @@ | ||
#include "main.hpp" | ||
/* | ||
* main.cpp | ||
* | ||
* Created on: 15.06.2024 | ||
* | ||
*/ | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
// Serial.setDebugOutput(true); | ||
#include <Arduino.h> | ||
#include <WiFi.h> | ||
#include <WiFiMulti.h> | ||
|
||
Serial.println(); | ||
Serial.println(); | ||
Serial.println(); | ||
#include <WebSocketsClient.h> | ||
|
||
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(); | ||
} |