2019-05-15 10:10:12 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
2019-08-27 13:09:07 +00:00
|
|
|
#include "../cfg/BTCConfig.h"
|
|
|
|
|
|
|
|
#ifdef USE_MQTT
|
2019-07-07 07:18:38 +00:00
|
|
|
#include <Arduino.h>
|
2019-05-15 10:10:12 +00:00
|
|
|
#include "ABMqtt.h"
|
2019-07-25 07:40:23 +00:00
|
|
|
#include "../../lib/PubSubClient/src/PubSubClient.h"
|
2019-07-07 07:18:38 +00:00
|
|
|
#include "BTCWifi.h"
|
|
|
|
#include "BTCWebServer.h"
|
2019-08-27 13:09:07 +00:00
|
|
|
#include "../Utility/DebugPort.h"
|
2019-05-15 10:10:12 +00:00
|
|
|
|
2019-08-27 13:09:07 +00:00
|
|
|
// Update these with values suitable for your network.
|
|
|
|
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
|
|
|
|
IPAddress ip(172, 16, 0, 100);
|
|
|
|
IPAddress MQTTserver(5, 196, 95, 208);
|
2019-05-15 10:10:12 +00:00
|
|
|
|
|
|
|
void MqttCallback(char* topic, byte* payload, unsigned int length) {
|
2019-08-27 13:09:07 +00:00
|
|
|
DebugPort.printf("MQTT callback %s ", topic);
|
|
|
|
for(int i=0; i<length; i++) {
|
|
|
|
DebugPort.printf("0x%02X ", payload[i]);
|
|
|
|
}
|
|
|
|
DebugPort.println();
|
|
|
|
char test[256];
|
|
|
|
int len = length < 256 ? length : 255;
|
|
|
|
strncpy(test, (char*)payload, len);
|
|
|
|
test[len];
|
|
|
|
DebugPort.println(test);
|
2019-05-15 10:10:12 +00:00
|
|
|
// handle message arrived
|
|
|
|
}
|
|
|
|
|
2019-08-27 13:09:07 +00:00
|
|
|
WiFiClient espClient;
|
|
|
|
PubSubClient MQTTclient(espClient);
|
|
|
|
|
2019-05-15 10:10:12 +00:00
|
|
|
void MqttSetup() {
|
2019-08-27 13:09:07 +00:00
|
|
|
MQTTclient.setServer(MQTTserver, 1883);
|
|
|
|
MQTTclient.setCallback(MqttCallback);
|
2019-05-16 11:12:29 +00:00
|
|
|
}
|
2019-08-27 13:09:07 +00:00
|
|
|
|
|
|
|
bool reconnect() {
|
|
|
|
static unsigned long HoldOff = 0;
|
|
|
|
|
|
|
|
if(HoldOff) {
|
|
|
|
long tDelta = millis() - HoldOff;
|
|
|
|
if(tDelta > 0)
|
|
|
|
HoldOff = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MQTTclient.connected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void doMQTT()
|
|
|
|
{
|
|
|
|
if(reconnect())
|
|
|
|
MQTTclient.loop();
|
|
|
|
}
|
|
|
|
|
2019-05-16 11:12:29 +00:00
|
|
|
#endif
|