14
14
#include < Network.h> // Built-in
15
15
#include < WiFi.h> // Built-in
16
16
17
- #include < esp_wifi.h> // IDF built-in
17
+ #include " esp_now.h" // IDF built-in
18
+ #include " esp_mac.h" // IDF built-in
19
+ #include < esp_wifi.h> // IDF built-in
20
+ #include " esp_wifi_types.h" // IDF built-in
18
21
19
22
#include < secrets.h> // Host name, SSIDs and passwords
20
23
@@ -23,6 +26,39 @@ bool RTK_CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC = false;
23
26
#define systemPrintf Serial.printf
24
27
#define systemPrintln Serial.println
25
28
29
+ // ****************************************
30
+ // ESP-NOW
31
+ // ****************************************
32
+
33
+ bool espNowDebug;
34
+ bool espNowDisplay;
35
+ bool espNowVerbose;
36
+
37
+ typedef enum
38
+ {
39
+ ESPNOW_OFF = 0 ,
40
+ ESPNOW_BROADCASTING,
41
+ ESPNOW_PAIRING,
42
+ ESPNOW_MAC_RECEIVED,
43
+ ESPNOW_PAIRED,
44
+ ESPNOW_MAX
45
+ } ESP_NOW_STATE;
46
+
47
+ // Create a struct for ESP NOW pairing
48
+ typedef struct PairMessage
49
+ {
50
+ uint8_t macAddress[6 ];
51
+ bool encrypt;
52
+ uint8_t channel;
53
+ uint8_t crc; // Simple check - add MAC together and limit to 8 bit
54
+ } PairMessage;
55
+
56
+ // ****************************************
57
+ // Web Server
58
+ // ****************************************
59
+
60
+ bool webServerDebug;
61
+
26
62
// ****************************************
27
63
// WiFi class
28
64
// ****************************************
@@ -44,6 +80,7 @@ class RTK_WIFI
44
80
private:
45
81
46
82
WIFI_CHANNEL_t _apChannel; // Channel required for soft AP, zero (0) use _channel
83
+ int16_t _apCount; // The number or remote APs detected in the WiFi network
47
84
IPAddress _apDnsAddress; // DNS IP address to use while translating names into IP addresses
48
85
IPAddress _apFirstDhcpAddress; // First IP address to use for DHCP
49
86
IPAddress _apGatewayAddress;// IP address of the gateway to the larger network (internet?)
@@ -65,12 +102,18 @@ class RTK_WIFI
65
102
uint8_t _staMacAddress[6 ]; // MAC address of the station
66
103
const char * _staRemoteApSsid; // SSID of remote AP
67
104
const char * _staRemoteApPassword; // Password of remote AP
68
- WIFI_ACTION_t _started; // Components that are started and running
105
+ volatile WIFI_ACTION_t _started; // Components that are started and running
69
106
WIFI_CHANNEL_t _stationChannel; // Channel required for station, zero (0) use _channel
70
107
bool _stationRunning; // True while station is starting or running
71
108
uint32_t _timer; // Reconnection timer
72
109
bool _verbose; // True causes more debug output to be displayed
73
110
111
+ // Display components begin started or stopped
112
+ // Inputs:
113
+ // text: Text describing the component list
114
+ // components: A bit mask of the components
115
+ void displayComponents (const char * text, WIFI_ACTION_t components);
116
+
74
117
// Start the WiFi event handler
75
118
void eventHandlerStart ();
76
119
@@ -187,8 +230,8 @@ class RTK_WIFI
187
230
// Inputs:
188
231
// channel: Channel number for the scan, zero (0) scan all channels
189
232
// Outputs:
190
- // Returns true if successful and false upon failure
191
- bool stationScanStart (WIFI_CHANNEL_t channel);
233
+ // Returns the number of access points
234
+ int16_t stationScanForAPs (WIFI_CHANNEL_t channel);
192
235
193
236
// Select the AP and channel to use for WiFi station
194
237
// Inputs:
@@ -198,6 +241,14 @@ class RTK_WIFI
198
241
// Returns the channel number of the AP
199
242
WIFI_CHANNEL_t stationSelectAP (uint8_t apCount, bool list);
200
243
244
+ // Stop and start WiFi components
245
+ // Inputs:
246
+ // stopping: WiFi components that need to be stopped
247
+ // starting: WiFi components that neet to be started
248
+ // Outputs:
249
+ // Returns true if the modes were successfully configured
250
+ bool stopStart (WIFI_ACTION_t stopping, WIFI_ACTION_t starting);
251
+
201
252
// Handle the WiFi event
202
253
// Inputs:
203
254
// event: Arduino ESP32 event number found on
@@ -295,7 +346,9 @@ class RTK_WIFI
295
346
bool stationRunning ();
296
347
297
348
// Test the WiFi modes
298
- void test ();
349
+ // Inputs:
350
+ // testDurationMsec: Milliseconds to run each test
351
+ void test (uint32_t testDurationMsec);
299
352
300
353
// Enable or disable verbose debug output
301
354
// Inputs:
@@ -310,6 +363,52 @@ class RTK_WIFI
310
363
311
364
RTK_WIFI wifi (false , false );
312
365
366
+ // ****************************************
367
+ // HTTP client connection class
368
+ // ****************************************
369
+
370
+ class HTTP_CLIENT_CONNECTION
371
+ {
372
+ private:
373
+ const char * _hostName;
374
+ const char * _url;
375
+
376
+ NetworkClient * _client;
377
+ int _headerLength;
378
+ int _pageLength;
379
+ uint16_t _portNumber;
380
+ uint8_t _hccState;
381
+ bool _tagEndFound;
382
+ int _tagEndOffset;
383
+ bool _tagStartFound;
384
+ int _tagStartOffset;
385
+ uint32_t _timer;
386
+ uint8_t _buffer[2048 ];
387
+
388
+ public:
389
+
390
+ bool _suppressFirstPageOutput;
391
+
392
+ // Constructor
393
+ // Inputs:
394
+ // hostName: Name of the remote host
395
+ // portNumber: Port number on the remote host for HTTP connection
396
+ // url: Web page address
397
+ HTTP_CLIENT_CONNECTION (const char * hostName,
398
+ uint16_t portNumber,
399
+ const char * url);
400
+
401
+ // Destructor
402
+ ~HTTP_CLIENT_CONNECTION ();
403
+
404
+ // Read the HTTP page
405
+ // Inputs:
406
+ // networkConnected: True while the network is connected
407
+ void update (bool networkConnected);
408
+ };
409
+
410
+ HTTP_CLIENT_CONNECTION SparkFun (" www.SparkFun.com" , 80 , " /" );
411
+
313
412
// *********************************************************************
314
413
// Entry point for the application
315
414
void setup ()
@@ -322,19 +421,36 @@ void setup()
322
421
// Verity the WiFi tables
323
422
wifi.verifyTables ();
324
423
325
- // Enable WiFi debugging
326
- // wifi.verbose(true);
327
- // wifi.debug(true);
424
+ // Enable verbose debugging
425
+ /*
426
+ espNowVerbose = true;
427
+ wifi.verbose(true);
428
+
429
+ // Enable debugging
430
+ espNowDebug = true;
431
+ wifi.debug(true);
432
+ */
433
+
434
+ // Enable network display
435
+ espNowDisplay = true ;
328
436
wifi.display (true );
329
437
330
438
// Set the mDNS host name
331
439
wifi.hostNameSet (mdnsHostName);
440
+
441
+ // Set the HTTP parameters
442
+ SparkFun._suppressFirstPageOutput = true ;
332
443
}
333
444
334
445
// *********************************************************************
335
446
// Idle loop for core 1 of the application
336
447
void loop ()
337
448
{
449
+ bool wifiOnline;
450
+
338
451
wifi.stationReconnectionRequest ();
339
- wifi.test ();
452
+ wifi.test (15 * 1000 );
453
+ wifiOnline = wifi.stationOnline ();
454
+ httpUpdate (&SparkFun, wifiOnline);
455
+ webServerUpdate (wifiOnline);
340
456
}
0 commit comments