diff --git a/WifiInterface.cpp b/WifiInterface.cpp index 467b181..5d04400 100644 --- a/WifiInterface.cpp +++ b/WifiInterface.cpp @@ -26,7 +26,8 @@ #include "WifiInboundHandler.h" -#define OK_SEARCH F("\r\nOK\r\n") + + const unsigned long LOOP_TIMEOUT = 2000; bool WifiInterface::connected = false; @@ -125,7 +126,7 @@ wifiSerialState WifiInterface::setup(Stream & setupStream, const FSH* SSid, con if (wifiState == WIFI_CONNECTED) { StringFormatter::send(wifiStream, F("ATE0\r\n")); // turn off the echo - checkForOK(200, OK_SEARCH, true); + checkForOK(200, true); } @@ -144,7 +145,7 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password, bool oldCmd = false; char macAddress[17]; // mac address extraction - + // First check... Restarting the Arduino does not restart the ES. // There may alrerady be a connection with data in the pipeline. // If there is, just shortcut the setup and continue to read the data as normal. @@ -154,25 +155,25 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password, } StringFormatter::send(wifiStream, F("AT\r\n")); // Is something here that understands AT? - if(!checkForOK(200, OK_SEARCH, true)) + if(!checkForOK(200, true)) 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 + checkForOK(2000, true); // Makes this visible on the console // Display the AT version information StringFormatter::send(wifiStream, F("AT+GMR\r\n")); - checkForOK(2000, OK_SEARCH, true, false); // Makes this visible on the console + checkForOK(2000, true, false); // Makes this visible on the console #ifdef DONT_TOUCH_WIFI_CONF DIAG(F("\nDONT_TOUCH_WIFI_CONF was set: Using existing config\n")); #else StringFormatter::send(wifiStream, F("AT+CWMODE=1\r\n")); // configure as "station" = WiFi client - checkForOK(1000, OK_SEARCH, true); // Not always OK, sometimes "no change" + checkForOK(1000, true); // Not always OK, sometimes "no change" // Older ES versions have AT+CWJAP, newer ones have AT+CWJAP_CUR and AT+CWHOSTNAME StringFormatter::send(wifiStream, F("AT+CWJAP?\r\n")); - if (checkForOK(2000, OK_SEARCH, true)) { + if (checkForOK(2000, true)) { oldCmd=true; while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE } @@ -197,14 +198,14 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password, if (oldCmd) { // AT command early version supports CWJAP/CWSAP StringFormatter::send(wifiStream, F("AT+CWJAP=\"%S\",\"%S\"\r\n"), SSid, password); - ipOK = checkForOK(WIFI_CONNECT_TIMEOUT, OK_SEARCH, true); + ipOK = checkForOK(WIFI_CONNECT_TIMEOUT, true); } else { // later version supports CWJAP_CUR StringFormatter::send(wifiStream, F("AT+CWHOSTNAME=\"%S\"\r\n"), hostname); // Set Host name for Wifi Client - checkForOK(2000, OK_SEARCH, true); // dont care if not supported + checkForOK(2000, true); // dont care if not supported StringFormatter::send(wifiStream, F("AT+CWJAP_CUR=\"%S\",\"%S\"\r\n"), SSid, password); - ipOK = checkForOK(WIFI_CONNECT_TIMEOUT, OK_SEARCH, true); + ipOK = checkForOK(WIFI_CONNECT_TIMEOUT, true); } if (ipOK) { @@ -222,14 +223,14 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password, // If we have not managed to get this going in station mode, go for AP mode // StringFormatter::send(wifiStream, F("AT+RST\r\n")); -// checkForOK(1000, OK_SEARCH, true); // Not always OK, sometimes "no change" +// checkForOK(1000, true); // Not always OK, sometimes "no change" int i=0; do { // configure as AccessPoint. Try really hard as this is the // last way out to get any Wifi connectivity. StringFormatter::send(wifiStream, F("AT+CWMODE=2\r\n")); - } while (!checkForOK(1000+i*500, OK_SEARCH, true) && i++<10); + } while (!checkForOK(1000+i*500, true) && i++<10); while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE @@ -248,7 +249,7 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password, } char macTail[]={macAddress[9],macAddress[10],macAddress[12],macAddress[13],macAddress[15],macAddress[16],'\0'}; - checkForOK(1000, OK_SEARCH, true, false); // suck up remainder of AT+CIFSR + checkForOK(1000, true, false); // suck up remainder of AT+CIFSR i=0; do { @@ -261,24 +262,24 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password, StringFormatter::send(wifiStream, F("AT+CWSAP%s=\"DCCEX_%s\",\"%S\",%d,4\r\n"), oldCmd ? "" : "_CUR", macTail, password, channel); } - } while (!checkForOK(WIFI_CONNECT_TIMEOUT, OK_SEARCH, true) && i++<2); // do twice if necessary but ignore failure as AP mode may still be ok + } while (!checkForOK(WIFI_CONNECT_TIMEOUT, true) && i++<2); // do twice if necessary but ignore failure as AP mode may still be ok if (i >= 2) DIAG(F("\nWarning: Setting AP SSID and password failed\n")); // but issue warning if (!oldCmd) { StringFormatter::send(wifiStream, F("AT+CIPRECVMODE=0\r\n"), port); // make sure transfer mode is correct - checkForOK(2000, OK_SEARCH, true); + checkForOK(2000, true); } } StringFormatter::send(wifiStream, F("AT+CIPSERVER=0\r\n")); // turn off tcp server (to clean connections before CIPMUX=1) - checkForOK(1000, OK_SEARCH, true); // ignore result in case it already was off + checkForOK(1000, true); // ignore result in case it already was off StringFormatter::send(wifiStream, F("AT+CIPMUX=1\r\n")); // configure for multiple connections - if (!checkForOK(1000, OK_SEARCH, true)) return WIFI_DISCONNECTED; + if (!checkForOK(1000, true)) return WIFI_DISCONNECTED; StringFormatter::send(wifiStream, F("AT+CIPSERVER=1,%d\r\n"), port); // turn on server on port - if (!checkForOK(1000, OK_SEARCH, true)) return WIFI_DISCONNECTED; + if (!checkForOK(1000, true)) return WIFI_DISCONNECTED; #endif //DONT_TOUCH_WIFI_CONF StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); // Display ip addresses to the DIAG @@ -301,7 +302,7 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password, LCD(4,F("%s"),ipString); // There is not enough room on some LCDs to put a title to this } // suck up anything after the IP. - if (!checkForOK(1000, OK_SEARCH, true, false)) return WIFI_DISCONNECTED; + if (!checkForOK(1000, true, false)) return WIFI_DISCONNECTED; LCD(5,F("PORT=%d\n"),port); return WIFI_CONNECTED; @@ -324,12 +325,16 @@ void WifiInterface::ATCommand(const byte * command) { } else { StringFormatter:: send(wifiStream, F("AT+%s\r\n"), command); - checkForOK(10000, OK_SEARCH, true); + checkForOK(10000, true); } } +bool WifiInterface::checkForOK( const unsigned int timeout, bool echo, bool escapeEcho) { + return checkForOK(timeout,F("\r\nOK\r\n"),echo,escapeEcho); +} + bool WifiInterface::checkForOK( const unsigned int timeout, const FSH * waitfor, bool echo, bool escapeEcho) { unsigned long startTime = millis(); char *locator = (char *)waitfor; diff --git a/WifiInterface.h b/WifiInterface.h index 8bf37e4..19f8a3a 100644 --- a/WifiInterface.h +++ b/WifiInterface.h @@ -38,7 +38,7 @@ public: const byte channel); static void loop(); static void ATCommand(const byte *command); - + private: static wifiSerialState setup(Stream &setupStream, const FSH *SSSid, const FSH *password, const FSH *hostname, int port, byte channel); @@ -46,6 +46,7 @@ private: static DCCEXParser parser; static wifiSerialState setup2(const FSH *SSSid, const FSH *password, const FSH *hostname, int port, byte channel); + static bool checkForOK(const unsigned int timeout, bool echo, bool escapeEcho = true); static bool checkForOK(const unsigned int timeout, const FSH *waitfor, bool echo, bool escapeEcho = true); static bool connected; };