Browse Source

fix mqtt

main
Hendrik Langer 7 years ago
parent
commit
6b80f143d0
  1. 22
      software/src/MyMQTT.cpp
  2. 16
      software/src/hardware.h
  3. 9
      software/src/main.cpp
  4. 10
      software/src/pusher.cpp
  5. 18
      software/src/pusher.h

22
software/src/MyMQTT.cpp

@ -1,7 +1,13 @@
#include "MyMQTT.h" #include "MyMQTT.h"
#include <AsyncMqttClient.h> #include <AsyncMqttClient.h>
#include "hardware.h" #include "hardware.h"
#include "shelf.h"
using namespace std;
extern Shelf* shelf;
MyMQTT::MyMQTT(void) MyMQTT::MyMQTT(void)
: host {mqtt_server}, : host {mqtt_server},
@ -31,6 +37,21 @@ void MyMQTT::onMqttMessage(char* topic, char* payload, AsyncMqttClientMessagePro
Serial.print("incoming: "); Serial.print("incoming: ");
Serial.println(topic); Serial.println(topic);
Serial.println(payload); 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) { void MyMQTT::onMqttConnect(bool sessionPresent) {
@ -65,7 +86,6 @@ void MyMQTT::connect(void) {
void MyMQTT::subscribe(void) { void MyMQTT::subscribe(void) {
uint16_t dispenseIdSub = mqttClient.subscribe("sensor/vendingmachine/dispense", 0); 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"); mqttClient.publish("sensor/vendingmachine/alive", 0, true, "test");
} }

16
software/src/hardware.h

@ -6,17 +6,17 @@
#define LED_BUILTIN 13 #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 PROGMEM LED_PIN = 4;
static constexpr uint8_t NUM_LEDS = 22; static constexpr uint8_t PROGMEM NUM_LEDS = 22;
#define LED_TYPE TM1829 #define LED_TYPE TM1829
#define COLOR_ORDER BRG #define COLOR_ORDER BRG
static const char* mqtt_server = "172.16.75.18"; static const char* PROGMEM mqtt_server = "172.16.75.18";
static constexpr uint32_t mqtt_port = 1883; static constexpr uint32_t PROGMEM mqtt_port = 1883;
static constexpr bool MQTT_SECURE = true; static constexpr bool PROGMEM MQTT_SECURE = true;
static const char* mqtt_username = "esp100"; static const char* PROGMEM mqtt_username = "esp100";
static const char* mqtt_password = "password"; static const char* PROGMEM mqtt_password = "password";
#endif /* _HARDWARE_H */ #endif /* _HARDWARE_H */

9
software/src/main.cpp

@ -9,16 +9,18 @@
#include "hardware.h" #include "hardware.h"
#include "wifi.h" #include "wifi.h"
#include "webserver.h" //#include "webserver.h"
#include "pusher.h" #include "pusher.h"
#include "shelf.h" #include "shelf.h"
#include "led.h" #include "led.h"
#include "MyMQTT.h" #include "MyMQTT.h"
using namespace std;
volatile SemaphoreHandle_t xPreferencesSemaphore = xSemaphoreCreateMutex(); volatile SemaphoreHandle_t xPreferencesSemaphore = xSemaphoreCreateMutex();
Wifi wifi; Wifi wifi;
Webserver webserver; //Webserver webserver;
MyMQTT mqtt; MyMQTT mqtt;
Pusher pusher; Pusher pusher;
Shelf* shelf; Shelf* shelf;
@ -41,9 +43,10 @@ void setup()
led.setup(); led.setup();
wifi.connect(); wifi.connect();
mqtt.connect(); mqtt.connect();
delay(1000);
shelf = new Shelf(8); shelf = new Shelf(8);
pusher.setup(); pusher.setup();
webserver.start(); // webserver.start();
} }
void loop() void loop()

10
software/src/pusher.cpp

@ -14,14 +14,16 @@ Pusher::Pusher(void) {
void Pusher::setup(void) { void Pusher::setup(void) {
int channel = LEDC_TIMER_0; int channel = LEDC_TIMER_0;
int pin = 22; for(int pin : SERVO_PINS) {
for(auto pin : SERVO_PINS) {
if (xSemaphoreTake(xSemaphore, TIMEOUT) == pdTRUE) { if (xSemaphoreTake(xSemaphore, TIMEOUT) == pdTRUE) {
ledcSetup(channel, 50, TIMER_WIDTH); ledcSetup(channel, 50, TIMER_WIDTH);
ledcAttachPin(pin, channel); /* ledcAttachPin(pin, channel);
ledcWrite(channel, MAX_PULSE_WIDTH); ledcWrite(channel, MAX_PULSE_WIDTH);
ledcDetachPin(pin); delay(200);
ledcDetachPin(pin);*/
xSemaphoreGive(xSemaphore); xSemaphoreGive(xSemaphore);
Serial.print("configured pin ");
Serial.println(pin);
} }
} }
} }

18
software/src/pusher.h

@ -5,18 +5,18 @@
#include "driver/ledc.h" #include "driver/ledc.h"
#include "semaphore.h" #include "semaphore.h"
static constexpr uint16_t MIN_PULSE_MS = 544; // the shortest pulse sent to a servo static constexpr uint16_t PROGMEM 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 PROGMEM 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 DEFAULT_PULSE_MS = 1500; // default pulse width
static constexpr int TIMER_WIDTH = LEDC_TIMER_15_BIT; static constexpr int PROGMEM 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 PROGMEM 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 PROGMEM 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 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 { class Pusher {
public: public:

Loading…
Cancel
Save