From 6b80f143d0c3d7993e03618def354561060a7596 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Sat, 6 Jan 2018 13:29:03 +0100 Subject: [PATCH] fix mqtt --- software/src/MyMQTT.cpp | 22 +++++++++++++++++++++- software/src/hardware.h | 16 ++++++++-------- software/src/main.cpp | 9 ++++++--- software/src/pusher.cpp | 10 ++++++---- software/src/pusher.h | 20 ++++++++++---------- 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/software/src/MyMQTT.cpp b/software/src/MyMQTT.cpp index 56a05d6..ae32106 100644 --- a/software/src/MyMQTT.cpp +++ b/software/src/MyMQTT.cpp @@ -1,7 +1,13 @@ #include "MyMQTT.h" #include + #include "hardware.h" +#include "shelf.h" + +using namespace std; + +extern Shelf* shelf; MyMQTT::MyMQTT(void) : host {mqtt_server}, @@ -31,6 +37,21 @@ 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); + } } void MyMQTT::onMqttConnect(bool sessionPresent) { @@ -65,7 +86,6 @@ void MyMQTT::connect(void) { void MyMQTT::subscribe(void) { 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"); } diff --git a/software/src/hardware.h b/software/src/hardware.h index 0a7f26e..dfe6e0a 100644 --- a/software/src/hardware.h +++ b/software/src/hardware.h @@ -6,17 +6,17 @@ #define LED_BUILTIN 13 -static constexpr int SERVO_PINS[] = {23, 22, 1, 3, 21, 19}; +static constexpr int PROGMEM SERVO_PINS[] = {23, 22, 1, 3, 21, 19}; -static constexpr uint8_t LED_PIN = 4; -static constexpr uint8_t NUM_LEDS = 22; +static constexpr uint8_t PROGMEM LED_PIN = 4; +static constexpr uint8_t PROGMEM NUM_LEDS = 22; #define LED_TYPE TM1829 #define COLOR_ORDER BRG -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_username = "esp100"; -static const char* mqtt_password = "password"; +static const char* PROGMEM mqtt_server = "172.16.75.18"; +static constexpr uint32_t PROGMEM mqtt_port = 1883; +static constexpr bool PROGMEM MQTT_SECURE = true; +static const char* PROGMEM mqtt_username = "esp100"; +static const char* PROGMEM mqtt_password = "password"; #endif /* _HARDWARE_H */ diff --git a/software/src/main.cpp b/software/src/main.cpp index 81ea71e..3ad83af 100644 --- a/software/src/main.cpp +++ b/software/src/main.cpp @@ -9,16 +9,18 @@ #include "hardware.h" #include "wifi.h" -#include "webserver.h" +//#include "webserver.h" #include "pusher.h" #include "shelf.h" #include "led.h" #include "MyMQTT.h" +using namespace std; + volatile SemaphoreHandle_t xPreferencesSemaphore = xSemaphoreCreateMutex(); Wifi wifi; -Webserver webserver; +//Webserver webserver; MyMQTT mqtt; Pusher pusher; Shelf* shelf; @@ -41,9 +43,10 @@ void setup() led.setup(); wifi.connect(); mqtt.connect(); + delay(1000); shelf = new Shelf(8); pusher.setup(); - webserver.start(); +// webserver.start(); } void loop() diff --git a/software/src/pusher.cpp b/software/src/pusher.cpp index f91019e..07c7db9 100644 --- a/software/src/pusher.cpp +++ b/software/src/pusher.cpp @@ -14,14 +14,16 @@ Pusher::Pusher(void) { void Pusher::setup(void) { int channel = LEDC_TIMER_0; - int pin = 22; - for(auto pin : SERVO_PINS) { + for(int pin : SERVO_PINS) { if (xSemaphoreTake(xSemaphore, TIMEOUT) == pdTRUE) { ledcSetup(channel, 50, TIMER_WIDTH); - ledcAttachPin(pin, channel); +/* ledcAttachPin(pin, channel); ledcWrite(channel, MAX_PULSE_WIDTH); - ledcDetachPin(pin); + delay(200); + ledcDetachPin(pin);*/ xSemaphoreGive(xSemaphore); + Serial.print("configured pin "); + Serial.println(pin); } } } diff --git a/software/src/pusher.h b/software/src/pusher.h index 7ae7dd0..3568b56 100644 --- a/software/src/pusher.h +++ b/software/src/pusher.h @@ -5,18 +5,18 @@ #include "driver/ledc.h" #include "semaphore.h" -static constexpr uint16_t MIN_PULSE_MS = 544; // the shortest pulse sent to a servo -static constexpr uint16_t MAX_PULSE_MS = 2400; // the longest pulse sent to a servo -static constexpr uint16_t DEFAULT_PULSE_MS = 1500; // default pulse width +static constexpr uint16_t PROGMEM MIN_PULSE_MS = 544; // the shortest pulse sent to a servo +static constexpr uint16_t PROGMEM MAX_PULSE_MS = 2400; // the longest pulse sent to a servo +static constexpr uint16_t PROGMEM DEFAULT_PULSE_MS = 1500; // default pulse width -static constexpr int TIMER_WIDTH = LEDC_TIMER_15_BIT; -static constexpr uint16_t MIN_PULSE_WIDTH = (1 << TIMER_WIDTH) * MIN_PULSE_MS / 20000; // the shortest pulse sent to a servo -static constexpr uint16_t MAX_PULSE_WIDTH = (1 << TIMER_WIDTH) * MAX_PULSE_MS / 20000; // the longest pulse sent to a servo -static constexpr uint16_t DEFAULT_PULSE_WIDTH = (1 << TIMER_WIDTH) * DEFAULT_PULSE_MS / 20000; // default pulse width +static constexpr int PROGMEM TIMER_WIDTH = LEDC_TIMER_15_BIT; +static constexpr uint16_t PROGMEM MIN_PULSE_WIDTH = (1 << TIMER_WIDTH) * MIN_PULSE_MS / 20000; // the shortest pulse sent to a servo +static constexpr uint16_t PROGMEM MAX_PULSE_WIDTH = (1 << TIMER_WIDTH) * MAX_PULSE_MS / 20000; // the longest pulse sent to a servo +static constexpr uint16_t PROGMEM DEFAULT_PULSE_WIDTH = (1 << TIMER_WIDTH) * DEFAULT_PULSE_MS / 20000; // default pulse width -static constexpr uint8_t LEDC_NUM_CHANNELS = 8; // default pulse width +static constexpr uint8_t PROGMEM LEDC_NUM_CHANNELS = 8; // default pulse width -static constexpr TickType_t TIMEOUT = portMAX_DELAY; +static constexpr TickType_t PROGMEM TIMEOUT = portMAX_DELAY; class Pusher { public: @@ -24,7 +24,7 @@ class Pusher { void setup(void); void push(int); private: - volatile SemaphoreHandle_t xSemaphore; + volatile SemaphoreHandle_t xSemaphore; }; #endif /* _PUSHER_H */