|
|
@ -1,15 +1,18 @@ |
|
|
|
#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}, |
|
|
@ -59,20 +62,28 @@ void MyMQTT::onMqttMessage(char* topic, char* payload, AsyncMqttClientMessagePro |
|
|
|
Serial.print("incoming: "); |
|
|
|
Serial.println(topic); |
|
|
|
Serial.println(payload); |
|
|
|
if (String(payload).equals("{'num':'0'}")) { |
|
|
|
shelf->dispense(0); |
|
|
|
} else if (String(payload).equals("{'num':'1'}")) { |
|
|
|
shelf->dispense(1); |
|
|
|
} else if (String(payload).equals("{'num':'2'}")) { |
|
|
|
shelf->dispense(2); |
|
|
|
} else if (String(payload).equals("{'num':'3'}")) { |
|
|
|
shelf->dispense(3); |
|
|
|
} else if (String(payload).equals("{'num':'4'}")) { |
|
|
|
shelf->dispense(4); |
|
|
|
} else if (String(payload).equals("{'num':'5'}")) { |
|
|
|
shelf->dispense(5); |
|
|
|
} else if (String(payload).equals("{'num':'6'}")) { |
|
|
|
shelf->dispense(6); |
|
|
|
|
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -126,6 +137,8 @@ bool MyMQTT::connected(void) { |
|
|
|
|
|
|
|
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"); |
|
|
|
} |
|
|
|
|
|
|
|