Skip to content

Commit

Permalink
Fix example build
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz89 committed Jun 16, 2024
1 parent 66a15c7 commit 9e1e857
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 208 deletions.
2 changes: 1 addition & 1 deletion examples/esp32/WebSocketClientSSLBundle/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
.vscode/ipch
*secret*
!*secrets.hpp.template
*x509_crt_bundle
*x509_crt_bundle.bin
108 changes: 0 additions & 108 deletions examples/esp32/WebSocketClientSSLBundle/example.cpp

This file was deleted.

17 changes: 0 additions & 17 deletions examples/esp32/WebSocketClientSSLBundle/include/main.hpp

This file was deleted.

This file was deleted.

3 changes: 2 additions & 1 deletion examples/esp32/WebSocketClientSSLBundle/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion examples/esp32/WebSocketClientSSLBundle/run_gen_script.py
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")
192 changes: 114 additions & 78 deletions examples/esp32/WebSocketClientSSLBundle/src/main.cpp
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();
}

0 comments on commit 9e1e857

Please sign in to comment.