Browse Source

change to asyncmqttclient

main
Hendrik Langer 7 years ago
parent
commit
37285dd744
  1. 3
      software/platformio.ini
  2. 71
      software/src/MyMQTT.cpp
  3. 15
      software/src/MyMQTT.h
  4. 7
      software/src/hardware.h
  5. 4
      software/src/main.cpp

3
software/platformio.ini

@ -23,6 +23,5 @@ lib_deps =
; FastLED ; FastLED
https://github.com/samguyer/FastLED.git https://github.com/samguyer/FastLED.git
; AsyncMqttClient ; AsyncMqttClient
; https://github.com/marvinroger/async-mqtt-client.git https://github.com/marvinroger/async-mqtt-client.git
MQTT
lib_ignore = ESPAsyncTCP lib_ignore = ESPAsyncTCP

71
software/src/MyMQTT.cpp

@ -1,55 +1,78 @@
#include "MyMQTT.h" #include "MyMQTT.h"
#include <MQTTClient.h> #include <AsyncMqttClient.h>
#include "hardware.h" #include "hardware.h"
MyMQTT::MyMQTT(void) MyMQTT::MyMQTT(void)
: host {mqtt_server}, : host {mqtt_server},
port {mqtt_port}, port {mqtt_port},
endpoint {mqtt_endpoint},
username {mqtt_username}, username {mqtt_username},
password {mqtt_password} password {mqtt_password}
{} {}
static void onMqttMessage(String &topic, String &payload) { extern "C" {
Serial.println("incoming: " + topic + " - " + payload); static void onMqttMessageWrapper(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
// static_cast<MyMQTT*>(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<MyMQTT*>(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) { void MyMQTT::connect(void) {
mqtt_ptr = this;
Serial.print("MQTT connecting"); Serial.print("MQTT connecting");
mqttClient.begin(host, port, net); mqttClient.onConnect(&onMqttConnectWrapper);
mqttClient.onMessage(&onMqttMessage); mqttClient.onSubscribe(&onMqttSubscribe);
while (!mqttClient.connect(host, username, password)) { 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("."); Serial.print(".");
delay(500); delay(500);
} }
Serial.println("MQTT connected");
subscribe();
}
void MyMQTT::loop(void) { Serial.println("MQTT connected");
auto available = (size_t)net.available();
if (available > 0) {
Serial.println("data available\n");
}
mqttClient.loop();
if(!mqttClient.connected()) {
Serial.println("MQTT connection lost.");
connect();
}
} }
void MyMQTT::subscribe(void) { void MyMQTT::subscribe(void) {
mqttClient.subscribe("sensor/vendingmachine/dispense"); uint16_t dispenseIdSub = mqttClient.subscribe("sensor/vendingmachine/dispense", 0);
// mqttClient.subscribe("sensor/vendingmachine/light"); uint16_t dispenseIdSub2 = mqttClient.subscribe("sensor/esp100/#", 0);
mqttClient.publish("sensor/vendingmachine/alive", 0, true, "test");
} }
void MyMQTT::publish(char* topic, char* payload) { void MyMQTT::publish(char* topic, char* payload) {
mqttClient.publish(topic, payload);
} }
void MyMQTT::publish(char* topic, String payload) { void MyMQTT::publish(char* topic, String payload) {
mqttClient.publish(topic, payload);
} }

15
software/src/MyMQTT.h

@ -1,8 +1,7 @@
#ifndef _MYMQTT_H #ifndef _MYMQTT_H
#define _MYMQTT_H #define _MYMQTT_H
#include <WiFiClientSecure.h> #include <AsyncMqttClient.h>
#include <MQTTClient.h>
#include "config.h" #include "config.h"
@ -10,19 +9,23 @@ class MyMQTT {
public: public:
MyMQTT(void); MyMQTT(void);
void connect(void); void connect(void);
void loop(void);
void subscribe(void); void subscribe(void);
void publish(char* topic, char* payload); void publish(char* topic, char* payload);
void publish(char* topic, String 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: private:
const char* host; const char* host;
const uint32_t port; const uint32_t port;
const char* endpoint; const char* endpoint;
const char* username; const char* username;
const char* password; const char* password;
WiFiClientSecure net; AsyncMqttClient mqttClient;
MQTTClient mqttClient; // static MyMQTT* mqtt_ptr;
}; };
static MyMQTT* mqtt_ptr = nullptr;
#endif /* _MYMQTT_H */ #endif /* _MYMQTT_H */

7
software/src/hardware.h

@ -13,11 +13,10 @@ static constexpr uint8_t NUM_LEDS = 22;
#define LED_TYPE TM1829 #define LED_TYPE TM1829
#define COLOR_ORDER BRG #define COLOR_ORDER BRG
static const char* mqtt_server = "home.xd0.de"; static const char* mqtt_server = "172.16.75.18";
static constexpr uint32_t mqtt_port = 8883; static constexpr uint32_t mqtt_port = 1883;
static constexpr bool MQTT_SECURE = true; static constexpr bool MQTT_SECURE = true;
static const char* mqtt_endpoint = "sensor/vendingmachine/"; static const char* mqtt_username = "esp100";
static const char* mqtt_username = "esp32";
static const char* mqtt_password = "password"; static const char* mqtt_password = "password";
#endif /* _HARDWARE_H */ #endif /* _HARDWARE_H */

4
software/src/main.cpp

@ -48,8 +48,6 @@ void setup()
void loop() void loop()
{ {
mqtt.loop();
/*
// turn the LED on (HIGH is the voltage level) // turn the LED on (HIGH is the voltage level)
digitalWrite(LED_BUILTIN, HIGH); digitalWrite(LED_BUILTIN, HIGH);
// wait for a second // wait for a second
@ -58,7 +56,7 @@ void loop()
digitalWrite(LED_BUILTIN, LOW); digitalWrite(LED_BUILTIN, LOW);
// wait for a second // wait for a second
delay(1000); delay(1000);
*/
delay(100); delay(100);
} }

Loading…
Cancel
Save