mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-12-27 14:21:25 +01:00
Surface Wifi setup into main prog setup()
Allows for setup with a UNO
This commit is contained in:
parent
669356df7d
commit
c97d07608b
46
CVReader.ino
46
CVReader.ino
@ -9,7 +9,7 @@
|
||||
// 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.
|
||||
// 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.
|
||||
//
|
||||
// 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
|
||||
// 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.
|
||||
// See the DCC:getLocoId example in the setup function.
|
||||
|
||||
|
||||
void myCallback(int result) {
|
||||
DIAG(F("\n getting Loco Id callback result=%d"),result);
|
||||
@ -50,31 +52,51 @@ DCCEXParser serialParser;
|
||||
int minMemory=32767;
|
||||
|
||||
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();
|
||||
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"));
|
||||
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);
|
||||
|
||||
malloc(1);
|
||||
|
||||
DIAG(F("\nReady for JMRI commands\n"));
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
DCC::loop(); // required to keep locos running and check powwer
|
||||
// The main sketch has responsibilities during loop()
|
||||
|
||||
// Responsibility 1: Handle DCC background processes
|
||||
// (loco reminders and power checks)
|
||||
DCC::loop();
|
||||
|
||||
// This line passes input on Serial to the DCCEXParser
|
||||
// Responsibility 2: handle any incoming commands on USB connection
|
||||
serialParser.loop(Serial);
|
||||
|
||||
// This line passes input on Wifi to another DCCEXParser
|
||||
if (WIFI_PORT>0) WifiInterface::loop();
|
||||
// Responsibility 3: Optionally handle any incoming WiFi traffic
|
||||
WifiInterface::loop(Serial1);
|
||||
|
||||
// Report any decrease in memory
|
||||
// Your additional code e.g. Report any decrease in memory
|
||||
int freeNow=freeMemory();
|
||||
if (freeNow<minMemory) {
|
||||
minMemory=freeNow;
|
||||
|
8
Config.h
8
Config.h
@ -1,11 +1,5 @@
|
||||
#ifndef 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.
|
||||
|
||||
@ -25,7 +19,7 @@ const byte PROG_BRAKE_PIN = 10;
|
||||
const float PROG_SENSE_FACTOR=1; // analgRead(PROG_SENSE_PIN) * PROG_SENSE_FACTOR = milliamps
|
||||
|
||||
// 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;
|
||||
|
||||
#endif
|
||||
|
@ -17,54 +17,54 @@ int WifiInterface::connectionId;
|
||||
byte WifiInterface::buffer[MAX_WIFI_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"));
|
||||
connected=setup2();
|
||||
connected=setup2(wifiStream, SSid, password,port);
|
||||
// TODO calloc the buffer and streamer and parser etc
|
||||
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);
|
||||
|
||||
StringFormatter::send(Serial1,F("AT+RST\r\n")); // reset module
|
||||
checkForOK(5000,END_DETAIL_SEARCH,true); // Show startup but ignore unreadable upto ready
|
||||
if (!checkForOK(5000,READY_SEARCH,false)) return false;
|
||||
StringFormatter::send(wifiStream,F("AT+RST\r\n")); // reset module
|
||||
checkForOK(wifiStream,5000,END_DETAIL_SEARCH,true); // Show startup but ignore unreadable upto ready
|
||||
if (!checkForOK(wifiStream,5000,READY_SEARCH,false)) return false;
|
||||
|
||||
StringFormatter::send(Serial1,F("AT+CWMODE=1\r\n")); // configure as access point
|
||||
if (!checkForOK(10000,OK_SEARCH,true)) return false;
|
||||
StringFormatter::send(wifiStream,F("AT+CWMODE=1\r\n")); // configure as access point
|
||||
if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
|
||||
|
||||
StringFormatter::send(Serial1,F("AT+CWJAP=\"%S\",\"%S\"\r\n"),WIFI_SSID,WIFI_PASS);
|
||||
if (!checkForOK(20000,OK_SEARCH,true)) return false;
|
||||
StringFormatter::send(wifiStream,F("AT+CWJAP=\"%S\",\"%S\"\r\n"),SSid,password);
|
||||
if (!checkForOK(wifiStream,20000,OK_SEARCH,true)) return false;
|
||||
|
||||
StringFormatter::send(Serial1,F("AT+CIFSR\r\n")); // get ip address //192.168.4.1
|
||||
if (!checkForOK(10000,OK_SEARCH,true)) return false;
|
||||
StringFormatter::send(wifiStream,F("AT+CIFSR\r\n")); // get ip address //192.168.4.1
|
||||
if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
|
||||
|
||||
StringFormatter::send(Serial1,F("AT+CIPMUX=1\r\n")); // configure for multiple connections
|
||||
if (!checkForOK(10000,OK_SEARCH,true)) return false;
|
||||
StringFormatter::send(wifiStream,F("AT+CIPMUX=1\r\n")); // configure for multiple connections
|
||||
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
|
||||
if (!checkForOK(10000,OK_SEARCH,true)) return false;
|
||||
StringFormatter::send(wifiStream,F("AT+CIPSERVER=1,%d\r\n"),port); // turn on server on port 80
|
||||
if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiInterface::checkForOK( const int timeout, const char * waitfor, bool echo) {
|
||||
long int time = millis()+timeout;
|
||||
bool WifiInterface::checkForOK(Stream & wifiStream, const int timeout, const char * waitfor, bool echo) {
|
||||
long int startTime = millis();
|
||||
char *locator=waitfor;
|
||||
DIAG(F("\nWifi setup Check: %S\n"),waitfor);
|
||||
while( time > millis()) {
|
||||
while(Serial1.available()) {
|
||||
int ch=Serial1.read();
|
||||
while( millis()-startTime < timeout) {
|
||||
while(wifiStream.available()) {
|
||||
int ch=wifiStream.read();
|
||||
if (echo) Serial.write(ch);
|
||||
if (ch!=pgm_read_byte_near(locator)) locator=waitfor;
|
||||
if (ch==pgm_read_byte_near(locator)) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
|
||||
WiThrottle::loop(); // check heartbeats
|
||||
|
||||
// read anything into a buffer, collecting info on the way
|
||||
while (loopstate!=99 && Serial1.available()) {
|
||||
int ch=Serial1.read();
|
||||
Serial.write(ch);
|
||||
while (loopstate!=99 && wifiStream.available()) {
|
||||
int ch=wifiStream.read();
|
||||
switch (loopstate) {
|
||||
case 0: // looking for +
|
||||
connectionId=0;
|
||||
@ -135,9 +134,9 @@ void WifiInterface::loop() {
|
||||
if (streamer.available()) { // there is a reply to send
|
||||
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');
|
||||
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
|
||||
}
|
||||
|
@ -9,13 +9,14 @@
|
||||
class WifiInterface {
|
||||
|
||||
public:
|
||||
static void setup();
|
||||
static void loop();
|
||||
static void setup(Stream & wifiStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password, int port);
|
||||
static void loop(Stream & wifiStream);
|
||||
|
||||
private:
|
||||
|
||||
static DCCEXParser parser;
|
||||
static bool setup2();
|
||||
static bool checkForOK( const int timeout, const char* waitfor, bool echo);
|
||||
static bool setup2(Stream & wifiStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password, int port);
|
||||
static bool checkForOK(Stream & wifiStream, const int timeout, const char* waitfor, bool echo);
|
||||
static bool connected;
|
||||
static byte loopstate;
|
||||
static int datalength;
|
||||
|
Loading…
Reference in New Issue
Block a user