2020-07-03 18:35:02 +02:00
|
|
|
/*
|
2020-08-20 11:38:59 +02:00
|
|
|
© 2020, Chris Harlow. All rights reserved.
|
2020-09-25 22:14:20 +02:00
|
|
|
© 2020, Harald Barth.
|
2020-08-20 11:38:59 +02:00
|
|
|
|
2020-09-25 22:14:20 +02:00
|
|
|
This file is part of CommandStation-EX
|
2020-08-20 11:38:59 +02:00
|
|
|
|
|
|
|
This is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-09-22 21:45:06 +02:00
|
|
|
|
2020-11-14 13:17:47 +01:00
|
|
|
#include "WifiInterface.h" /* config.h included there */
|
2020-09-22 21:45:06 +02:00
|
|
|
#include <avr/pgmspace.h>
|
2020-06-12 15:28:35 +02:00
|
|
|
#include "DIAG.h"
|
2020-06-12 20:56:40 +02:00
|
|
|
#include "StringFormatter.h"
|
2020-10-26 14:31:51 +01:00
|
|
|
|
2020-10-05 19:42:31 +02:00
|
|
|
#include "WifiInboundHandler.h"
|
2020-09-25 19:51:08 +02:00
|
|
|
|
2021-01-03 10:11:11 +01:00
|
|
|
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";
|
2020-08-20 11:38:59 +02:00
|
|
|
const unsigned long LOOP_TIMEOUT = 2000;
|
|
|
|
bool WifiInterface::connected = false;
|
2020-10-05 19:42:31 +02:00
|
|
|
Stream * WifiInterface::wifiStream;
|
|
|
|
|
2020-11-21 23:23:27 +01:00
|
|
|
#ifndef WIFI_CONNECT_TIMEOUT
|
2020-11-23 00:46:14 +01:00
|
|
|
// Tested how long it takes to FAIL an unknown SSID on firmware 1.7.4.
|
|
|
|
#define WIFI_CONNECT_TIMEOUT 14000
|
2020-11-21 23:23:27 +01:00
|
|
|
#endif
|
2020-10-05 19:42:31 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Figure out number of serial ports depending on hardware
|
|
|
|
//
|
2021-02-08 09:26:32 +01:00
|
|
|
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO)
|
2020-10-05 19:42:31 +02:00
|
|
|
#define NUM_SERIAL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560))
|
|
|
|
#define NUM_SERIAL 3
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NUM_SERIAL
|
|
|
|
#define NUM_SERIAL 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool WifiInterface::setup(long serial_link_speed,
|
2020-12-09 12:57:38 +01:00
|
|
|
const FSH *wifiESSID,
|
|
|
|
const FSH *wifiPassword,
|
|
|
|
const FSH *hostname,
|
2020-10-05 19:42:31 +02:00
|
|
|
const int port) {
|
|
|
|
|
2020-10-27 21:50:46 +01:00
|
|
|
wifiSerialState wifiUp = WIFI_NOAT;
|
2020-10-05 19:42:31 +02:00
|
|
|
|
|
|
|
#if NUM_SERIAL == 0
|
|
|
|
// no warning about unused parameters.
|
|
|
|
(void) serial_link_speed;
|
|
|
|
(void) wifiESSID;
|
|
|
|
(void) wifiPassword;
|
|
|
|
(void) hostname;
|
|
|
|
(void) port;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if NUM_SERIAL > 0
|
|
|
|
Serial1.begin(serial_link_speed);
|
|
|
|
wifiUp = setup(Serial1, wifiESSID, wifiPassword, hostname, port);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Other serials are tried, depending on hardware.
|
|
|
|
#if NUM_SERIAL > 1
|
2020-10-27 21:50:46 +01:00
|
|
|
if (wifiUp == WIFI_NOAT)
|
2020-10-05 19:42:31 +02:00
|
|
|
{
|
|
|
|
Serial2.begin(serial_link_speed);
|
|
|
|
wifiUp = setup(Serial2, wifiESSID, wifiPassword, hostname, port);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if NUM_SERIAL > 2
|
2020-10-27 21:50:46 +01:00
|
|
|
if (wifiUp == WIFI_NOAT)
|
2020-10-05 19:42:31 +02:00
|
|
|
{
|
|
|
|
Serial3.begin(serial_link_speed);
|
|
|
|
wifiUp = setup(Serial3, wifiESSID, wifiPassword, hostname, port);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-10-28 21:16:41 +01:00
|
|
|
if (wifiUp == WIFI_NOAT) // here and still not AT commands found
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DCCEXParser::setAtCommandCallback(ATCommand);
|
|
|
|
// CAUTION... ONLY CALL THIS ONCE
|
|
|
|
WifiInboundHandler::setup(wifiStream);
|
2020-10-27 21:50:46 +01:00
|
|
|
if (wifiUp == WIFI_CONNECTED)
|
|
|
|
connected = true;
|
|
|
|
else
|
|
|
|
connected = false;
|
|
|
|
return connected;
|
2020-10-05 19:42:31 +02:00
|
|
|
}
|
2020-08-19 14:12:39 +02:00
|
|
|
|
2020-12-09 12:57:38 +01:00
|
|
|
wifiSerialState WifiInterface::setup(Stream & setupStream, const FSH* SSid, const FSH* password,
|
|
|
|
const FSH* hostname, int port) {
|
2020-10-27 21:50:46 +01:00
|
|
|
wifiSerialState wifiState;
|
2020-09-22 21:45:06 +02:00
|
|
|
static uint8_t ntry = 0;
|
|
|
|
ntry++;
|
2020-08-20 11:38:59 +02:00
|
|
|
|
|
|
|
wifiStream = &setupStream;
|
|
|
|
|
2020-09-22 21:45:06 +02:00
|
|
|
DIAG(F("\n++ Wifi Setup Try %d ++\n"), ntry);
|
|
|
|
|
2020-10-27 21:50:46 +01:00
|
|
|
wifiState = setup2( SSid, password, hostname, port);
|
|
|
|
|
|
|
|
if (wifiState == WIFI_NOAT) {
|
|
|
|
DIAG(F("\n++ Wifi Setup NO AT ++\n"));
|
|
|
|
return wifiState;
|
|
|
|
}
|
2020-08-24 17:46:16 +02:00
|
|
|
|
2020-10-27 21:50:46 +01:00
|
|
|
if (wifiState == WIFI_CONNECTED) {
|
2020-08-26 14:18:34 +02:00
|
|
|
StringFormatter::send(wifiStream, F("ATE0\r\n")); // turn off the echo
|
|
|
|
checkForOK(200, OK_SEARCH, true);
|
|
|
|
}
|
2020-10-05 19:42:31 +02:00
|
|
|
|
|
|
|
|
2020-10-27 21:50:46 +01:00
|
|
|
DIAG(F("\n++ Wifi Setup %S ++\n"), wifiState == WIFI_CONNECTED ? F("CONNECTED") : F("DISCONNECTED"));
|
|
|
|
return wifiState;
|
2020-06-12 20:56:40 +02:00
|
|
|
}
|
|
|
|
|
2020-11-14 20:13:57 +01:00
|
|
|
#ifdef DONT_TOUCH_WIFI_CONF
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-variable"
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
#endif
|
2020-12-09 12:57:38 +01:00
|
|
|
wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
|
|
|
|
const FSH* hostname, int port) {
|
2020-09-22 21:45:06 +02:00
|
|
|
bool ipOK = false;
|
|
|
|
bool oldCmd = false;
|
2020-08-26 14:18:34 +02:00
|
|
|
|
2020-08-27 12:18:19 +02:00
|
|
|
char macAddress[17]; // mac address extraction
|
2020-08-26 14:18:34 +02:00
|
|
|
|
|
|
|
// 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.
|
2020-08-24 17:46:16 +02:00
|
|
|
if (checkForOK(200,IPD_SEARCH, true)) {
|
|
|
|
DIAG(F("\nPreconfigured Wifi already running with data waiting\n"));
|
2020-10-05 19:42:31 +02:00
|
|
|
// loopstate=4; // carry on from correct place... or not as the case may be
|
2020-10-27 21:50:46 +01:00
|
|
|
return WIFI_CONNECTED;
|
2020-08-24 17:46:16 +02:00
|
|
|
}
|
|
|
|
|
2020-09-22 21:45:06 +02:00
|
|
|
StringFormatter::send(wifiStream, F("AT\r\n")); // Is something here that understands AT?
|
|
|
|
if(!checkForOK(200, OK_SEARCH, true))
|
2020-10-27 21:50:46 +01:00
|
|
|
return WIFI_NOAT; // No AT compatible WiFi module here
|
2020-09-22 21:45:06 +02:00
|
|
|
|
2020-08-26 14:18:34 +02:00
|
|
|
StringFormatter::send(wifiStream, F("ATE1\r\n")); // Turn on the echo, se we can see what's happening
|
2020-09-22 21:45:06 +02:00
|
|
|
checkForOK(2000, OK_SEARCH, true); // Makes this visible on the console
|
2020-08-26 14:18:34 +02:00
|
|
|
|
2020-08-27 12:18:19 +02:00
|
|
|
// Display the AT version information
|
2020-08-26 14:18:34 +02:00
|
|
|
StringFormatter::send(wifiStream, F("AT+GMR\r\n"));
|
2020-08-20 11:38:59 +02:00
|
|
|
checkForOK(2000, OK_SEARCH, true, false); // Makes this visible on the console
|
|
|
|
|
2020-11-14 13:17:47 +01:00
|
|
|
#ifdef DONT_TOUCH_WIFI_CONF
|
|
|
|
DIAG(F("\nDONT_TOUCH_WIFI_CONF was set: Using existing config\n"));
|
|
|
|
#else
|
2020-09-22 21:45:06 +02:00
|
|
|
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"
|
|
|
|
|
2021-01-30 13:10:15 +01:00
|
|
|
// 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)) {
|
|
|
|
oldCmd=true;
|
|
|
|
while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE
|
|
|
|
}
|
2020-09-22 21:45:06 +02:00
|
|
|
|
2020-11-18 23:53:06 +01:00
|
|
|
const char *yourNetwork = "Your network ";
|
2021-01-30 16:34:06 +01:00
|
|
|
if (strncmp_P(yourNetwork, (const char*)SSid, 13) == 0 || strncmp_P("", (const char*)SSid, 13) == 0) {
|
2021-01-30 13:10:15 +01:00
|
|
|
if (strncmp_P(yourNetwork, (const char*)password, 13) == 0) {
|
|
|
|
// If the source code looks unconfigured, check if the
|
|
|
|
// ESP8266 is preconfigured in station mode.
|
|
|
|
// We check the first 13 chars of the SSid and the password
|
|
|
|
|
|
|
|
// give a preconfigured ES8266 a chance to connect to a router
|
|
|
|
// 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))
|
|
|
|
ipOK = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// SSID was configured, so we assume station (client) mode.
|
|
|
|
if (oldCmd) {
|
2020-09-22 21:45:06 +02:00
|
|
|
// AT command early version supports CWJAP/CWSAP
|
2021-01-30 18:19:25 +01:00
|
|
|
StringFormatter::send(wifiStream, F("AT+CWJAP=\"%S\",\"%S\"\r\n"), SSid, password);
|
|
|
|
ipOK = checkForOK(WIFI_CONNECT_TIMEOUT, OK_SEARCH, true);
|
2020-09-22 21:45:06 +02:00
|
|
|
} 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
|
2020-08-27 12:18:19 +02:00
|
|
|
|
2021-01-30 18:19:25 +01:00
|
|
|
StringFormatter::send(wifiStream, F("AT+CWJAP_CUR=\"%S\",\"%S\"\r\n"), SSid, password);
|
|
|
|
ipOK = checkForOK(WIFI_CONNECT_TIMEOUT, OK_SEARCH, true);
|
2020-09-22 21:45:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ipOK) {
|
|
|
|
// But we really only have the ESSID and password correct
|
2021-01-30 13:10:15 +01:00
|
|
|
// Let's check for IP (via DHCP)
|
2020-09-22 21:45:06 +02:00
|
|
|
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))
|
|
|
|
ipOK = true;
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 11:38:59 +02:00
|
|
|
|
|
|
|
if (!ipOK) {
|
2020-09-22 21:45:06 +02:00
|
|
|
// If we have not managed to get this going in station mode, go for AP mode
|
|
|
|
|
2021-01-30 18:15:05 +01:00
|
|
|
// StringFormatter::send(wifiStream, F("AT+RST\r\n"));
|
|
|
|
// checkForOK(1000, OK_SEARCH, 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 (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE
|
2020-08-20 11:38:59 +02:00
|
|
|
|
2020-09-22 21:45:06 +02:00
|
|
|
// Figure out MAC addr
|
2021-01-30 16:34:06 +01:00
|
|
|
StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); // not TOMATO
|
2020-09-22 21:45:06 +02:00
|
|
|
// looking fpr mac addr eg +CIFSR:APMAC,"be:dd:c2:5c:6b:b7"
|
|
|
|
if (checkForOK(5000, (const char*) F("+CIFSR:APMAC,\""), true,false)) {
|
|
|
|
// Copy 17 byte mac address
|
|
|
|
for (int i=0; i<17;i++) {
|
|
|
|
while(!wifiStream->available());
|
|
|
|
macAddress[i]=wifiStream->read();
|
|
|
|
StringFormatter::printEscape(macAddress[i]);
|
2020-08-26 14:18:34 +02:00
|
|
|
}
|
2021-01-30 16:34:06 +01:00
|
|
|
} else {
|
|
|
|
memset(macAddress,'f',sizeof(macAddress));
|
2020-09-22 21:45:06 +02:00
|
|
|
}
|
|
|
|
char macTail[]={macAddress[9],macAddress[10],macAddress[12],macAddress[13],macAddress[15],macAddress[16],'\0'};
|
|
|
|
|
2021-01-30 13:10:15 +01:00
|
|
|
while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE
|
2020-09-22 21:45:06 +02:00
|
|
|
|
2021-01-30 18:15:05 +01:00
|
|
|
i=0;
|
2021-01-30 13:10:15 +01:00
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
// password configured by user
|
|
|
|
StringFormatter::send(wifiStream, F("AT+CWSAP%s=\"DCCEX_%s\",\"%S\",1,4\r\n"), oldCmd ? "" : "_CUR", macTail, password);
|
|
|
|
}
|
|
|
|
} while (!checkForOK(WIFI_CONNECT_TIMEOUT, OK_SEARCH, 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
|
2020-08-20 11:38:59 +02:00
|
|
|
|
2021-01-30 13:10:15 +01:00
|
|
|
if (!oldCmd) {
|
2020-08-20 11:38:59 +02:00
|
|
|
StringFormatter::send(wifiStream, F("AT+CIPRECVMODE=0\r\n"), port); // make sure transfer mode is correct
|
|
|
|
checkForOK(2000, OK_SEARCH, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 07:33:04 +01:00
|
|
|
StringFormatter::send(wifiStream, F("AT+CIPSERVER=0\r\n")); // turn off tcp server (to clean connections before CIPMUX=1)
|
2020-11-21 22:20:15 +01:00
|
|
|
checkForOK(1000, OK_SEARCH, true); // ignore result in case it already was off
|
2020-08-26 14:18:34 +02:00
|
|
|
|
2020-08-20 11:38:59 +02:00
|
|
|
StringFormatter::send(wifiStream, F("AT+CIPMUX=1\r\n")); // configure for multiple connections
|
2020-11-21 22:20:15 +01:00
|
|
|
if (!checkForOK(1000, OK_SEARCH, true)) return WIFI_DISCONNECTED;
|
2020-08-20 11:38:59 +02:00
|
|
|
|
|
|
|
StringFormatter::send(wifiStream, F("AT+CIPSERVER=1,%d\r\n"), port); // turn on server on port
|
2020-11-21 22:20:15 +01:00
|
|
|
if (!checkForOK(1000, OK_SEARCH, true)) return WIFI_DISCONNECTED;
|
2020-11-14 13:17:47 +01:00
|
|
|
#endif //DONT_TOUCH_WIFI_CONF
|
2020-08-23 20:56:47 +02:00
|
|
|
|
2020-08-26 14:18:34 +02:00
|
|
|
StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); // Display ip addresses to the DIAG
|
2021-02-07 11:28:05 +01:00
|
|
|
if (!checkForOK(1000, (const char *)F("IP,\"") , true, false)) return WIFI_DISCONNECTED;
|
|
|
|
// Copy the IP address
|
|
|
|
{
|
|
|
|
const byte MAX_IP_LENGTH=15;
|
|
|
|
char ipString[MAX_IP_LENGTH+1];
|
2021-02-08 13:28:16 +01:00
|
|
|
ipString[MAX_IP_LENGTH]='\0'; // protection against missing " character on end.
|
2021-02-07 11:28:05 +01:00
|
|
|
byte ipLen=0;
|
|
|
|
for(byte ipLen=0;ipLen<MAX_IP_LENGTH;ipLen++) {
|
|
|
|
while(!wifiStream->available());
|
|
|
|
int ipChar=wifiStream->read();
|
|
|
|
StringFormatter::printEscape(ipChar);
|
|
|
|
if (ipChar=='"') {
|
|
|
|
ipString[ipLen]='\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ipString[ipLen]=ipChar;
|
|
|
|
}
|
|
|
|
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.
|
2020-11-21 22:20:15 +01:00
|
|
|
if (!checkForOK(1000, OK_SEARCH, true, false)) return WIFI_DISCONNECTED;
|
2021-02-07 11:28:05 +01:00
|
|
|
LCD(5,F("PORT=%d\n"),port);
|
2020-08-26 14:18:34 +02:00
|
|
|
|
2020-10-27 21:50:46 +01:00
|
|
|
return WIFI_CONNECTED;
|
2020-06-12 15:28:35 +02:00
|
|
|
}
|
2020-11-14 20:13:57 +01:00
|
|
|
#ifdef DONT_TOUCH_WIFI_CONF
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-08-20 11:38:59 +02:00
|
|
|
|
|
|
|
// This function is used to allow users to enter <+ commands> through the DCCEXParser
|
|
|
|
// Once the user has made whatever changes to the AT commands, a <+X> command can be used
|
|
|
|
// to force on the connectd flag so that the loop will start picking up wifi traffic.
|
|
|
|
// If the settings are corrupted <+RST> will clear this and then you must restart the arduino.
|
|
|
|
|
2020-08-01 17:32:16 +02:00
|
|
|
void WifiInterface::ATCommand(const byte * command) {
|
2020-08-26 14:18:34 +02:00
|
|
|
command++;
|
2020-08-20 11:38:59 +02:00
|
|
|
if (*command=='X') {
|
|
|
|
connected = true;
|
|
|
|
DIAG(F("\n++++++ Wifi Connction forced on ++++++++\n"));
|
|
|
|
}
|
|
|
|
else {
|
2020-08-26 14:18:34 +02:00
|
|
|
StringFormatter:: send(wifiStream, F("AT+%s\r\n"), command);
|
2020-08-20 11:38:59 +02:00
|
|
|
checkForOK(10000, OK_SEARCH, true);
|
|
|
|
}
|
2020-08-01 17:32:16 +02:00
|
|
|
}
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-10-05 19:42:31 +02:00
|
|
|
|
2020-08-19 14:12:39 +02:00
|
|
|
|
2020-08-20 11:38:59 +02:00
|
|
|
bool WifiInterface::checkForOK( const unsigned int timeout, const char * waitfor, bool echo, bool escapeEcho) {
|
2020-06-29 12:37:05 +02:00
|
|
|
unsigned long startTime = millis();
|
2020-08-20 11:38:59 +02:00
|
|
|
char const *locator = waitfor;
|
|
|
|
DIAG(F("\nWifi Check: [%E]"), waitfor);
|
|
|
|
while ( millis() - startTime < timeout) {
|
|
|
|
while (wifiStream->available()) {
|
|
|
|
int ch = wifiStream->read();
|
|
|
|
if (echo) {
|
2020-09-06 21:23:26 +02:00
|
|
|
if (escapeEcho) StringFormatter::printEscape( ch); /// THIS IS A DIAG IN DISGUISE
|
2020-08-20 11:38:59 +02:00
|
|
|
else DIAG(F("%c"), ch);
|
|
|
|
}
|
2021-01-03 10:11:11 +01:00
|
|
|
if (ch != GETFLASH(locator)) locator = waitfor;
|
|
|
|
if (ch == GETFLASH(locator)) {
|
2020-06-12 20:56:40 +02:00
|
|
|
locator++;
|
2021-01-03 10:11:11 +01:00
|
|
|
if (!GETFLASH(locator)) {
|
2020-08-20 11:38:59 +02:00
|
|
|
DIAG(F("\nFound in %dms"), millis() - startTime);
|
2020-06-12 20:56:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 11:38:59 +02:00
|
|
|
DIAG(F("\nTIMEOUT after %dms\n"), timeout);
|
2020-06-12 20:56:40 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-08-20 11:38:59 +02:00
|
|
|
|
2020-08-01 17:32:16 +02:00
|
|
|
void WifiInterface::loop() {
|
2020-10-05 19:42:31 +02:00
|
|
|
if (connected) {
|
|
|
|
WifiInboundHandler::loop();
|
2020-08-20 11:38:59 +02:00
|
|
|
}
|
|
|
|
}
|