-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from LeeLeahy2/9-2
9_2: Add example 9_2_PPP_State_Machine for Arduino v3
- Loading branch information
Showing
5 changed files
with
612 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
Example_Sketches/9_2_PPP_State_Machine/9_2_PPP_State_Machine.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <PPP.h> | ||
|
||
//---------------------------------------- | ||
|
||
#define ARDUINO_EVENT_LARA_ON ((arduino_event_id_t)-1) | ||
|
||
//---------------------------------------- | ||
|
||
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; | ||
|
||
//---------------------------------------- | ||
// System initialization | ||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
|
||
// Initialize the network | ||
Network.onEvent(networkOnEvent); | ||
|
||
// Set the HTTPS parameters | ||
google.hostName = "www.google.com"; | ||
google.url = "/"; | ||
google.isInternetAvailable = pppIsInternetAvailable; | ||
// google.suppressFirstPageOutput = true; | ||
|
||
SparkFun.hostName = "www.SparkFun.com"; | ||
SparkFun.url = "/"; | ||
SparkFun.isInternetAvailable = pppIsInternetAvailable; | ||
// SparkFun.suppressFirstPageOutput = true; | ||
|
||
// Start LARA | ||
pppEvent(ARDUINO_EVENT_LARA_ON); | ||
} | ||
|
||
//---------------------------------------- | ||
// Main loop | ||
void loop() | ||
{ | ||
pppUpdate(); | ||
httpUpdate(&google, 80, 15 * 1000); | ||
httpUpdate(&SparkFun, 80, 16 * 1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//---------------------------------------- | ||
// Process network events | ||
void networkOnEvent(arduino_event_id_t event, arduino_event_info_t info) | ||
{ | ||
IPAddress ipv4; | ||
|
||
switch (event) | ||
{ | ||
default: | ||
if (event != ARDUINO_EVENT_PPP_GOT_IP6) | ||
Serial.printf("ERROR: Unknown Arduino event: %d\r\n", event); | ||
break; | ||
|
||
case ARDUINO_EVENT_PPP_START: | ||
case ARDUINO_EVENT_PPP_CONNECTED: | ||
case ARDUINO_EVENT_PPP_GOT_IP: | ||
case ARDUINO_EVENT_PPP_LOST_IP: | ||
case ARDUINO_EVENT_PPP_DISCONNECTED: | ||
case ARDUINO_EVENT_PPP_STOP: | ||
pppEvent(event); | ||
break; | ||
} | ||
} |
Oops, something went wrong.