Skip to content

Commit 83a69fc

Browse files
committed
Refactor Ethernet / WiFi Network Initialization
Signed-off-by: John Walicki <[email protected]>
1 parent 3c0e3e7 commit 83a69fc

File tree

1 file changed

+45
-31
lines changed

1 file changed

+45
-31
lines changed

WatsonIoT/src/main.cpp

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ Preferences prefs;
9090
String _ssid; // your network SSID (name) - loaded from NVM
9191
String _pswd; // your network password - loaded from NVM
9292
int networksStored;
93-
static bool eth_connected = false;
94-
static bool wificonnected = false;
93+
static bool bEthConnected = false;
94+
static bool bEthConnecting = false;
95+
static bool bWiFiConnected = false;
96+
9597

9698
// --------------------------------------------------------------------------------------------
9799
// ADXL Accelerometer
@@ -665,13 +667,13 @@ void NetworkEvent(WiFiEvent_t event) {
665667
break;
666668
case SYSTEM_EVENT_STA_DISCONNECTED:
667669
Serial.println("Disconnected from WiFi access point");
668-
wificonnected = false;
670+
bWiFiConnected = false;
669671
break;
670672
case SYSTEM_EVENT_STA_GOT_IP: // 7
671673
Serial.println("ESP32 station got IP from connected AP");
672674
Serial.print("Obtained IP address: ");
673675
Serial.println( WiFi.localIP() );
674-
if( eth_connected ) {
676+
if( bEthConnected ) {
675677
Serial.println("Ethernet is already connected");
676678
}
677679
break;
@@ -682,8 +684,7 @@ void NetworkEvent(WiFiEvent_t event) {
682684
break;
683685
case SYSTEM_EVENT_ETH_CONNECTED:
684686
Serial.println("ETH Connected");
685-
Serial.print("ETH MAC: ");
686-
Serial.println(ETH.macAddress());
687+
bEthConnecting = true;
687688
break;
688689
case SYSTEM_EVENT_ETH_GOT_IP:
689690
Serial.print("ETH MAC: ");
@@ -696,7 +697,7 @@ void NetworkEvent(WiFiEvent_t event) {
696697
Serial.print(", ");
697698
Serial.print(ETH.linkSpeed());
698699
Serial.println("Mbps");
699-
eth_connected = true;
700+
bEthConnected = true;
700701

701702
// Switch the MQTT connection to Ethernet from WiFi (or initially)
702703
// Preference the Ethernet wired interface if its available
@@ -710,7 +711,7 @@ void NetworkEvent(WiFiEvent_t event) {
710711
break;
711712
case SYSTEM_EVENT_ETH_DISCONNECTED:
712713
Serial.println("ETH Disconnected");
713-
eth_connected = false;
714+
bEthConnected = false;
714715
// Disconnect the MQTT client
715716
if( mqtt.connected() ){
716717
Serial.println("Previously connected to Ethernet, try to switch the MQTT connection to WiFi");
@@ -719,7 +720,7 @@ void NetworkEvent(WiFiEvent_t event) {
719720
break;
720721
case SYSTEM_EVENT_ETH_STOP:
721722
Serial.println("ETH Stopped");
722-
eth_connected = false;
723+
bEthConnected = false;
723724
break;
724725
case SYSTEM_EVENT_STA_STOP:
725726
Serial.println("WiFi Stopped");
@@ -782,23 +783,33 @@ void setup() {
782783

783784
strip.setBrightness(50); // Dim the LED to 20% - 0 off, 255 full bright
784785

785-
// Start WiFi connection
786+
// Start Network connections
786787
WiFi.onEvent(NetworkEvent);
787-
WiFi.mode(WIFI_STA);
788788

789-
wificonnected = WiFiScanAndConnect();
790-
if( !wificonnected ) {
789+
// Start the ETH interface, if it is available, before WiFi
790+
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE);
791+
delay(5000);
792+
if( bEthConnecting ) {
793+
while( !bEthConnected ) {
794+
Serial.println("Waiting for Ethernet to start...");
795+
delay( 500 );
796+
}
797+
}
798+
799+
WiFi.mode(WIFI_STA);
800+
bWiFiConnected = WiFiScanAndConnect();
801+
if( !bWiFiConnected ) {
791802
// If the sensor has been registered in the past
792803
// at least one WiFi network will have been stored in NVM
793804
// and if the Ethernet cable is connected, do not
794805
// loop in SmartConfig, just use the hardwired connection.
795-
if( !numNetworksStored() && !eth_connected ) {
806+
if( numNetworksStored() && bEthConnected ) {
807+
Serial.println("Previously registered device, use hardwired Ethernet connection.");
808+
} else {
796809
while( !startSmartConfig() ) {
797810
// loop in SmartConfig until the user provides
798811
// the correct WiFi SSID and password
799812
}
800-
} else {
801-
Serial.println("Use hardwired Ethernet connection.");
802813
}
803814
} else {
804815
Serial.println("WiFi Connected");
@@ -811,11 +822,6 @@ void setup() {
811822
Serial.print("WiFi MAC: ");
812823
Serial.println(WiFi.macAddress());
813824

814-
// Start the ETH interface
815-
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE);
816-
Serial.print("ETH MAC: ");
817-
Serial.println(ETH.macAddress());
818-
819825
// Use the reverse octet Mac Address as the MQTT deviceID
820826
//snprintf(deviceID,13,"%02X%02X%02X%02X%02X%02X",mac[5],mac[4],mac[3],mac[2],mac[1],mac[0]);
821827
snprintf(deviceID,13,"%02X%02X%02X%02X%02X%02X",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
@@ -1092,6 +1098,7 @@ int numScannedNetworks() {
10921098
return n;
10931099
}
10941100

1101+
10951102
//Return how many networks are stored in the NVM
10961103
int numNetworksStored() {
10971104
prefs.begin("networks", true);
@@ -1103,6 +1110,7 @@ int numNetworksStored() {
11031110
return networksStored;
11041111
}
11051112

1113+
11061114
//Each network as an id so reading the network stored with said ID.
11071115
void readNetworkStored(int netId)
11081116
{
@@ -1123,6 +1131,7 @@ void readNetworkStored(int netId)
11231131
Serial.println("xxxxxx");
11241132
}
11251133

1134+
11261135
//Save a pair of SSID and PSWD to NVM
11271136
void storeNetwork(String ssid, String pswd)
11281137
{
@@ -1148,6 +1157,7 @@ void storeNetwork(String ssid, String pswd)
11481157
Serial.println(" networks stored in NVM");
11491158
}
11501159

1160+
11511161
//Clear all networks stored in the NVM, force SmartConfig
11521162
void clearNetworks() {
11531163
Serial.println("Clear all stored networks from NVM");
@@ -1160,6 +1170,7 @@ void clearNetworks() {
11601170
prefs.end();
11611171
}
11621172

1173+
11631174
//Joins the previous functions, gets the stored networks and compares to the available, if there is a match and connects, return true
11641175
//if no match or unable to connect, return false.
11651176
bool WiFiScanAndConnect()
@@ -1200,6 +1211,7 @@ bool WiFiScanAndConnect()
12001211
return false;
12011212
}
12021213

1214+
12031215
// Executes the Smart config routine and if connected, will save the network for future use
12041216
bool startSmartConfig()
12051217
{
@@ -1210,7 +1222,7 @@ bool startSmartConfig()
12101222

12111223
// Wait for SmartConfig packet from mobile
12121224
Serial.print("Waiting for SmartConfig or Router to restart" );
1213-
while( !WiFi.smartConfigDone() || eth_connected ) {
1225+
while( !WiFi.smartConfigDone() ) {
12141226
delay(500);
12151227
Serial.print("Waiting for SmartConfig or WiFi Router to recover (10 minutes ~ 650 ticks): " );
12161228
Serial.println( RouterDownTimeOut );
@@ -1230,22 +1242,24 @@ bool startSmartConfig()
12301242
Serial.println("Router Recovery? Restart OpenEEW device to retry saved networks");
12311243
esp_restart();
12321244
}
1245+
1246+
if( bEthConnected ) {
1247+
// Ethernet cable was connected during SmartConfig
1248+
if( numNetworksStored() ) {
1249+
Serial.println("Previously registered device, Skip SmartConfig, Use hardwired Ethernet connection.");
1250+
// Skip SmartConfig
1251+
WiFi.stopSmartConfig();
1252+
return true;
1253+
}
1254+
}
12331255
}
12341256

1257+
Serial.println("SmartConfig received.");
12351258
for( int i=0;i<4;i++){
12361259
delay(500);
12371260
NeoPixelStatus( LED_CONNECT_WIFI ); // Success - blink green
12381261
}
12391262

1240-
if( eth_connected ) {
1241-
// Ethernet cable was connected during or before SmartConfig
1242-
Serial.println("Skip SmartConfig. Use hardwired Ethernet connection.");
1243-
// Skip SmartConfig
1244-
WiFi.stopSmartConfig();
1245-
return true;
1246-
}
1247-
Serial.println("SmartConfig received.");
1248-
12491263
// Wait for WiFi to connect to AP
12501264
Serial.println("Waiting for WiFi");
12511265
unsigned long t0 = millis();

0 commit comments

Comments
 (0)