|
|
|
#include "MyMQTT.h"
|
|
|
|
|
|
|
|
#include <AsyncMqttClient.h>
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
#include "hardware.h"
|
|
|
|
#include "shelf.h"
|
|
|
|
#include "wifi.h"
|
|
|
|
#include "led.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
extern Shelf* shelf;
|
|
|
|
extern Wifi wifi;
|
|
|
|
extern Led led;
|
|
|
|
|
|
|
|
MyMQTT::MyMQTT(void)
|
|
|
|
: host {mqtt_server},
|
|
|
|
port {mqtt_port},
|
|
|
|
username {mqtt_username},
|
|
|
|
password {mqtt_password}
|
|
|
|
{}
|
|
|
|
|
|
|
|
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::onMqttDisconnectWrapper(AsyncMqttClientDisconnectReason reason) {
|
|
|
|
// static_cast<MyMQTT*>(parm)->onMqttConnect(NULL);
|
|
|
|
mqtt_ptr->onMqttDisconnect(reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyMQTT::reconnectTask(void* parameters) {
|
|
|
|
while (!wifi.connected()) {
|
|
|
|
Serial.print(".");
|
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println("MQTT reconnecting!");
|
|
|
|
this->connect();
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyMQTT::cTaskWrapper(void* parameters) {
|
|
|
|
static_cast<MyMQTT*>(parameters)->reconnectTask(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
StaticJsonBuffer<200> jsonBuffer;
|
|
|
|
JsonObject& root = jsonBuffer.parseObject(payload);
|
|
|
|
if (!root.success()) {
|
|
|
|
Serial.println("parseObject() failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (String(topic).equals("sensor/vendingmachine/dispense")) {
|
|
|
|
if (root.containsKey("num")) {
|
|
|
|
shelf->dispense(root["num"]);
|
|
|
|
}
|
|
|
|
} else if (String(topic).equals("sensor/vendingmachine/color") || String(topic).equals("sensor/esp100/set")) {
|
|
|
|
if (root.containsKey("color")) {
|
|
|
|
uint8_t r = root["color"][0];
|
|
|
|
uint8_t g = root["color"][1];
|
|
|
|
uint8_t b = root["color"][2];
|
|
|
|
led.changeColor(r,g,b);
|
|
|
|
}
|
|
|
|
if (root.containsKey("brightness")) {
|
|
|
|
uint8_t brightness = root["brightness"];
|
|
|
|
led.changeBrightness(brightness);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyMQTT::onMqttConnect(bool sessionPresent) {
|
|
|
|
Serial.println("CONNECTED! callback works");
|
|
|
|
subscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyMQTT::onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
|
|
|
Serial.println("Disconnected from MQTT.");
|
|
|
|
|
|
|
|
xTaskCreate(
|
|
|
|
&cTaskWrapper, /* Task function. */
|
|
|
|
"mqttReconnectTask", /* String with name of task. */
|
|
|
|
1024, /* Stack size in words. */
|
|
|
|
this, /* Parameter passed as input of the task */
|
|
|
|
1, /* Priority of the task. */
|
|
|
|
&mqttTaskHandle); /* Task handle. */
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyMQTT::connect(void) {
|
|
|
|
mqtt_ptr = this;
|
|
|
|
Serial.print("MQTT connecting");
|
|
|
|
|
|
|
|
mqttClient.onConnect(&onMqttConnectWrapper);
|
|
|
|
mqttClient.onDisconnect(&onMqttDisconnectWrapper);
|
|
|
|
mqttClient.onSubscribe(&onMqttSubscribe);
|
|
|
|
mqttClient.onMessage(&onMqttMessageWrapper);
|
|
|
|
|
|
|
|
mqttClient.setServer(host, port);
|
|
|
|
mqttClient.setCredentials(username, password);
|
|
|
|
mqttClient.setKeepAlive(60);
|
|
|
|
//#if ASYNC_TCP_SSL_ENABLED
|
|
|
|
// mqttClient.setSecure(true);
|
|
|
|
//#endif
|
|
|
|
|
|
|
|
mqttClient.connect();
|
|
|
|
|
|
|
|
while(!mqttClient.connected()) {
|
|
|
|
Serial.print(".");
|
|
|
|
delay(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println("MQTT connected");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyMQTT::connected(void) {
|
|
|
|
return mqttClient.connected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyMQTT::subscribe(void) {
|
|
|
|
uint16_t dispenseIdSub = mqttClient.subscribe("sensor/vendingmachine/dispense", 0);
|
|
|
|
uint16_t colorIdSub = mqttClient.subscribe("sensor/vendingmachine/color", 0);
|
|
|
|
uint16_t color2IdSub = mqttClient.subscribe("sensor/esp100/set", 0);
|
|
|
|
mqttClient.publish("sensor/vendingmachine/alive", 0, true, "test");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyMQTT::publish(char* topic, char* payload) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyMQTT::publish(char* topic, String payload) {
|
|
|
|
|
|
|
|
}
|