From c6cff3b4bdbce801af655d4156718f224fdfb0d4 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Tue, 27 Oct 2020 21:50:46 +0100 Subject: [PATCH] wifi setup tristate --- WifiInterface.cpp | 47 ++++++++++++++++++++++++++++------------------- WifiInterface.h | 6 ++++-- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/WifiInterface.cpp b/WifiInterface.cpp index b718b4b..65b5047 100644 --- a/WifiInterface.cpp +++ b/WifiInterface.cpp @@ -57,7 +57,7 @@ bool WifiInterface::setup(long serial_link_speed, const __FlashStringHelper *hostname, const int port) { - bool wifiUp = false; + wifiSerialState wifiUp = WIFI_NOAT; #if NUM_SERIAL == 0 // no warning about unused parameters. @@ -75,7 +75,7 @@ bool WifiInterface::setup(long serial_link_speed, // Other serials are tried, depending on hardware. #if NUM_SERIAL > 1 - if (!wifiUp) + if (wifiUp == WIFI_NOAT) { Serial2.begin(serial_link_speed); wifiUp = setup(Serial2, wifiESSID, wifiPassword, hostname, port); @@ -83,18 +83,22 @@ bool WifiInterface::setup(long serial_link_speed, #endif #if NUM_SERIAL > 2 - if (!wifiUp) + if (wifiUp == WIFI_NOAT) { Serial3.begin(serial_link_speed); wifiUp = setup(Serial3, wifiESSID, wifiPassword, hostname, port); } #endif - -return wifiUp; + if (wifiUp == WIFI_CONNECTED) + connected = true; + else + connected = false; + return connected; } -bool WifiInterface::setup(Stream & setupStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password, - const __FlashStringHelper* hostname, int port) { +wifiSerialState WifiInterface::setup(Stream & setupStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password, + const __FlashStringHelper* hostname, int port) { + wifiSerialState wifiState; static uint8_t ntry = 0; ntry++; @@ -102,9 +106,14 @@ bool WifiInterface::setup(Stream & setupStream, const __FlashStringHelper* SSid DIAG(F("\n++ Wifi Setup Try %d ++\n"), ntry); - connected = setup2( SSid, password, hostname, port); + wifiState = setup2( SSid, password, hostname, port); + + if (wifiState == WIFI_NOAT) { + DIAG(F("\n++ Wifi Setup NO AT ++\n")); + return wifiState; + } - if (connected) { + if (wifiState == WIFI_CONNECTED) { StringFormatter::send(wifiStream, F("ATE0\r\n")); // turn off the echo checkForOK(200, OK_SEARCH, true); } @@ -112,12 +121,12 @@ bool WifiInterface::setup(Stream & setupStream, const __FlashStringHelper* SSid DCCEXParser::setAtCommandCallback(ATCommand); WifiInboundHandler::setup(wifiStream); - DIAG(F("\n++ Wifi Setup %S ++\n"), connected ? F("OK") : F("FAILED")); - return connected; + DIAG(F("\n++ Wifi Setup %S ++\n"), wifiState == WIFI_CONNECTED ? F("CONNECTED") : F("DISCONNECTED")); + return wifiState; } -bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringHelper* password, - const __FlashStringHelper* hostname, int port) { +wifiSerialState WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringHelper* password, + const __FlashStringHelper* hostname, int port) { bool ipOK = false; bool oldCmd = false; @@ -129,12 +138,12 @@ bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringH if (checkForOK(200,IPD_SEARCH, true)) { DIAG(F("\nPreconfigured Wifi already running with data waiting\n")); // loopstate=4; // carry on from correct place... or not as the case may be - return true; + return WIFI_CONNECTED; } StringFormatter::send(wifiStream, F("AT\r\n")); // Is something here that understands AT? if(!checkForOK(200, OK_SEARCH, true)) - return false; // No AT compatible WiFi module here + return WIFI_NOAT; // No AT compatible WiFi module here StringFormatter::send(wifiStream, F("ATE1\r\n")); // Turn on the echo, se we can see what's happening checkForOK(2000, OK_SEARCH, true); // Makes this visible on the console @@ -237,16 +246,16 @@ bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringH checkForOK(10000, OK_SEARCH, true); // ignore result in case it already was off StringFormatter::send(wifiStream, F("AT+CIPMUX=1\r\n")); // configure for multiple connections - if (!checkForOK(10000, OK_SEARCH, true)) return false; + if (!checkForOK(10000, OK_SEARCH, true)) return WIFI_DISCONNECTED; StringFormatter::send(wifiStream, F("AT+CIPSERVER=1,%d\r\n"), port); // turn on server on port - if (!checkForOK(10000, OK_SEARCH, true)) return false; + if (!checkForOK(10000, OK_SEARCH, true)) return WIFI_DISCONNECTED; StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); // Display ip addresses to the DIAG - if (!checkForOK(10000, OK_SEARCH, true, false)) return false; + if (!checkForOK(10000, OK_SEARCH, true, false)) return WIFI_DISCONNECTED; DIAG(F("\nPORT=%d\n"),port); - return true; + return WIFI_CONNECTED; } diff --git a/WifiInterface.h b/WifiInterface.h index b2bdd20..033cbc9 100644 --- a/WifiInterface.h +++ b/WifiInterface.h @@ -24,6 +24,8 @@ #include #include +enum wifiSerialState { WIFI_NOAT, WIFI_DISCONNECTED, WIFI_CONNECTED }; + class WifiInterface { @@ -37,11 +39,11 @@ public: static void ATCommand(const byte *command); private: - static bool setup(Stream &setupStream, const __FlashStringHelper *SSSid, const __FlashStringHelper *password, + static wifiSerialState setup(Stream &setupStream, const __FlashStringHelper *SSSid, const __FlashStringHelper *password, const __FlashStringHelper *hostname, int port); static Stream *wifiStream; static DCCEXParser parser; - static bool setup2(const __FlashStringHelper *SSSid, const __FlashStringHelper *password, + static wifiSerialState setup2(const __FlashStringHelper *SSSid, const __FlashStringHelper *password, const __FlashStringHelper *hostname, int port); static bool checkForOK(const unsigned int timeout, const char *waitfor, bool echo, bool escapeEcho = true); static bool connected;