2020-06-12 15:28:35 +02:00
|
|
|
#include "WifiInterface.h"
|
|
|
|
#include "Config.h"
|
|
|
|
#include "DIAG.h"
|
2020-06-12 20:56:40 +02:00
|
|
|
#include "StringFormatter.h"
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-06-12 20:56:40 +02:00
|
|
|
const char READY_SEARCH[]="\r\nready\r\n";
|
|
|
|
const char OK_SEARCH[]="\r\nOK\r\n";
|
2020-06-12 15:28:35 +02:00
|
|
|
|
|
|
|
bool WifiInterface::connected=false;
|
|
|
|
|
2020-06-12 20:56:40 +02:00
|
|
|
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()
|
2020-06-12 15:28:35 +02:00
|
|
|
{
|
|
|
|
Serial1.begin(WIFI_BAUD_RATE); // initialize serial for ESP module
|
|
|
|
|
2020-06-12 20:56:40 +02:00
|
|
|
delay(1000);
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-06-12 20:56:40 +02:00
|
|
|
StringFormatter::send(Serial1,F("AT+RST\r\n")); // reset module
|
|
|
|
if (!checkForOK(10000,READY_SEARCH)) return false;
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-06-12 20:56:40 +02:00
|
|
|
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;
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-06-12 20:56:40 +02:00
|
|
|
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;
|
2020-06-12 15:28:35 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 20:56:40 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-06-12 15:28:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
void WifiInterface::loop(DCCEXParser & parser) {
|
2020-06-12 20:56:40 +02:00
|
|
|
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
|
2020-06-12 15:28:35 +02:00
|
|
|
|
|
|
|
}
|