1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-04-20 20:21:18 +02:00

Merge branch 'wifisetupfix' into nanoEvery2

This commit is contained in:
Asbelos 2021-02-08 09:44:00 +00:00
commit c1a8206667
3 changed files with 69 additions and 56 deletions

View File

@ -172,66 +172,71 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
StringFormatter::send(wifiStream, F("AT+CWMODE=1\r\n")); // configure as "station" = WiFi client 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, OK_SEARCH, true); // Not always OK, sometimes "no change"
// If the source code looks unconfigured, check if the // Older ES versions have AT+CWJAP, newer ones have AT+CWJAP_CUR and AT+CWHOSTNAME
// ESP8266 is preconfigured. We check the first 13 chars StringFormatter::send(wifiStream, F("AT+CWJAP?\r\n"));
// of the SSid. if (checkForOK(2000, OK_SEARCH, true)) {
oldCmd=true;
while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE
}
const char *yourNetwork = "Your network "; const char *yourNetwork = "Your network ";
if (strncmp_P(yourNetwork, (const char*)SSid, 13) == 0 || ((const char *)SSid)[0] == '\0') { if (strncmp_P(yourNetwork, (const char*)SSid, 13) == 0 || strncmp_P("", (const char*)SSid, 13) == 0) {
delay(8000); // give a preconfigured ES8266 a chance to connect to a router if (strncmp_P(yourNetwork, (const char*)password, 13) == 0) {
// typical connect time approx 7 seconds // If the source code looks unconfigured, check if the
StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); // ESP8266 is preconfigured in station mode.
if (checkForOK(5000, (const char*) F("+CIFSR:STAIP"), true,false)) // We check the first 13 chars of the SSid and the password
if (!checkForOK(1000, (const char*) F("0.0.0.0"), true,false))
ipOK = true;
} else { // Should this really be "else" here /haba
if (!ipOK) {
// 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
// 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) {
// AT command early version supports CWJAP/CWSAP // AT command early version supports CWJAP/CWSAP
if (SSid) { StringFormatter::send(wifiStream, F("AT+CWJAP=\"%S\",\"%S\"\r\n"), SSid, password);
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, OK_SEARCH, true);
}
DIAG(F("\n**\n"));
} else { } else {
// later version supports CWJAP_CUR // later version supports CWJAP_CUR
StringFormatter::send(wifiStream, F("AT+CWHOSTNAME=\"%S\"\r\n"), hostname); // Set Host name for Wifi Client 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, OK_SEARCH, true); // dont care if not supported
if (SSid) { StringFormatter::send(wifiStream, F("AT+CWJAP_CUR=\"%S\",\"%S\"\r\n"), SSid, password);
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, OK_SEARCH, true);
}
} }
if (ipOK) { if (ipOK) {
// But we really only have the ESSID and password correct // But we really only have the ESSID and password correct
// Let's check for IP // Let's check for IP (via DHCP)
ipOK = false; ipOK = false;
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,false)) if (checkForOK(5000, (const char*) F("+CIFSR:STAIP"), true,false))
if (!checkForOK(1000, (const char*) F("0.0.0.0"), true,false)) if (!checkForOK(1000, (const char*) F("0.0.0.0"), true,false))
ipOK = true; ipOK = true;
} }
}
} }
if (!ipOK) { if (!ipOK) {
// If we have not managed to get this going in station mode, go for AP mode // If we have not managed to get this going in station mode, go for AP mode
StringFormatter::send(wifiStream, F("AT+CWMODE=2\r\n")); // configure as AccessPoint. // StringFormatter::send(wifiStream, F("AT+RST\r\n"));
checkForOK(1000, OK_SEARCH, true); // Not always OK, sometimes "no change" // 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
// Figure out MAC addr // Figure out MAC addr
StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); StringFormatter::send(wifiStream, F("AT+CIFSR\r\n")); // not TOMATO
// looking fpr mac addr eg +CIFSR:APMAC,"be:dd:c2:5c:6b:b7" // 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, (const char*) F("+CIFSR:APMAC,\""), true,false)) {
// Copy 17 byte mac address // Copy 17 byte mac address
@ -240,27 +245,27 @@ wifiSerialState WifiInterface::setup2(const FSH* SSid, const FSH* password,
macAddress[i]=wifiStream->read(); macAddress[i]=wifiStream->read();
StringFormatter::printEscape(macAddress[i]); StringFormatter::printEscape(macAddress[i]);
} }
} else {
memset(macAddress,'f',sizeof(macAddress));
} }
char macTail[]={macAddress[9],macAddress[10],macAddress[12],macAddress[13],macAddress[15],macAddress[16],'\0'}; char macTail[]={macAddress[9],macAddress[10],macAddress[12],macAddress[13],macAddress[15],macAddress[16],'\0'};
if (oldCmd) { while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE
while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE
int i=0; i=0;
do { do {
if (strncmp_P(yourNetwork, (const char*)password, 13) == 0) { if (strncmp_P(yourNetwork, (const char*)password, 13) == 0) {
// unconfigured // unconfigured
StringFormatter::send(wifiStream, F("AT+CWSAP=\"DCCEX_%s\",\"PASS_%s\",1,4\r\n"), macTail, macTail); StringFormatter::send(wifiStream, F("AT+CWSAP%s=\"DCCEX_%s\",\"PASS_%s\",1,4\r\n"), oldCmd ? "" : "_CUR", macTail, macTail);
} else { } else {
// password configured by user // password configured by user
StringFormatter::send(wifiStream, F("AT+CWSAP=\"DCCEX_%s\",\"%s\",1,4\r\n"), macTail, password); StringFormatter::send(wifiStream, F("AT+CWSAP%s=\"DCCEX_%s\",\"%S\",1,4\r\n"), oldCmd ? "" : "_CUR", macTail, password);
} }
} while (i++<2 && !checkForOK(WIFI_CONNECT_TIMEOUT, OK_SEARCH, true)); // do twice if necessary but ignore failure as AP mode may still be ok } while (!checkForOK(WIFI_CONNECT_TIMEOUT, OK_SEARCH, true) && i++<2); // do twice if necessary but ignore failure as AP mode may still be ok
} else { if (i >= 2)
DIAG(F("\nWarning: Setting AP SSID and password failed\n")); // but issue warning
StringFormatter::send(wifiStream, F("AT+CWSAP_CUR=\"DCCEX_%s\",\"PASS_%s\",1,4\r\n"), macTail, macTail);
checkForOK(20000, OK_SEARCH, true); // can ignore failure as SSid mode may still be ok
if (!oldCmd) {
StringFormatter::send(wifiStream, F("AT+CIPRECVMODE=0\r\n"), port); // make sure transfer mode is correct StringFormatter::send(wifiStream, F("AT+CIPRECVMODE=0\r\n"), port); // make sure transfer mode is correct
checkForOK(2000, OK_SEARCH, true); checkForOK(2000, OK_SEARCH, true);
} }

View File

@ -45,10 +45,14 @@ The configuration file for DCC-EX Command Station
// WIFI_SSID is the network name IF you want to use your existing home network. // WIFI_SSID is the network name IF you want to use your existing home network.
// Do NOT change this if you want to use the WiFi in Access Point (AP) mode. // Do NOT change this if you want to use the WiFi in Access Point (AP) mode.
// //
// If you do NOT set the WIFI_SSID, the WiFi chip will first try // If you do NOT set the WIFI_SSID and do NOT set the WIFI_PASSWORD,
// to connect to the previously configured network and if that fails // then the WiFi chip will first try to connect to the previously
// fall back to Access Point mode. The SSID of the AP will be // configured network and if that fails fall back to Access Point mode.
// automatically set to DCCEX_*. // The SSID of the AP will be automatically set to DCCEX_*.
// If you DO set the WIFI_SSID then the WiFi chip will try to connect
// to that (home) network in station (client) mode. If a WIFI_PASSWORD
// is set (recommended), that password will be used for AP mode.
// The AP mode password must be at least 8 characters long.
// //
// Your SSID may not conain ``"'' (double quote, ASCII 0x22). // Your SSID may not conain ``"'' (double quote, ASCII 0x22).
#define WIFI_SSID "Your network name" #define WIFI_SSID "Your network name"

View File

@ -1,7 +1,11 @@
#ifndef version_h #ifndef version_h
#define version_h #define version_h
#define VERSION "3.0.3" #include "StringFormatter.h"
#define VERSION "3.0.4"
// 3.0.4 Includes:
// Wifi startup bugfixes
// 3.0.3 Includes: // 3.0.3 Includes:
// <W addr> command to write loco address and clear consist // <W addr> command to write loco address and clear consist
// <R> command will allow for consist address // <R> command will allow for consist address