Got basic MQTT session working to test.mosquito.org :-)
This commit is contained in:
parent
c79522233c
commit
f6f721bd8a
|
@ -85,7 +85,7 @@
|
||||||
This example code is in the public domain.
|
This example code is in the public domain.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//#include "src/WiFi/ABMqtt.h"
|
#include "WiFi/ABMqtt.h"
|
||||||
#include "cfg/BTCConfig.h"
|
#include "cfg/BTCConfig.h"
|
||||||
#include "cfg/pins.h"
|
#include "cfg/pins.h"
|
||||||
#include "RTC/Timers.h"
|
#include "RTC/Timers.h"
|
||||||
|
@ -410,6 +410,9 @@ void setup() {
|
||||||
#if USE_WEBSERVER == 1
|
#if USE_WEBSERVER == 1
|
||||||
initWebServer();
|
initWebServer();
|
||||||
#endif // USE_WEBSERVER
|
#endif // USE_WEBSERVER
|
||||||
|
#if USE_MQTT == 1
|
||||||
|
MqttSetup();
|
||||||
|
#endif // USE_MQTT
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // USE_WIFI
|
#endif // USE_WIFI
|
||||||
|
@ -1621,6 +1624,10 @@ void doStreaming()
|
||||||
#if USE_WEBSERVER == 1
|
#if USE_WEBSERVER == 1
|
||||||
bHaveWebClient = doWebServer();
|
bHaveWebClient = doWebServer();
|
||||||
#endif //USE_WEBSERVER
|
#endif //USE_WEBSERVER
|
||||||
|
#if USE_MQTT == 1
|
||||||
|
doMQTT();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#endif // USE_WIFI
|
#endif // USE_WIFI
|
||||||
|
|
||||||
|
|
|
@ -81,6 +81,9 @@ CGPIOInfoScreen::animate()
|
||||||
_drawBitmap(23, 14, StartIconInfo);
|
_drawBitmap(23, 14, StartIconInfo);
|
||||||
_drawBitmap(30, 14, StopIconInfo);
|
_drawBitmap(30, 14, StopIconInfo);
|
||||||
break;
|
break;
|
||||||
|
case CGPIOin1::Stop:
|
||||||
|
_drawBitmap(23, 14, StopIconInfo);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
_drawBitmap(40, 16, GPIOin.getState(0) ? CloseIconInfo : OpenIconInfo);
|
_drawBitmap(40, 16, GPIOin.getState(0) ? CloseIconInfo : OpenIconInfo);
|
||||||
|
|
||||||
|
|
|
@ -216,6 +216,7 @@ CGPIOSetupScreen::animate()
|
||||||
case CGPIOin1::Start: pMsg = " Input 1: Starts heater upon closure. "; break;
|
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::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::StartStop: pMsg = " Input 1: Starts or Stops heater upon closure. "; break;
|
||||||
|
case CGPIOin1::Stop: pMsg = " Input 1: Stops heater upon closure. "; break;
|
||||||
}
|
}
|
||||||
if(pMsg)
|
if(pMsg)
|
||||||
_scrollMessage(56, pMsg, _scrollChar);
|
_scrollMessage(56, pMsg, _scrollChar);
|
||||||
|
|
|
@ -2,32 +2,76 @@
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
|
|
||||||
#ifdef USEMQTT
|
#include "../cfg/BTCConfig.h"
|
||||||
|
|
||||||
|
#ifdef USE_MQTT
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "ABMqtt.h"
|
#include "ABMqtt.h"
|
||||||
#include "../../lib/PubSubClient/src/PubSubClient.h"
|
#include "../../lib/PubSubClient/src/PubSubClient.h"
|
||||||
#include "BTCWifi.h"
|
#include "BTCWifi.h"
|
||||||
#include "BTCWebServer.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) {
|
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
|
// handle message arrived
|
||||||
}
|
}
|
||||||
|
|
||||||
void MqttSetup() {
|
|
||||||
WiFiClient espClient;
|
WiFiClient espClient;
|
||||||
PubSubClient client(espClient);
|
PubSubClient MQTTclient(espClient);
|
||||||
|
|
||||||
long lastReconnectAttempt = 0;
|
void MqttSetup() {
|
||||||
|
MQTTclient.setServer(MQTTserver, 1883);
|
||||||
|
MQTTclient.setCallback(MqttCallback);
|
||||||
|
}
|
||||||
|
|
||||||
boolean reconnect() {
|
bool reconnect() {
|
||||||
if (client.connect("arduinoClient")) {
|
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...
|
// Once connected, publish an announcement...
|
||||||
client.publish("outTopic", "hello world");
|
MQTTclient.publish("Afterburner/test", "hello world");
|
||||||
// ... and resubscribe
|
// ... and resubscribe
|
||||||
client.subscribe("inTopic");
|
// MQTTclient.subscribe("inTopic");
|
||||||
|
// MQTTclient.subscribe("Test/Test/Test/Test");
|
||||||
|
MQTTclient.subscribe("Afterburner");
|
||||||
|
MQTTclient.publish("Afterburner/test", "the end is nigh");
|
||||||
}
|
}
|
||||||
return client.connected();
|
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
|
#endif
|
|
@ -5,12 +5,13 @@
|
||||||
|
|
||||||
|
|
||||||
//#include "../../lib/PubSubClient/src/PubSubClient.h"
|
//#include "../../lib/PubSubClient/src/PubSubClient.h"
|
||||||
// #include "BTCWifi.h""
|
#include "BTCWifi.h"
|
||||||
// #include "BTCWebServer.h"
|
#include "BTCWebServer.h"
|
||||||
|
|
||||||
|
|
||||||
void MqttCallback(char* topic, byte* payload, unsigned int length);
|
void MqttCallback(char* topic, byte* payload, unsigned int length);
|
||||||
void MqttSetup();
|
void MqttSetup();
|
||||||
|
void doMQTT();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,7 @@
|
||||||
#define USE_WIFI 1
|
#define USE_WIFI 1
|
||||||
#define USE_OTA 1
|
#define USE_OTA 1
|
||||||
#define USE_WEBSERVER 1
|
#define USE_WEBSERVER 1
|
||||||
|
#define USE_MQTT 1
|
||||||
|
|
||||||
#define USE_PORTAL_TRIGGER_PIN 0
|
#define USE_PORTAL_TRIGGER_PIN 0
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue