Merge pull request #421 from mc-hamster/master

Fix for Bug #420 : Wifi in station mode sometimes enters loops of repeatedly joining
This commit is contained in:
Jm Casler 2020-09-19 16:46:05 -07:00 committed by GitHub
commit 51ab853658
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 150 additions and 48 deletions

View File

@ -31,8 +31,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "graphics/images.h" #include "graphics/images.h"
#include "main.h" #include "main.h"
#include "mesh-pb-constants.h" #include "mesh-pb-constants.h"
#include "utils.h"
#include "meshwifi/meshwifi.h" #include "meshwifi/meshwifi.h"
#include "utils.h"
using namespace meshtastic; /** @todo remove */ using namespace meshtastic; /** @todo remove */
@ -715,7 +715,6 @@ void Screen::drawDebugInfoWiFiTrampoline(OLEDDisplay *display, OLEDDisplayUiStat
screen->debugInfo.drawFrameWiFi(display, state, x, y); screen->debugInfo.drawFrameWiFi(display, state, x, y);
} }
// restore our regular frame list // restore our regular frame list
void Screen::setFrames() void Screen::setFrames()
{ {
@ -858,8 +857,13 @@ void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, i
display->drawString(x, y, String("WiFi - Not Connected")); display->drawString(x, y, String("WiFi - Not Connected"));
} else { } else {
display->drawString(x, y, String("WiFi - Connected")); display->drawString(x, y, String("WiFi - Connected"));
display->drawString(x + SCREEN_WIDTH - display->getStringWidth("RSSI " + String(WiFi.RSSI())), y,
"RSSI " + String(WiFi.RSSI()));
} }
if (radioConfig.preferences.wifi_ap_mode) { if (radioConfig.preferences.wifi_ap_mode) {
display->drawString(x, y + FONT_HEIGHT * 1, "IP " + String(WiFi.softAPIP().toString().c_str())); display->drawString(x, y + FONT_HEIGHT * 1, "IP " + String(WiFi.softAPIP().toString().c_str()));
} else { } else {
@ -878,7 +882,6 @@ void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, i
#endif #endif
} }
void DebugInfo::drawFrameSettings(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) void DebugInfo::drawFrameSettings(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y)
{ {
displayedNodeNum = 0; // Not currently showing a node pane displayedNodeNum = 0; // Not currently showing a node pane
@ -889,28 +892,20 @@ void DebugInfo::drawFrameSettings(OLEDDisplay *display, OLEDDisplayUiState *stat
display->setTextAlignment(TEXT_ALIGN_LEFT); display->setTextAlignment(TEXT_ALIGN_LEFT);
char batStr[20]; char batStr[20];
if (powerStatus->getHasBattery()) if (powerStatus->getHasBattery()) {
{
int batV = powerStatus->getBatteryVoltageMv() / 1000; int batV = powerStatus->getBatteryVoltageMv() / 1000;
int batCv = (powerStatus->getBatteryVoltageMv() % 1000) / 10; int batCv = (powerStatus->getBatteryVoltageMv() % 1000) / 10;
snprintf(batStr, sizeof(batStr), "B %01d.%02dV %3d%% %c%c", snprintf(batStr, sizeof(batStr), "B %01d.%02dV %3d%% %c%c", batV, batCv, powerStatus->getBatteryChargePercent(),
batV, powerStatus->getIsCharging() ? '+' : ' ', powerStatus->getHasUSB() ? 'U' : ' ');
batCv,
powerStatus->getBatteryChargePercent(),
powerStatus->getIsCharging() ? '+' : ' ',
powerStatus->getHasUSB() ? 'U' : ' ');
// Line 1 // Line 1
display->drawString(x, y, batStr); display->drawString(x, y, batStr);
} } else {
else
{
// Line 1 // Line 1
display->drawString(x, y, String("USB")); display->drawString(x, y, String("USB"));
} }
// TODO: Display status of the BT radio // TODO: Display status of the BT radio
// display->drawString(x + SCREEN_WIDTH - display->getStringWidth("BT On"), y, "BT On"); // display->drawString(x + SCREEN_WIDTH - display->getStringWidth("BT On"), y, "BT On");
@ -925,20 +920,15 @@ void DebugInfo::drawFrameSettings(OLEDDisplay *display, OLEDDisplayUiState *stat
minutes %= 60; minutes %= 60;
hours %= 24; hours %= 24;
display->drawString(x, y + FONT_HEIGHT * 1, String(days) + "d " display->drawString(x, y + FONT_HEIGHT * 1,
+ (hours < 10 ? "0" : "") + String(hours) + ":" String(days) + "d " + (hours < 10 ? "0" : "") + String(hours) + ":" + (minutes < 10 ? "0" : "") +
+ (minutes < 10 ? "0" : "") + String(minutes) + ":" String(minutes) + ":" + (seconds < 10 ? "0" : "") + String(seconds));
+ (seconds < 10 ? "0" : "") + String(seconds)); display->drawString(x + SCREEN_WIDTH - display->getStringWidth("Mode " + String(channelSettings.modem_config)),
display->drawString(x + SCREEN_WIDTH - display->getStringWidth("Mode " + String(channelSettings.modem_config)), y + FONT_HEIGHT * 1, "Mode " + String(channelSettings.modem_config)); y + FONT_HEIGHT * 1, "Mode " + String(channelSettings.modem_config));
// Line 3
// TODO: Use this line for WiFi information.
// display->drawString(x + (SCREEN_WIDTH - (display->getStringWidth("WiFi: 192.168.0.100"))) / 2, y + FONT_HEIGHT * 2, "WiFi: 192.168.0.100");
// Line 4 // Line 4
drawGPScoordinates(display, x, y + FONT_HEIGHT * 3, gpsStatus); drawGPScoordinates(display, x, y + FONT_HEIGHT * 3, gpsStatus);
/* Display a heartbeat pixel that blinks every time the frame is redrawn */ /* Display a heartbeat pixel that blinks every time the frame is redrawn */
#ifdef SHOW_REDRAWS #ifdef SHOW_REDRAWS
if (heartbeat) if (heartbeat)

View File

@ -8,6 +8,24 @@
WebServer webserver(80); WebServer webserver(80);
const uint16_t maxMessages = 50;
struct message_t {
char sender[10];
char message[250];
int32_t gpsLat;
int32_t gpsLong;
uint32_t time;
bool fromMe;
};
struct messages_t
{
message_t history[maxMessages]; // 900 positions to save up to 1200 seconds (15 minutes). uInt for each temerature sensor, Input and Setpoint.
};
messages_t messages_history;
String something = ""; String something = "";
String sender = ""; String sender = "";
@ -27,15 +45,19 @@ void handleWebResponse()
void initWebServer() void initWebServer()
{ {
webserver.onNotFound(handleNotFound); webserver.onNotFound(handleNotFound);
// webserver.on("/", handleJSONChatHistory); webserver.on("/json/chat/send/channel", handleJSONChatHistory);
// webserver.on("/json/chat/history", handleJSONChatHistory); webserver.on("/json/chat/send/user", handleJSONChatHistory);
webserver.on("/json/chat/history/channel", handleJSONChatHistory);
webserver.on("/json/chat/history/user", handleJSONChatHistory);
webserver.on("/json/stats", handleJSONChatHistory);
webserver.on("/hotspot-detect.html", handleHotspot); webserver.on("/hotspot-detect.html", handleHotspot);
webserver.on("/", []() { webserver.send(200, "text/plain", "Everything is awesome!"); }); webserver.on("/", handleRoot);
webserver.begin(); webserver.begin();
} }
void handleJSONChatHistory() void handleJSONChatHistory()
{ {
int i;
String out = ""; String out = "";
out += "{\n"; out += "{\n";
@ -47,6 +69,14 @@ void handleJSONChatHistory()
out += "\"" + something + "\""; out += "\"" + something + "\"";
out += "]\n"; out += "]\n";
for (i = 0; i < maxMessages; i++) {
out += "[";
out += "\"" + String(messages_history.history[i].sender) + "\"";
out += ",";
out += "\"" + String(messages_history.history[i].message) + "\"";
out += "]\n";
}
out += "\n"; out += "\n";
out += " }\n"; out += " }\n";
out += "}\n"; out += "}\n";
@ -57,7 +87,8 @@ void handleJSONChatHistory()
void handleNotFound() void handleNotFound()
{ {
String message = "File Not Found\n\n"; String message = "";
message += "File Not Found\n\n";
message += "URI: "; message += "URI: ";
message += webserver.uri(); message += webserver.uri();
message += "\nMethod: "; message += "\nMethod: ";
@ -71,8 +102,6 @@ void handleNotFound()
} }
Serial.println(message); Serial.println(message);
webserver.send(404, "text/plain", message); webserver.send(404, "text/plain", message);
/*
*/
} }
/* /*
@ -82,6 +111,11 @@ void handleHotspot()
{ {
DEBUG_MSG("Hotspot Request\n"); DEBUG_MSG("Hotspot Request\n");
/*
If we don't do a redirect, be sure to return a "Success" message
otherwise iOS will have trouble detecting that the connection to the SoftAP worked.
*/
String out = ""; String out = "";
// out += "Success\n"; // out += "Success\n";
out += "<meta http-equiv=\"refresh\" content=\"0;url=http://meshtastic.org/\" />\n"; out += "<meta http-equiv=\"refresh\" content=\"0;url=http://meshtastic.org/\" />\n";
@ -89,6 +123,61 @@ void handleHotspot()
return; return;
} }
void handleRoot()
{
DEBUG_MSG("Hotspot Request\n");
/*
If we don't do a redirect, be sure to return a "Success" message
otherwise iOS will have trouble detecting that the connection to the SoftAP worked.
*/
String out = "";
// out += "Success\n";
out += "<!doctype html>\n"
"<html>\n"
"<head>\n"
"<meta charset=\"utf-8\">\n"
"<title>Meshtastic - WebUI</title>\n"
"</head>\n"
"\n"
"<body>\n"
"<center>\n"
" <form>\n"
" <table width=\"900\" height=\"100%\" border=\"0\">\n"
" <tbody>\n"
" <tr>\n"
" <td align=\"center\">Meshtastic - WebUI (Pre-Alpha)</td>\n"
" </tr>\n"
" <tr>\n"
" <td><table width=\"100%\" height=\"100%\" border=\"1\">\n"
" <tbody>\n"
" <tr>\n"
" <td width=\"50\" height=\"100%\">Channels:<br>\n"
" <br>\n"
" #general<br>\n"
" #stuff<br>\n"
" #things<br>\n"
" <br></td>\n"
" <td height=\"100%\" valign=\"top\"><div>Everything is awesome!</div></td>\n"
" </tr>\n"
" </tbody>\n"
" </table></td>\n"
" </tr>\n"
" <tr>\n"
" <td align=\"center\"><input type=\"text\" size=\"50\">\n"
" <input type=\"submit\"></td>\n"
" </tr>\n"
" </tbody>\n"
" </table>\n"
" </form>\n"
"</center>\n"
"</body>\n"
"</html>\n";
webserver.send(200, "text/html", out);
return;
}
void notifyWebUI() void notifyWebUI()
{ {
DEBUG_MSG("************ Got a message! ************\n"); DEBUG_MSG("************ Got a message! ************\n");

View File

@ -14,3 +14,5 @@ void handleJSONChatHistory();
void notifyWebUI(); void notifyWebUI();
void handleHotspot(); void handleHotspot();
void handleRoot();

View File

@ -3,8 +3,8 @@
#include "configuration.h" #include "configuration.h"
#include "main.h" #include "main.h"
#include "meshwifi/meshhttp.h" #include "meshwifi/meshhttp.h"
#include <WiFi.h>
#include <DNSServer.h> #include <DNSServer.h>
#include <WiFi.h>
static void WiFiEvent(WiFiEvent_t event); static void WiFiEvent(WiFiEvent_t event);
@ -16,6 +16,9 @@ bool isWifiAvailable()
const char *wifiPsw = radioConfig.preferences.wifi_password; const char *wifiPsw = radioConfig.preferences.wifi_password;
if (*wifiName && *wifiPsw) { if (*wifiName && *wifiPsw) {
// Once every 10 seconds, try to reconnect.
return 1; return 1;
} else { } else {
return 0; return 0;
@ -26,7 +29,7 @@ bool isWifiAvailable()
void deinitWifi() void deinitWifi()
{ {
/* /*
Note from Jm (Sept 16, 2020): Note from Jm (jm@casler.org - Sept 16, 2020):
A bug in the ESP32 SDK was introduced in Oct 2019 that keeps the WiFi radio from A bug in the ESP32 SDK was introduced in Oct 2019 that keeps the WiFi radio from
turning back on after it's shut off. See: turning back on after it's shut off. See:
@ -38,7 +41,7 @@ void deinitWifi()
WiFi.mode(WIFI_MODE_NULL); WiFi.mode(WIFI_MODE_NULL);
DEBUG_MSG("WiFi Turned Off\n"); DEBUG_MSG("WiFi Turned Off\n");
WiFi.printDiag(Serial); // WiFi.printDiag(Serial);
} }
// Startup WiFi // Startup WiFi
@ -53,7 +56,7 @@ void initWifi()
const char *wifiPsw = radioConfig.preferences.wifi_password; const char *wifiPsw = radioConfig.preferences.wifi_password;
/* /*
if (1) { if (0) {
radioConfig.preferences.wifi_ap_mode = 1; radioConfig.preferences.wifi_ap_mode = 1;
strcpy(radioConfig.preferences.wifi_ssid, "MeshTest2"); strcpy(radioConfig.preferences.wifi_ssid, "MeshTest2");
strcpy(radioConfig.preferences.wifi_password, "12345678"); strcpy(radioConfig.preferences.wifi_password, "12345678");
@ -76,7 +79,6 @@ void initWifi()
dnsServer.start(53, "*", apIP); dnsServer.start(53, "*", apIP);
} else { } else {
WiFi.mode(WIFI_MODE_STA); WiFi.mode(WIFI_MODE_STA);
WiFi.onEvent(WiFiEvent); WiFi.onEvent(WiFiEvent);
@ -116,9 +118,9 @@ static void WiFiEvent(WiFiEvent_t event)
break; break;
case SYSTEM_EVENT_STA_DISCONNECTED: case SYSTEM_EVENT_STA_DISCONNECTED:
DEBUG_MSG("Disconnected from WiFi access point\n"); DEBUG_MSG("Disconnected from WiFi access point\n");
// Event 5
// Reconnect WiFi reconnectWiFi();
initWifi();
break; break;
case SYSTEM_EVENT_STA_AUTHMODE_CHANGE: case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
DEBUG_MSG("Authentication mode of access point has changed\n"); DEBUG_MSG("Authentication mode of access point has changed\n");
@ -189,12 +191,29 @@ static void WiFiEvent(WiFiEvent_t event)
break; break;
default: default:
break; break;
} }
} }
void handleDNSResponse() { void handleDNSResponse()
{
if (radioConfig.preferences.wifi_ap_mode) { if (radioConfig.preferences.wifi_ap_mode) {
dnsServer.processNextRequest(); dnsServer.processNextRequest();
} }
} }
void reconnectWiFi()
{
const char *wifiName = radioConfig.preferences.wifi_ssid;
const char *wifiPsw = radioConfig.preferences.wifi_password;
if (radioConfig.has_preferences) {
if (*wifiName && *wifiPsw) {
DEBUG_MSG("... Reconnecting to WiFi access point");
WiFi.mode(WIFI_MODE_STA);
WiFi.begin(wifiName, wifiPsw);
}
}
}

View File

@ -5,8 +5,8 @@
#include <functional> #include <functional>
#ifdef HAS_WIFI #ifdef HAS_WIFI
#include <WiFi.h>
#include <DNSServer.h> #include <DNSServer.h>
#include <WiFi.h>
#endif #endif
void initWifi(); void initWifi();
@ -14,3 +14,5 @@ void deinitWifi();
bool isWifiAvailable(); bool isWifiAvailable();
void handleDNSResponse(); void handleDNSResponse();
void reconnectWiFi();