ESP32_ChinaDieselHeater_Con.../src/WiFi/ABMqtt.cpp

77 lines
1.9 KiB
C++
Raw Normal View History

2019-05-15 10:10:12 +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"
#include "../../lib/PubSubClient/src/PubSubClient.h"
2019-07-07 07:18:38 +00:00
#include "BTCWifi.h"
#include "BTCWebServer.h"
#include "../Utility/DebugPort.h"
2019-05-15 10:10:12 +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) {
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
}
WiFiClient espClient;
PubSubClient MQTTclient(espClient);
2019-05-15 10:10:12 +00:00
void MqttSetup() {
MQTTclient.setServer(MQTTserver, 1883);
MQTTclient.setCallback(MqttCallback);
}
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();
}
#endif