Skip to content

Commit 648e7c5

Browse files
Fixes and improvements for ESP32 in AP mode:
- Now correctly reports the device's IP in AP mode for UDP discovery requests. Previously reported as 0.0.0.0. - Split logic between AP mode and Infra modes since WiFi.status() returns a meaningless value in AP mode. AP mode now immediately establishes TCP/UDP servers. Server establishment moved to own function. - Also adds null-pointer guards to tcp/udp loop while servers are not established yet. Not relevant for AP mode, but included here due to simplicity. Tested on ESP32 in WIFI_MODE_AP_ONLY. This PR does NOT address the infrastructure modes or the WIFI_MODE_ATTEMPT_INFRASTRUCTURE_FAIL_TO_AP mode in particular, which has some issues in the failover trigger. Local changes are ready and can be included in future PRs if desired.
1 parent 9d418ed commit 648e7c5

2 files changed

Lines changed: 86 additions & 57 deletions

File tree

src/WifiControl.cpp

Lines changed: 83 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ void WifiControl::startAccessPointMode()
6161
WiFi.setHostname(WIFI_HOSTNAME);
6262
#endif
6363

64-
WiFi.softAP(WIFI_HOSTNAME, WIFI_AP_MODE_WPAKEY);
64+
WiFi.mode(WIFI_AP);
6565
WiFi.softAPConfig(local_ip, gateway, subnet);
66+
WiFi.softAP(WIFI_HOSTNAME, WIFI_AP_MODE_WPAKEY);
67+
68+
establishServers();
6669
}
6770

6871
String wifiStatus(int status)
@@ -111,49 +114,64 @@ String WifiControl::getStatus()
111114
result += WiFi.getHostname();
112115
#endif
113116

114-
result += "," + WiFi.localIP().toString() + ":" + WIFI_PORT;
117+
result += "," + getIP() + ":" + WIFI_PORT;
115118
result += "," + String(WIFI_INFRASTRUCTURE_MODE_SSID) + "," + String(WIFI_HOSTNAME);
116119

117120
return result;
118121
}
119122

123+
void WifiControl::establishServers()
124+
{
125+
delete _tcpServer;
126+
_tcpServer = new WiFiServer(WIFI_PORT);
127+
_tcpServer->begin();
128+
_tcpServer->setNoDelay(true);
129+
130+
delete _udp;
131+
_udp = new WiFiUDP();
132+
_udp->begin(4031);
133+
}
134+
135+
String WifiControl::getIP()
136+
{
137+
return WIFI_MODE == WIFI_MODE_DISABLED ? "NONE"
138+
: WIFI_MODE == WIFI_MODE_AP_ONLY ? WiFi.softAPIP().toString()
139+
: WiFi.localIP().toString();
140+
}
141+
120142
void WifiControl::loop()
121143
{
122144
if (WIFI_MODE == WIFI_MODE_DISABLED)
123145
{
124146
return;
125147
}
126-
if (_status != WiFi.status())
148+
149+
if (WIFI_MODE != WIFI_MODE_AP_ONLY)
127150
{
128-
_status = WiFi.status();
129-
LOG(DEBUG_WIFI, "[WIFI]: Connected status changed to %s", wifiStatus(_status).c_str());
130-
if (_status == WL_CONNECTED)
151+
if (_status != WiFi.status())
131152
{
132-
delete _tcpServer;
133-
_tcpServer = new WiFiServer(WIFI_PORT);
134-
_tcpServer->begin();
135-
_tcpServer->setNoDelay(true);
136-
137-
delete _udp;
138-
_udp = new WiFiUDP();
139-
_udp->begin(4031);
140-
141-
LOG(DEBUG_WIFI,
142-
"[WIFI]: Connecting to SSID %s at %s:%d",
143-
WIFI_INFRASTRUCTURE_MODE_SSID,
144-
WiFi.localIP().toString().c_str(),
145-
WIFI_PORT);
153+
_status = WiFi.status();
154+
LOG(DEBUG_WIFI, "[WIFI]: Connected status changed to %s", wifiStatus(_status).c_str());
155+
if (_status == WL_CONNECTED)
156+
{
157+
establishServers();
158+
LOG(DEBUG_WIFI,
159+
"[WIFI]: Connecting to SSID %s at %s:%d",
160+
WIFI_INFRASTRUCTURE_MODE_SSID,
161+
WiFi.localIP().toString().c_str(),
162+
WIFI_PORT);
163+
}
164+
165+
if (_status != WL_CONNECTED)
166+
{
167+
infraToAPFailover();
168+
return;
169+
}
146170
}
147171
}
148172

149173
_mount->loop();
150174

151-
if (_status != WL_CONNECTED)
152-
{
153-
infraToAPFailover();
154-
return;
155-
}
156-
157175
tcpLoop();
158176
udpLoop();
159177
}
@@ -172,57 +190,66 @@ void WifiControl::infraToAPFailover()
172190

