1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-27 01:56:14 +01:00

Restartable Wifi interface.

This commit is contained in:
Asbelos 2020-08-20 10:38:59 +01:00
parent 6ad50df2a7
commit 55d175adb0
2 changed files with 242 additions and 215 deletions

View File

@ -1,20 +1,20 @@
/* /*
* © 2020, Chris Harlow. All rights reserved. © 2020, Chris Harlow. All rights reserved.
*
* This file is part of Asbelos DCC API This file is part of Asbelos DCC API
*
* This is free software: you can redistribute it and/or modify This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. (at your option) any later version.
*
* It is distributed in the hope that it will be useful, It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>. along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "WifiInterface.h" #include "WifiInterface.h"
#include "DIAG.h" #include "DIAG.h"
@ -46,27 +46,24 @@ void WifiInterface::setup(Stream & setupStream, const __FlashStringHelper* SSid
DIAG(F("\n++++++ Wifi Setup In Progress ++++++++\n")); DIAG(F("\n++++++ Wifi Setup In Progress ++++++++\n"));
connected = setup2( SSid, password, hostname, servername, port); connected = setup2( SSid, password, hostname, servername, port);
// TODO calloc the buffer and streamer and parser etc
DIAG(F("\n++++++ Wifi Setup %S ++++++++\n"), connected ? F("OK") : F("FAILED")); DIAG(F("\n++++++ Wifi Setup %S ++++++++\n"), connected ? F("OK") : F("FAILED"));
} }
bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringHelper* password, bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port) { const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port) {
int ipOK = 0; int ipOK = 0;
delay(1000);
StringFormatter::send(wifiStream,F("AT+RST\r\n")); // reset module
checkForOK(5000,READY_SEARCH,false); // generally not interesting to DCC
StringFormatter::send(wifiStream, F("AT+GMR\r\n")); // request AT version StringFormatter::send(wifiStream, F("AT+GMR\r\n")); // request AT version
checkForOK(2000,OK_SEARCH,true); // Makes this visible on the console checkForOK(2000, OK_SEARCH, true, false); // Makes this visible on the console
StringFormatter::send(wifiStream, F("AT+CWMODE=1\r\n")); // configure as client StringFormatter::send(wifiStream, F("AT+CWMODE=1\r\n")); // configure as client
checkForOK(1000, OK_SEARCH, true); // Not always OK, sometimes "no change" checkForOK(1000, OK_SEARCH, true); // Not always OK, sometimes "no change"
delay(8000); // give preconfigured ES8266 a chance to connect
StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); StringFormatter::send(wifiStream, F("AT+CIFSR\r\n"));
if(checkForOK(5000, (const char*) F("+CIFSR:STAIP"),true)) if (checkForOK(5000, (const char*) F("+CIFSR:STAIP"), true,false))
if(!checkForOK(1000, (const char*) F("0.0.0.0"),true)) if (!checkForOK(1000, (const char*) F("0.0.0.0"), true,false))
ipOK = 1; ipOK = 1;
if (!ipOK) { if (!ipOK) {
@ -97,7 +94,7 @@ bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringH
(void)servername; // avoid compiler warning from commented out AT_MDNS above (void)servername; // avoid compiler warning from commented out AT_MDNS above
} }
StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); // get ip address //192.168.4.1 StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); // get ip address //192.168.4.1
if (!checkForOK(10000,OK_SEARCH,true)) return false; if (!checkForOK(10000, OK_SEARCH, true, false)) return false;
} }
StringFormatter::send(wifiStream, F("AT+CIPMUX=1\r\n")); // configure for multiple connections StringFormatter::send(wifiStream, F("AT+CIPMUX=1\r\n")); // configure for multiple connections
@ -109,23 +106,38 @@ bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringH
return true; return true;
} }
// 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.
void WifiInterface::ATCommand(const byte * command) { void WifiInterface::ATCommand(const byte * command) {
if (*command=='X') {
connected = true;
DIAG(F("\n++++++ Wifi Connction forced on ++++++++\n"));
}
else {
StringFormatter:: send(wifiStream, F("AT+%s\r\n"), command + 1); StringFormatter:: send(wifiStream, F("AT+%s\r\n"), command + 1);
checkForOK(10000, OK_SEARCH, true); checkForOK(10000, OK_SEARCH, true);
} }
}
void WifiInterface::setHTTPCallback(HTTP_CALLBACK callback) { void WifiInterface::setHTTPCallback(HTTP_CALLBACK callback) {
httpCallback = callback; httpCallback = callback;
} }
bool WifiInterface::checkForOK( const unsigned int timeout, const char * waitfor, bool echo) { bool WifiInterface::checkForOK( const unsigned int timeout, const char * waitfor, bool echo, bool escapeEcho) {
unsigned long startTime = millis(); unsigned long startTime = millis();
char const *locator = waitfor; char const *locator = waitfor;
DIAG(F("\nWifi Check: [%E]"), waitfor); DIAG(F("\nWifi Check: [%E]"), waitfor);
while ( millis() - startTime < timeout) { while ( millis() - startTime < timeout) {
while (wifiStream->available()) { while (wifiStream->available()) {
int ch = wifiStream->read(); int ch = wifiStream->read();
if (echo) StringFormatter::printEscape(&DIAGSERIAL,ch); /// THIS IS A DIAG IN DISGUISE if (echo) {
if (escapeEcho) StringFormatter::printEscape(&DIAGSERIAL, ch); /// THIS IS A DIAG IN DISGUISE
else DIAG(F("%c"), ch);
}
if (ch != pgm_read_byte_near(locator)) locator = waitfor; if (ch != pgm_read_byte_near(locator)) locator = waitfor;
if (ch == pgm_read_byte_near(locator)) { if (ch == pgm_read_byte_near(locator)) {
locator++; locator++;
@ -171,7 +183,7 @@ void WifiInterface::loop() {
int ch = wifiStream->read(); int ch = wifiStream->read();
// echo the char to the diagnostic stream in escaped format // echo the char to the diagnostic stream in escaped format
// StringFormatter::printEscape(&DIAGSERIAL,ch); // DIAG in disguise StringFormatter::printEscape(&DIAGSERIAL,ch); // DIAG in disguise
switch (loopstate) { switch (loopstate) {
case 0: // looking for +IPD case 0: // looking for +IPD
@ -216,6 +228,11 @@ void WifiInterface::loop() {
wifiStream->print((char *) buffer); wifiStream->print((char *) buffer);
loopTimeoutStart = millis(); loopTimeoutStart = millis();
loopstate = closeAfter ? 11 : 0; loopstate = closeAfter ? 11 : 0;
break;
}
if (ch == '.') { // busy during send, delay and retry
loopstate = 12; // look for SEND OK finished
break;
} }
break; break;
case 11: // Waiting for SEND OK or ERROR to complete so we can closeAfter case 11: // Waiting for SEND OK or ERROR to complete so we can closeAfter
@ -229,6 +246,16 @@ void WifiInterface::loop() {
loopstate = 0; // wait for +IPD loopstate = 0; // wait for +IPD
} }
break; break;
case 12: // Waiting for OK after send busy
if (ch == 'K') { // assume its in SEND OK
DIAG(F("\n\n WIFI BUSY RETRYING SEND \n"));
StringFormatter::send(wifiStream, F("AT+CIPSEND=%d,%d\r\n"), connectionId, streamer.available() - 1);
loopTimeoutStart = millis();
loopstate = 10; // non-blocking loop waits for > before sending
break;
}
break;
} // switch } // switch
} // while } // while
if (loopstate != 99) return; if (loopstate != 99) return;
@ -236,7 +263,7 @@ void WifiInterface::loop() {
// AT this point we have read an incoming message into the buffer // AT this point we have read an incoming message into the buffer
streamer.print('\0'); // null the end of the buffer so we can treat it as a string streamer.print('\0'); // null the end of the buffer so we can treat it as a string
DIAG(F("\n%d Wifi(%d)<-[%e]\n"),millis(),connectionId,buffer); DIAG(F("\n%l Wifi(%d)<-[%e]\n"), millis(),connectionId, buffer);
streamer.setBufferContentPosition(0, 0); // reset write position to start of buffer streamer.setBufferContentPosition(0, 0); // reset write position to start of buffer
// SIDE EFFECT WARNING::: // SIDE EFFECT WARNING:::
// We know that parser will read the entire buffer before starting to write to it. // We know that parser will read the entire buffer before starting to write to it.
@ -265,7 +292,7 @@ void WifiInterface::loop() {
} }
// prepare to send reply // prepare to send reply
streamer.print('\0'); // null the end of the buffer so we can treat it as a string streamer.print('\0'); // null the end of the buffer so we can treat it as a string
DIAG(F("%d WiFi(%d)->[%e] l(%d)\n"),millis(),connectionId,buffer,streamer.available()-1); DIAG(F("%l WiFi(%d)->[%e] l(%d)\n"), millis(), connectionId, buffer, streamer.available() - 1);
StringFormatter::send(wifiStream, F("AT+CIPSEND=%d,%d\r\n"), connectionId, streamer.available() - 1); StringFormatter::send(wifiStream, F("AT+CIPSEND=%d,%d\r\n"), connectionId, streamer.available() - 1);
loopTimeoutStart = millis(); loopTimeoutStart = millis();
loopstate = 10; // non-blocking loop waits for > before sending loopstate = 10; // non-blocking loop waits for > before sending

View File

@ -40,7 +40,7 @@ class WifiInterface {
static DCCEXParser parser; static DCCEXParser parser;
static bool setup2( const __FlashStringHelper* SSSid, const __FlashStringHelper* password, static bool setup2( const __FlashStringHelper* SSSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port); const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port);
static bool checkForOK(const unsigned int timeout, const char* waitfor, bool echo); static bool checkForOK(const unsigned int timeout, const char* waitfor, bool echo, bool escapeEcho=true);
static bool isHTTP(); static bool isHTTP();
static HTTP_CALLBACK httpCallback; static HTTP_CALLBACK httpCallback;
static bool connected; static bool connected;