MQTT: add client code
This commit is contained in:
parent
47f10f54dd
commit
f81326f3ef
|
|
@ -5,3 +5,7 @@
|
|||
|
||||
#define WIFI_SSID "Wifi_SSID"
|
||||
#define WIFI_PASSWORD "secretPassword"
|
||||
|
||||
#define MQTT_SERVER "mqttserver.fritz.box"
|
||||
#define MQTT_USER "user"
|
||||
#define MQTT_PASSWORD "password"
|
||||
|
|
|
|||
|
|
@ -14,8 +14,9 @@ board = esp32dev
|
|||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
lib_deps =
|
||||
witnessmenow/UniversalTelegramBot @ ^1.3.0
|
||||
adafruit/Adafruit GFX Library @ 1.12.3
|
||||
witnessmenow/UniversalTelegramBot @ ~1.3.0
|
||||
adafruit/Adafruit GFX Library @ ~1.12.3
|
||||
knolleary/PubSubClient @ ~2.8
|
||||
build_flags=-DTELEGRAM_DEBUG
|
||||
; OTA update path: hostname is flipdottelegram.fritz.box
|
||||
upload_protocol = espota
|
||||
|
|
|
|||
105
src/main.cpp
105
src/main.cpp
|
|
@ -3,6 +3,8 @@
|
|||
#include <WiFiClientSecure.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <time.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include "Telegram.h"
|
||||
#include "FlipDotDrv.h"
|
||||
|
|
@ -12,6 +14,26 @@
|
|||
String Text = "";
|
||||
const char *const HOSTNAME = "flipdottelegram";
|
||||
|
||||
/* ------------ MQTT variables -------------- */
|
||||
WiFiClient espClient;
|
||||
PubSubClient mqtt(espClient);
|
||||
#define FLIPDOT_AVAILABILITY_TOPIC "flipdotdisplay/available"
|
||||
#define FLIPDOT_SWITCH_TOPIC "flipdotdisplay/switch"
|
||||
#define FLIPDOT_TEXT_TOPIC "flipdotdisplay/text"
|
||||
void mqttCallback(char *topic, unsigned char *message, unsigned int length);
|
||||
|
||||
void publishOpen()
|
||||
{
|
||||
mqtt.publish(FLIPDOT_SWITCH_TOPIC "/state", "open");
|
||||
mqtt.publish(FLIPDOT_TEXT_TOPIC "/state", Text.c_str());
|
||||
}
|
||||
|
||||
void publishClose()
|
||||
{
|
||||
mqtt.publish(FLIPDOT_SWITCH_TOPIC "/state", "close");
|
||||
mqtt.publish(FLIPDOT_TEXT_TOPIC "/state", Text.c_str());
|
||||
}
|
||||
|
||||
/* ------------ OTA section and variables -------------- */
|
||||
bool OTAStarted;
|
||||
void onOTAStart()
|
||||
|
|
@ -109,6 +131,10 @@ void setup()
|
|||
/* setup ArduinoOTA */
|
||||
ArduinoOTA.setHostname(HOSTNAME);
|
||||
ArduinoOTA.onStart(onOTAStart);
|
||||
|
||||
/* prepare MQTT client */
|
||||
mqtt.setServer(MQTT_SERVER, 1883);
|
||||
mqtt.setCallback(mqttCallback);
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
|
@ -157,6 +183,21 @@ void loop()
|
|||
lastTelegramRefresh = now;
|
||||
tele.cyclic(now);
|
||||
}
|
||||
|
||||
/* handle MQTT transmissions */
|
||||
if (!mqtt.loop())
|
||||
{
|
||||
Serial.println("MQTT not connected");
|
||||
|
||||
// try to connect to MQTT broker again
|
||||
if (mqtt.connect(HOSTNAME, MQTT_USER, MQTT_PASSWORD, FLIPDOT_AVAILABILITY_TOPIC, 2, true, "offline"))
|
||||
{
|
||||
// publish availability message
|
||||
mqtt.subscribe(FLIPDOT_SWITCH_TOPIC);
|
||||
mqtt.subscribe(FLIPDOT_TEXT_TOPIC);
|
||||
mqtt.publish(FLIPDOT_AVAILABILITY_TOPIC, "available");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -170,6 +211,8 @@ void loop()
|
|||
|
||||
ArduinoOTA.end();
|
||||
|
||||
mqtt.disconnect();
|
||||
|
||||
oldWifiStatus = WiFi.status();
|
||||
}
|
||||
}
|
||||
|
|
@ -231,12 +274,12 @@ void loop()
|
|||
}
|
||||
|
||||
/* function to write text onto the canvas
|
||||
*
|
||||
*
|
||||
* @param text - C string to be printed
|
||||
* @param background - boolean switch if the background should be transparent (false) or black (true)
|
||||
* @param x - position in X direction to write the Text, -1 centers the text
|
||||
* @param y - position in Y direction to write the Text, -1 centers the text
|
||||
*
|
||||
*
|
||||
* */
|
||||
void displayText(const char *text, bool background, int16_t x, int16_t y)
|
||||
{
|
||||
|
|
@ -283,6 +326,7 @@ void displayText(const char *text, bool background, int16_t x, int16_t y)
|
|||
String printOpen(String Args)
|
||||
{
|
||||
Text = "offen";
|
||||
publishOpen();
|
||||
|
||||
return Text;
|
||||
}
|
||||
|
|
@ -290,6 +334,7 @@ String printOpen(String Args)
|
|||
String printClose(String Args)
|
||||
{
|
||||
Text = "leider zu";
|
||||
publishClose();
|
||||
|
||||
return Text;
|
||||
}
|
||||
|
|
@ -300,6 +345,62 @@ String printText(String Args)
|
|||
Serial.print(Args);
|
||||
Serial.println("'");
|
||||
Text = Args;
|
||||
publishClose();
|
||||
|
||||
return Text;
|
||||
}
|
||||
|
||||
void mqttCallback(char *topic, unsigned char *message, unsigned int length)
|
||||
{
|
||||
String MessageString;
|
||||
|
||||
/* Convert all char* to Strings for simpler handling */
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
MessageString += message[i];
|
||||
}
|
||||
String ToppicString(topic);
|
||||
|
||||
/* if topic is the switch topic check for on, off, open, close */
|
||||
if (ToppicString == FLIPDOT_SWITCH_TOPIC)
|
||||
{
|
||||
Serial.println("Switch topic received: " + MessageString);
|
||||
|
||||
if ((MessageString == "open") ||
|
||||
(MessageString == "on"))
|
||||
{
|
||||
printOpen(MessageString);
|
||||
}
|
||||
else if ((MessageString == "close") ||
|
||||
(MessageString == "off"))
|
||||
{
|
||||
printClose(MessageString);
|
||||
}
|
||||
}
|
||||
|
||||
/* if topic is the text topic try deserializing the json text */
|
||||
if (ToppicString == FLIPDOT_TEXT_TOPIC)
|
||||
{
|
||||
Serial.println("Text topic received: " + MessageString);
|
||||
JsonDocument doc;
|
||||
if (deserializeJson(doc, (const char *)message, length) == DeserializationError::Ok)
|
||||
{
|
||||
MessageString = doc["value"] | String("none"); // the pipe operand tells the default value, if "value" is not existing
|
||||
|
||||
if ((MessageString != "none"))
|
||||
{
|
||||
printText(MessageString);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(FLIPDOT_TEXT_TOPIC);
|
||||
Serial.println(" has no [\"value\"]!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(FLIPDOT_TEXT_TOPIC);
|
||||
Serial.println(" is no json!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user