Skip to content

Commit 6b1c934

Browse files
authored
Merge pull request #23 from LeeLeahy2/4-13-multiple-wifi-users
4-13: Add example 4_13_Multiple_WiFi_Users
2 parents 8d61bd7 + d598c48 commit 6b1c934

File tree

6 files changed

+1943
-618
lines changed

6 files changed

+1943
-618
lines changed

Example_Sketches/4_13_Multiple_WiFi_Users/4_13_Multiple_WiFi_Users.ino

Lines changed: 125 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
#include <Network.h> // Built-in
1515
#include <WiFi.h> // Built-in
1616

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
1821

1922
#include <secrets.h> // Host name, SSIDs and passwords
2023

@@ -23,6 +26,39 @@ bool RTK_CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC = false;
2326
#define systemPrintf Serial.printf
2427
#define systemPrintln Serial.println
2528

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+
2662
//****************************************
2763
// WiFi class
2864
//****************************************
@@ -44,6 +80,7 @@ class RTK_WIFI
4480
private:
4581

4682
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
4784
IPAddress _apDnsAddress; // DNS IP address to use while translating names into IP addresses
4885
IPAddress _apFirstDhcpAddress; // First IP address to use for DHCP
4986
IPAddress _apGatewayAddress;// IP address of the gateway to the larger network (internet?)
@@ -65,12 +102,18 @@ class RTK_WIFI
65102
uint8_t _staMacAddress[6]; // MAC address of the station
66103
const char * _staRemoteApSsid; // SSID of remote AP
67104
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
69106
WIFI_CHANNEL_t _stationChannel; // Channel required for station, zero (0) use _channel
70107
bool _stationRunning; // True while station is starting or running
71108
uint32_t _timer; // Reconnection timer
72109
bool _verbose; // True causes more debug output to be displayed
73110

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+
74117
// Start the WiFi event handler
75118
void eventHandlerStart();
76119

@@ -187,8 +230,8 @@ class RTK_WIFI
187230
// Inputs:
188231
// channel: Channel number for the scan, zero (0) scan all channels
189232
// 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);
192235

193236
// Select the AP and channel to use for WiFi station
194237
// Inputs:
@@ -198,6 +241,14 @@ class RTK_WIFI
198241
// Returns the channel number of the AP
199242
WIFI_CHANNEL_t stationSelectAP(uint8_t apCount, bool list);
200243

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+
201252
// Handle the WiFi event
202253
// Inputs:
203254
// event: Arduino ESP32 event number found on
@@ -295,7 +346,9 @@ class RTK_WIFI
295346
bool stationRunning();
296347

297348
// Test the WiFi modes
298-
void test();
349+
// Inputs:
350+
// testDurationMsec: Milliseconds to run each test
351+
void test(uint32_t testDurationMsec);
299352

300353
// Enable or disable verbose debug output
301354
// Inputs:
@@ -310,6 +363,52 @@ class RTK_WIFI
310363

311364
RTK_WIFI wifi(false, false);
312365

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+
313412
//*********************************************************************
314413
// Entry point for the application
315414
void setup()
@@ -322,19 +421,36 @@ void setup()
322421
// Verity the WiFi tables
323422
wifi.verifyTables();
324423

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;
328436
wifi.display(true);
329437

330438
// Set the mDNS host name
331439
wifi.hostNameSet(mdnsHostName);
440+
441+
// Set the HTTP parameters
442+
SparkFun._suppressFirstPageOutput = true;
332443
}
333444

334445
//*********************************************************************
335446
// Idle loop for core 1 of the application
336447
void loop()
337448
{
449+
bool wifiOnline;
450+
338451
wifi.stationReconnectionRequest();
339-
wifi.test();
452+
wifi.test(15 * 1000);
453+
wifiOnline = wifi.stationOnline();
454+
httpUpdate(&SparkFun, wifiOnline);
455+
webServerUpdate(wifiOnline);
340456
}

0 commit comments

Comments
 (0)