Got basic MQTT session working to test.mosquito.org :-)

This commit is contained in:
Ray Jones 2019-08-27 23:09:07 +10:00
parent c79522233c
commit f6f721bd8a
6 changed files with 76 additions and 19 deletions

View file

@ -85,7 +85,7 @@
This example code is in the public domain.
*/
//#include "src/WiFi/ABMqtt.h"
#include "WiFi/ABMqtt.h"
#include "cfg/BTCConfig.h"
#include "cfg/pins.h"
#include "RTC/Timers.h"
@ -410,6 +410,9 @@ void setup() {
#if USE_WEBSERVER == 1
initWebServer();
#endif // USE_WEBSERVER
#if USE_MQTT == 1
MqttSetup();
#endif // USE_MQTT
}
#endif // USE_WIFI
@ -1621,6 +1624,10 @@ void doStreaming()
#if USE_WEBSERVER == 1
bHaveWebClient = doWebServer();
#endif //USE_WEBSERVER
#if USE_MQTT == 1
doMQTT();
#endif
#endif // USE_WIFI

View file

@ -81,6 +81,9 @@ CGPIOInfoScreen::animate()
_drawBitmap(23, 14, StartIconInfo);
_drawBitmap(30, 14, StopIconInfo);
break;
case CGPIOin1::Stop:
_drawBitmap(23, 14, StopIconInfo);
break;
}
_drawBitmap(40, 16, GPIOin.getState(0) ? CloseIconInfo : OpenIconInfo);

View file

@ -216,6 +216,7 @@ CGPIOSetupScreen::animate()
case CGPIOin1::Start: pMsg = " Input 1: Starts heater upon closure. "; break;
case CGPIOin1::Run: pMsg = " Input 1: Starts heater when held closed, stops when opened. "; break;
case CGPIOin1::StartStop: pMsg = " Input 1: Starts or Stops heater upon closure. "; break;
case CGPIOin1::Stop: pMsg = " Input 1: Stops heater upon closure. "; break;
}
if(pMsg)
_scrollMessage(56, pMsg, _scrollChar);

View file

@ -2,32 +2,76 @@
//
//
#ifdef USEMQTT
#include "../cfg/BTCConfig.h"
#ifdef USE_MQTT
#include <Arduino.h>
#include "ABMqtt.h"
#include "../../lib/PubSubClient/src/PubSubClient.h"
#include "BTCWifi.h"
#include "BTCWebServer.h"
#include "../Utility/DebugPort.h"
// 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);
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);
// handle message arrived
}
WiFiClient espClient;
PubSubClient MQTTclient(espClient);
void MqttSetup() {
WiFiClient espClient;
PubSubClient client(espClient);
long lastReconnectAttempt = 0;
boolean reconnect() {
if (client.connect("arduinoClient")) {
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
}
return client.connected();
}
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

View file

@ -4,13 +4,14 @@
#define _ABMQTT_h
// #include "../../lib/PubSubClient/src/PubSubClient.h"
// #include "BTCWifi.h""
// #include "BTCWebServer.h"
//#include "../../lib/PubSubClient/src/PubSubClient.h"
#include "BTCWifi.h"
#include "BTCWebServer.h"
void MqttCallback(char* topic, byte* payload, unsigned int length);
void MqttSetup();
void doMQTT();
#endif

View file

@ -42,6 +42,7 @@
#define USE_WIFI 1
#define USE_OTA 1
#define USE_WEBSERVER 1
#define USE_MQTT 1
#define USE_PORTAL_TRIGGER_PIN 0