Added MQTT topic for controller to NV store.
Found PubSubClient is blocking, causing WD reboots
This commit is contained in:
parent
f6f721bd8a
commit
253bc3f728
|
@ -10,7 +10,7 @@
|
|||
|
||||
[env:esp32dev]
|
||||
platform = espressif32
|
||||
lib_extra_dirs = ~/Documents/Arduino/libraries
|
||||
;lib_extra_dirs = ~/Documents/Arduino/libraries
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
board_build.partitions = min_spiffs.csv
|
||||
|
@ -19,9 +19,9 @@ upload_speed = 921600
|
|||
;upload_protocol = espota
|
||||
upload_port = COM16
|
||||
upload_protocol = esptool
|
||||
upload_flags =
|
||||
--port=3232
|
||||
monitor_speed = 115200
|
||||
;upload_flags =
|
||||
; --port=3232
|
||||
;monitor_speed = 115200
|
||||
extra_scripts = post:add_CRC.py
|
||||
; replace shitty Arduino millis with a linear time version
|
||||
build_flags =
|
||||
|
|
|
@ -430,6 +430,7 @@ sMQTTparams::load()
|
|||
validatedLoad("host", host, 127, "hostIP");
|
||||
validatedLoad("username", username, 31, "username");
|
||||
validatedLoad("password", password, 31, "password");
|
||||
validatedLoad("topic", topic, 31, "Afterburner");
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
|
@ -443,6 +444,7 @@ sMQTTparams::save()
|
|||
preferences.putString("host", host);
|
||||
preferences.putString("username", username);
|
||||
preferences.putString("password", password);
|
||||
preferences.putString("topic", topic);
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
|
|
|
@ -219,12 +219,14 @@ struct sMQTTparams : public CESP32_NVStorage {
|
|||
char host[128];
|
||||
char username[32];
|
||||
char password[32];
|
||||
char topic[32];
|
||||
void init() {
|
||||
enabled = false;
|
||||
port = 1234;
|
||||
memset(host, 0, 128);
|
||||
memset(username, 0, 32);
|
||||
memset(password, 0, 32);
|
||||
memset(topic, 0, 32);
|
||||
}
|
||||
sMQTTparams& operator=(const sMQTTparams& rhs) {
|
||||
enabled = rhs.enabled;
|
||||
|
@ -232,9 +234,11 @@ struct sMQTTparams : public CESP32_NVStorage {
|
|||
memcpy(host, rhs.host, 128);
|
||||
memcpy(username, rhs.username, 32);
|
||||
memcpy(password, rhs.password, 32);
|
||||
memcpy(topic, rhs.topic, 32);
|
||||
host[127] = 0;
|
||||
username[31] = 0;
|
||||
password[31] = 0;
|
||||
topic[31] = 0;
|
||||
return *this;
|
||||
}
|
||||
void load();
|
||||
|
|
|
@ -4,13 +4,14 @@
|
|||
|
||||
#include "../cfg/BTCConfig.h"
|
||||
|
||||
#ifdef USE_MQTT
|
||||
#if USE_MQTT == 1
|
||||
#include <Arduino.h>
|
||||
#include "ABMqtt.h"
|
||||
#include "../../lib/PubSubClient/src/PubSubClient.h"
|
||||
#include "../../lib/aysync-mqtt-client/src/AsyncMqttClient.h"
|
||||
#include "BTCWifi.h"
|
||||
#include "BTCWebServer.h"
|
||||
#include "../Utility/DebugPort.h"
|
||||
#include "../Utility/NVStorage.h"
|
||||
|
||||
// Update these with values suitable for your network.
|
||||
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
||||
|
@ -26,17 +27,27 @@ void MqttCallback(char* topic, byte* payload, unsigned int length) {
|
|||
char test[256];
|
||||
int len = length < 256 ? length : 255;
|
||||
strncpy(test, (char*)payload, len);
|
||||
test[len];
|
||||
test[len] = 0;
|
||||
DebugPort.println(test);
|
||||
// handle message arrived
|
||||
}
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient MQTTclient(espClient);
|
||||
//WiFiClient espClient;
|
||||
AsyncMqttClient MQTTclient;
|
||||
|
||||
void MqttSetup() {
|
||||
bool MqttSetup() {
|
||||
MQTTclient.setServer(MQTTserver, 1883);
|
||||
MQTTclient.setCallback(MqttCallback);
|
||||
espClient.setTimeout(5);
|
||||
const sMQTTparams params = NVstore.getMQTTinfo();
|
||||
if(params.enabled) {
|
||||
if(strlen(params.host)) {
|
||||
// DebugPort.printf("MQTT: setting server to %s:%d\r\n", params.host, params.port);
|
||||
// MQTTclient.setServer(params.host, params.port);
|
||||
MQTTclient.setCallback(MqttCallback);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool reconnect() {
|
||||
|
@ -46,23 +57,28 @@ bool reconnect() {
|
|||
long tDelta = millis() - HoldOff;
|
||||
if(tDelta > 0)
|
||||
HoldOff = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(HoldOff == 0 && !MQTTclient.connected()) {
|
||||
DebugPort.println("Attempting MQTT connection");
|
||||
if (MQTTclient.connect("afterburnerClient")) {
|
||||
DebugPort.println("MQTT connected");
|
||||
// Once connected, publish an announcement...
|
||||
MQTTclient.publish("Afterburner/test", "hello world");
|
||||
// ... and resubscribe
|
||||
// MQTTclient.subscribe("inTopic");
|
||||
// MQTTclient.subscribe("Test/Test/Test/Test");
|
||||
MQTTclient.subscribe("Afterburner");
|
||||
MQTTclient.publish("Afterburner/test", "the end is nigh");
|
||||
}
|
||||
else {
|
||||
DebugPort.printf("MQTT connect failed, rc = %d, try again in 5 seconds\r\n", MQTTclient.state());
|
||||
HoldOff = millis() + 5000;
|
||||
if(MqttSetup()) {
|
||||
|
||||
if(NVstore.getMQTTinfo().enabled && HoldOff == 0 && !MQTTclient.connected()) {
|
||||
DebugPort.println("Attempting MQTT connection");
|
||||
if (MQTTclient.connect("afterburnerClient")) {
|
||||
DebugPort.println("MQTT connected");
|
||||
// Once connected, publish an announcement...
|
||||
// MQTTclient.publish("Afterburner/test", "hello world");
|
||||
// ... and resubscribe
|
||||
const sMQTTparams params = NVstore.getMQTTinfo();
|
||||
char topic[128];
|
||||
sprintf(topic, "%s/JSONin", params.topic);
|
||||
MQTTclient.subscribe(topic);
|
||||
// MQTTclient.publish("Afterburner/test", "the end is nigh");
|
||||
}
|
||||
else {
|
||||
DebugPort.printf("MQTT connect failed, rc = %d, try again in 5 seconds\r\n", MQTTclient.state());
|
||||
HoldOff = millis() + 5000;
|
||||
}
|
||||
}
|
||||
}
|
||||
return MQTTclient.connected();
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
|
||||
void MqttCallback(char* topic, byte* payload, unsigned int length);
|
||||
void MqttSetup();
|
||||
bool MqttSetup();
|
||||
void doMQTT();
|
||||
|
||||
#endif
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
#define USE_WIFI 1
|
||||
#define USE_OTA 1
|
||||
#define USE_WEBSERVER 1
|
||||
#define USE_MQTT 1
|
||||
#define USE_MQTT 0
|
||||
|
||||
#define USE_PORTAL_TRIGGER_PIN 0
|
||||
|
||||
|
|
Loading…
Reference in a new issue