mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-30 11:36:13 +01:00
Fixes for unowifi connecting, fix for baud rate and garbled text, start inbound messages using base handler already made
This commit is contained in:
parent
9a9715ccbc
commit
72ced97abf
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -5,6 +5,5 @@ Release/*
|
||||||
.clang_complete
|
.clang_complete
|
||||||
.gcc-flags.json
|
.gcc-flags.json
|
||||||
.pio/
|
.pio/
|
||||||
.vscode/
|
.vscode/*
|
||||||
config.h
|
config.h
|
||||||
.vscode/extensions.json
|
|
||||||
|
|
|
@ -57,7 +57,12 @@ void setup()
|
||||||
|
|
||||||
// Responsibility 1: Start the usb connection for diagnostics
|
// Responsibility 1: Start the usb connection for diagnostics
|
||||||
// This is normally Serial but uses SerialUSB on a SAMD processor
|
// This is normally Serial but uses SerialUSB on a SAMD processor
|
||||||
|
// Uno Wifi Rev2 is stupid wont read past this baud rate for some reason
|
||||||
|
#ifdef ARDUINO_AVR_UNO_WIFI_REV2
|
||||||
|
Serial.begin(74880);
|
||||||
|
#else
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
#endif
|
||||||
DIAG(F("DCC++ EX v%S"),F(VERSION));
|
DIAG(F("DCC++ EX v%S"),F(VERSION));
|
||||||
|
|
||||||
CONDITIONAL_LCD_START {
|
CONDITIONAL_LCD_START {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* © 2021, Chris Harlow. All rights reserved.
|
* © 2021, Chris Harlow, Anthony Williams. All rights reserved.
|
||||||
*
|
*
|
||||||
* This file is part of DCC-EX/CommandStation-EX
|
* This file is part of DCC-EX/CommandStation-EX
|
||||||
*
|
*
|
||||||
|
@ -23,81 +23,287 @@
|
||||||
#include "WifiInterfaceRev2.h"
|
#include "WifiInterfaceRev2.h"
|
||||||
#include "DIAG.h"
|
#include "DIAG.h"
|
||||||
#include "CommandDistributor.h"
|
#include "CommandDistributor.h"
|
||||||
#include <SPI.h>
|
|
||||||
#include <WiFiNINA.h>
|
|
||||||
|
|
||||||
|
WifiInterface * WifiInterface::singleton;
|
||||||
|
|
||||||
WiFiServer WifiInterface::server(2560);
|
WiFiServer WifiInterface::server(2560);
|
||||||
bool WifiInterface::connected=false;
|
bool WifiInterface::connected = false;
|
||||||
/**
|
/**
|
||||||
* @brief Setup Wifi Connection
|
* @brief Setup Wifi Connection
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
bool WifiInterface::setup(long serial_link_speed,
|
bool WifiInterface::setup(long serial_link_speed,
|
||||||
const FSH *wifiESSID,
|
const FSH *wifiESSID,
|
||||||
const FSH *wifiPassword,
|
const FSH *wifiPassword,
|
||||||
const FSH *hostname,
|
const FSH *hostname,
|
||||||
const int port) {
|
const int port)
|
||||||
|
{
|
||||||
|
singleton = new WifiInterface(serial_link_speed, wifiESSID, wifiPassword, hostname, port);
|
||||||
|
//return singleton->connected;
|
||||||
|
}
|
||||||
|
|
||||||
|
WifiInterface::WifiInterface(long serial_link_speed,
|
||||||
|
const FSH *wifiESSID,
|
||||||
|
const FSH *wifiPassword,
|
||||||
|
const FSH *hostname,
|
||||||
|
const int port)
|
||||||
|
{
|
||||||
(void)serial_link_speed;
|
(void)serial_link_speed;
|
||||||
(void)port; // obsolete
|
(void)port; // obsolete
|
||||||
(void)hostname; // To be implemented
|
(void)hostname; // To be implemented
|
||||||
|
|
||||||
if (WiFi.status() == WL_NO_MODULE) {
|
if (WiFi.status() == WL_NO_MODULE)
|
||||||
DIAG(F("Wifi- hardware failed\n"));
|
{
|
||||||
return false;
|
DIAG(F("Wifi- hardware failed\n"));
|
||||||
}
|
}
|
||||||
DIAG(F("Wifi Firmware=%s expected=%S"),WiFi.firmwareVersion(),F(WIFI_FIRMWARE_LATEST_VERSION));
|
DIAG(F("Wifi Firmware=%s expected=%S"), WiFi.firmwareVersion(), F(WIFI_FIRMWARE_LATEST_VERSION));
|
||||||
|
|
||||||
|
|
||||||
int status = WL_IDLE_STATUS;
|
int status = WL_IDLE_STATUS;
|
||||||
int attempts = 4;
|
int attempts = 4;
|
||||||
while (status != WL_CONNECTED) {
|
while (status != WL_CONNECTED)
|
||||||
if (attempts-- <= 0) {
|
{
|
||||||
|
if (attempts-- <= 0)
|
||||||
|
{
|
||||||
DIAG(F("\nFAILED - No Wifi\n"));
|
DIAG(F("\nFAILED - No Wifi\n"));
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
DIAG(F("\nAttempting to connect to %s\n"),wifiESSID);
|
DIAG(F("\nAttempting to connect to %s\n"), wifiESSID);
|
||||||
status = WiFi.begin(wifiESSID, wifiPassword);
|
status = WiFi.begin(wifiESSID, wifiPassword);
|
||||||
// wait 10 seconds for connection:
|
// wait 10 seconds for connection:
|
||||||
delay(10000);
|
delay(10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
server.begin(); // start the server on port 2560
|
server.begin(); // start the server on port 2560
|
||||||
|
|
||||||
IPAddress ip = WiFi.localIP();
|
IPAddress ip = WiFi.localIP();
|
||||||
LCD(4,F("IP: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
|
LCD(4, F("IP: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
|
||||||
LCD(5,F("Port:2560"));
|
LCD(5, F("Port:2560"));
|
||||||
outboundRing=new RingStream(OUTBOUND_RING_SIZE);
|
}
|
||||||
|
|
||||||
|
void WifiInterface::loop() {
|
||||||
|
singleton->loop1();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Main loop for the WifiInterfaceRev2
|
* @brief Main loop for the WifiInterfaceRev2
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void WifiInterface::loop()
|
void WifiInterface::loop1()
|
||||||
{
|
{
|
||||||
WiFiClient client = server.available(); // listen for incoming clients
|
|
||||||
if (client)
|
if (loop2() != INBOUND_IDLE)
|
||||||
{
|
return;
|
||||||
// read bytes from a client
|
// WiFiClient client = server.available(); // listen for incoming clients
|
||||||
byte buffer[MAX_NINA_BUFFER];
|
// if (client)
|
||||||
int count = client.read(buffer, MAX_NINA_BUFFER-1);
|
// {
|
||||||
buffer[count] = '\0'; // terminate the string properly
|
// // read bytes from a client
|
||||||
if (Diag::WIFI) DIAG(F("WIFI:%e\n"), buffer);
|
// byte buffer[MAX_NINA_BUFFER];
|
||||||
// TEMPORARY - Assume all clients are client 1, this will confuse WiThrottle!
|
// int count = client.read(buffer, MAX_NINA_BUFFER - 1);
|
||||||
outboundRing->mark(1);
|
// buffer[count] = '\0'; // terminate the string properly
|
||||||
// TEMPORARY - Assume all clients are client 1, this will confuse WiThrottle!
|
// if (Diag::WIFI)
|
||||||
CommandDistributor::parse(1,buffer,outboundRing);
|
// DIAG(F("WIFI:%e\n"), buffer);
|
||||||
outboundRing->commit();
|
// // TEMPORARY - Assume all clients are client 1, this will confuse WiThrottle!
|
||||||
int socketOut=outboundRing->read();
|
// outboundRing->mark(1);
|
||||||
if (socketOut>=0) {
|
// // TEMPORARY - Assume all clients are client 1, this will confuse WiThrottle!
|
||||||
int count=outboundRing->count();
|
// CommandDistributor::parse(1, buffer, outboundRing);
|
||||||
if (Diag::WIFI) DIAG(F("Wifi Reply count=:%d\n"), count);
|
// outboundRing->commit();
|
||||||
for(;count>0;count--) client.write(outboundRing->read());
|
// int socketOut = outboundRing->read();
|
||||||
client.flush(); //maybe
|
// if (socketOut >= 0)
|
||||||
}
|
// {
|
||||||
}
|
// int count = outboundRing->count();
|
||||||
|
// if (Diag::WIFI)
|
||||||
|
// DIAG(F("Wifi Reply count=:%d\n"), count);
|
||||||
|
// for (; count > 0; count--)
|
||||||
|
// client.write(outboundRing->read());
|
||||||
|
// client.flush(); //maybe
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WifiInterface::INBOUND_STATE WifiInterface::loop2()
|
||||||
|
{
|
||||||
|
WiFiClient client = server.available(); // listen for incoming clients
|
||||||
|
if (client)
|
||||||
|
{
|
||||||
|
while (client.available())
|
||||||
|
{
|
||||||
|
int ch = client.read();
|
||||||
|
|
||||||
|
// echo the char to the diagnostic stream in escaped format
|
||||||
|
if (Diag::WIFI)
|
||||||
|
{
|
||||||
|
// DIAG(F(" %d/"), loopState);
|
||||||
|
StringFormatter::printEscape(ch); // DIAG in disguise
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (loopState)
|
||||||
|
{
|
||||||
|
case ANYTHING: // looking for +IPD, > , busy , n,CONNECTED, n,CLOSED, ERROR, SEND OK
|
||||||
|
|
||||||
|
if (ch == '+')
|
||||||
|
{
|
||||||
|
loopState = IPD;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch == '>')
|
||||||
|
{
|
||||||
|
if (Diag::WIFI)
|
||||||
|
DIAG(F("[XMIT %d]"), currentReplySize);
|
||||||
|
// for (int i = 0; i < currentReplySize; i++)
|
||||||
|
// {
|
||||||
|
// int cout = outboundRing->read();
|
||||||
|
// client.print(cout);
|
||||||
|
// if (Diag::WIFI)
|
||||||
|
// StringFormatter::printEscape(cout); // DIAG in disguise
|
||||||
|
// }
|
||||||
|
clientPendingCIPSEND = -1;
|
||||||
|
pendingCipsend = false;
|
||||||
|
loopState = SKIPTOEND;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch == 'R')
|
||||||
|
{ // Received ... bytes
|
||||||
|
loopState = SKIPTOEND;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch == 'S')
|
||||||
|
{ // SEND OK probably
|
||||||
|
loopState = SKIPTOEND;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch == 'b')
|
||||||
|
{ // This is a busy indicator... probabaly must restart a CIPSEND
|
||||||
|
pendingCipsend = (clientPendingCIPSEND >= 0);
|
||||||
|
loopState = SKIPTOEND;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch >= '0' && ch <= '9')
|
||||||
|
{
|
||||||
|
runningClientId = ch - '0';
|
||||||
|
loopState = GOT_CLIENT_ID;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch == 'E' || ch == 'l')
|
||||||
|
{ // ERROR or "link is not valid"
|
||||||
|
if (clientPendingCIPSEND >= 0)
|
||||||
|
{
|
||||||
|
// A CIPSEND was errored... just toss it away
|
||||||
|
//purgeCurrentCIPSEND();
|
||||||
|
}
|
||||||
|
loopState = SKIPTOEND;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IPD: // Looking for I in +IPD
|
||||||
|
loopState = (ch == 'I') ? IPD1 : SKIPTOEND;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IPD1: // Looking for P in +IPD
|
||||||
|
loopState = (ch == 'P') ? IPD2 : SKIPTOEND;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IPD2: // Looking for D in +IPD
|
||||||
|
loopState = (ch == 'D') ? IPD3 : SKIPTOEND;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IPD3: // Looking for , After +IPD
|
||||||
|
loopState = (ch == ',') ? IPD4_CLIENT : SKIPTOEND;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IPD4_CLIENT: // reading connection id
|
||||||
|
if (ch >= '0' || ch <= '9')
|
||||||
|
{
|
||||||
|
runningClientId = ch - '0';
|
||||||
|
loopState = IPD5;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
loopState = SKIPTOEND;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IPD5: // Looking for , After +IPD,client
|
||||||
|
loopState = (ch == ',') ? IPD6_LENGTH : SKIPTOEND;
|
||||||
|
dataLength = 0; // ready to start collecting the length
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IPD6_LENGTH: // reading for length
|
||||||
|
if (ch == ':')
|
||||||
|
{
|
||||||
|
if (dataLength == 0)
|
||||||
|
{
|
||||||
|
loopState = ANYTHING;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (Diag::WIFI)
|
||||||
|
DIAG(F("\nWifi inbound data(%d:%d):"), runningClientId, dataLength);
|
||||||
|
// if (server.freeSpace() <= (dataLength + 1))
|
||||||
|
// {
|
||||||
|
// // This input would overflow the inbound ring, ignore it
|
||||||
|
// loopState = IPD_IGNORE_DATA;
|
||||||
|
// if (Diag::WIFI)
|
||||||
|
// DIAG(F("\nWifi OVERFLOW IGNORING:"));
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// server.mark(runningClientId);
|
||||||
|
loopState = IPD_DATA;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
dataLength = dataLength * 10 + (ch - '0');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IPD_DATA: // reading data
|
||||||
|
server.write(ch);
|
||||||
|
dataLength--;
|
||||||
|
if (dataLength == 0)
|
||||||
|
{
|
||||||
|
//server.commit();
|
||||||
|
loopState = ANYTHING;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case IPD_IGNORE_DATA: // ignoring data that would not fit in inbound ring
|
||||||
|
dataLength--;
|
||||||
|
if (dataLength == 0)
|
||||||
|
loopState = ANYTHING;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GOT_CLIENT_ID: // got x before CLOSE or CONNECTED
|
||||||
|
loopState = (ch == ',') ? GOT_CLIENT_ID2 : SKIPTOEND;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case GOT_CLIENT_ID2: // got "x,"
|
||||||
|
if (ch == 'C')
|
||||||
|
{
|
||||||
|
// got "x C" before CLOSE or CONNECTED, or CONNECT FAILED
|
||||||
|
//if (runningClientId == clientPendingCIPSEND)
|
||||||
|
//purgeCurrentCIPSEND();
|
||||||
|
}
|
||||||
|
loopState = SKIPTOEND;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SKIPTOEND: // skipping for /n
|
||||||
|
if (ch == '\n')
|
||||||
|
loopState = ANYTHING;
|
||||||
|
break;
|
||||||
|
} // switch
|
||||||
|
}
|
||||||
|
} // available
|
||||||
|
return (loopState == ANYTHING) ? INBOUND_IDLE : INBOUND_BUSY;
|
||||||
|
}
|
||||||
|
|
||||||
|
// void WifiInboundHandler::purgeCurrentCIPSEND() {
|
||||||
|
// // A CIPSEND was sent but errored... or the client closed just toss it away
|
||||||
|
// if (Diag::WIFI) DIAG(F("Wifi: DROPPING CIPSEND=%d,%d\n"),clientPendingCIPSEND,currentReplySize);
|
||||||
|
// for (int i=0;i<=currentReplySize;i++) outboundRing->read();
|
||||||
|
// pendingCipsend=false;
|
||||||
|
// clientPendingCIPSEND=-1;
|
||||||
|
// }
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -30,15 +30,60 @@ class WifiInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static bool setup(long serial_link_speed, // ignored
|
static bool setup(long serial_link_speed, // ignored
|
||||||
const FSH *wifiESSID,
|
const FSH *wifiESSID,
|
||||||
const FSH *wifiPassword,
|
const FSH *wifiPassword,
|
||||||
const FSH *hostname,
|
const FSH *hostname,
|
||||||
const int port = 2560); // ignored
|
const int port = 2560); // ignored
|
||||||
static void loop();
|
static void loop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static WiFiServer server;
|
static WifiInterface *singleton;
|
||||||
static bool connected;
|
|
||||||
static RingStream * outboundRing;
|
WifiInterface(long serial_link_speed,
|
||||||
|
const FSH *wifiESSID,
|
||||||
|
const FSH *wifiPassword,
|
||||||
|
const FSH *hostname,
|
||||||
|
const int port);
|
||||||
|
|
||||||
|
enum INBOUND_STATE : byte
|
||||||
|
{
|
||||||
|
INBOUND_BUSY, // keep calling in loop()
|
||||||
|
INBOUND_IDLE // Nothing happening, outbound may xcall CIPSEND
|
||||||
|
};
|
||||||
|
|
||||||
|
enum LOOP_STATE : byte
|
||||||
|
{
|
||||||
|
ANYTHING, // ready for +IPD, n CLOSED, n CONNECTED, busy etc...
|
||||||
|
SKIPTOEND, // skip to newline
|
||||||
|
|
||||||
|
// +IPD,client,length:data
|
||||||
|
IPD, // got +
|
||||||
|
IPD1, // got +I
|
||||||
|
IPD2, // got +IP
|
||||||
|
IPD3, // got +IPD
|
||||||
|
IPD4_CLIENT, // got +IPD, reading cient id
|
||||||
|
IPD5, // got +IPD,c
|
||||||
|
IPD6_LENGTH, // got +IPD,c, reading length
|
||||||
|
IPD_DATA, // got +IPD,c,ll,: collecting data
|
||||||
|
IPD_IGNORE_DATA, // got +IPD,c,ll,: ignoring the data that won't fit inblound Ring
|
||||||
|
|
||||||
|
GOT_CLIENT_ID, // clientid prefix to CONNECTED / CLOSED
|
||||||
|
GOT_CLIENT_ID2 // clientid prefix to CONNECTED / CLOSED
|
||||||
|
};
|
||||||
|
|
||||||
|
LOOP_STATE loopState = ANYTHING;
|
||||||
|
int runningClientId; // latest client inbound processing data or CLOSE
|
||||||
|
int dataLength; // dataLength of +IPD
|
||||||
|
int clientPendingCIPSEND = -1;
|
||||||
|
int currentReplySize;
|
||||||
|
bool pendingCipsend;
|
||||||
|
void purgeCurrentCIPSEND();
|
||||||
|
|
||||||
|
static WiFiServer server;
|
||||||
|
static bool connected;
|
||||||
|
//static RingStream *outboundRing;
|
||||||
|
void loop1();
|
||||||
|
INBOUND_STATE loop2();
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
// WIFI_ON: All prereqs for running with WIFI are met
|
// WIFI_ON: All prereqs for running with WIFI are met
|
||||||
//
|
//
|
||||||
|
|
||||||
#if ENABLE_WIFI && (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_SAMD_ZERO))
|
#if ENABLE_WIFI && (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_SAMD_ZERO)) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
|
||||||
#define WIFI_ON true
|
#define WIFI_ON true
|
||||||
#else
|
#else
|
||||||
#define WIFI_ON false
|
#define WIFI_ON false
|
||||||
|
|
|
@ -63,10 +63,11 @@ framework = arduino
|
||||||
lib_deps =
|
lib_deps =
|
||||||
${env.lib_deps}
|
${env.lib_deps}
|
||||||
DIO2
|
DIO2
|
||||||
|
WiFiNINA
|
||||||
arduino-libraries/Ethernet
|
arduino-libraries/Ethernet
|
||||||
SPI
|
SPI
|
||||||
marcoschwartz/LiquidCrystal_I2C
|
marcoschwartz/LiquidCrystal_I2C
|
||||||
monitor_speed = 115200
|
monitor_speed = 74880
|
||||||
monitor_flags = --echo
|
monitor_flags = --echo
|
||||||
build_flags = "-DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_UNO_WIFI_DEV_ED -DARDUINO_ARCH_AVR -DESP_CH_UART -DESP_CH_UART_BR=19200"g
|
build_flags = "-DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_UNO_WIFI_DEV_ED -DARDUINO_ARCH_AVR -DESP_CH_UART -DESP_CH_UART_BR=19200"g
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user