Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 134 additions & 30 deletions docs/examples/esp32/plain_upload.ino
Original file line number Diff line number Diff line change
@@ -1,50 +1,154 @@
#include <HTTPClient.h>
#include <NetworkClientSecure.h>
const char *rootCACertificate = R"string_literal(
-----BEGIN CERTIFICATE-----
MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB
iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl
cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV
BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAw
MjAxMDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBiDELMAkGA1UEBhMCVVMxEzARBgNV
BAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0plcnNleSBDaXR5MR4wHAYDVQQKExVU
aGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNVBAMTJVVTRVJUcnVzdCBSU0EgQ2Vy
dGlmaWNhdGlvbiBBdXRob3JpdHkwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIK
AoICAQCAEmUXNg7D2wiz0KxXDXbtzSfTTK1Qg2HiqiBNCS1kCdzOiZ/MPans9s/B
3PHTsdZ7NygRK0faOca8Ohm0X6a9fZ2jY0K2dvKpOyuR+OJv0OwWIJAJPuLodMkY
tJHUYmTbf6MG8YgYapAiPLz+E/CHFHv25B+O1ORRxhFnRghRy4YUVD+8M/5+bJz/
Fp0YvVGONaanZshyZ9shZrHUm3gDwFA66Mzw3LyeTP6vBZY1H1dat//O+T23LLb2
VN3I5xI6Ta5Mirdcm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ALtRZh8tSQZXq9EfX7mRB
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 <WiFi.h>
#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("Accept", "application/json");
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);
}
}
Loading