From 37285dd744200988c5d21fef5d0a2603bdc5d78a Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Sat, 6 Jan 2018 11:06:54 +0100 Subject: [PATCH] change to asyncmqttclient --- software/platformio.ini | 3 +- software/src/MyMQTT.cpp | 71 +++++++++++++++++++++++++++-------------- software/src/MyMQTT.h | 15 +++++---- software/src/hardware.h | 7 ++-- software/src/main.cpp | 4 +-- 5 files changed, 61 insertions(+), 39 deletions(-) diff --git a/software/platformio.ini b/software/platformio.ini index 47225ee..7337fff 100644 --- a/software/platformio.ini +++ b/software/platformio.ini @@ -23,6 +23,5 @@ lib_deps = ; FastLED https://github.com/samguyer/FastLED.git ; AsyncMqttClient -; https://github.com/marvinroger/async-mqtt-client.git - MQTT + https://github.com/marvinroger/async-mqtt-client.git lib_ignore = ESPAsyncTCP diff --git a/software/src/MyMQTT.cpp b/software/src/MyMQTT.cpp index 445f5c7..56a05d6 100644 --- a/software/src/MyMQTT.cpp +++ b/software/src/MyMQTT.cpp @@ -1,55 +1,78 @@ #include "MyMQTT.h" -#include +#include #include "hardware.h" MyMQTT::MyMQTT(void) : host {mqtt_server}, port {mqtt_port}, - endpoint {mqtt_endpoint}, username {mqtt_username}, password {mqtt_password} {} -static void onMqttMessage(String &topic, String &payload) { - Serial.println("incoming: " + topic + " - " + payload); +extern "C" { +static void onMqttMessageWrapper(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { +// static_cast(parm)->onMqttMessage(NULL); + Serial.println("test"); + mqtt_ptr->onMqttMessage(topic, payload, properties, len, index, total); +} + +static void onMqttSubscribe(uint16_t packetId, uint8_t qos) { + Serial.println("subscribe ack"); +} +} + +void MyMQTT::onMqttConnectWrapper(bool sessionPresent) { +// static_cast(parm)->onMqttConnect(NULL); + mqtt_ptr->onMqttConnect(sessionPresent); +} + +void MyMQTT::onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { + Serial.print("incoming: "); + Serial.println(topic); + Serial.println(payload); +} + +void MyMQTT::onMqttConnect(bool sessionPresent) { + Serial.println("CONNECTED! callback works"); + subscribe(); } void MyMQTT::connect(void) { + mqtt_ptr = this; Serial.print("MQTT connecting"); - mqttClient.begin(host, port, net); - mqttClient.onMessage(&onMqttMessage); - while (!mqttClient.connect(host, username, password)) { + mqttClient.onConnect(&onMqttConnectWrapper); + mqttClient.onSubscribe(&onMqttSubscribe); + mqttClient.onMessage(&onMqttMessageWrapper); + + mqttClient.setServer(host, port); + mqttClient.setCredentials(username, password); +//#if ASYNC_TCP_SSL_ENABLED +// mqttClient.setSecure(true); +//#endif + + mqttClient.connect(); + + while(!mqttClient.connected()) { Serial.print("."); delay(500); } - Serial.println("MQTT connected"); - subscribe(); -} -void MyMQTT::loop(void) { - auto available = (size_t)net.available(); - if (available > 0) { - Serial.println("data available\n"); - } - mqttClient.loop(); + Serial.println("MQTT connected"); - if(!mqttClient.connected()) { - Serial.println("MQTT connection lost."); - connect(); - } } void MyMQTT::subscribe(void) { - mqttClient.subscribe("sensor/vendingmachine/dispense"); -// mqttClient.subscribe("sensor/vendingmachine/light"); + uint16_t dispenseIdSub = mqttClient.subscribe("sensor/vendingmachine/dispense", 0); + uint16_t dispenseIdSub2 = mqttClient.subscribe("sensor/esp100/#", 0); + mqttClient.publish("sensor/vendingmachine/alive", 0, true, "test"); } void MyMQTT::publish(char* topic, char* payload) { - mqttClient.publish(topic, payload); + } void MyMQTT::publish(char* topic, String payload) { - mqttClient.publish(topic, payload); + } diff --git a/software/src/MyMQTT.h b/software/src/MyMQTT.h index 69ce73d..c98a2fd 100644 --- a/software/src/MyMQTT.h +++ b/software/src/MyMQTT.h @@ -1,8 +1,7 @@ #ifndef _MYMQTT_H #define _MYMQTT_H -#include -#include +#include #include "config.h" @@ -10,19 +9,23 @@ class MyMQTT { public: MyMQTT(void); void connect(void); - void loop(void); void subscribe(void); void publish(char* topic, char* payload); void publish(char* topic, String payload); -// static void onMqttMessage(String &topic, String &payload); + void onMqttConnect(bool sessionPresent); + void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); +// static void onMqttMessageWrapper(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); + static void onMqttConnectWrapper(bool sessionPresent); private: const char* host; const uint32_t port; const char* endpoint; const char* username; const char* password; - WiFiClientSecure net; - MQTTClient mqttClient; + AsyncMqttClient mqttClient; +// static MyMQTT* mqtt_ptr; }; +static MyMQTT* mqtt_ptr = nullptr; + #endif /* _MYMQTT_H */ diff --git a/software/src/hardware.h b/software/src/hardware.h index 399c288..0a7f26e 100644 --- a/software/src/hardware.h +++ b/software/src/hardware.h @@ -13,11 +13,10 @@ static constexpr uint8_t NUM_LEDS = 22; #define LED_TYPE TM1829 #define COLOR_ORDER BRG -static const char* mqtt_server = "home.xd0.de"; -static constexpr uint32_t mqtt_port = 8883; +static const char* mqtt_server = "172.16.75.18"; +static constexpr uint32_t mqtt_port = 1883; static constexpr bool MQTT_SECURE = true; -static const char* mqtt_endpoint = "sensor/vendingmachine/"; -static const char* mqtt_username = "esp32"; +static const char* mqtt_username = "esp100"; static const char* mqtt_password = "password"; #endif /* _HARDWARE_H */ diff --git a/software/src/main.cpp b/software/src/main.cpp index 28e3cde..81ea71e 100644 --- a/software/src/main.cpp +++ b/software/src/main.cpp @@ -48,8 +48,6 @@ void setup() void loop() { - mqtt.loop(); -/* // turn the LED on (HIGH is the voltage level) digitalWrite(LED_BUILTIN, HIGH); // wait for a second @@ -58,7 +56,7 @@ void loop() digitalWrite(LED_BUILTIN, LOW); // wait for a second delay(1000); -*/ + delay(100); }