Skip to content

Commit

Permalink
Merge pull request #4 from LeeLeahy2/4-6
Browse files Browse the repository at this point in the history
4_6: Add example 4_6_WiFi_HTTPS for Arduino v3
  • Loading branch information
nseidle authored Aug 20, 2024
2 parents f83ae42 + f70023c commit 010b9b9
Show file tree
Hide file tree
Showing 8 changed files with 572 additions and 0 deletions.
79 changes: 79 additions & 0 deletions Example_Sketches/4_6_WiFi_HTTPS/4_6_WiFi_HTTPS.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <Network.h>
#include <NetworkClientSecure.h>
#include <WiFi.h>
#include <WiFiGeneric.h>
#include <WiFiMulti.h>

#include "Cert_AWS.h"
#include "Cert_Google.h"

#include "secrets.h"

bool RTK_CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC = false;

//----------------------------------------

typedef bool (* IS_CONNECTED)();

//----------------------------------------

typedef struct _HTTPS_CLIENT_CONNECTION
{
IS_CONNECTED isInternetAvailable;
const char * rootCA;
const char * hostName;
const char * url;

// The following parameters are initialized to zero (false)
bool suppressFirstPageOutput;
NetworkClientSecure client;
int headerLength;
int pageLength;
uint8_t hccState;
bool tagEndFound;
int tagEndOffset;
bool tagStartFound;
int tagStartOffset;
uint32_t timer;
uint8_t buffer[2048];
} HTTPS_CLIENT_CONNECTION;

HTTPS_CLIENT_CONNECTION google;
HTTPS_CLIENT_CONNECTION SparkFun;

WiFiMulti wifiMulti;

//----------------------------------------
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println("SparkFun RTK EVK - Test Sketch");

// Initialize the network
Network.onEvent(networkOnEvent);

// Initialize WiFi
wifiMulti.addAP(wifiSSID, wifiPassword);
wifiMulti.run();

// Set the HTTPS parameters
google.rootCA = caGoogle;
google.hostName = "www.google.com";
google.url = "/";
google.isInternetAvailable = wifiIsInternetAvailable;
google.suppressFirstPageOutput = true;

SparkFun.rootCA = caAmazonWebServices;
SparkFun.hostName = "www.sparkfun.com";
SparkFun.url = "/";
SparkFun.isInternetAvailable = wifiIsInternetAvailable;
SparkFun.suppressFirstPageOutput = true;
}

//----------------------------------------
void loop()
{
httpsUpdate(&google, 443, 15 * 1000);
httpsUpdate(&SparkFun, 443, 16 * 1000);
}
27 changes: 27 additions & 0 deletions Example_Sketches/4_6_WiFi_HTTPS/Cert_AWS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//----------------------------------------
// AWS certificate for PointPerfect API
// CN=Amazon Root CA 1,O=Amazon,C=US PEM
// See: https://www.amazontrust.com/repository/
const char * caAmazonWebServices = R"=====(
-----BEGIN CERTIFICATE-----
MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF
ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6
b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL
MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv
b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj
ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM
9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw
IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6
VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L
93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm
jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC
AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA
A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI
U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs
N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv
o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU
5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy
rqXRfboQnoZsG4q5WTP468SQvvG5
-----END CERTIFICATE-----
)=====";

