mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-23 08:06:13 +01:00
Wifi Interface Setup
Performs connection etcc... still not passing cmds to dcc
This commit is contained in:
parent
e0c76a9dc4
commit
0a60a36d30
|
@ -1,5 +1,6 @@
|
|||
#ifndef DCCEXParser_h
|
||||
#define DCCEXParser_h
|
||||
#include <Arduino.h>
|
||||
struct DCCEXParser
|
||||
{
|
||||
DCCEXParser();
|
||||
|
|
|
@ -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
|
||||
|
||||
// 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);
|
||||
delay(1000);
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
|
||||
#ifndef WifiInterface_h
|
||||
#define WifiInterface_h
|
||||
#include <WiFiEsp.h>
|
||||
#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
|
||||
|
|
Loading…
Reference in New Issue
Block a user