1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-27 01:56:14 +01:00

Surface Wifi setup into main prog setup()

Allows for setup with a UNO
This commit is contained in:
Asbelos 2020-06-28 10:21:27 +01:00
parent 669356df7d
commit c97d07608b
4 changed files with 68 additions and 52 deletions

View File

@ -9,7 +9,7 @@
// the usb or wifi streamm. It demonstrates how a command may be intercepted // the usb or wifi streamm. It demonstrates how a command may be intercepted
// or even a new command created without having to break open the API library code. // or even a new command created without having to break open the API library code.
// The filter is permitted to use or modify the parameter list before passing it on to // The filter is permitted to use or modify the parameter list before passing it on to
// the standard parser. By setting the opcode to ZERO, the standard parser will // the standard parser. By setting the opcode to 0, the standard parser will
// just ignore the command on the assumption that you have already handled it. // just ignore the command on the assumption that you have already handled it.
// //
// The filter must be enabled by calling the DCC EXParser::setFilter method, see use in setup(). // The filter must be enabled by calling the DCC EXParser::setFilter method, see use in setup().
@ -34,6 +34,8 @@ void myFilter(Stream & stream, byte & opcode, byte & paramCount, int p[]) {
// Callback functions are necessary if you call any API that must wait for a response from the // Callback functions are necessary if you call any API that must wait for a response from the
// programming track. The API must return immediately otherwise other loop() functions would be blocked. // programming track. The API must return immediately otherwise other loop() functions would be blocked.
// Your callback function will be invoked when the data arrives from the prog track. // Your callback function will be invoked when the data arrives from the prog track.
// See the DCC:getLocoId example in the setup function.
void myCallback(int result) { void myCallback(int result) {
DIAG(F("\n getting Loco Id callback result=%d"),result); DIAG(F("\n getting Loco Id callback result=%d"),result);
@ -50,31 +52,51 @@ DCCEXParser serialParser;
int minMemory=32767; int minMemory=32767;
void setup() { void setup() {
Serial.begin(SERIAL_BAUD_RATE);
// The main sketch has responsibilities during setup()
// Responsibility 1: Start the usb connection for diagnostics and possible JMRI input
Serial.begin(115200);
// Responsibility 2: Start the DCC engine.
DCC::begin(); DCC::begin();
if (WIFI_PORT>0) WifiInterface::setup();
// Responsibility 3: Optionally Start the WiFi interface if required.
// NOTE: On a Uno you will have to provide a SoftwareSerial
// configured for the pins connected to the Wifi card
// and a 9600 baud rate.
// setup(serial, F(router name), F(password) , port)
//
Serial1.begin(115200);
WifiInterface::setup(Serial1, F("BTHub5-M6PT"), F("49de8d4862"),3532); // (3532 is 0xDCC decimal... )
// This is just for demonstration purposes
DIAG(F("\n===== CVReader demonstrating DCC::getLocoId() call ==========\n")); DIAG(F("\n===== CVReader demonstrating DCC::getLocoId() call ==========\n"));
DCC::getLocoId(myCallback); // myCallback will be called with the result DCC::getLocoId(myCallback); // myCallback will be called with the result
DIAG(F("\n===== DCC::getLocoId has returned, but wont be executed until we are in loop() ======\n")); DIAG(F("\n===== DCC::getLocoId has returned, but the callback wont be executed until we are in loop() ======\n"));
// Optionally tell parser to use my example filter // Optionally tell the command parser to use my example filter.
// This will intercept JMRI commands from both USB and Wifi
DCCEXParser::setFilter(myFilter); DCCEXParser::setFilter(myFilter);
malloc(1);
DIAG(F("\nReady for JMRI commands\n")); DIAG(F("\nReady for JMRI commands\n"));
} }
void loop() { void loop() {
DCC::loop(); // required to keep locos running and check powwer // The main sketch has responsibilities during loop()
// This line passes input on Serial to the DCCEXParser // Responsibility 1: Handle DCC background processes
// (loco reminders and power checks)
DCC::loop();
// Responsibility 2: handle any incoming commands on USB connection
serialParser.loop(Serial); serialParser.loop(Serial);
// This line passes input on Wifi to another DCCEXParser // Responsibility 3: Optionally handle any incoming WiFi traffic
if (WIFI_PORT>0) WifiInterface::loop(); WifiInterface::loop(Serial1);
// Report any decrease in memory // Your additional code e.g. Report any decrease in memory
int freeNow=freeMemory(); int freeNow=freeMemory();
if (freeNow<minMemory) { if (freeNow<minMemory) {
minMemory=freeNow; minMemory=freeNow;

View File

@ -1,11 +1,5 @@
#ifndef Config_h #ifndef Config_h
#define Config_h #define Config_h
const int WIFI_PORT =0xDCC; // (0xDCC is 3532 decimal) OR set to zero for no wifi
const char WIFI_SSID[] PROGMEM = "BTHub5-M6PT"; // your network SSID (name)
const char WIFI_PASS[] PROGMEM = "49de8d4862"; // your network password
const long WIFI_BAUD_RATE=115200;
const long SERIAL_BAUD_RATE=115200;
// This hardware configuration would normally be setup using a bunch of #ifdefs. // This hardware configuration would normally be setup using a bunch of #ifdefs.
@ -25,7 +19,7 @@ const byte PROG_BRAKE_PIN = 10;
const float PROG_SENSE_FACTOR=1; // analgRead(PROG_SENSE_PIN) * PROG_SENSE_FACTOR = milliamps const float PROG_SENSE_FACTOR=1; // analgRead(PROG_SENSE_PIN) * PROG_SENSE_FACTOR = milliamps
// Allocations with memory implications..! // Allocations with memory implications..!
// Base system takes approx 700 bytes + 8 per loco. Turnouts, Sensors etc are dynamically created // Base system takes approx 900 bytes + 8 per loco. Turnouts, Sensors etc are dynamically created
const byte MAX_LOCOS=50; const byte MAX_LOCOS=50;
#endif #endif

View File

@ -17,54 +17,54 @@ int WifiInterface::connectionId;
byte WifiInterface::buffer[MAX_WIFI_BUFFER]; byte WifiInterface::buffer[MAX_WIFI_BUFFER];
MemStream WifiInterface::streamer(buffer,sizeof(buffer)); MemStream WifiInterface::streamer(buffer,sizeof(buffer));
void WifiInterface::setup() { void WifiInterface::setup(Stream & wifiStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password, int port) {
DIAG(F("\n++++++ Wifi Setup In Progress ++++++++\n")); DIAG(F("\n++++++ Wifi Setup In Progress ++++++++\n"));
connected=setup2(); connected=setup2(wifiStream, SSid, password,port);
// TODO calloc the buffer and streamer and parser etc // TODO calloc the buffer and streamer and parser etc
DIAG(F("\n++++++ Wifi Setup %S ++++++++\n"), connected?F("OK"):F("FAILED")); DIAG(F("\n++++++ Wifi Setup %S ++++++++\n"), connected?F("OK"):F("FAILED"));
} }
bool WifiInterface::setup2() bool WifiInterface::setup2(Stream & wifiStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password, int port)
{ {
Serial1.begin(WIFI_BAUD_RATE); // initialize serial for ESP module
delay(1000); delay(1000);
StringFormatter::send(Serial1,F("AT+RST\r\n")); // reset module StringFormatter::send(wifiStream,F("AT+RST\r\n")); // reset module
checkForOK(5000,END_DETAIL_SEARCH,true); // Show startup but ignore unreadable upto ready checkForOK(wifiStream,5000,END_DETAIL_SEARCH,true); // Show startup but ignore unreadable upto ready
if (!checkForOK(5000,READY_SEARCH,false)) return false; if (!checkForOK(wifiStream,5000,READY_SEARCH,false)) return false;
StringFormatter::send(Serial1,F("AT+CWMODE=1\r\n")); // configure as access point StringFormatter::send(wifiStream,F("AT+CWMODE=1\r\n")); // configure as access point
if (!checkForOK(10000,OK_SEARCH,true)) return false; if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
StringFormatter::send(Serial1,F("AT+CWJAP=\"%S\",\"%S\"\r\n"),WIFI_SSID,WIFI_PASS); StringFormatter::send(wifiStream,F("AT+CWJAP=\"%S\",\"%S\"\r\n"),SSid,password);
if (!checkForOK(20000,OK_SEARCH,true)) return false; if (!checkForOK(wifiStream,20000,OK_SEARCH,true)) return false;
StringFormatter::send(Serial1,F("AT+CIFSR\r\n")); // get ip address //192.168.4.1 StringFormatter::send(wifiStream,F("AT+CIFSR\r\n")); // get ip address //192.168.4.1
if (!checkForOK(10000,OK_SEARCH,true)) return false; if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
StringFormatter::send(Serial1,F("AT+CIPMUX=1\r\n")); // configure for multiple connections StringFormatter::send(wifiStream,F("AT+CIPMUX=1\r\n")); // configure for multiple connections
if (!checkForOK(10000,OK_SEARCH,true)) return false; if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
StringFormatter::send(Serial1,F("AT+CIPSERVER=1,%d\r\n"),WIFI_PORT); // turn on server on port 80 StringFormatter::send(wifiStream,F("AT+CIPSERVER=1,%d\r\n"),port); // turn on server on port 80
if (!checkForOK(10000,OK_SEARCH,true)) return false; if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
return true; return true;
} }
bool WifiInterface::checkForOK( const int timeout, const char * waitfor, bool echo) { bool WifiInterface::checkForOK(Stream & wifiStream, const int timeout, const char * waitfor, bool echo) {
long int time = millis()+timeout; long int startTime = millis();
char *locator=waitfor; char *locator=waitfor;
DIAG(F("\nWifi setup Check: %S\n"),waitfor); DIAG(F("\nWifi setup Check: %S\n"),waitfor);
while( time > millis()) { while( millis()-startTime < timeout) {
while(Serial1.available()) { while(wifiStream.available()) {
int ch=Serial1.read(); int ch=wifiStream.read();
if (echo) Serial.write(ch); if (echo) Serial.write(ch);
if (ch!=pgm_read_byte_near(locator)) locator=waitfor; if (ch!=pgm_read_byte_near(locator)) locator=waitfor;
if (ch==pgm_read_byte_near(locator)) { if (ch==pgm_read_byte_near(locator)) {
locator++; locator++;
if (!pgm_read_byte_near(locator)) { if (!pgm_read_byte_near(locator)) {
DIAG(F("\nOK after %dms\n"),millis()-time+timeout); DIAG(F("\nOK after %dms\n"),millis()-startTime);
return true; return true;
} }
} }
@ -75,15 +75,14 @@ bool WifiInterface::checkForOK( const int timeout, const char * waitfor, bool ec
} }
void WifiInterface::loop() { void WifiInterface::loop(Stream & wifiStream) {
if (!connected) return; if (!connected) return;
WiThrottle::loop(); // check heartbeats WiThrottle::loop(); // check heartbeats
// read anything into a buffer, collecting info on the way // read anything into a buffer, collecting info on the way
while (loopstate!=99 && Serial1.available()) { while (loopstate!=99 && wifiStream.available()) {
int ch=Serial1.read(); int ch=wifiStream.read();
Serial.write(ch);
switch (loopstate) { switch (loopstate) {
case 0: // looking for + case 0: // looking for +
connectionId=0; connectionId=0;
@ -135,9 +134,9 @@ void WifiInterface::loop() {
if (streamer.available()) { // there is a reply to send if (streamer.available()) { // there is a reply to send
DIAG(F("WiFiInterface Responding (%d) %s\n"),connectionId,buffer); DIAG(F("WiFiInterface Responding (%d) %s\n"),connectionId,buffer);
StringFormatter::send(Serial1,F("AT+CIPSEND=%d,%d\r\n"),connectionId,streamer.available()); StringFormatter::send(wifiStream,F("AT+CIPSEND=%d,%d\r\n"),connectionId,streamer.available());
streamer.write('\0'); streamer.write('\0');
if (checkForOK(1000,PROMPT_SEARCH,true)) Serial1.print((char *) buffer); if (checkForOK(wifiStream,1000,PROMPT_SEARCH,true)) wifiStream.print((char *) buffer);
} }
loopstate=0; // go back to looking for +IPD loopstate=0; // go back to looking for +IPD
} }

View File

@ -9,13 +9,14 @@
class WifiInterface { class WifiInterface {
public: public:
static void setup(); static void setup(Stream & wifiStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password, int port);
static void loop(); static void loop(Stream & wifiStream);
private: private:
static DCCEXParser parser; static DCCEXParser parser;
static bool setup2(); static bool setup2(Stream & wifiStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password, int port);
static bool checkForOK( const int timeout, const char* waitfor, bool echo); static bool checkForOK(Stream & wifiStream, const int timeout, const char* waitfor, bool echo);
static bool connected; static bool connected;
static byte loopstate; static byte loopstate;
static int datalength; static int datalength;