35 changes: 35 additions & 0 deletions Example_Sketches/4_6_WiFi_HTTPS/Cert_Google.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//----------------------------------------
// Google certificate
// /C=US/O=Google Trust Services LLC/CN=GTS Root R1
const char * caGoogle =
"-----BEGIN CERTIFICATE-----\n"
"MIIFYjCCBEqgAwIBAgIQd70NbNs2+RrqIQ/E8FjTDTANBgkqhkiG9w0BAQsFADBX\n"
"MQswCQYDVQQGEwJCRTEZMBcGA1UEChMQR2xvYmFsU2lnbiBudi1zYTEQMA4GA1UE\n"
"CxMHUm9vdCBDQTEbMBkGA1UEAxMSR2xvYmFsU2lnbiBSb290IENBMB4XDTIwMDYx\n"
"OTAwMDA0MloXDTI4MDEyODAwMDA0MlowRzELMAkGA1UEBhMCVVMxIjAgBgNVBAoT\n"
"GUdvb2dsZSBUcnVzdCBTZXJ2aWNlcyBMTEMxFDASBgNVBAMTC0dUUyBSb290IFIx\n"
"MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAthECix7joXebO9y/lD63\n"
"ladAPKH9gvl9MgaCcfb2jH/76Nu8ai6Xl6OMS/kr9rH5zoQdsfnFl97vufKj6bwS\n"
"iV6nqlKr+CMny6SxnGPb15l+8Ape62im9MZaRw1NEDPjTrETo8gYbE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QADggEBADSkHrEoo9C0dhemMXoh6dFSPsjbdBZBiLg9\n"
"NR3t5P+T4Vxfq7vqfM/b5A3Ri1fyJm9bvhdGaJQ3b2t6yMAYN/olUazsaL+yyEn9\n"
"WprKASOshIArAoyZl+tJaox118fessmXn1hIVw41oeQa1v1vg4Fv74zPl6/AhSrw\n"
"9U5pCZEt4Wi4wStz6dTZ/CLANx8LZh1J7QJVj2fhMtfTJr9w4z30Z209fOU0iOMy\n"
"+qduBmpvvYuR7hZL6Dupszfnw0Skfths18dG9ZKb59UhvmaSGZRVbNQpsg3BZlvi\n"
"d0lIKO2d1xozclOzgjXPYovJJIultzkMu34qQb9Sz/yilrbCgj8=\n"
"-----END CERTIFICATE-----\n";
184 changes: 184 additions & 0 deletions Example_Sketches/4_6_WiFi_HTTPS/HTTPS.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
//----------------------------------------

enum HTTPS_STATE
{
HTTPS_STATE_WAIT_NETWORK = 0,
HTTPS_STATE_CONNECT_HOST,
HTTPS_STATE_WAIT_RESPONSE,
HTTPS_STATE_READ_PAGE,
HTTPS_STATE_CLOSE_PAGE,
HTTPS_STATE_INIT_DELAY,
HTTPS_STATE_DELAY,
};

