From fffc2ae9fcacd5c48ce7ebb007f02f5e3bce5a7e Mon Sep 17 00:00:00 2001 From: Nate Baird <85893453+radiantBear@users.noreply.github.com> Date: Tue, 17 Mar 2026 02:00:09 +0000 Subject: [PATCH 1/2] Flesh out esp32 example code --- docs/examples/esp32/plain_upload.ino | 163 ++++++++++++++++++++++----- 1 file changed, 133 insertions(+), 30 deletions(-) diff --git a/docs/examples/esp32/plain_upload.ino b/docs/examples/esp32/plain_upload.ino index 72e21f3..7402a38 100644 --- a/docs/examples/esp32/plain_upload.ino +++ b/docs/examples/esp32/plain_upload.ino @@ -1,50 +1,153 @@ #include +#include +const char *rootCACertificate = R"string_literal( +-----BEGIN CERTIFICATE----- +MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB +iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl +cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV +BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAw +MjAxMDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBiDELMAkGA1UEBhMCVVMxEzARBgNV +BAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0plcnNleSBDaXR5MR4wHAYDVQQKExVU +aGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNVBAMTJVVTRVJUcnVzdCBSU0EgQ2Vy +dGlmaWNhdGlvbiBBdXRob3JpdHkwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIK +AoICAQCAEmUXNg7D2wiz0KxXDXbtzSfTTK1Qg2HiqiBNCS1kCdzOiZ/MPans9s/B +3PHTsdZ7NygRK0faOca8Ohm0X6a9fZ2jY0K2dvKpOyuR+OJv0OwWIJAJPuLodMkY +tJHUYmTbf6MG8YgYapAiPLz+E/CHFHv25B+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 +XHlKYC6SQK5MNyosycdiyA5d9zZbyuAlJQG03RoHnHcAP9Dc1ew91Pq7P8yF1m9/ +qS3fuQL39ZeatTXaw2ewh0qpKJ4jjv9cJ2vhsE/zB+4ALtRZh8tSQZXq9EfX7mRB +VXyNWQKV3WKdwrnuWih0hKWbt5DHDAff9Yk2dDLWKMGwsAvgnEzDHNb842m1R0aB +L6KCq9NjRHDEjf8tM7qtj3u1cIiuPhnPQCjY/MiQu12ZIvVS5ljFH4gxQ+6IHdfG +jjxDah2nGN59PRbxYvnKkKj9 +-----END CERTIFICATE----- +)string_literal"; // Root certificate for upload_url. Valid until 2038 +const char *upload_url = "https://eecs.engineering.oregonstate.edu/education/damsecure/public/api/data"; +const char *api_token = "Bearer 1|lBIopC6bcRzL7Jb2yopy74yTJkVltziyLVNX8V3Gc4eb41df"; + #include +#define EAP_IDENTITY "onid@oregonstate.edu" +#define EAP_USERNAME "onid@oregonstate.edu" +#define EAP_PASSWORD "eduroam-password" +const char *ssid = "eduroam"; + + +void connectWiFi(); + +void uploadData(int data); +void sendRequest(NetworkClientSecure *client, int data); +void processResponse(HTTPClient *https, int httpCode); + + +void setup() { + Serial.begin(115200); + delay(10); + + connectWiFi(); +} -#define API_URL "https://eecs.engineering.oregonstate.edu/education/damsecure/public/api/data" -#define TOKEN "" -#define SSID "" -#define PASSWORD "" -const char* ca = \ -"-----BEGIN CERTIFICATE-----\n"\ -"..."\ -"-----END CERTIFICATE-----\n"; +void loop() { + static int iterations = 1; + iterations++; + + uploadData(iterations); -int getNextNumber() { - static int number = 0; - return number++; + delay(1000 * 15); } -void connectToWiFi() { + +void connectWiFi() { + int counter = 0; + + Serial.printf("Connecting to network: %s\n", ssid); + WiFi.disconnect(true); - WiFi.begin(SSID, PASSWORD); - Serial.print("Connecting to WiFi"); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, WPA2_AUTH_PEAP, EAP_IDENTITY, EAP_USERNAME, EAP_PASSWORD); + while (WiFi.status() != WL_CONNECTED) { - Serial.print('.'); delay(500); + Serial.print("."); + counter++; + if (counter >= 60) { + // Unable to connect after 30 seconds timeout - reset board + ESP.restart(); + } } + + Serial.println("WiFi connected"); } -void setup() { - Serial.begin(115200); - connectToWiFi(); - HTTPClient http; - http.begin(API_URL, ca); +void uploadData(int data) { + NetworkClientSecure *client = new NetworkClientSecure; - http.addHeader("Accept", "application/json"); - http.addHeader("Content-Type", "text/plain"); - http.addHeader("Authorization", "Bearer " TOKEN); + if (!client) { + Serial.println("Unable to allocate client"); + return; + } - int status = http.POST(String(getNextNumber())); + client->setCACert(rootCACertificate); + sendRequest(client, data); + delete client; +} - Serial.print("Status: "); - Serial.println(status); - Serial.print("Response: "); - Serial.println(http.getString()); +void sendRequest(NetworkClientSecure *client, int data) { + // Using a different scoping block (function) for HTTPClient https to make sure it is + // destroyed before NetworkClientSecure *client is + HTTPClient https; + + Serial.println("Connecting to DamSecure..."); + if (!https.begin(*client, upload_url)) { + Serial.println("Unable to connect to DamSecure"); + return; + } + + Serial.println("Making POST request..."); + https.addHeader("Authorization", api_token); + https.addHeader("Content-Type", "text/plain"); + int httpCode = https.POST(String(data)); - http.end(); - WiFi.disconnect(); + processResponse(&https, httpCode); + https.end(); +} + + +void processResponse(HTTPClient *https, int httpCode) { + // httpCode will be negative on error + if (httpCode <= 0) { + Serial.print("POST failed, error: "); + Serial.println(https->errorToString(httpCode).c_str()); + return; + } + + // HTTP request has been sent and server response header has been handled + Serial.printf("POST response code: %d\n", httpCode); + + // file found at server + if ( + httpCode == HTTP_CODE_OK || + httpCode == HTTP_CODE_CREATED || + httpCode == HTTP_CODE_MOVED_PERMANENTLY + ) { + String payload = https->getString(); + Serial.print("POST accepted, id: "); + Serial.println(payload); + } } From 02a1cb3b1ea29581cb3f8a6fa4b0ac2e77c4ccd4 Mon Sep 17 00:00:00 2001 From: Nate Baird <85893453+radiantBear@users.noreply.github.com> Date: Tue, 17 Mar 2026 02:03:47 +0000 Subject: [PATCH 2/2] Add accept header --- docs/examples/esp32/plain_upload.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/examples/esp32/plain_upload.ino b/docs/examples/esp32/plain_upload.ino index 7402a38..a3b18fa 100644 --- a/docs/examples/esp32/plain_upload.ino +++ b/docs/examples/esp32/plain_upload.ino @@ -120,6 +120,7 @@ void sendRequest(NetworkClientSecure *client, int data) { } Serial.println("Making POST request..."); + https.addHeader("Accept", "application/json"); https.addHeader("Authorization", api_token); https.addHeader("Content-Type", "text/plain"); int httpCode = https.POST(String(data));