Skip to content

Commit

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

#include "esp_netif.h"
#include "secrets.h"

bool RTK_CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC = false;

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

typedef bool (* IS_CONNECTED)();

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

typedef struct _HTTP_CLIENT_CONNECTION
{
IS_CONNECTED isInternetAvailable;
const char * hostName;
const char * url;

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

HTTP_CLIENT_CONNECTION google;
HTTP_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.hostName = "www.google.com";
google.url = "/";
google.isInternetAvailable = wifiIsInternetAvailable;
// google.suppressFirstPageOutput = true;

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

//----------------------------------------
void loop()
{
httpUpdate(&google, 80, 15 * 1000);
httpUpdate(&SparkFun, 80, 16 * 1000);
}
178 changes: 178 additions & 0 deletions Example_Sketches/4_5_WiFi_HTTP/HTTP.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
//----------------------------------------

enum HTTP_STATE
{
HTTP_STATE_WAIT_NETWORK = 0,
HTTP_STATE_CONNECT_HOST,
HTTP_STATE_WAIT_RESPONSE,
HTTP_STATE_READ_PAGE,
HTTP_STATE_CLOSE_PAGE,
HTTP_STATE_INIT_DELAY,
HTTP_STATE_DELAY,
};

//----------------------------------------
// Read the www.google.com web-page
void httpUpdate(HTTP_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 HTTP_STATE_WAIT_NETWORK:
if (hcc->isInternetAvailable())
hcc->hccState = HTTP_STATE_CONNECT_HOST;
break;

// Connect to Google
case HTTP_STATE_CONNECT_HOST:
// Has the network failed
if (!hcc->isInternetAvailable())
{
hcc->hccState = HTTP_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 = HTTP_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 = HTTP_STATE_WAIT_RESPONSE;
}
break;

case HTTP_STATE_WAIT_RESPONSE:
// Has the network failed or the connection closed
if ((!hcc->isInternetAvailable()) || (!hcc->client.connected()))
{
hcc->hccState = HTTP_STATE_CLOSE_PAGE;
break;
}

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

// Read the web-page
case HTTP_STATE_READ_PAGE:
// Has the network failed or the connection closed
if ((!hcc->isInternetAvailable()) || (!hcc->client.connected()))
{
hcc->hccState = HTTP_STATE_CLOSE_PAGE;
break;
}

// Check for end-of-file
dataBytes = hcc->client.available();
if (hcc->tagEndFound && (!dataBytes))
{
hcc->suppressFirstPageOutput = true;
hcc->hccState = HTTP_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 = HTTP_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 HTTP_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 = HTTP_STATE_INIT_DELAY;
break;

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

// Delay for a while
case HTTP_STATE_DELAY:
// Delay before attempting again
if ((millis() - hcc->timer) >= delayMsec)
hcc->hccState = HTTP_STATE_WAIT_NETWORK;
break;
}
}
23 changes: 23 additions & 0 deletions Example_Sketches/4_5_WiFi_HTTP/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;
}
}
85 changes: 85 additions & 0 deletions Example_Sketches/4_5_WiFi_HTTP/WiFi.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
bool wifiConnected;

//----------------------------------------
// Determine if WiFi controller has an IP address
bool wifiIsInternetAvailable()
{
return wifiConnected;
}

//----------------------------------------
void wifiEvent(arduino_event_id_t event, arduino_event_info_t info)
{
char ssid[sizeof(info.wifi_sta_connected.ssid) + 1];
IPAddress ipAddress;

switch (event)
{
default:
Serial.printf("ERROR: Unknown WiFi event: %d\r\n", event);
break;

case ARDUINO_EVENT_WIFI_OFF:
Serial.println("WiFi Off");
wifiConnected = false;
break;

case ARDUINO_EVENT_WIFI_READY:
Serial.println("WiFi Ready");
wifiConnected = false;
break;

case ARDUINO_EVENT_WIFI_SCAN_DONE:
Serial.println("WiFi Scan Done");
// wifi_event_sta_scan_done_t info.wifi_scan_done;
wifiConnected = false;
break;

case ARDUINO_EVENT_WIFI_STA_START:
Serial.println("WiFi STA Started");
wifiConnected = false;
break;

case ARDUINO_EVENT_WIFI_STA_STOP:
Serial.println("WiFi STA Stopped");
wifiConnected = false;
break;

case ARDUINO_EVENT_WIFI_STA_CONNECTED:
memcpy(ssid, info.wifi_sta_connected.ssid, info.wifi_sta_connected.ssid_len);
ssid[info.wifi_sta_connected.ssid_len] = 0;
Serial.printf("WiFi STA connected to %s\r\n", ssid);
wifiConnected = false;
break;

case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
memcpy(ssid, info.wifi_sta_disconnected.ssid, info.wifi_sta_disconnected.ssid_len);
ssid[info.wifi_sta_disconnected.ssid_len] = 0;
Serial.printf("WiFi STA disconnected from %s\r\n", ssid);
// wifi_event_sta_disconnected_t info.wifi_sta_disconnected;
wifiConnected = false;
break;

case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE:
Serial.println("WiFi STA Auth Mode Changed");
// wifi_event_sta_authmode_change_t info.wifi_sta_authmode_change;
wifiConnected = false;
break;

case ARDUINO_EVENT_WIFI_STA_GOT_IP:
ipAddress = WiFi.localIP();
Serial.print("WiFi STA Got IP: ");
Serial.println(ipAddress);
wifiConnected = true;
break;

case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
Serial.println("WiFi STA Got **IP6**");
break;

case ARDUINO_EVENT_WIFI_STA_LOST_IP:
Serial.println("WiFi STA Lost IP");
wifiConnected = false;
break;
}
}
Loading

0 comments on commit f83ae42

Please sign in to comment.