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
|
#ifndef DCCEXParser_h
|
||||||
#define DCCEXParser_h
|
#define DCCEXParser_h
|
||||||
|
#include <Arduino.h>
|
||||||
struct DCCEXParser
|
struct DCCEXParser
|
||||||
{
|
{
|
||||||
DCCEXParser();
|
DCCEXParser();
|
||||||
|
|
|
@ -1,49 +1,78 @@
|
||||||
#include "WifiInterface.h"
|
#include "WifiInterface.h"
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "DIAG.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;
|
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
|
Serial1.begin(WIFI_BAUD_RATE); // initialize serial for ESP module
|
||||||
WiFi.init(&Serial1); // initialize ESP module
|
|
||||||
|
|
||||||
// check for the presence of the shield
|
delay(1000);
|
||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
|
||||||
Serial.println("WiFi shield not present");
|
StringFormatter::send(Serial1,F("AT+RST\r\n")); // reset module
|
||||||
return;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// attempt to connect to WiFi network
|
bool WifiInterface::checkForOK( const int timeout,char * search) {
|
||||||
int status = WiFi.begin(WIFI_SSID, WIFI_PASS);
|
long int time = millis()+timeout;
|
||||||
|
byte locator=0;
|
||||||
|
DIAG(F("\nWifi setup Check:"),search);
|
||||||
if (status==WL_CONNECTED) {
|
while( time > millis()) {
|
||||||
connected=true;
|
while(Serial1.available()) {
|
||||||
|
int ch=Serial1.read();
|
||||||
// start the server on port WIFI_PORT
|
Serial.write(ch);
|
||||||
server.begin();
|
if (ch!=search[locator]) locator=0;
|
||||||
|
if (ch==search[locator]){
|
||||||
// print the SSID of the network you're attached to
|
locator++;
|
||||||
DIAG(F("\nWifi Connected SSID: %s IP=%d.%d.%d.%d port %d\n "),
|
if (!search[locator]) {
|
||||||
WiFi.SSID(), WiFi.localIP()[0],WiFi.localIP()[1],WiFi.localIP()[2],WiFi.localIP()[3],WIFI_PORT);
|
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) {
|
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
|
#ifndef WifiInterface_h
|
||||||
#define WifiInterface_h
|
#define WifiInterface_h
|
||||||
#include <WiFiEsp.h>
|
|
||||||
#include "DCCEXParser.h"
|
#include "DCCEXParser.h"
|
||||||
|
|
||||||
class WifiInterface {
|
class WifiInterface {
|
||||||
|
@ -11,10 +10,11 @@ class WifiInterface {
|
||||||
static void loop(DCCEXParser & parser);
|
static void loop(DCCEXParser & parser);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static WiFiEspServer server;
|
static bool setup2();
|
||||||
static WiFiEspClient client;
|
static bool checkForOK(const int timeout, char * search);
|
||||||
static bool connected;
|
static bool connected;
|
||||||
static bool haveClient;
|
static const byte MAX_BUFFER=64;
|
||||||
|
static byte inboundBuffer[MAX_BUFFER];
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user