diff --git a/DCCEXParser.h b/DCCEXParser.h index 5efb18f..5bcfbd5 100644 --- a/DCCEXParser.h +++ b/DCCEXParser.h @@ -1,5 +1,6 @@ #ifndef DCCEXParser_h #define DCCEXParser_h +#include struct DCCEXParser { DCCEXParser(); diff --git a/WifiInterface.cpp b/WifiInterface.cpp index edf6805..121ad1c 100644 --- a/WifiInterface.cpp +++ b/WifiInterface.cpp @@ -1,49 +1,78 @@ #include "WifiInterface.h" #include "Config.h" #include "DIAG.h" +#include "StringFormatter.h" +const char READY_SEARCH[]="\r\nready\r\n"; +const char OK_SEARCH[]="\r\nOK\r\n"; -WiFiEspServer WifiInterface::server(WIFI_PORT); -WiFiEspClient WifiInterface::client; -bool WifiInterface::haveClient=false; bool WifiInterface::connected=false; -void WifiInterface::setup() +void WifiInterface::setup() { + DIAG(F("\n++++++ Wifi Setup In Progress ++++++++\n")); + connected=setup2(); + DIAG(F("\n++++++ Wifi Setup %s ++++++++\n"), connected?"OK":"FAILED"); +} + +bool WifiInterface::setup2() { Serial1.begin(WIFI_BAUD_RATE); // initialize serial for ESP module - WiFi.init(&Serial1); // initialize ESP module + + delay(1000); - // check for the presence of the shield - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - return; - } - - // attempt to connect to WiFi network - int status = WiFi.begin(WIFI_SSID, WIFI_PASS); - - - if (status==WL_CONNECTED) { - connected=true; - - // start the server on port WIFI_PORT - server.begin(); - - // print the SSID of the network you're attached to - DIAG(F("\nWifi Connected SSID: %s IP=%d.%d.%d.%d port %d\n "), - WiFi.SSID(), WiFi.localIP()[0],WiFi.localIP()[1],WiFi.localIP()[2],WiFi.localIP()[3],WIFI_PORT); + StringFormatter::send(Serial1,F("AT+RST\r\n")); // reset module + if (!checkForOK(10000,READY_SEARCH)) return false; + + StringFormatter::send(Serial1,F("AT+CWMODE=1\r\n")); // configure as access point + if (!checkForOK(10000,OK_SEARCH)) return false; + + StringFormatter::send(Serial1,F("AT+CWJAP=\"%s\",\"%s\"\r\n"),WIFI_SSID,WIFI_PASS); + if (!checkForOK(20000,OK_SEARCH)) return false; + + StringFormatter::send(Serial1,F("AT+CIFSR\r\n")); // get ip address //192.168.4.1 + if (!checkForOK(10000,OK_SEARCH)) return false; + + StringFormatter::send(Serial1,F("AT+CIPMUX=1\r\n")); // configure for multiple connections + if (!checkForOK(10000,OK_SEARCH)) return false; + + StringFormatter::send(Serial1,F("AT+CIPSERVER=1,%d\r\n"),WIFI_PORT); // turn on server on port 80 + if (!checkForOK(10000,OK_SEARCH)) return false; + + return true; +} + +bool WifiInterface::checkForOK( const int timeout,char * search) { + long int time = millis()+timeout; + byte locator=0; + DIAG(F("\nWifi setup Check:"),search); + while( time > millis()) { + while(Serial1.available()) { + int ch=Serial1.read(); + Serial.write(ch); + if (ch!=search[locator]) locator=0; + if (ch==search[locator]){ + locator++; + if (!search[locator]) { + DIAG(F("\nOK after %dms\n"),millis()-time+timeout); + return true; + } + } + } } + DIAG(F("\nTIMEOUT after %dms\n"),timeout); + return false; } - void WifiInterface::loop(DCCEXParser & parser) { - if (!connected) return; + if (!connected) return; + while(Serial1.available()) Serial.write(Serial1.read()); + // TODO Read incoming... + // if starts +IPD + // get session id + // fill buffer + // set session id into PrintAT + // call parser + // implement parser flush - WiFiEspClient xclient= server.available(); // listen for incoming clients - if (xclient.connected()) { - DIAG(F("\nNew Wifi Client connected\n")); - parser.loop(xclient); - xclient.stop(); - } } diff --git a/WifiInterface.h b/WifiInterface.h index 42e9f02..f86fb23 100644 --- a/WifiInterface.h +++ b/WifiInterface.h @@ -1,7 +1,6 @@ #ifndef WifiInterface_h #define WifiInterface_h -#include #include "DCCEXParser.h" class WifiInterface { @@ -11,10 +10,11 @@ class WifiInterface { static void loop(DCCEXParser & parser); private: - static WiFiEspServer server; - static WiFiEspClient client; + static bool setup2(); + static bool checkForOK(const int timeout, char * search); static bool connected; - static bool haveClient; + static const byte MAX_BUFFER=64; + static byte inboundBuffer[MAX_BUFFER]; }; #endif