Added nonblocking wifi manager - Also added STA mode on timeout of 20 seconds

This commit is contained in:
James Clark 2018-10-28 19:59:34 +11:00
parent 15a3b6d3ef
commit 33bb526a54
4 changed files with 39 additions and 53 deletions

View file

@ -1,10 +1,4 @@
#include "BTCWifi.h"
//this is for telnet only
// select which pin will trigger the configuration portal when set to LOW
WiFiManager wm;
@ -36,6 +30,7 @@ void inittelnetdebug(String HOST_NAME)
hostNameWifi.concat(".local");
Debug.begin(HOST_NAME);
Debug.setSerialEnabled(true);
Debug.setResetCmdEnabled(true); // Enable the reset command
@ -81,7 +76,7 @@ void DoDebug()
}
}
void initWifi(int initpin)
void initWifi(int initpin,const char *failedssid, const char *failedpassword)
{
@ -94,16 +89,20 @@ void initWifi(int initpin)
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
// then goes into a blocking loop awaiting configuration and will return success result
wm.setConfigPortalTimeout(20);
wm.setConfigPortalBlocking(false);
res = wm.autoConnect(); // auto generated AP name from chipid
if(!res) {
Serial.println("Failed to connect");
// ESP.restart();
Serial.println("Setting up ESP as AP");
WiFi.softAP(failedssid, failedpassword);
}
else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
}
@ -140,5 +139,3 @@ void doWiFiManager(){
startTime = millis();
}
}

View file

@ -1,12 +1,14 @@
#include <Arduino.h>
#include <WiFiManager.h>
#include <WiFi.h>
#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug
extern RemoteDebug Debug;
/*
const char *failedssid;
const char *failedpassword;
*/
extern RemoteDebug Debug;
void doWiFiManager();
void initWifi(int initpin);
void initWifi(int initpin,const char *failedssid, const char *failedpassword);
void DoDebug();
void inittelnetdebug(String HOST_NAME);

View file