173191
void WifiControl::tcpLoop()
174192
{
175-
if (client && client.connected())
193+
if (!_tcpServer)
194+
return;
195+
196+
if (!client.connected())
176197
{
177-
while (client.available())
198+
client = _tcpServer->accept();
199+
if (!client.connected())
200+
return;
201+
}
202+
203+
int peek;
204+
int avail;
205+
while ((avail = client.available()) != 0)
206+
{
207+
LOG(DEBUG_WIFI, "[WIFITCP]: Available bytes %d. Peeking.", avail);
208+
209+
// Peek first byte and check for ACK (0x06) handshake
210+
peek = client.peek();
211+
LOG(DEBUG_WIFI, "[WIFITCP]: First byte is %x", peek);
212+
if (peek == 0x06)
178213
{
179-
LOG(DEBUG_WIFI, "[WIFITCP]: Available bytes %d. Peeking.", client.available());
214+
client.read();
215+
LOG(DEBUG_WIFI, "[WIFITCP]: Query <-- Handshake request");
216+
client.write("P");
217+
LOG(DEBUG_WIFI, "[WIFITCP]: Reply --> P (polar mode)");
218+
}
219+
else
220+
{
221+
String cmd = client.readStringUntil('#');
222+
LOG(DEBUG_WIFI, "[WIFITCP]: Query <-- %s#", cmd.c_str());
223+
const char *retVal = _cmdProcessor->processCommand(cmd);
180224

181-
// Peek first byte and check for ACK (0x06) handshake
182-
LOG(DEBUG_WIFI, "[WIFITCP]: First byte is %x", client.peek());
183-
if (client.peek() == 0x06)
225+
if (retVal[0] != '\0')
184226
{
185-
client.read();
186-
LOG(DEBUG_WIFI, "[WIFITCP]: Query <-- Handshake request");
187-
client.write("P");
188-
LOG(DEBUG_WIFI, "[WIFITCP]: Reply --> P (polar mode)");
227+
client.write(retVal);
228+
LOG(DEBUG_WIFI, "[WIFITCP]: Reply --> %s", retVal);
189229
}
190230
else
191231
{
192-
String cmd = client.readStringUntil('#');
193-
LOG(DEBUG_WIFI, "[WIFITCP]: Query <-- %s#", cmd.c_str());
194-
const char *retVal = _cmdProcessor->processCommand(cmd);
195-
196-
if (retVal[0] != '\0')
197-
{
198-
client.write(retVal);
199-
LOG(DEBUG_WIFI, "[WIFITCP]: Reply --> %s", retVal);
200-
}
201-
else
202-
{
203-
LOG(DEBUG_WIFI, "[WIFITCP]: No Reply");
204-
}
232+
LOG(DEBUG_WIFI, "[WIFITCP]: No Reply");
205233
}
206-
207-
_mount->loop();
208234
}
209-
}
210-
else
211-
{
212-
client = _tcpServer->available();
235+
236+
_mount->loop();
213237
}
214238
}
215239

216240
void WifiControl::udpLoop()
217241
{
242+
if (!_udp)
243+
return;
244+
218245
int packetSize = _udp->parsePacket();
219246
if (packetSize)
220247
{
221248
String lookingFor = "skyfi:";
222249
String reply = "skyfi:";
223250
reply += WIFI_HOSTNAME;
224251
reply += "@";
225-
reply += WiFi.localIP().toString();
252+
reply += getIP();
226253
LOG(DEBUG_WIFI,
227254
"[WIFIUDP]: Received %d bytes from %s, port %d",
228255
packetSize,

src/WifiControl.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class WifiControl
2929
void infraToAPFailover();
3030
void tcpLoop();
3131
void udpLoop();
32-
wl_status_t _status;
32+
void establishServers();
33+
String getIP();
34+
wl_status_t _status = WL_DISCONNECTED;
3335
Mount *_mount;
3436
LcdMenu *_lcdMenu;
3537
MeadeCommandProcessor *_cmdProcessor;

0 commit comments

Comments
 (0)