From 3a6aee95be4d75335a9048660cff226d70877a00 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Thu, 4 Jan 2018 15:20:34 +0100 Subject: [PATCH] add MQTT test --- software/platformio.ini | 4 +++ software/src/MyMQTT.cpp | 55 +++++++++++++++++++++++++++++++++++++++++ software/src/MyMQTT.h | 28 +++++++++++++++++++++ software/src/hardware.h | 11 +++++++-- software/src/led.cpp | 21 ++++++++++++++++ software/src/led.h | 3 +++ software/src/main.cpp | 7 ++++++ 7 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 software/src/MyMQTT.cpp create mode 100644 software/src/MyMQTT.h diff --git a/software/platformio.ini b/software/platformio.ini index 1b01db6..47225ee 100644 --- a/software/platformio.ini +++ b/software/platformio.ini @@ -13,6 +13,7 @@ platform = espressif32 board = esp32dev framework = arduino +;build_flags = -DASYNC_TCP_SSL_ENABLED=1 lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer.git https://github.com/me-no-dev/AsyncTCP.git @@ -21,4 +22,7 @@ lib_deps = FS ; FastLED https://github.com/samguyer/FastLED.git +; AsyncMqttClient +; https://github.com/marvinroger/async-mqtt-client.git + MQTT lib_ignore = ESPAsyncTCP diff --git a/software/src/MyMQTT.cpp b/software/src/MyMQTT.cpp new file mode 100644 index 0000000..445f5c7 --- /dev/null +++ b/software/src/MyMQTT.cpp @@ -0,0 +1,55 @@ +#include "MyMQTT.h" + +#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); +} + +void MyMQTT::connect(void) { + Serial.print("MQTT connecting"); + + mqttClient.begin(host, port, net); + mqttClient.onMessage(&onMqttMessage); + while (!mqttClient.connect(host, username, password)) { + 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(); + + if(!mqttClient.connected()) { + Serial.println("MQTT connection lost."); + connect(); + } +} + +void MyMQTT::subscribe(void) { + mqttClient.subscribe("sensor/vendingmachine/dispense"); +// mqttClient.subscribe("sensor/vendingmachine/light"); +} + +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 new file mode 100644 index 0000000..69ce73d --- /dev/null +++ b/software/src/MyMQTT.h @@ -0,0 +1,28 @@ +#ifndef _MYMQTT_H +#define _MYMQTT_H + +#include +#include + +#include "config.h" + +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); + private: + const char* host; + const uint32_t port; + const char* endpoint; + const char* username; + const char* password; + WiFiClientSecure net; + MQTTClient mqttClient; +}; + +#endif /* _MYMQTT_H */ diff --git a/software/src/hardware.h b/software/src/hardware.h index 802b8dd..399c288 100644 --- a/software/src/hardware.h +++ b/software/src/hardware.h @@ -1,8 +1,8 @@ #ifndef _HARDWARE_H #define _HARDWARE_H -#define WIFI_SSID "nether.net" -#define WIFI_PASSWORD "threepwood" +#define WIFI_SSID "ssid" +#define WIFI_PASSWORD "password" #define LED_BUILTIN 13 @@ -13,4 +13,11 @@ 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 constexpr bool MQTT_SECURE = true; +static const char* mqtt_endpoint = "sensor/vendingmachine/"; +static const char* mqtt_username = "esp32"; +static const char* mqtt_password = "password"; + #endif /* _HARDWARE_H */ diff --git a/software/src/led.cpp b/software/src/led.cpp index 753ba0b..7d4f004 100644 --- a/software/src/led.cpp +++ b/software/src/led.cpp @@ -21,4 +21,25 @@ void Led::setup(void) { leds[NUM_LEDS-2] = CRGB::White; leds[NUM_LEDS-1] = CRGB::White; FastLED.show(); + + xTaskCreate( + &cTaskWrapper, /* Task function. */ + "ledAnimationTask", /* String with name of task. */ + 1024, /* Stack size in words. */ + this, /* Parameter passed as input of the task */ + 1, /* Priority of the task. */ + &ledTaskHandle); /* Task handle. */ +} + +void Led::animationTask(void* parameters) { + while(true) { + + vTaskDelay(50 / portTICK_PERIOD_MS); + } + Serial.println("LED animation end!"); + vTaskDelete(NULL); +} + +void Led::cTaskWrapper(void* parameters) { + static_cast(parameters)->animationTask(NULL); } diff --git a/software/src/led.h b/software/src/led.h index 8e9ce36..5efc322 100644 --- a/software/src/led.h +++ b/software/src/led.h @@ -12,6 +12,9 @@ class Led { void setup(void); private: CRGB leds[NUM_LEDS]; + void animationTask(void*); + static void cTaskWrapper(void*); + TaskHandle_t ledTaskHandle; }; #endif /* _LED_H */ diff --git a/software/src/main.cpp b/software/src/main.cpp index 6ed0c1f..28e3cde 100644 --- a/software/src/main.cpp +++ b/software/src/main.cpp @@ -13,11 +13,13 @@ #include "pusher.h" #include "shelf.h" #include "led.h" +#include "MyMQTT.h" volatile SemaphoreHandle_t xPreferencesSemaphore = xSemaphoreCreateMutex(); Wifi wifi; Webserver webserver; +MyMQTT mqtt; Pusher pusher; Shelf* shelf; Led led; @@ -38,6 +40,7 @@ void setup() led.setup(); wifi.connect(); + mqtt.connect(); shelf = new Shelf(8); pusher.setup(); webserver.start(); @@ -45,6 +48,8 @@ void setup() void loop() { + mqtt.loop(); +/* // turn the LED on (HIGH is the voltage level) digitalWrite(LED_BUILTIN, HIGH); // wait for a second @@ -53,5 +58,7 @@ void loop() digitalWrite(LED_BUILTIN, LOW); // wait for a second delay(1000); +*/ +delay(100); }