Skip to content

Commit

Permalink
Fix for "Wifi in station mode sometimes enters loops of repeatedly jo…
Browse files Browse the repository at this point in the history
…ining... meshtastic#420"

Fix for Wifi in station mode sometimes enters loops of repeatedly joining... meshtastic#420
  • Loading branch information
mc-hamster committed Sep 19, 2020
1 parent 945f726 commit 464a422
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 41 deletions.
48 changes: 19 additions & 29 deletions src/graphics/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#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 */

Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
20 changes: 18 additions & 2 deletions src/meshwifi/meshhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = "";
Expand Down Expand Up @@ -50,6 +57,7 @@ void initWebServer()

void handleJSONChatHistory()
{
int i;

String out = "";
out += "{\n";
Expand All @@ -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";
Expand Down
33 changes: 24 additions & 9 deletions src/meshwifi/meshwifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ bool isWifiAvailable()

if (*wifiName && *wifiPsw) {


//

// Once every 10 seconds, try to reconnect.

return 1;
} else {
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
}
}
4 changes: 3 additions & 1 deletion src/meshwifi/meshwifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
#include <functional>

#ifdef HAS_WIFI
#include <WiFi.h>
#include <DNSServer.h>
#include <WiFi.h>
#endif

void initWifi();
void deinitWifi();
bool isWifiAvailable();

void handleDNSResponse();

void reconnectWiFi();

0 comments on commit 464a422

Please sign in to comment.