mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-23 08:06:13 +01:00
Finish wifi interface fixes from serial123 branch, Fixes for flash string missing
This commit is contained in:
parent
9ad8dd6cbd
commit
eb887fdbd2
|
@ -116,20 +116,21 @@ void setup()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if ENABLE_WIFI
|
#if ENABLE_WIFI
|
||||||
|
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
|
||||||
bool wifiUp = false;
|
bool wifiUp = false;
|
||||||
|
|
||||||
Serial1.begin(WIFI_SERIAL_LINK_SPEED);
|
Serial1.begin(WIFI_SERIAL_LINK_SPEED);
|
||||||
wifiUp = WifiInterface::setup(Serial1, WIFI_SSID, WIFI_PASSWORD, WIFI_HOSTNAME, WIFI_PORT);
|
wifiUp = WifiInterface::setup(Serial1, F(WIFI_SSID), F(WIFI_PASSWORD), F(WIFI_HOSTNAME), WIFI_PORT);
|
||||||
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
|
|
||||||
if (!wifiUp)
|
if (!wifiUp)
|
||||||
{
|
{
|
||||||
Serial2.begin(WIFI_SERIAL_LINK_SPEED);
|
Serial2.begin(WIFI_SERIAL_LINK_SPEED);
|
||||||
wifiUp = WifiInterface::setup(Serial2, WIFI_SSID, WIFI_PASSWORD, WIFI_HOSTNAME, WIFI_PORT);
|
wifiUp = WifiInterface::setup(Serial2, F(WIFI_SSID), F(WIFI_PASSWORD), F(WIFI_HOSTNAME), WIFI_PORT);
|
||||||
}
|
}
|
||||||
if (!wifiUp)
|
if (!wifiUp)
|
||||||
{
|
{
|
||||||
Serial3.begin(WIFI_SERIAL_LINK_SPEED);
|
Serial3.begin(WIFI_SERIAL_LINK_SPEED);
|
||||||
wifiUp = WifiInterface::setup(Serial3, WIFI_SSID, WIFI_PASSWORD, WIFI_HOSTNAME, WIFI_PORT);
|
wifiUp = WifiInterface::setup(Serial3, F(WIFI_SSID), F(WIFI_PASSWORD), F(WIFI_HOSTNAME), WIFI_PORT);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
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 <avr/pgmspace.h>
|
||||||
#include "WifiInterface.h"
|
#include "WifiInterface.h"
|
||||||
#include "DIAG.h"
|
#include "DIAG.h"
|
||||||
#include "StringFormatter.h"
|
#include "StringFormatter.h"
|
||||||
|
@ -39,13 +41,15 @@ MemStream WifiInterface::streamer(buffer, MAX_WIFI_BUFFER);
|
||||||
Stream * WifiInterface::wifiStream = NULL;
|
Stream * WifiInterface::wifiStream = NULL;
|
||||||
HTTP_CALLBACK WifiInterface::httpCallback = 0;
|
HTTP_CALLBACK WifiInterface::httpCallback = 0;
|
||||||
|
|
||||||
|
bool WifiInterface::setup(Stream & setupStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password,
|
||||||
void WifiInterface::setup(Stream & setupStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password,
|
|
||||||
const __FlashStringHelper* hostname, int port) {
|
const __FlashStringHelper* hostname, int port) {
|
||||||
|
static uint8_t ntry = 0;
|
||||||
|
ntry++;
|
||||||
|
|
||||||
wifiStream = &setupStream;
|
wifiStream = &setupStream;
|
||||||
|
|
||||||
DIAG(F("\n++++++ Wifi Setup In Progress ++++++++\n"));
|
DIAG(F("\n++ Wifi Setup Try %d ++\n"), ntry);
|
||||||
|
|
||||||
connected = setup2( SSid, password, hostname, port);
|
connected = setup2( SSid, password, hostname, port);
|
||||||
|
|
||||||
if (connected) {
|
if (connected) {
|
||||||
|
@ -53,12 +57,14 @@ void WifiInterface::setup(Stream & setupStream, const __FlashStringHelper* SSid
|
||||||
checkForOK(200, OK_SEARCH, true);
|
checkForOK(200, OK_SEARCH, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
DIAG(F("\n++++++ Wifi Setup %S ++++++++\n"), connected ? F("OK") : F("FAILED"));
|
DIAG(F("\n++ Wifi Setup %S ++\n"), connected ? F("OK") : F("FAILED"));
|
||||||
|
return connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringHelper* password,
|
bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringHelper* password,
|
||||||
const __FlashStringHelper* hostname, int port) {
|
const __FlashStringHelper* hostname, int port) {
|
||||||
int ipOK = 0;
|
bool ipOK = false;
|
||||||
|
bool oldCmd = false;
|
||||||
|
|
||||||
char macAddress[17]; // mac address extraction
|
char macAddress[17]; // mac address extraction
|
||||||
|
|
||||||
|
@ -71,66 +77,99 @@ bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringH
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StringFormatter::send(wifiStream, F("AT\r\n")); // Is something here that understands AT?
|
||||||
|
if(!checkForOK(200, OK_SEARCH, true))
|
||||||
|
return false; // No AT compatible WiFi module here
|
||||||
|
|
||||||
StringFormatter::send(wifiStream, F("ATE1\r\n")); // Turn on the echo, se we can see what's happening
|
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, OK_SEARCH, true); // Makes this visible on the console
|
||||||
|
|
||||||
// Display the AT version information
|
// Display the AT version information
|
||||||
StringFormatter::send(wifiStream, F("AT+GMR\r\n"));
|
StringFormatter::send(wifiStream, F("AT+GMR\r\n"));
|
||||||
checkForOK(2000, OK_SEARCH, true, false); // Makes this visible on the console
|
checkForOK(2000, OK_SEARCH, true, false); // Makes this visible on the console
|
||||||
|
|
||||||
delay(8000); // give a preconfigured ES8266 a chance to connect to a router
|
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"
|
||||||
StringFormatter::send(wifiStream, F("AT+CIFSR\r\n"));
|
|
||||||
|
|
||||||
// looking fpr mac addr eg +CIFSR:APMAC,"be:dd:c2:5c:6b:b7"
|
// If the source code looks unconfigured, check if the
|
||||||
if (checkForOK(5000, (const char*) F("+CIFSR:APMAC,\""), true,false)) {
|
// ESP8266 is preconfigured. We check the first 13 chars
|
||||||
// Copy 17 byte mac address
|
// of the password.
|
||||||
for (int i=0; i<17;i++) {
|
if (strncmp_P("Your network ",(const char*)password,13) == 0) {
|
||||||
while(!wifiStream->available());
|
delay(8000); // give a preconfigured ES8266 a chance to connect to a router
|
||||||
macAddress[i]=wifiStream->read();
|
|
||||||
StringFormatter::printEscape(macAddress[i]);
|
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))
|
||||||
char macTail[]={macAddress[9],macAddress[10],macAddress[12],macAddress[13],macAddress[15],macAddress[16],'\0'};
|
ipOK = true;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
// AT command early version supports CWJAP/CWSAP
|
||||||
|
if (SSid) {
|
||||||
|
StringFormatter::send(wifiStream, F("AT+CWJAP=\"%S\",\"%S\"\r\n"), SSid, password);
|
||||||
|
ipOK = checkForOK(16000, OK_SEARCH, true);
|
||||||
|
}
|
||||||
|
DIAG(F("\n**\n"));
|
||||||
|
|
||||||
|
} 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
|
||||||
|
|
||||||
if (checkForOK(5000, (const char*) F("+CIFSR:STAIP"), true,false))
|
if (SSid) {
|
||||||
if (!checkForOK(1000, (const char*) F("0.0.0.0"), true,false))
|
StringFormatter::send(wifiStream, F("AT+CWJAP_CUR=\"%S\",\"%S\"\r\n"), SSid, password);
|
||||||
ipOK = 1;
|
ipOK = checkForOK(20000, OK_SEARCH, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delay(8000); // give a preconfigured ES8266 a chance to connect to a router
|
||||||
|
|
||||||
|
if (ipOK) {
|
||||||
|
// But we really only have the ESSID and password correct
|
||||||
|
// Let's check for IP
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!ipOK) {
|
if (!ipOK) {
|
||||||
StringFormatter::send(wifiStream, F("AT+CWMODE=3\r\n")); // configure as server or access point
|
// 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.
|
||||||
checkForOK(1000, OK_SEARCH, true); // Not always OK, sometimes "no change"
|
checkForOK(1000, OK_SEARCH, true); // Not always OK, sometimes "no change"
|
||||||
|
|
||||||
// Older ES versions have AT+CWJAP, newer ones have AT+CWJAP_CUR and AT+CWHOSTNAME
|
// Figure out MAC addr
|
||||||
StringFormatter::send(wifiStream, F("AT+CWJAP?\r\n"));
|
StringFormatter::send(wifiStream, F("AT+CIFSR\r\n"));
|
||||||
if (checkForOK(2000, OK_SEARCH, true)) {
|
// looking fpr mac addr eg +CIFSR:APMAC,"be:dd:c2:5c:6b:b7"
|
||||||
while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE
|
if (checkForOK(5000, (const char*) F("+CIFSR:APMAC,\""), true,false)) {
|
||||||
|
// Copy 17 byte mac address
|
||||||
// AT command early version supports CWJAP/CWSAP
|
for (int i=0; i<17;i++) {
|
||||||
if (SSid) {
|
while(!wifiStream->available());
|
||||||
StringFormatter::send(wifiStream, F("AT+CWJAP=\"%S\",\"%S\"\r\n"), SSid, password);
|
macAddress[i]=wifiStream->read();
|
||||||
checkForOK(16000, OK_SEARCH, true); // can ignore failure as AP mode may still be ok
|
StringFormatter::printEscape(macAddress[i]);
|
||||||
}
|
}
|
||||||
DIAG(F("\n**\n"));
|
}
|
||||||
|
char macTail[]={macAddress[9],macAddress[10],macAddress[12],macAddress[13],macAddress[15],macAddress[16],'\0'};
|
||||||
// establish the APname
|
|
||||||
|
if (oldCmd) {
|
||||||
|
while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE
|
||||||
|
|
||||||
StringFormatter::send(wifiStream, F("AT+CWSAP=\"DCCEX_%s\",\"PASS_%s\",1,4\r\n"), macTail, macTail);
|
StringFormatter::send(wifiStream, F("AT+CWSAP=\"DCCEX_%s\",\"PASS_%s\",1,4\r\n"), macTail, macTail);
|
||||||
checkForOK(16000, OK_SEARCH, true); // can ignore failure as AP mode may still be ok
|
checkForOK(16000, OK_SEARCH, true); // can ignore failure as AP mode may still be ok
|
||||||
|
|
||||||
}
|
} else {
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
if (SSid) {
|
|
||||||
StringFormatter::send(wifiStream, F("AT+CWJAP_CUR=\"%S\",\"%S\"\r\n"), SSid, password);
|
|
||||||
checkForOK(20000, OK_SEARCH, true); // can ignore failure as AP mode may still be ok
|
|
||||||
}
|
|
||||||
|
|
||||||
StringFormatter::send(wifiStream, F("AT+CWSAP_CUR=\"DCCEX_%s\",\"PASS_%s\",1,4\r\n"), macTail, macTail);
|
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
|
checkForOK(20000, OK_SEARCH, true); // can ignore failure as SSid mode may still be ok
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ typedef void (*HTTP_CALLBACK)(Print * stream, byte * cmd);
|
||||||
class WifiInterface {
|
class WifiInterface {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void setup(Stream & setupStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password,
|
static bool setup(Stream & setupStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password,
|
||||||
const __FlashStringHelper* hostname, int port);
|
const __FlashStringHelper* hostname, int port);
|
||||||
static void loop();
|
static void loop();
|
||||||
static void ATCommand(const byte * command);
|
static void ATCommand(const byte * command);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user