2019-05-15 12:10:12 +02:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
2019-08-27 15:09:07 +02:00
|
|
|
#include "../cfg/BTCConfig.h"
|
|
|
|
|
2019-08-30 12:08:54 +02:00
|
|
|
#if USE_MQTT == 1
|
2019-09-01 06:43:00 +02:00
|
|
|
|
2019-07-07 09:18:38 +02:00
|
|
|
#include <Arduino.h>
|
2019-05-15 12:10:12 +02:00
|
|
|
#include "ABMqtt.h"
|
2019-08-31 09:34:56 +02:00
|
|
|
#include "../../lib/async-mqtt-client/src/AsyncMqttClient.h"
|
2019-07-07 09:18:38 +02:00
|
|
|
#include "BTCWifi.h"
|
|
|
|
#include "BTCWebServer.h"
|
2019-08-27 15:09:07 +02:00
|
|
|
#include "../Utility/DebugPort.h"
|
2019-08-30 12:08:54 +02:00
|
|
|
#include "../Utility/NVStorage.h"
|
2019-05-15 12:10:12 +02:00
|
|
|
|
2019-09-01 06:43:00 +02:00
|
|
|
#define USE_RTOS_MQTTTIMER
|
|
|
|
//#define USE_LOCAL_MQTTSTRINGS
|
|
|
|
|
|
|
|
//IPAddress testMQTTserver(5, 196, 95, 208); // test.mosquito.org
|
|
|
|
//IPAddress testMQTTserver(18, 194, 98, 249); // broker.hivemq.com
|
2019-05-15 12:10:12 +02:00
|
|
|
|
2019-08-31 09:34:56 +02:00
|
|
|
AsyncMqttClient MQTTclient;
|
|
|
|
char topicnameJSONin[128];
|
|
|
|
|
2019-09-01 06:43:00 +02:00
|
|
|
#ifdef USE_LOCAL_MQTTSTRINGS
|
|
|
|
char mqttHost[128];
|
|
|
|
char mqttUser[32];
|
|
|
|
char mqttPass[32];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_RTOS_MQTTTIMER
|
|
|
|
TimerHandle_t mqttReconnectTimer = NULL;
|
|
|
|
#else
|
|
|
|
unsigned long mqttReconnect = 0;
|
|
|
|
#endif
|
|
|
|
|
2019-08-31 09:34:56 +02:00
|
|
|
void connectToMqtt() {
|
2019-09-01 06:43:00 +02:00
|
|
|
#ifdef USE_RTOS_MQTTTIMER
|
|
|
|
xTimerStop(mqttReconnectTimer, 0);
|
|
|
|
#else
|
|
|
|
mqttReconnect = 0;
|
|
|
|
#endif
|
|
|
|
if(!MQTTclient.connected()) {
|
|
|
|
DebugPort.println("MQTT: Connecting...");
|
|
|
|
if(NVstore.getMQTTinfo().enabled) {
|
|
|
|
MQTTclient.connect();
|
|
|
|
}
|
|
|
|
}
|
2019-08-31 09:34:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void onMqttConnect(bool sessionPresent)
|
|
|
|
{
|
2019-09-01 06:43:00 +02:00
|
|
|
#ifdef USE_RTOS_MQTTTIMER
|
|
|
|
xTimerStop(mqttReconnectTimer, 0);
|
|
|
|
#else
|
|
|
|
mqttReconnect = 0;
|
|
|
|
#endif
|
|
|
|
|
2019-08-31 09:34:56 +02:00
|
|
|
DebugPort.println("MQTT: Connected to broker.");
|
|
|
|
// DebugPort.printf("Session present: %d\r\n", sessionPresent);
|
|
|
|
|
2019-09-01 06:43:00 +02:00
|
|
|
// create the topicname we use to accept incoming JSON
|
|
|
|
DebugPort.printf("MQTT: base topic name \"%s\"\r\n", NVstore.getMQTTinfo().topic);
|
|
|
|
sprintf(topicnameJSONin, "%s/JSONin", NVstore.getMQTTinfo().topic);
|
|
|
|
// subscribe to that topic
|
2019-08-31 09:34:56 +02:00
|
|
|
DebugPort.printf("MQTT: Subscribing to \"%s\"\r\n", topicnameJSONin);
|
2019-09-01 06:43:00 +02:00
|
|
|
MQTTclient.subscribe(topicnameJSONin, NVstore.getMQTTinfo().qos);
|
|
|
|
|
2019-08-31 09:34:56 +02:00
|
|
|
// spit out an "I'm here" message
|
2019-09-01 06:43:00 +02:00
|
|
|
char lcltopic[128];
|
|
|
|
sprintf(lcltopic, "%s/Status", NVstore.getMQTTinfo().topic);
|
|
|
|
MQTTclient.publish(lcltopic, NVstore.getMQTTinfo().qos, true, "onMqttConnect");
|
2019-08-31 09:34:56 +02:00
|
|
|
|
|
|
|
#ifdef MQTT_DBG_LOOPBACK
|
|
|
|
// testo - loopback
|
2019-09-01 06:43:00 +02:00
|
|
|
sprintflcl(topic, "%s/JSONout", NVstore.getMQTTinfo().topic);
|
|
|
|
MQTTclient.subscribe(lcltopic, NVstore.getMQTTinfo().qos);
|
2019-08-31 09:34:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
resetJSONmoderator();
|
|
|
|
}
|
|
|
|
|
|
|
|
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total)
|
|
|
|
{
|
|
|
|
// handle message arrived
|
|
|
|
DebugPort.printf("MQTT: onMessage %s ", topic);
|
|
|
|
#ifdef MQTT_DBG_RAWBYTES
|
|
|
|
for(int i=0; i<len; i++) {
|
2019-08-27 15:09:07 +02:00
|
|
|
DebugPort.printf("0x%02X ", payload[i]);
|
|
|
|
}
|
|
|
|
DebugPort.println();
|
2019-08-31 09:34:56 +02:00
|
|
|
#endif
|
|
|
|
// string may not neccesarily be null terminated, make sure it is
|
|
|
|
char tidyString[1024];
|
|
|
|
int maxlen = sizeof(tidyString)-1;
|
|
|
|
int lenLimit = len < maxlen ? len : maxlen;
|
|
|
|
strncpy(tidyString, (char*)payload, lenLimit);
|
|
|
|
tidyString[lenLimit] = 0;
|
|
|
|
DebugPort.println(tidyString);
|
|
|
|
|
|
|
|
if(strcmp(topic, topicnameJSONin) == 0) { // check if incoming topic is our JSONin topic
|
|
|
|
interpretJsonCommand(tidyString);
|
|
|
|
}
|
2019-05-15 12:10:12 +02:00
|
|
|
}
|
|
|
|
|
2019-08-31 09:34:56 +02:00
|
|
|
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
2019-09-01 06:43:00 +02:00
|
|
|
DebugPort.print("MQTT: Disconnected, reason: ");
|
|
|
|
// ref: DisconnectReasons.hpp
|
|
|
|
switch(reason) {
|
|
|
|
case AsyncMqttClientDisconnectReason::TCP_DISCONNECTED: DebugPort.println("TCP disconnected"); break;
|
|
|
|
case AsyncMqttClientDisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION: DebugPort.println("protocol version"); break;
|
|
|
|
case AsyncMqttClientDisconnectReason::MQTT_IDENTIFIER_REJECTED: DebugPort.println("Identifier rejected"); break;
|
|
|
|
case AsyncMqttClientDisconnectReason::MQTT_SERVER_UNAVAILABLE: DebugPort.println("Server unavailable"); break;
|
|
|
|
case AsyncMqttClientDisconnectReason::MQTT_MALFORMED_CREDENTIALS: DebugPort.println("Malformed credentials"); break;
|
|
|
|
case AsyncMqttClientDisconnectReason::MQTT_NOT_AUTHORIZED: DebugPort.println("No authorised"); break;
|
|
|
|
case AsyncMqttClientDisconnectReason::ESP8266_NOT_ENOUGH_SPACE: DebugPort.println("Not enough space"); break;
|
|
|
|
case AsyncMqttClientDisconnectReason::TLS_BAD_FINGERPRINT: DebugPort.println("Bad TLS fingerprint"); break;
|
|
|
|
}
|
2019-08-27 15:09:07 +02:00
|
|
|
|
2019-09-01 06:43:00 +02:00
|
|
|
if (WiFi.isConnected()) {
|
|
|
|
if(NVstore.getMQTTinfo().enabled) {
|
|
|
|
#ifdef USE_RTOS_MQTTTIMER
|
|
|
|
xTimerStart(mqttReconnectTimer, 0);
|
|
|
|
#else
|
|
|
|
mqttReconnect = millis() + 5000;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 13:12:29 +02:00
|
|
|
}
|
2019-08-27 15:09:07 +02:00
|
|
|
|
2019-08-31 09:34:56 +02:00
|
|
|
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
|
|
|
|
DebugPort.println("MQTT: Subscribe acknowledged.");
|
|
|
|
// DebugPort.printf(" packetId: %d\r\n", packetId);
|
|
|
|
// DebugPort.printf(" qos: %d\r\n", qos);
|
|
|
|
}
|
2019-08-27 15:09:07 +02:00
|
|
|
|
2019-08-31 09:34:56 +02:00
|
|
|
bool mqttInit()
|
|
|
|
{
|
2019-09-01 06:43:00 +02:00
|
|
|
#ifdef USE_RTOS_MQTTTIMER
|
|
|
|
if(mqttReconnectTimer==NULL)
|
|
|
|
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
|
|
|
|
#else
|
|
|
|
mqttReconnect = 0;
|
|
|
|
#endif
|
2019-08-27 15:09:07 +02:00
|
|
|
|
2019-08-31 09:34:56 +02:00
|
|
|
memset(topicnameJSONin, 0, sizeof(topicnameJSONin));
|
2019-09-01 06:43:00 +02:00
|
|
|
|
|
|
|
MQTTclient.disconnect(true);
|
|
|
|
long escape = millis() + 10000;
|
|
|
|
while(MQTTclient.connected()) {
|
|
|
|
long tDelta = millis()-escape;
|
|
|
|
if(tDelta > 0) {
|
|
|
|
DebugPort.println("MQTT: TIMEOUT waiting for broker disconnect");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 09:34:56 +02:00
|
|
|
const sMQTTparams params = NVstore.getMQTTinfo();
|
|
|
|
if(params.enabled) {
|
2019-09-01 06:43:00 +02:00
|
|
|
#ifdef USE_LOCAL_MQTTSTRINGS
|
|
|
|
strncpy(mqttHost, params.host, 127);
|
|
|
|
strncpy(mqttUser, params.username, 31);
|
|
|
|
strncpy(mqttPass, params.password, 31);
|
|
|
|
mqttHost[127] = 0;
|
|
|
|
mqttUser[31] = 0;
|
|
|
|
mqttPass[31] = 0;
|
|
|
|
DebugPort.printf("MQTT: setting broker to %s:%d\r\n", mqttHost, params.port);
|
|
|
|
DebugPort.printf("MQTT: %s/%s\r\n", mqttUser, mqttPass);
|
|
|
|
MQTTclient.setServer(mqttHost, params.port);
|
|
|
|
MQTTclient.setCredentials(mqttUser, mqttPass);
|
|
|
|
#else
|
|
|
|
// the client only stores a pointer - this must not be a volatile memory location!
|
|
|
|
// - NO STACK vars!!!
|
|
|
|
DebugPort.printf("MQTT: setting broker to %s:%d\r\n", NVstore.getMQTTinfo().host, NVstore.getMQTTinfo().port);
|
|
|
|
MQTTclient.setServer(NVstore.getMQTTinfo().host, NVstore.getMQTTinfo().port);
|
|
|
|
DebugPort.printf("MQTT: %s/%s\r\n", NVstore.getMQTTinfo().username, NVstore.getMQTTinfo().password);
|
|
|
|
MQTTclient.setCredentials(NVstore.getMQTTinfo().username, NVstore.getMQTTinfo().password);
|
|
|
|
#endif
|
|
|
|
static bool setCallbacks = false;
|
|
|
|
// callbacks should only be added once (vector of callbacks in client!)
|
|
|
|
if(!setCallbacks) {
|
|
|
|
MQTTclient.onConnect(onMqttConnect);
|
|
|
|
MQTTclient.onMessage(onMqttMessage);
|
|
|
|
MQTTclient.onDisconnect(onMqttDisconnect);
|
|
|
|
MQTTclient.onSubscribe(onMqttSubscribe);
|
|
|
|
setCallbacks = true;
|
2019-08-27 15:09:07 +02:00
|
|
|
}
|
2019-09-01 06:43:00 +02:00
|
|
|
// connection takes pplace via delayed start method
|
|
|
|
return true;
|
2019-08-27 15:09:07 +02:00
|
|
|
}
|
2019-08-31 09:34:56 +02:00
|
|
|
return false;
|
2019-08-27 15:09:07 +02:00
|
|
|
}
|
|
|
|
|
2019-08-31 09:34:56 +02:00
|
|
|
bool mqttPublishJSON(const char* str)
|
2019-08-27 15:09:07 +02:00
|
|
|
{
|
2019-08-31 09:34:56 +02:00
|
|
|
if(MQTTclient.connected()) {
|
|
|
|
const sMQTTparams params = NVstore.getMQTTinfo();
|
|
|
|
char topic[128];
|
|
|
|
sprintf(topic, "%s/JSONout", params.topic);
|
|
|
|
MQTTclient.publish(topic, params.qos, false, str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-08-27 15:09:07 +02:00
|
|
|
}
|
|
|
|
|
2019-09-01 06:43:00 +02:00
|
|
|
void kickMQTT() {
|
|
|
|
if (WiFi.isConnected()) {
|
|
|
|
if(NVstore.getMQTTinfo().enabled) {
|
|
|
|
#ifdef USE_RTOS_MQTTTIMER
|
|
|
|
xTimerStart(mqttReconnectTimer, 0);
|
|
|
|
#else
|
|
|
|
mqttReconnect = millis() + 5000;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void doMQTT()
|
|
|
|
{
|
|
|
|
// most MQTT is managed via callbacks!!!
|
|
|
|
if(NVstore.getMQTTinfo().enabled) {
|
|
|
|
#ifndef USE_RTOS_MQTTTIMER
|
|
|
|
if(mqttReconnect) {
|
|
|
|
long tDelta = millis() - mqttReconnect;
|
|
|
|
if(tDelta > 0) {
|
|
|
|
mqttReconnect = 0;
|
|
|
|
connectToMqtt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_RTOS_MQTTTIMER
|
|
|
|
if (!MQTTclient.connected() && WiFi.isConnected() && !xTimerIsTimerActive(mqttReconnectTimer)) {
|
|
|
|
xTimerStart(mqttReconnectTimer, 0);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (!MQTTclient.connected() && WiFi.isConnected() && mqttReconnect==0) {
|
|
|
|
mqttReconnect = millis() + 5000;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-08-31 09:34:56 +02:00
|
|
|
|
2019-09-04 12:59:01 +02:00
|
|
|
bool isMQTTconnected() {
|
|
|
|
return MQTTclient.connected();
|
|
|
|
}
|
|
|
|
|
2019-05-16 13:12:29 +02:00
|
|
|
#endif
|