//----------------------------------------
// Read the remote host web-page
void httpsUpdate(HTTPS_CLIENT_CONNECTION * hcc, uint16_t portNumber, uint32_t delayMsec)
{
int bytesRead;
int dataBytes;
const char * tagEnd = "</html>";
const char * tagStart = "<html";

switch (hcc->hccState)
{
// Wait for the PPP connection
case HTTPS_STATE_WAIT_NETWORK:
if (hcc->isInternetAvailable())
{
hcc->client.setCACert(hcc->rootCA);
hcc->hccState = HTTPS_STATE_CONNECT_HOST;
}
break;

// Connect to the remote host
case HTTPS_STATE_CONNECT_HOST:
// Has the network failed
if (!hcc->isInternetAvailable())
{
Serial.println("Link failed");
hcc->hccState = HTTPS_STATE_WAIT_NETWORK;
break;
}

// Connect to the remote host
if (!hcc->client.connect(hcc->hostName, portNumber))
{
Serial.printf("Connection to %s:%d failed!\r\n", hcc->hostName, portNumber);
hcc->hccState = HTTPS_STATE_INIT_DELAY;
}
else
{
Serial.printf("Connection to %s:%d successful\r\n", hcc->hostName, portNumber);

// Request the web page
hcc->client.printf("GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", hcc->url, hcc->hostName);

// No data read yet
hcc->headerLength = 0;
hcc->pageLength = 0;
hcc->tagEndFound = false;
hcc->tagEndOffset = 0;
hcc->tagStartFound = false;
hcc->tagStartOffset = 0;
hcc->hccState = HTTPS_STATE_WAIT_RESPONSE;
}
break;

case HTTPS_STATE_WAIT_RESPONSE:
// Has the network failed or the connection closed
if ((!hcc->isInternetAvailable()) || (!hcc->client.connected()))
{
Serial.println("Link failed or client disconnected");
hcc->hccState = HTTPS_STATE_CLOSE_PAGE;
break;
}

// Wait for the response
if (hcc->client.available())
hcc->hccState = HTTPS_STATE_READ_PAGE;
break;

// Read the web-page
case HTTPS_STATE_READ_PAGE:
// Has the network failed or the connection closed
if ((!hcc->isInternetAvailable()) || (!hcc->client.connected()))
{
Serial.println("Link failed or client disconnected");
hcc->hccState = HTTPS_STATE_CLOSE_PAGE;
break;
}

// Check for end-of-file
dataBytes = hcc->client.available();
if (hcc->tagEndFound && (!dataBytes))
{
hcc->suppressFirstPageOutput = true;
hcc->hccState = HTTPS_STATE_CLOSE_PAGE;
}

// Determine if data was received
else if (dataBytes > 0)
{
// Read as much data as possible
if (dataBytes > sizeof(hcc->buffer))
dataBytes = sizeof(hcc->buffer);
bytesRead = hcc->client.read(hcc->buffer, dataBytes);

// Check for a read error
if (bytesRead < 0)
{
Serial.println("\r\n\nRead error!");
hcc->hccState = HTTPS_STATE_CLOSE_PAGE;
break;
}

// Display the web page on the first pass
if (!hcc->suppressFirstPageOutput)
Serial.write(hcc->buffer, bytesRead);

// Check for the start-of-page
dataBytes = 0;
if (!hcc->tagStartFound)
{
for (; dataBytes < bytesRead; dataBytes++)
if ((!hcc->tagStartFound) && (hcc->buffer[dataBytes] == tagStart[hcc->tagStartOffset]))
{
hcc->tagStartOffset += 1;
hcc->tagStartFound = (hcc->tagStartOffset == strlen(tagStart));
if (hcc->tagStartFound)
{
hcc->headerLength = hcc->pageLength
+ dataBytes
- strlen(tagStart);
dataBytes += 1;
hcc->pageLength = - (dataBytes + strlen(tagStart));
break;
}
}
else
hcc->tagStartOffset = 0;
}

// Account for the data read
hcc->pageLength += bytesRead;

// Check for the end-of-page
if (hcc->tagStartFound)
{
for (; dataBytes < bytesRead; dataBytes++)
if ((!hcc->tagEndFound) && (hcc->buffer[dataBytes] == tagEnd[hcc->tagEndOffset]))
{
hcc->tagEndOffset += 1;
hcc->tagEndFound = (hcc->tagEndOffset == strlen(tagEnd));
}
else
hcc->tagEndOffset = 0;
}
}
break;

// Close the socket
case HTTPS_STATE_CLOSE_PAGE:
// Done with this network client
Serial.printf("\rRead %d header bytes and %d page bytes from %s\r\n",
hcc->headerLength, hcc->pageLength, hcc->hostName);
hcc->client.stop();
hcc->hccState = HTTPS_STATE_INIT_DELAY;
break;

// Start the delay
case HTTPS_STATE_INIT_DELAY:
Serial.println("----------");
hcc->timer = millis();
hcc->hccState = HTTPS_STATE_DELAY;
break;

// Delay for a while
case HTTPS_STATE_DELAY:
// Delay before attempting again
if ((millis() - hcc->timer) >= delayMsec)
hcc->hccState = HTTPS_STATE_WAIT_NETWORK;
break;
}
}
23 changes: 23 additions & 0 deletions Example_Sketches/4_6_WiFi_HTTPS/Network.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//----------------------------------------
void networkOnEvent(arduino_event_id_t event, arduino_event_info_t info)
{
switch (event)
{
default:
break;

case ARDUINO_EVENT_WIFI_OFF:
case ARDUINO_EVENT_WIFI_READY:
case ARDUINO_EVENT_WIFI_SCAN_DONE:
case ARDUINO_EVENT_WIFI_STA_START:
case ARDUINO_EVENT_WIFI_STA_STOP:
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE:
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
wifiEvent(event, info);
break;
}
}
Loading

0 comments on commit 010b9b9

Please sign in to comment.