@@ -90,8 +90,10 @@ Preferences prefs;
90
90
String _ssid; // your network SSID (name) - loaded from NVM
91
91
String _pswd; // your network password - loaded from NVM
92
92
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
+
95
97
96
98
// --------------------------------------------------------------------------------------------
97
99
// ADXL Accelerometer
@@ -665,13 +667,13 @@ void NetworkEvent(WiFiEvent_t event) {
665
667
break ;
666
668
case SYSTEM_EVENT_STA_DISCONNECTED:
667
669
Serial.println (" Disconnected from WiFi access point" );
668
- wificonnected = false ;
670
+ bWiFiConnected = false ;
669
671
break ;
670
672
case SYSTEM_EVENT_STA_GOT_IP: // 7
671
673
Serial.println (" ESP32 station got IP from connected AP" );
672
674
Serial.print (" Obtained IP address: " );
673
675
Serial.println ( WiFi.localIP () );
674
- if ( eth_connected ) {
676
+ if ( bEthConnected ) {
675
677
Serial.println (" Ethernet is already connected" );
676
678
}
677
679
break ;
@@ -682,8 +684,7 @@ void NetworkEvent(WiFiEvent_t event) {
682
684
break ;
683
685
case SYSTEM_EVENT_ETH_CONNECTED:
684
686
Serial.println (" ETH Connected" );
685
- Serial.print (" ETH MAC: " );
686
- Serial.println (ETH.macAddress ());
687
+ bEthConnecting = true ;
687
688
break ;
688
689
case SYSTEM_EVENT_ETH_GOT_IP:
689
690
Serial.print (" ETH MAC: " );
@@ -696,7 +697,7 @@ void NetworkEvent(WiFiEvent_t event) {
696
697
Serial.print (" , " );
697
698
Serial.print (ETH.linkSpeed ());
698
699
Serial.println (" Mbps" );
699
- eth_connected = true ;
700
+ bEthConnected = true ;
700
701
701
702
// Switch the MQTT connection to Ethernet from WiFi (or initially)
702
703
// Preference the Ethernet wired interface if its available
@@ -710,7 +711,7 @@ void NetworkEvent(WiFiEvent_t event) {
710
711
break ;
711
712
case SYSTEM_EVENT_ETH_DISCONNECTED:
712
713
Serial.println (" ETH Disconnected" );
713
- eth_connected = false ;
714
+ bEthConnected = false ;
714
715
// Disconnect the MQTT client
715
716
if ( mqtt.connected () ){
716
717
Serial.println (" Previously connected to Ethernet, try to switch the MQTT connection to WiFi" );
@@ -719,7 +720,7 @@ void NetworkEvent(WiFiEvent_t event) {
719
720
break ;
720
721
case SYSTEM_EVENT_ETH_STOP:
721
722
Serial.println (" ETH Stopped" );
722
- eth_connected = false ;
723
+ bEthConnected = false ;
723
724
break ;
724
725
case SYSTEM_EVENT_STA_STOP:
725
726
Serial.println (" WiFi Stopped" );
@@ -782,23 +783,33 @@ void setup() {
782
783
783
784
strip.setBrightness (50 ); // Dim the LED to 20% - 0 off, 255 full bright
784
785
785
- // Start WiFi connection
786
+ // Start Network connections
786
787
WiFi.onEvent (NetworkEvent);
787
- WiFi.mode (WIFI_STA);
788
788
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 ) {
791
802
// If the sensor has been registered in the past
792
803
// at least one WiFi network will have been stored in NVM
793
804
// and if the Ethernet cable is connected, do not
794
805
// 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 {
796
809
while ( !startSmartConfig () ) {
797
810
// loop in SmartConfig until the user provides
798
811
// the correct WiFi SSID and password
799
812
}
800
- } else {
801
- Serial.println (" Use hardwired Ethernet connection." );
802
813
}
803
814
} else {
804
815
Serial.println (" WiFi Connected" );
@@ -811,11 +822,6 @@ void setup() {
811
822
Serial.print (" WiFi MAC: " );
812
823
Serial.println (WiFi.macAddress ());
813
824
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
-
819
825
// Use the reverse octet Mac Address as the MQTT deviceID
820
826
// snprintf(deviceID,13,"%02X%02X%02X%02X%02X%02X",mac[5],mac[4],mac[3],mac[2],mac[1],mac[0]);
821
827
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() {
1092
1098
return n;
1093
1099
}
1094
1100
1101
+
1095
1102
// Return how many networks are stored in the NVM
1096
1103
int numNetworksStored () {
1097
1104
prefs.begin (" networks" , true );
@@ -1103,6 +1110,7 @@ int numNetworksStored() {
1103
1110
return networksStored;
1104
1111
}
1105
1112
1113
+
1106
1114
// Each network as an id so reading the network stored with said ID.
1107
1115
void readNetworkStored (int netId)
1108
1116
{
@@ -1123,6 +1131,7 @@ void readNetworkStored(int netId)
1123
1131
Serial.println (" xxxxxx" );
1124
1132
}
1125
1133
1134
+
1126
1135
// Save a pair of SSID and PSWD to NVM
1127
1136
void storeNetwork (String ssid, String pswd)
1128
1137
{
@@ -1148,6 +1157,7 @@ void storeNetwork(String ssid, String pswd)
1148
1157
Serial.println (" networks stored in NVM" );
1149
1158
}
1150
1159
1160
+
1151
1161
// Clear all networks stored in the NVM, force SmartConfig
1152
1162
void clearNetworks () {
1153
1163
Serial.println (" Clear all stored networks from NVM" );
@@ -1160,6 +1170,7 @@ void clearNetworks() {
1160
1170
prefs.end ();
1161
1171
}
1162
1172
1173
+
1163
1174
// Joins the previous functions, gets the stored networks and compares to the available, if there is a match and connects, return true
1164
1175
// if no match or unable to connect, return false.
1165
1176
bool WiFiScanAndConnect ()
@@ -1200,6 +1211,7 @@ bool WiFiScanAndConnect()
1200
1211
return false ;
1201
1212
}
1202
1213
1214
+
1203
1215
// Executes the Smart config routine and if connected, will save the network for future use
1204
1216
bool startSmartConfig ()
1205
1217
{
@@ -1210,7 +1222,7 @@ bool startSmartConfig()
1210
1222
1211
1223
// Wait for SmartConfig packet from mobile
1212
1224
Serial.print (" Waiting for SmartConfig or Router to restart" );
1213
- while ( !WiFi.smartConfigDone () || eth_connected ) {
1225
+ while ( !WiFi.smartConfigDone () ) {
1214
1226
delay (500 );
1215
1227
Serial.print (" Waiting for SmartConfig or WiFi Router to recover (10 minutes ~ 650 ticks): " );
1216
1228
Serial.println ( RouterDownTimeOut );
@@ -1230,22 +1242,24 @@ bool startSmartConfig()
1230
1242
Serial.println (" Router Recovery? Restart OpenEEW device to retry saved networks" );
1231
1243
esp_restart ();
1232
1244
}
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
+ }
1233
1255
}
1234
1256
1257
+ Serial.println (" SmartConfig received." );
1235
1258
for ( int i=0 ;i<4 ;i++){
1236
1259
delay (500 );
1237
1260
NeoPixelStatus ( LED_CONNECT_WIFI ); // Success - blink green
1238
1261
}
1239
1262
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
-
1249
1263
// Wait for WiFi to connect to AP
1250
1264
Serial.println (" Waiting for WiFi" );
1251
1265
unsigned long t0 = millis ();
0 commit comments