@@ -61,50 +61,66 @@ void EmbUI::onWiFiMode(WiFiEventModeChange event_info){
61
61
#endif // ESP8266
62
62
63
63
#ifdef ESP32
64
- // need to test it under ESP32 (might not need any scheduler to handle both Client and AP at the same time)
65
- void EmbUI::WiFiEvent (WiFiEvent_t event, WiFiEventInfo_t info) // , WiFiEventInfo_t info
64
+ void EmbUI::WiFiEvent (WiFiEvent_t event, WiFiEventInfo_t info)
66
65
{
67
66
switch (event){
68
67
case SYSTEM_EVENT_AP_START:
69
68
LOG (println, F (" UI WiFi: Access-point started" ));
70
69
setup_mDns ();
71
70
break ;
71
+
72
72
case SYSTEM_EVENT_STA_CONNECTED:
73
- LOG (print, F (" UI WiFi: STA connected - " ));
73
+ LOG (println, F (" UI WiFi: STA connected" ));
74
+
74
75
if (_cb_STAConnected)
75
76
_cb_STAConnected (); // execule callback
76
77
break ;
78
+
77
79
case SYSTEM_EVENT_STA_GOT_IP:
78
80
WiFi.mode (WIFI_STA); // Shutdown internal Access Point
81
+
82
+ /* this is a weird hack to mitigate DHCP-client hostname issue
83
+ * https://github.com/espressif/arduino-esp32/issues/2537
84
+ * we use some IDF functions to restart dhcp-client, that has been disabled before STA connect
85
+ */
86
+ tcpip_adapter_dhcpc_start (TCPIP_ADAPTER_IF_STA);
87
+ tcpip_adapter_ip_info_t iface;
88
+ tcpip_adapter_get_ip_info (TCPIP_ADAPTER_IF_STA, &iface);
89
+ if (!iface.ip .addr ){
90
+ LOG (println, F (" UI WiFi: DHCP discover..." ));
91
+ return ;
92
+ }
93
+
79
94
LOG (printf_P, PSTR (" SSID:'%s', IP: " ), WiFi.SSID ().c_str ()); // IPAddress(info.got_ip.ip_info.ip.addr)
80
- LOG (println, WiFi. localIP ( ));
95
+ LOG (println, IPAddress (iface. ip . addr ));
81
96
82
97
if (WiFi.getMode () != WIFI_MODE_STA){ // Switch to STA only mode once IP obtained
83
- WiFi.mode (WIFI_MODE_STA);
98
+ WiFi.mode (WIFI_MODE_STA);
84
99
LOG (println, F (" UI WiFi: switch to STA mode" ));
85
100
}
86
- if (_cb_STAGotIP)
87
- _cb_STAGotIP (); // execule callback
88
101
102
+ embuischedw.detach ();
89
103
setup_mDns ();
104
+ if (_cb_STAGotIP)
105
+ _cb_STAGotIP (); // execule callback
90
106
break ;
107
+
91
108
case SYSTEM_EVENT_STA_DISCONNECTED:
92
109
LOG (printf_P, PSTR (" UI WiFi: Disconnected, reason: %d\n " ), info.disconnected .reason );
93
110
// https://github.com/espressif/arduino-esp32/blob/master/tools/sdk/include/esp32/esp_wifi_types.h
94
111
if (WiFi.getMode () != WIFI_MODE_APSTA){
95
- WiFi.mode (WIFI_MODE_APSTA); // Enable internal AP if station connection is lost
96
- LOG (println, F (" UI WiFi: Switch to AP-Station mode" ));
112
+ embuischedw.once (WIFI_BEGIN_DELAY, [this ](){ WiFi.mode (WIFI_MODE_APSTA);
113
+ LOG (println, F (" UI WiFi: Switch to AP-Station mode" ));
114
+ embuischedw.detach ();} );
97
115
}
98
116
if (_cb_STADisconnected)
99
117
_cb_STADisconnected (); // execule callback
100
-
101
- embuischedw.once (WIFI_RECONNECT_TIMER, [this ](){ WiFi.begin (); } ); // esp32 doesn't auto-reconnects, so reschedule it
102
118
break ;
119
+
103
120
default :
104
121
break ;
105
122
}
106
- // handle network events for timelib
107
- timeProcessor.WiFiEvent (event, info);
123
+ timeProcessor.WiFiEvent (event, info); // handle network events for timelib
108
124
}
109
125
#endif // ESP32
110
126
@@ -117,13 +133,6 @@ void EmbUI::wifi_init(){
117
133
var (FPSTR (P_hostname), hn, true );
118
134
}
119
135
120
- #ifdef ESP8266
121
- WiFi.hostname (hn);
122
- #elif defined ESP32
123
- WiFi.setHostname (hn.c_str ());
124
- // WiFi.softAPsetHostname(hn);
125
- #endif
126
-
127
136
if (appwd.length ()<WIFI_PSK_MIN_LENGTH)
128
137
appwd = " " ;
129
138
@@ -137,20 +146,41 @@ void EmbUI::wifi_init(){
137
146
LOG (println, F (" AP-only mode" ));
138
147
WiFi.mode (WIFI_AP);
139
148
} else {
140
- LOG (println, F (" AP/STA mode" ));
141
- WiFi.mode (WIFI_AP_STA);
149
+ LOG (println, F (" STA mode" ));
150
+ WiFi.mode (WIFI_STA); // we start in STA mode, esp32 can't set client's hotname in ap/sta
151
+
152
+ #ifdef ESP8266
153
+ WiFi.hostname (hn);
154
+ WiFi.begin (); // use internaly stored last known credentials for connection
155
+ #elif defined ESP32
156
+ /* this is a weird hack to mitigate DHCP hostname issue
157
+ * order of initialization does matter, pls keep it like this till fixed in upstream
158
+ * https://github.com/espressif/arduino-esp32/issues/2537
159
+ */
160
+ WiFi.config (INADDR_NONE, INADDR_NONE, INADDR_NONE);
142
161
WiFi.begin (); // use internaly stored last known credentials for connection
162
+ if (!WiFi.setHostname (hn.c_str ()))
163
+ LOG (println, F (" UI WiFi: Failed to set hostname :(" ));
164
+ #endif
143
165
LOG (println, F (" UI WiFi: STA reconecting..." ));
144
166
}
145
167
}
146
168
147
169
void EmbUI::wifi_connect (const char *ssid, const char *pwd)
148
170
{
149
- if (ssid) {
150
- WiFi.begin (ssid, pwd);
151
- LOG (printf_P, PSTR (" UI WiFi: client connecting to SSID:%s, pwd:%s\n " ), ssid, pwd ? pwd : " " );
171
+ if (ssid){
172
+ String _ssid (ssid); String _pwd (pwd); // I need objects to pass it to the lambda
173
+ embuischedw.once (WIFI_BEGIN_DELAY, [_ssid, _pwd, this ](){
174
+ LOG (printf_P, PSTR (" UI WiFi: client connecting to SSID:%s, pwd:%s\n " ), _ssid.c_str (), _pwd.c_str ());
175
+ #ifdef ESP32
176
+ WiFi.disconnect ();
177
+ WiFi.config (INADDR_NONE, INADDR_NONE, INADDR_NONE);
178
+ #endif
179
+ WiFi.begin (_ssid.c_str (), _pwd.c_str ());
180
+ embuischedw.detach ();
181
+ });
152
182
} else {
153
- WiFi.begin ();
183
+ embuischedw. once (WIFI_BEGIN_DELAY, [ this ](){ WiFi.begin (); embuischedw. detach ();} );
154
184
}
155
185
}
156
186
@@ -186,18 +216,14 @@ void EmbUI::setup_mDns(){
186
216
void EmbUI::getAPmac (){
187
217
if (*mc) return ;
188
218
189
- uint8_t _mac[7 ];
219
+ uint8_t _mac[6 ];
190
220
191
221
#ifdef ESP32
192
- // uint64_t chipid = ESP.getEfuseMac();
193
- // memset(_mac,0,sizeof(_mac));
194
- // memcpy(_mac,&chipid,6);
195
-
196
222
if (WiFi.getMode () == WIFI_MODE_NULL)
197
223
WiFi.mode (WIFI_MODE_AP);
198
224
#endif
199
225
WiFi.softAPmacAddress (_mac);
200
226
201
- LOG (printf_P,PSTR (" UI MAC:%02X%02X%02X\n " ), _mac[3 ], _mac[4 ], _mac[5 ]);
227
+ LOG (printf_P,PSTR (" UI MAC ID :%02X%02X%02X\n " ), _mac[3 ], _mac[4 ], _mac[5 ]);
202
228
sprintf_P (mc, PSTR (" %02X%02X%02X" ), _mac[3 ], _mac[4 ], _mac[5 ]);
203
229
}
0 commit comments