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
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

71
software/src/MyMQTT.cpp

@ -1,55 +1,78 @@
#include "MyMQTT.h"
#include <MQTTClient.h>
#include <AsyncMqttClient.h>
#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<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) {
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);
}

15
software/src/MyMQTT.h

@ -1,8 +1,7 @@
#ifndef _MYMQTT_H
#define _MYMQTT_H
#include <WiFiClientSecure.h>
#include <MQTTClient.h>
#include <AsyncMqttClient.h>
#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 */

7
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 */

4
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);
}

Loading…
Cancel
Save