From 82fbedbf4155fcf27b84818292ca80fca1a63a4a Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Fri, 18 Sep 2020 18:51:42 -0700 Subject: [PATCH 1/5] Auto formatting of meshwifi.cpp --- src/meshwifi/meshwifi.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/meshwifi/meshwifi.cpp b/src/meshwifi/meshwifi.cpp index f395ff7b..a88ed6ae 100644 --- a/src/meshwifi/meshwifi.cpp +++ b/src/meshwifi/meshwifi.cpp @@ -3,14 +3,14 @@ #include "configuration.h" #include "main.h" #include "meshwifi/meshhttp.h" -#include #include +#include static void WiFiEvent(WiFiEvent_t event); DNSServer dnsServer; -bool isWifiAvailable() +bool isWifiAvailable() { const char *wifiName = radioConfig.preferences.wifi_ssid; const char *wifiPsw = radioConfig.preferences.wifi_password; @@ -66,7 +66,7 @@ void initWifi() if (*wifiName && *wifiPsw) { if (radioConfig.preferences.wifi_ap_mode) { - + IPAddress apIP(192, 168, 42, 1); WiFi.onEvent(WiFiEvent); @@ -76,7 +76,6 @@ void initWifi() dnsServer.start(53, "*", apIP); - } else { WiFi.mode(WIFI_MODE_STA); WiFi.onEvent(WiFiEvent); @@ -189,11 +188,11 @@ static void WiFiEvent(WiFiEvent_t event) break; default: break; - } } -void handleDNSResponse() { +void handleDNSResponse() +{ if (radioConfig.preferences.wifi_ap_mode) { dnsServer.processNextRequest(); } From 6e3b22c624db852ca8e76e142e57e16c2991c82c Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Fri, 18 Sep 2020 20:42:35 -0700 Subject: [PATCH 2/5] Stub for a handler of the root (/) of the web server with a html table and form for chat --- src/meshwifi/meshhttp.cpp | 62 ++++++++++++++++++++++++++++++++++++++- src/meshwifi/meshhttp.h | 2 ++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index c2f6ed34..0f8988dd 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -30,7 +30,7 @@ void initWebServer() // webserver.on("/", handleJSONChatHistory); // webserver.on("/json/chat/history", handleJSONChatHistory); webserver.on("/hotspot-detect.html", handleHotspot); - webserver.on("/", []() { webserver.send(200, "text/plain", "Everything is awesome!"); }); + webserver.on("/", handleRoot); webserver.begin(); } @@ -82,6 +82,11 @@ void handleHotspot() { 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 += "\n"; @@ -89,6 +94,61 @@ void handleHotspot() 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 += "\n" + "\n" + "\n" + "\n" + "Meshtastic - WebUI\n" + "\n" + "\n" + "\n" + "
\n" + "
\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
Meshtastic - WebUI (Pre-Alpha)
\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
Channels:
\n" + "
\n" + " #general
\n" + " #stuff
\n" + " #things
\n" + "
Everything is awesome!
\n" + "
\n" + "
\n" + "
\n" + "\n" + "\n"; + webserver.send(200, "text/html", out); + return; +} + void notifyWebUI() { DEBUG_MSG("************ Got a message! ************\n"); diff --git a/src/meshwifi/meshhttp.h b/src/meshwifi/meshhttp.h index 8c607f6f..b1a5dbb9 100644 --- a/src/meshwifi/meshhttp.h +++ b/src/meshwifi/meshhttp.h @@ -14,3 +14,5 @@ void handleJSONChatHistory(); void notifyWebUI(); void handleHotspot(); + +void handleRoot(); \ No newline at end of file From c57a9a86132ddf15f0c4869a042566cef2124544 Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 19 Sep 2020 11:24:55 -0700 Subject: [PATCH 3/5] Update from my laptop --- src/meshwifi/meshhttp.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 0f8988dd..d341b863 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -27,8 +27,11 @@ void handleWebResponse() void initWebServer() { webserver.onNotFound(handleNotFound); - // webserver.on("/", handleJSONChatHistory); - // webserver.on("/json/chat/history", handleJSONChatHistory); + webserver.on("/json/chat/send/channel", 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("/", handleRoot); webserver.begin(); @@ -57,7 +60,8 @@ void handleJSONChatHistory() void handleNotFound() { - String message = "File Not Found\n\n"; + String message = ""; + message += "File Not Found\n\n"; message += "URI: "; message += webserver.uri(); message += "\nMethod: "; @@ -71,8 +75,6 @@ void handleNotFound() } Serial.println(message); webserver.send(404, "text/plain", message); - /* - */ } /* From 7c44daf8f47fc8c19b9e0cfcedc4c1e10e9ac0dd Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 19 Sep 2020 12:50:43 -0700 Subject: [PATCH 4/5] pushing my chances to personal branch so i can get the changes from the laptop --- src/meshwifi/meshhttp.cpp | 11 +++++++++++ src/meshwifi/meshwifi.cpp | 13 +++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 0f8988dd..86348655 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -8,6 +8,17 @@ WebServer webserver(80); +struct message { + char sender[10]; + char message[250]; + int32_t gpsLat; + int32_t gpsLong; + uint32_t time; + bool fromMe; +}; + +struct message arrayMessages[50]; + String something = ""; String sender = ""; diff --git a/src/meshwifi/meshwifi.cpp b/src/meshwifi/meshwifi.cpp index a88ed6ae..112b5034 100644 --- a/src/meshwifi/meshwifi.cpp +++ b/src/meshwifi/meshwifi.cpp @@ -16,6 +16,11 @@ bool isWifiAvailable() const char *wifiPsw = radioConfig.preferences.wifi_password; if (*wifiName && *wifiPsw) { + + + // + + return 1; } else { return 0; @@ -26,7 +31,7 @@ bool isWifiAvailable() 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 turning back on after it's shut off. See: @@ -52,16 +57,16 @@ void initWifi() const char *wifiName = radioConfig.preferences.wifi_ssid; const char *wifiPsw = radioConfig.preferences.wifi_password; - /* - if (1) { + if (0) { radioConfig.preferences.wifi_ap_mode = 1; strcpy(radioConfig.preferences.wifi_ssid, "MeshTest2"); strcpy(radioConfig.preferences.wifi_password, "12345678"); } else { radioConfig.preferences.wifi_ap_mode = 0; - strcpy(radioConfig.preferences.wifi_ssid, "meshtastic"); + strcpy(radioConfig.preferences.wifi_ssid, "meshtastic1"); strcpy(radioConfig.preferences.wifi_password, "meshtastic!"); } + /* */ if (*wifiName && *wifiPsw) { From 464a42258fc625c9b16a15773cb13272be4e444b Mon Sep 17 00:00:00 2001 From: Jm Casler Date: Sat, 19 Sep 2020 16:38:59 -0700 Subject: [PATCH 5/5] Fix for "Wifi in station mode sometimes enters loops of repeatedly joining... #420" Fix for Wifi in station mode sometimes enters loops of repeatedly joining... #420 --- src/graphics/Screen.cpp | 48 ++++++++++++++++----------------------- src/meshwifi/meshhttp.cpp | 20 ++++++++++++++-- src/meshwifi/meshwifi.cpp | 33 +++++++++++++++++++-------- src/meshwifi/meshwifi.h | 4 +++- 4 files changed, 64 insertions(+), 41 deletions(-) diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index 7e85fe8a..d0d0d921 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -31,8 +31,8 @@ along with this program. If not, see . #include "graphics/images.h" #include "main.h" #include "mesh-pb-constants.h" -#include "utils.h" #include "meshwifi/meshwifi.h" +#include "utils.h" using namespace meshtastic; /** @todo remove */ @@ -715,7 +715,6 @@ void Screen::drawDebugInfoWiFiTrampoline(OLEDDisplay *display, OLEDDisplayUiStat screen->debugInfo.drawFrameWiFi(display, state, x, y); } - // restore our regular frame list void Screen::setFrames() { @@ -854,12 +853,17 @@ void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, i if (radioConfig.preferences.wifi_ap_mode) { display->drawString(x, y, String("WiFi - Software AP")); - } else if ( WiFi.status() != WL_CONNECTED ) { + } else if (WiFi.status() != WL_CONNECTED) { display->drawString(x, y, String("WiFi - Not Connected")); } else { 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) { display->drawString(x, y + FONT_HEIGHT * 1, "IP " + String(WiFi.softAPIP().toString().c_str())); } else { @@ -868,7 +872,7 @@ void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, i display->drawString(x, y + FONT_HEIGHT * 2, "SSID " + String(wifiName)); display->drawString(x, y + FONT_HEIGHT * 3, "PWD " + String(wifiPsw)); - + /* Display a heartbeat pixel that blinks every time the frame is redrawn */ #ifdef SHOW_REDRAWS if (heartbeat) @@ -878,7 +882,6 @@ void DebugInfo::drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, i #endif } - void DebugInfo::drawFrameSettings(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) { displayedNodeNum = 0; // Not currently showing a node pane @@ -889,29 +892,21 @@ void DebugInfo::drawFrameSettings(OLEDDisplay *display, OLEDDisplayUiState *stat display->setTextAlignment(TEXT_ALIGN_LEFT); char batStr[20]; - if (powerStatus->getHasBattery()) - { + if (powerStatus->getHasBattery()) { int batV = powerStatus->getBatteryVoltageMv() / 1000; int batCv = (powerStatus->getBatteryVoltageMv() % 1000) / 10; - snprintf(batStr, sizeof(batStr), "B %01d.%02dV %3d%% %c%c", - batV, - batCv, - powerStatus->getBatteryChargePercent(), - powerStatus->getIsCharging() ? '+' : ' ', - powerStatus->getHasUSB() ? 'U' : ' '); + snprintf(batStr, sizeof(batStr), "B %01d.%02dV %3d%% %c%c", batV, batCv, powerStatus->getBatteryChargePercent(), + powerStatus->getIsCharging() ? '+' : ' ', powerStatus->getHasUSB() ? 'U' : ' '); // Line 1 display->drawString(x, y, batStr); - } - else - { + } else { // Line 1 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"); // Line 2 @@ -925,20 +920,15 @@ void DebugInfo::drawFrameSettings(OLEDDisplay *display, OLEDDisplayUiState *stat minutes %= 60; hours %= 24; - display->drawString(x, y + FONT_HEIGHT * 1, String(days) + "d " - + (hours < 10 ? "0" : "") + String(hours) + ":" - + (minutes < 10 ? "0" : "") + String(minutes) + ":" - + (seconds < 10 ? "0" : "") + String(seconds)); - display->drawString(x + SCREEN_WIDTH - display->getStringWidth("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"); + display->drawString(x, y + FONT_HEIGHT * 1, + String(days) + "d " + (hours < 10 ? "0" : "") + String(hours) + ":" + (minutes < 10 ? "0" : "") + + String(minutes) + ":" + (seconds < 10 ? "0" : "") + String(seconds)); + display->drawString(x + SCREEN_WIDTH - display->getStringWidth("Mode " + String(channelSettings.modem_config)), + y + FONT_HEIGHT * 1, "Mode " + String(channelSettings.modem_config)); // Line 4 drawGPScoordinates(display, x, y + FONT_HEIGHT * 3, gpsStatus); - /* Display a heartbeat pixel that blinks every time the frame is redrawn */ #ifdef SHOW_REDRAWS if (heartbeat) diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 4899e6ae..6b377673 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -8,7 +8,9 @@ WebServer webserver(80); -struct message { +const uint16_t maxMessages = 50; + +struct message_t { char sender[10]; char message[250]; int32_t gpsLat; @@ -17,7 +19,12 @@ struct message { bool fromMe; }; -struct message arrayMessages[50]; +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 sender = ""; @@ -50,6 +57,7 @@ void initWebServer() void handleJSONChatHistory() { + int i; String out = ""; out += "{\n"; @@ -61,6 +69,14 @@ void handleJSONChatHistory() out += "\"" + something + "\""; 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"; diff --git a/src/meshwifi/meshwifi.cpp b/src/meshwifi/meshwifi.cpp index 112b5034..e31065ef 100644 --- a/src/meshwifi/meshwifi.cpp +++ b/src/meshwifi/meshwifi.cpp @@ -17,9 +17,7 @@ bool isWifiAvailable() if (*wifiName && *wifiPsw) { - - // - + // Once every 10 seconds, try to reconnect. return 1; } else { @@ -43,7 +41,7 @@ void deinitWifi() WiFi.mode(WIFI_MODE_NULL); DEBUG_MSG("WiFi Turned Off\n"); - WiFi.printDiag(Serial); + // WiFi.printDiag(Serial); } // Startup WiFi @@ -57,17 +55,17 @@ void initWifi() const char *wifiName = radioConfig.preferences.wifi_ssid; const char *wifiPsw = radioConfig.preferences.wifi_password; + /* if (0) { radioConfig.preferences.wifi_ap_mode = 1; strcpy(radioConfig.preferences.wifi_ssid, "MeshTest2"); strcpy(radioConfig.preferences.wifi_password, "12345678"); } else { radioConfig.preferences.wifi_ap_mode = 0; - strcpy(radioConfig.preferences.wifi_ssid, "meshtastic1"); + strcpy(radioConfig.preferences.wifi_ssid, "meshtastic"); strcpy(radioConfig.preferences.wifi_password, "meshtastic!"); } - /* - */ + */ if (*wifiName && *wifiPsw) { if (radioConfig.preferences.wifi_ap_mode) { @@ -120,9 +118,9 @@ static void WiFiEvent(WiFiEvent_t event) break; case SYSTEM_EVENT_STA_DISCONNECTED: DEBUG_MSG("Disconnected from WiFi access point\n"); + // Event 5 - // Reconnect WiFi - initWifi(); + reconnectWiFi(); break; case SYSTEM_EVENT_STA_AUTHMODE_CHANGE: DEBUG_MSG("Authentication mode of access point has changed\n"); @@ -201,4 +199,21 @@ void handleDNSResponse() if (radioConfig.preferences.wifi_ap_mode) { 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); + } + } } \ No newline at end of file diff --git a/src/meshwifi/meshwifi.h b/src/meshwifi/meshwifi.h index 44fff707..bcafa700 100644 --- a/src/meshwifi/meshwifi.h +++ b/src/meshwifi/meshwifi.h @@ -5,8 +5,8 @@ #include #ifdef HAS_WIFI -#include #include +#include #endif void initWifi(); @@ -14,3 +14,5 @@ void deinitWifi(); bool isWifiAvailable(); void handleDNSResponse(); + +void reconnectWiFi();