@@ -42,7 +42,7 @@ enum WIFI_STATION_STATES
42
42
{
43
43
WIFI_STATION_STATE_OFF,
44
44
WIFI_STATION_STATE_WAIT_NO_USERS,
45
- WIFI_STATION_STATE_RESTART ,
45
+ WIFI_STATION_STATE_RESTART_DELAY ,
46
46
WIFI_STATION_STATE_STARTING,
47
47
WIFI_STATION_STATE_ONLINE,
48
48
WIFI_STATION_STATE_STABLE,
@@ -51,9 +51,14 @@ enum WIFI_STATION_STATES
51
51
};
52
52
uint8_t wifiStationState;
53
53
54
- const char *wifiStationStateName[] = {
55
- " WIFI_STATION_STATE_OFF" , " WIFI_STATION_STATE_WAIT_NO_USERS" , " WIFI_STATION_STATE_RESTART" ,
56
- " WIFI_STATION_STATE_STARTING" , " WIFI_STATION_STATE_ONLINE" , " WIFI_STATION_STATE_STABLE" ,
54
+ const char * wifiStationStateName[] =
55
+ {
56
+ " WIFI_STATION_STATE_OFF" ,
57
+ " WIFI_STATION_STATE_WAIT_NO_USERS" ,
58
+ " WIFI_STATION_STATE_RESTART_DELAY" ,
59
+ " WIFI_STATION_STATE_STARTING" ,
60
+ " WIFI_STATION_STATE_ONLINE" ,
61
+ " WIFI_STATION_STATE_STABLE" ,
57
62
};
58
63
const int wifiStationStateNameEntries = sizeof (wifiStationStateName) / sizeof (wifiStationStateName[0 ]);
59
64
@@ -935,12 +940,12 @@ void wifiStationUpdate()
935
940
| enabled | |
936
941
| | |
937
942
V !enabled | |
938
- WIFI_STATION_STATE_RESTART_DELAY ----------' |
939
- | |
940
- | Timeout |
941
- | Complete |
942
- V !enabled |
943
- .----> WIFI_STATION_STATE_STARTING -------------. |
943
+ .--> WIFI_STATION_STATE_RESTART_DELAY ----------' |
944
+ | | |
945
+ | | Timeout |
946
+ | | Complete |
947
+ | V !enabled |
948
+ | WIFI_STATION_STATE_STARTING -------------. |
944
949
| | | |
945
950
| | WiFi connected | |
946
951
| V !enabled | |
@@ -952,18 +957,14 @@ void wifiStationUpdate()
952
957
| | | |
953
958
| | !enabled | |
954
959
| V | |
955
- | +<--------------------------' |
956
- | | |
960
+ | Display +<--------------------------' |
961
+ | delay | |
957
962
| V |
958
963
| WIFI_STATION_STATE_WAIT_NO_USERS |
959
964
| | |
960
965
| | No Users |
961
- | V !enabled |
962
- | +--------------------------------'
963
- | Display |
964
- | delay | enabled
965
- | V
966
- '------ WIFI_STATION_STATE_RESTART
966
+ | enabled V !enabled |
967
+ '-------------------+--------------------------------'
967
968
968
969
Network Loss Handling:
969
970
@@ -1032,13 +1033,8 @@ void wifiStationUpdate()
1032
1033
timer = millis ();
1033
1034
startTimeout = 0 ;
1034
1035
1035
- // Display the major state transition
1036
- if (settings.debugWifiState )
1037
- systemPrintf (" --------------- %s Starting ---------------\r\n " ,
1038
- networkInterfaceTable[NETWORK_WIFI_STATION].name );
1039
-
1040
1036
// Start WiFi station
1041
- wifiStationSetState (WIFI_STATION_STATE_STARTING );
1037
+ wifiStationSetState (WIFI_STATION_STATE_RESTART_DELAY );
1042
1038
}
1043
1039
break ;
1044
1040
@@ -1075,6 +1071,8 @@ void wifiStationUpdate()
1075
1071
networkInterfaceTable[NETWORK_WIFI_STATION].name );
1076
1072
wifiStationOff (__FILE__, __LINE__);
1077
1073
}
1074
+
1075
+ // Reset the start timeout
1078
1076
wifiStationSetState (WIFI_STATION_STATE_OFF);
1079
1077
}
1080
1078
@@ -1083,67 +1081,83 @@ void wifiStationUpdate()
1083
1081
{
1084
1082
// Clear the bits to perform the restart operation
1085
1083
wifi.clearStarted (WIFI_STA_RECONNECT);
1086
- wifiStationSetState (WIFI_STATION_STATE_RESTART);
1084
+
1085
+ // Display the restart delay and then start WiFi station
1086
+ if (startTimeout && settings.debugWifiState )
1087
+ {
1088
+ // Display the delay
1089
+ uint32_t seconds = startTimeout / MILLISECONDS_IN_A_SECOND;
1090
+ uint32_t minutes = seconds / SECONDS_IN_A_MINUTE;
1091
+ seconds -= minutes * SECONDS_IN_A_MINUTE;
1092
+ systemPrintf (" WiFi: Delaying %2d:%02d before restarting WiFi\r\n " , minutes, seconds);
1093
+ }
1094
+ timer = millis ();
1095
+ wifiStationSetState (WIFI_STATION_STATE_RESTART_DELAY);
1087
1096
}
1088
1097
}
1089
1098
break ;
1090
1099
1091
- // Display the restart delay and then start WiFi station
1092
- case WIFI_STATION_STATE_RESTART:
1093
- if (startTimeout && settings.debugWifiState )
1100
+ // Perform the restart delay
1101
+ case WIFI_STATION_STATE_RESTART_DELAY:
1102
+ // Stop WiFi station if necessary
1103
+ if (enabled == false )
1094
1104
{
1095
- // Display the delay
1096
- uint32_t seconds = startTimeout / MILLISECONDS_IN_A_SECOND;
1097
- uint32_t minutes = seconds / SECONDS_IN_A_MINUTE;
1098
- seconds -= minutes * SECONDS_IN_A_MINUTE;
1099
- systemPrintf (" WiFi: Delaying %2d:%02d before restarting WiFi\r\n " , minutes, seconds);
1105
+ wifiStationSetState (WIFI_STATION_STATE_OFF);
1106
+ break ;
1100
1107
}
1101
- timer = millis ();
1108
+
1109
+ // Delay before starting WiFi
1110
+ if ((millis () - timer) < startTimeout)
1111
+ break ;
1112
+
1113
+ // Display the major state transition
1114
+ if (settings.debugWifiState )
1115
+ systemPrintf (" --------------- %s Starting ---------------\r\n " ,
1116
+ networkInterfaceTable[NETWORK_WIFI_STATION].name );
1117
+
1118
+ // Timeout complete
1102
1119
wifiStationSetState (WIFI_STATION_STATE_STARTING);
1103
- break ;
1120
+
1121
+ // |
1122
+ // | Fall through
1123
+ // V
1104
1124
1105
1125
// At least one consumer is requesting a network
1106
1126
case WIFI_STATION_STATE_STARTING:
1107
- // Delay before starting WiFi
1108
- if ((millis () - timer) >= startTimeout)
1109
- {
1110
- timer = millis ();
1111
-
1112
- // Increase the timeout
1113
- startTimeout <<= 1 ;
1114
- if (!startTimeout)
1115
- startTimeout = WIFI_MIN_TIMEOUT;
1116
- else if (startTimeout > WIFI_MAX_TIMEOUT)
1117
- startTimeout = WIFI_MAX_TIMEOUT;
1127
+ // Increase the timeout
1128
+ startTimeout <<= 1 ;
1129
+ if (!startTimeout)
1130
+ startTimeout = WIFI_MIN_TIMEOUT;
1131
+ else if (startTimeout > WIFI_MAX_TIMEOUT)
1132
+ startTimeout = WIFI_MAX_TIMEOUT;
1118
1133
1119
- // Account for this connection attempt
1120
- connectionAttempts++;
1134
+ // Account for this connection attempt
1135
+ connectionAttempts++;
1121
1136
1122
- // Attempt to start WiFi station
1123
- if (wifiStationOn (__FILE__, __LINE__))
1124
- {
1125
- // Successfully connected to a remote AP
1126
- if (settings.debugWifiState )
1127
- systemPrintf (" WiFi: WiFi station successfully started \r\n " );
1137
+ // Attempt to start WiFi station
1138
+ if (wifiStationOn (__FILE__, __LINE__) == false )
1139
+ {
1140
+ // Failed to connect to a remote AP
1141
+ if (settings.debugWifiState )
1142
+ systemPrintf (" WiFi: WiFi station failed to start! \r\n " );
1128
1143
1129
- // WiFi station is now available
1130
- wifiStationSetState (WIFI_STATION_STATE_ONLINE);
1131
- }
1132
- else
1133
- {
1134
- // Failed to connect to a remote AP
1135
- if (settings.debugWifiState )
1136
- systemPrintf (" WiFi: WiFi station failed to start!\r\n " );
1144
+ // Start the next network interface if necessary
1145
+ if (connectionAttempts >= 2 )
1146
+ networkStartNextInterface (NETWORK_WIFI_STATION);
1137
1147
1138
- // Restart WiFi after delay
1139
- // Clear the bits to perform the restart operation
1140
- wifi.clearStarted (WIFI_STA_RECONNECT);
1141
- wifiStationSetState (WIFI_STATION_STATE_RESTART);
1148
+ // Perform the restart delay
1149
+ timer = millis ();
1150
+ wifiStationSetState (WIFI_STATION_STATE_RESTART_DELAY);
1151
+ }
1152
+ else
1153
+ {
1154
+ // Successfully connected to a remote AP
1155
+ if (settings.debugWifiState )
1156
+ systemPrintf (" WiFi: WiFi station successfully started\r\n " );
1142
1157
1143
- // Start the next network interface if necessary
1144
- if (connectionAttempts >= 2 )
1145
- networkStartNextInterface (NETWORK_WIFI_STATION);
1146
- }
1158
+ // WiFi station is now available
1159
+ timer = millis ();
1160
+ wifiStationSetState (WIFI_STATION_STATE_ONLINE);
1147
1161
}
1148
1162
break ;
1149
1163
0 commit comments