1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-23 16:16:13 +01:00

redo flow through wifisetup again

This commit is contained in:
Harald Barth 2021-01-30 13:10:15 +01:00
parent aba937f42f
commit d577606ee9
2 changed files with 47 additions and 42 deletions

View File

@ -172,29 +172,33 @@ wifiSerialState WifiInterface::setup2(const __FlashStringHelper* SSid, const __F
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
// ESP8266 is preconfigured. We check the first 13 chars
// of the SSid.
const char *yourNetwork = "Your network ";
if (strncmp_P(yourNetwork, (const char*)SSid, 13) == 0 || ((const char *)SSid)[0] == '\0') {
delay(8000); // give a preconfigured ES8266 a chance to connect to a router
// typical connect time approx 7 seconds
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 { // Should this really be "else" here /haba
if (!ipOK) {
// Older ES versions have AT+CWJAP, newer ones have AT+CWJAP_CUR and AT+CWHOSTNAME // Older ES versions have AT+CWJAP, newer ones have AT+CWJAP_CUR and AT+CWHOSTNAME
StringFormatter::send(wifiStream, F("AT+CWJAP?\r\n")); StringFormatter::send(wifiStream, F("AT+CWJAP?\r\n"));
if (checkForOK(2000, OK_SEARCH, true)) { if (checkForOK(2000, OK_SEARCH, true)) {
oldCmd=true; oldCmd=true;
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
}
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*)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) {
// AT command early version supports CWJAP/CWSAP // AT command early version supports CWJAP/CWSAP
if (SSid) { if (SSid) { // why 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);
} }
@ -206,7 +210,7 @@ wifiSerialState WifiInterface::setup2(const __FlashStringHelper* SSid, const __F
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) { if (SSid) { // why 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);
} }
@ -214,7 +218,7 @@ wifiSerialState WifiInterface::setup2(const __FlashStringHelper* SSid, const __F
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))
@ -222,7 +226,6 @@ wifiSerialState WifiInterface::setup2(const __FlashStringHelper* SSid, const __F
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
@ -243,24 +246,23 @@ wifiSerialState WifiInterface::setup2(const __FlashStringHelper* SSid, const __F
} }
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; int i=0;
int ret=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

@ -52,10 +52,13 @@ 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.
// //
// 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"