|
|
|
@ -26,11 +26,9 @@
|
|
|
|
|
|
|
|
|
|
#include "WifiInboundHandler.h"
|
|
|
|
|
|
|
|
|
|
const char FLASH READY_SEARCH[] = "\r\nready\r\n";
|
|
|
|
|
const char FLASH OK_SEARCH[] = "\r\nOK\r\n";
|
|
|
|
|
const char FLASH END_DETAIL_SEARCH[] = "@ 1000";
|
|
|
|
|
const char FLASH SEND_OK_SEARCH[] = "\r\nSEND OK\r\n";
|
|
|
|
|
const char FLASH IPD_SEARCH[] = "+IPD";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const unsigned long LOOP_TIMEOUT = 2000;
|
|
|
|
|
bool WifiInterface::connected = false;
|
|
|
|
|
Stream * WifiInterface::wifiStream;
|
|
|
|
@ -60,7 +58,8 @@ bool WifiInterface::setup(long serial_link_speed,
|
|
|
|
|
const FSH *wifiESSID,
|
|
|
|
|
const FSH *wifiPassword,
|
|
|
|
|
const FSH *hostname,
|
|
|
|
|
const int port) {
|
|
|
|
|
const int port,
|
|
|
|
|
const byte channel) {
|
|
|
|
|
|
|
|
|
|
wifiSerialState wifiUp = WIFI_NOAT;
|
|
|
|
|
|
|
|
|
@ -75,7 +74,7 @@ bool WifiInterface::setup(long serial_link_speed,
|
|
|
|
|
|
|
|
|
|
#if NUM_SERIAL > 0
|
|
|
|
|
Serial1.begin(serial_link_speed);
|
|
|
|
|
wifiUp = setup(Serial1, wifiESSID, wifiPassword, hostname, port);
|
|
|
|
|
wifiUp = setup(Serial1, wifiESSID, wifiPassword, hostname, port, channel);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Other serials are tried, depending on hardware.
|
|
|
|
@ -83,7 +82,7 @@ bool WifiInterface::setup(long serial_link_speed,
|
|
|
|
|
if (wifiUp == WIFI_NOAT)
|
|
|
|
|
{
|
|
|
|
|
Serial2.begin(serial_link_speed);
|
|
|
|
|
wifiUp = setup(Serial2, wifiESSID, wifiPassword, hostname, port);
|
|
|
|
|
wifiUp = setup(Serial2, wifiESSID, wifiPassword, hostname, port, channel);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
@ -91,7 +90,7 @@ bool WifiInterface::setup(long serial_link_speed,
|
|
|
|
|
if (wifiUp == WIFI_NOAT)
|
|
|
|
|
{
|
|
|
|
|
Serial3.begin(serial_link_speed);
|
|
|
|
|
wifiUp = setup(Serial3, wifiESSID, wifiPassword, hostname, port);
|
|
|
|
|
wifiUp = setup(Serial3, wifiESSID, wifiPassword, hostname, port, channel);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
@ -109,7 +108,7 @@ bool WifiInterface::setup(long serial_link_speed,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wifiSerialState WifiInterface::setup(Stream & setupStream, const FSH* SSid, const FSH* password,
|
|
|
|
|
const FSH* hostname, int port) {
|
|
|
|
|
const FSH* hostname, int port, byte channel) {
|
|
|
|
|
wifiSerialState wifiState;
|
|
|
|
|
static uint8_t ntry = 0;
|
|
|
|
|
ntry++;
|
|
|
|
@ -118,7 +117,7 @@ wifiSerialState WifiInterface::setup(Stream & setupStream, const FSH* SSid, con
|
|
|
|
|
|
|
|
|
|
DIAG(F("\n++ Wifi Setup Try %d ++\n"), ntry);
|
|
|
|
|
|
|
|
|
|
wifiState = setup2( SSid, password, hostname, port);
|
|
|
|
|
wifiState = setup2( SSid, password, hostname, port, channel);
|
|
|
|
|
|
|
|
|
|
if (wifiState == WIFI_NOAT) {
|
|
|
|
|
DIAG(F("\n++ Wifi Setup NO AT ++\n"));
|
|
|
|
@ -127,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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -141,7 +140,7 @@ wifiSerialState WifiInterface::setup(Stream & setupStream, const FSH* SSid, con
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
|
#endif
|
|
|
|
|
wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
|
|
|
|
|
const FSH* hostname, int port) {
|
|
|
|
|
const FSH* hostname, int port, byte channel) {
|
|
|
|
|
bool ipOK = false;
|
|
|
|
|
bool oldCmd = false;
|
|
|
|
|
|
|
|
|
@ -150,32 +149,31 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
|
|
|
|
|
// 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.
|
|
|
|
|
if (checkForOK(200,IPD_SEARCH, true)) {
|
|
|
|
|
if (checkForOK(200,F("+IPD"), 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 WIFI_CONNECTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
@ -191,8 +189,8 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
|
|
|
|
|
// typical connect time approx 7 seconds
|
|
|
|
|
delay(8000);
|
|
|
|
|
StringFormatter::send(wifiStream, F("AT+CIFSR\r\n"));
|
|
|
|
|
if (checkForOK(5000, (const char*) F("+CIFSR:STAIP"), true,false))
|
|
|
|
|
if (!checkForOK(1000, (const char*) F("0.0.0.0"), true,false))
|
|
|
|
|
if (checkForOK(5000, F("+CIFSR:STAIP"), true,false))
|
|
|
|
|
if (!checkForOK(1000, F("0.0.0.0"), true,false))
|
|
|
|
|
ipOK = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
@ -200,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) {
|
|
|
|
@ -215,8 +213,8 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
|
|
|
|
|
// Let's check for IP (via DHCP)
|
|
|
|
|
ipOK = false;
|
|
|
|
|
StringFormatter::send(wifiStream, F("AT+CIFSR\r\n"));
|
|
|
|
|
if (checkForOK(5000, (const char*) F("+CIFSR:STAIP"), true,false))
|
|
|
|
|
if (!checkForOK(1000, (const char*) F("0.0.0.0"), true,false))
|
|
|
|
|
if (checkForOK(5000, F("+CIFSR:STAIP"), true,false))
|
|
|
|
|
if (!checkForOK(1000, F("0.0.0.0"), true,false))
|
|
|
|
|
ipOK = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -225,21 +223,21 @@ 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
|
|
|
|
|
|
|
|
|
|
// Figure out MAC addr
|
|
|
|
|
StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); // not TOMATO
|
|
|
|
|
// looking fpr mac addr eg +CIFSR:APMAC,"be:dd:c2:5c:6b:b7"
|
|
|
|
|
if (checkForOK(5000, (const char*) F("+CIFSR:APMAC,\""), true,false)) {
|
|
|
|
|
if (checkForOK(5000, F("+CIFSR:APMAC,\""), true,false)) {
|
|
|
|
|
// Copy 17 byte mac address
|
|
|
|
|
for (int i=0; i<17;i++) {
|
|
|
|
|
while(!wifiStream->available());
|
|
|
|
@ -251,39 +249,41 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
|
|
|
|
|
}
|
|
|
|
|
char macTail[]={macAddress[9],macAddress[10],macAddress[12],macAddress[13],macAddress[15],macAddress[16],'\0'};
|
|
|
|
|
|
|
|
|
|
while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE
|
|
|
|
|
checkForOK(1000, true, false); // suck up remainder of AT+CIFSR
|
|
|
|
|
|
|
|
|
|
i=0;
|
|
|
|
|
do {
|
|
|
|
|
if (strncmp_P(yourNetwork, (const char*)password, 13) == 0) {
|
|
|
|
|
// unconfigured
|
|
|
|
|
StringFormatter::send(wifiStream, F("AT+CWSAP%s=\"DCCEX_%s\",\"PASS_%s\",1,4\r\n"), oldCmd ? "" : "_CUR", macTail, macTail);
|
|
|
|
|
StringFormatter::send(wifiStream, F("AT+CWSAP%s=\"DCCEX_%s\",\"PASS_%s\",%d,4\r\n"),
|
|
|
|
|
oldCmd ? "" : "_CUR", macTail, macTail, channel);
|
|
|
|
|
} else {
|
|
|
|
|
// password configured by user
|
|
|
|
|
StringFormatter::send(wifiStream, F("AT+CWSAP%s=\"DCCEX_%s\",\"%S\",1,4\r\n"), oldCmd ? "" : "_CUR", macTail, 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
|
|
|
|
|
if (!checkForOK(1000, (const char *)F("IP,\"") , true, false)) return WIFI_DISCONNECTED;
|
|
|
|
|
if (!checkForOK(1000, F("IP,\"") , true, false)) return WIFI_DISCONNECTED;
|
|
|
|
|
// Copy the IP address
|
|
|
|
|
{
|
|
|
|
|
const byte MAX_IP_LENGTH=15;
|
|
|
|
@ -302,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;
|
|
|
|
@ -325,15 +325,19 @@ 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, const char * waitfor, bool echo, bool escapeEcho) {
|
|
|
|
|
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 const *locator = waitfor;
|
|
|
|
|
char *locator = (char *)waitfor;
|
|
|
|
|
DIAG(F("\nWifi Check: [%E]"), waitfor);
|
|
|
|
|
while ( millis() - startTime < timeout) {
|
|
|
|
|
while (wifiStream->available()) {
|
|
|
|
@ -342,7 +346,7 @@ bool WifiInterface::checkForOK( const unsigned int timeout, const char * waitfor
|
|
|
|
|
if (escapeEcho) StringFormatter::printEscape( ch); /// THIS IS A DIAG IN DISGUISE
|
|
|
|
|
else DIAG(F("%c"), ch);
|
|
|
|
|
}
|
|
|
|
|
if (ch != GETFLASH(locator)) locator = waitfor;
|
|
|
|
|
if (ch != GETFLASH(locator)) locator = (char *)waitfor;
|
|
|
|
|
if (ch == GETFLASH(locator)) {
|
|
|
|
|
locator++;
|
|
|
|
|
if (!GETFLASH(locator)) {
|
|
|
|
|