Browse Source

add MQTT test

main
Hendrik Langer 7 years ago
parent
commit
3a6aee95be
  1. 4
      software/platformio.ini
  2. 55
      software/src/MyMQTT.cpp
  3. 28
      software/src/MyMQTT.h
  4. 11
      software/src/hardware.h
  5. 21
      software/src/led.cpp
  6. 3
      software/src/led.h
  7. 7
      software/src/main.cpp

4
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

55
software/src/MyMQTT.cpp

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

28
software/src/MyMQTT.h

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

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

21
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<Led*>(parameters)->animationTask(NULL);
}

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

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

Loading…
Cancel
Save