@ -68,7 +68,7 @@ void Bluetooth_Init()
for(BTidx = 0; BTidx < maxTries; BTidx++) {
PRNT.print(" @ ");
PRNT.print(BTRates[BTidx]);
PRNT.print(" baud... ");
PRNT.printf(" baud... ");
Serial2.begin(BTRates[BTidx], SERIAL_8N1, Rx2Pin, Tx2Pin); // open serial port at a std.baud rate
delay(10);
Serial2.print("\r\n"); // clear the throat!
@ -97,7 +97,6 @@ void Bluetooth_Init()
// plough on and assume 9600 baud, but at the mercy of whatever the module name is...
PRNT.println("FAILED to detect a HC-05 Bluetooth module :-(");
// leave the EN pin high - if other style module keeps it powered!
// assume it is 9600, and just (try to) use it like that...
// we will sense the STATE line to prove a client is hanging off the link...
PRNT.println("ASSUMING a HC-05 module @ 9600baud (Unknown name)");
@ -111,14 +110,14 @@ void Bluetooth_Init()
PRNT.println("HC-05 found");
do { // so we can break!
PRNT.print(" Setting Name to \"Diesel Heater\"... ");
PRNT.printf(" Setting Name to \"Diesel Heater\"... ");
if(!Bluetooth_ATCommand("AT+NAME=\"Diesel Heater\"\r\n")) {
PRNT.println("FAILED");
break;
}
PRNT.println("OK");
PRNT.print(" Setting baud rate to 9600N81...");
PRNT.printf(" Setting baud rate to 9600N81...");
if(!Bluetooth_ATCommand("AT+UART=9600,1,0\r\n")) {
PRNT.println("FAILED");
break;
@ -156,7 +155,7 @@ void Bluetooth_Check()
void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm)
{
PRNT.print(millis());
PRNT.print("ms ");
PRNT.printf("ms ");
// DebugReportFrame(pHdr, Frame, lineterm ? "\r\n" : " ");
DebugReportFrame(pHdr, Frame, " ");
@ -169,11 +168,11 @@ void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm
digitalWrite(LED, !digitalRead(LED)); // toggle LED
}
else {
PRNT.print("Bluetooth data not sent, CRC error ");
PRNT.printf("Bluetooth data not sent, CRC error ");
}
}
else {
PRNT.print("No Bluetooth client");
PRNT.printf("No Bluetooth client");
// force LED off
digitalWrite(LED, 0);
}
@ -235,7 +234,7 @@ void Bluetooth_SendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm
{
char fullMsg[32];
PRNT.print(millis());
PRNT.printf(millis());
DebugReportFrame(pHdr, Frame, lineterm ? "\r\n" : " ");
delay(40);
if(SerialBT.hasClient()) {

View file

@ -1,17 +1,4 @@
//comment this out to remove TELNET
#define TELNET
#ifdef TELNET
#define PRNT Debug
#endif
#ifndef TELNET
#define PRNT Serial
#endif
/*
Chinese Heater Half Duplex Serial Data Sending Tool
@ -85,7 +72,8 @@
#define HOST_NAME "remotedebug-sample"
#define TRIGGER_PIN 0
#define FAILEDSSID "BTCESP32"
#define FAILEDPASSWORD "thereisnospoon"
//comment this out to remove TELNET
@ -182,7 +170,7 @@ void PrepareTxFrame(const CProtocol& basisFrame, CProtocol& TxFrame, bool isBTCm
void setup()
{
initWifi(TRIGGER_PIN);
initWifi(TRIGGER_PIN, FAILEDSSID, FAILEDPASSWORD);
inittelnetdebug(HOST_NAME);
// initialize serial port to interact with the "blue wire"
// 25000 baud, Tx and Rx channels of Chinese heater comms interface:
@ -241,6 +229,7 @@ void loop()
{
unsigned long timenow = millis();
doWiFiManager();
//PRNT.println(WiFi.localIP());
DoDebug();
// check for test commands received from PC Over USB
@ -297,7 +286,7 @@ void loop()
// This will be the first activity for considerable period on the blue wire
// The heater always responds to a controller frame, but otherwise never by itself
if(BlueWireData.available() && (RxTimeElapsed > 100)) {
PRNT.print("Re-sync'd with OEM Controller. ");
PRNT.printf("Re-sync'd with OEM Controller. ");
PRNT.print(RxTimeElapsed);
PRNT.println("ms Idle time.");
hasOEMController = true;
@ -386,7 +375,7 @@ void loop()
CommState.set(CommStates::Idle);
#ifdef SHOW_HEAP
Serial.print("Free heap ");
Serial.printf("Free heap ");
Serial.println(ESP.getFreeHeap());
#endif
}
@ -395,13 +384,13 @@ void loop()
void DebugReportFrame(const char* hdr, const CProtocol& Frame, const char* ftr)
{
PRNT.print(hdr); // header
PRNT.printf(hdr); // header
for(int i=0; i<24; i++) {
char str[16];
sprintf(str, " %02X", Frame.Data[i]); // build 2 dig hex values
PRNT.print(str); // and print
PRNT.printf(str); // and print
}
PRNT.print(ftr); // footer
PRNT.printf(ftr); // footer
}
@ -434,28 +423,28 @@ void Command_Interpret(const char* pLine)
pLine += 4;
cVal = (unsigned char)((atof(pLine) * 10.0) + 0.5);
pNVStorage->setPmin(cVal);
PRNT.print("Pump min = ");
PRNT.printf("Pump min = ");
PRNT.println(cVal);
}
else if(strncmp(pLine, "Pmax", 4) == 0) {
pLine += 4;
cVal = (unsigned char)((atof(pLine) * 10.0) + 0.5);
pNVStorage->setPmax(cVal);
PRNT.print("Pump max = ");
PRNT.printf("Pump max = ");
PRNT.println(cVal);
}
else if(strncmp(pLine, "Fmin", 4) == 0) {
pLine += 4;
sVal = atoi(pLine);
pNVStorage->setFmin(sVal);
PRNT.print("Fan min = ");
PRNT.printf("Fan min = ");
PRNT.println(sVal);
}
else if(strncmp(pLine, "Fmax", 4) == 0) {
pLine += 4;
sVal = atoi(pLine);
pNVStorage->setFmax(sVal);
PRNT.print("Fan max = ");
PRNT.printf("Fan max = ");
PRNT.println(int(sVal));
}
else if(strncmp(pLine, "save", 4) == 0) {
@ -466,21 +455,20 @@ void Command_Interpret(const char* pLine)
pLine += 4;
cVal = atoi(pLine);
pNVStorage->setTemperature(cVal);
PRNT.print("degC = ");
PRNT.printf("degC = ");
PRNT.println(cVal);
}
else if(strncmp(pLine, "Mode", 4) == 0) {
pLine += 4;
cVal = !pNVStorage->getThermostatMode();
pNVStorage->setThermostatMode(cVal);
PRNT.print("Mode now ");
PRNT.printf("Mode now ");
PRNT.println(cVal ? "Thermostat" : "Fixed Hz");
}
else {
PRNT.print(pLine);
PRNT.printf(pLine);
PRNT.println(" ????");
}
}
}