Browse Source

make everything work

main
Hendrik Langer 7 years ago
parent
commit
964c5d5ba9
  1. 4
      software/platformio.ini
  2. 40
      software/src/MyMQTT.cpp
  3. 6
      software/src/MyMQTT.h
  4. 30
      software/src/hardware.h
  5. 3
      software/src/led.cpp
  6. 3
      software/src/led.h
  7. 14
      software/src/main.cpp
  8. 11
      software/src/pusher.cpp
  9. 14
      software/src/shelf.cpp
  10. 4
      software/src/shelf.h
  11. 7
      software/src/wifi.cpp
  12. 1
      software/src/wifi.h

4
software/platformio.ini

@ -20,8 +20,8 @@ lib_deps =
; ESP Async WebServer ; ESP Async WebServer
; AsyncTCP ; AsyncTCP
FS FS
; FastLED FastLED
https://github.com/samguyer/FastLED.git ; https://github.com/samguyer/FastLED.git
; AsyncMqttClient ; AsyncMqttClient
https://github.com/marvinroger/async-mqtt-client.git https://github.com/marvinroger/async-mqtt-client.git
lib_ignore = ESPAsyncTCP lib_ignore = ESPAsyncTCP

40
software/src/MyMQTT.cpp

@ -4,10 +4,12 @@
#include "hardware.h" #include "hardware.h"
#include "shelf.h" #include "shelf.h"
#include "wifi.h"
using namespace std; using namespace std;
extern Shelf* shelf; extern Shelf* shelf;
extern Wifi wifi;
MyMQTT::MyMQTT(void) MyMQTT::MyMQTT(void)
: host {mqtt_server}, : host {mqtt_server},
@ -33,6 +35,26 @@ void MyMQTT::onMqttConnectWrapper(bool sessionPresent) {
mqtt_ptr->onMqttConnect(sessionPresent); mqtt_ptr->onMqttConnect(sessionPresent);
} }
void MyMQTT::onMqttDisconnectWrapper(AsyncMqttClientDisconnectReason reason) {
// static_cast<MyMQTT*>(parm)->onMqttConnect(NULL);
mqtt_ptr->onMqttDisconnect(reason);
}
void MyMQTT::reconnectTask(void* parameters) {
while (!wifi.connected()) {
Serial.print(".");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
Serial.println("MQTT reconnecting!");
this->connect();
vTaskDelete(NULL);
}
void MyMQTT::cTaskWrapper(void* parameters) {
static_cast<MyMQTT*>(parameters)->reconnectTask(NULL);
}
void MyMQTT::onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { void MyMQTT::onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
Serial.print("incoming: "); Serial.print("incoming: ");
Serial.println(topic); Serial.println(topic);
@ -59,16 +81,30 @@ void MyMQTT::onMqttConnect(bool sessionPresent) {
subscribe(); subscribe();
} }
void MyMQTT::onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
xTaskCreate(
&cTaskWrapper, /* Task function. */
"mqttReconnectTask", /* String with name of task. */
1024, /* Stack size in words. */
this, /* Parameter passed as input of the task */
1, /* Priority of the task. */
&mqttTaskHandle); /* Task handle. */
}
void MyMQTT::connect(void) { void MyMQTT::connect(void) {
mqtt_ptr = this; mqtt_ptr = this;
Serial.print("MQTT connecting"); Serial.print("MQTT connecting");
mqttClient.onConnect(&onMqttConnectWrapper); mqttClient.onConnect(&onMqttConnectWrapper);
mqttClient.onDisconnect(&onMqttDisconnectWrapper);
mqttClient.onSubscribe(&onMqttSubscribe); mqttClient.onSubscribe(&onMqttSubscribe);
mqttClient.onMessage(&onMqttMessageWrapper); mqttClient.onMessage(&onMqttMessageWrapper);
mqttClient.setServer(host, port); mqttClient.setServer(host, port);
mqttClient.setCredentials(username, password); mqttClient.setCredentials(username, password);
mqttClient.setKeepAlive(60);
//#if ASYNC_TCP_SSL_ENABLED //#if ASYNC_TCP_SSL_ENABLED
// mqttClient.setSecure(true); // mqttClient.setSecure(true);
//#endif //#endif
@ -84,6 +120,10 @@ void MyMQTT::connect(void) {
} }
bool MyMQTT::connected(void) {
return mqttClient.connected();
}
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);
mqttClient.publish("sensor/vendingmachine/alive", 0, true, "test"); mqttClient.publish("sensor/vendingmachine/alive", 0, true, "test");

6
software/src/MyMQTT.h

@ -10,12 +10,15 @@ class MyMQTT {
MyMQTT(void); MyMQTT(void);
void connect(void); void connect(void);
void subscribe(void); void subscribe(void);
bool connected(void);
void publish(char* topic, char* payload); void publish(char* topic, char* payload);
void publish(char* topic, String payload); void publish(char* topic, String payload);
void onMqttConnect(bool sessionPresent); void onMqttConnect(bool sessionPresent);
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
// static void onMqttMessageWrapper(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total); // static void onMqttMessageWrapper(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
static void onMqttConnectWrapper(bool sessionPresent); static void onMqttConnectWrapper(bool sessionPresent);
static void onMqttDisconnectWrapper(AsyncMqttClientDisconnectReason reason);
private: private:
const char* host; const char* host;
const uint32_t port; const uint32_t port;
@ -23,6 +26,9 @@ class MyMQTT {
const char* username; const char* username;
const char* password; const char* password;
AsyncMqttClient mqttClient; AsyncMqttClient mqttClient;
static void cTaskWrapper(void*);
void reconnectTask(void*);
TaskHandle_t mqttTaskHandle;
// static MyMQTT* mqtt_ptr; // static MyMQTT* mqtt_ptr;
}; };

30
software/src/hardware.h

@ -6,7 +6,9 @@
#define LED_BUILTIN 13 #define LED_BUILTIN 13
static constexpr int PROGMEM SERVO_PINS[] = {23, 22, 1, 3, 21, 19}; static constexpr int PROGMEM SERVO_PINS[] = {23, 22, 21, 19, 18, 5};
//UART: 1 and 3
static constexpr uint8_t PROGMEM LED_PIN = 4; static constexpr uint8_t PROGMEM LED_PIN = 4;
static constexpr uint8_t PROGMEM NUM_LEDS = 21; static constexpr uint8_t PROGMEM NUM_LEDS = 21;
@ -16,7 +18,31 @@ static constexpr uint8_t PROGMEM NUM_LEDS = 21;
static const char* PROGMEM mqtt_server = "172.16.75.18"; static const char* PROGMEM mqtt_server = "172.16.75.18";
static constexpr uint32_t PROGMEM mqtt_port = 1883; static constexpr uint32_t PROGMEM mqtt_port = 1883;
static constexpr bool PROGMEM MQTT_SECURE = true; static constexpr bool PROGMEM MQTT_SECURE = true;
static const char* PROGMEM mqtt_username = "esp100"; static const char* PROGMEM mqtt_username = "vending";
static const char* PROGMEM mqtt_password = "password"; static const char* PROGMEM mqtt_password = "password";
#endif /* _HARDWARE_H */ #endif /* _HARDWARE_H */
/* ******** pin diagram (esp32-st) ********
* __________
* 3v3 | | GND -- GND
* RST | | 23 -- Servo0
* 36 | | 22 -- Servo1
* 39 | | 01 (UART_TX)
* 34 | | 03 (UART_RX)
* 35 | | 21 -- Servo2
* 32 | ESP32-ST | GND
* 33 | | 19 -- Servo3
* 25 | Develop- | 18 -- Servo4
* 26 | ment | 05 -- Servo5
* 27 | Board | 17
* 14 | | 16
* 12 | | 04 -- TM1829_Data
* GND | | 00
* 13 | | 02 (LED1)
* 09 | | 15
* 10 | | 08
* 11 | | 07
* Vin -- 5V |__________| 06
*
****************************************** */

3
software/src/led.cpp

@ -1,4 +1,5 @@
#define FASTLED_ALLOW_INTERRUPTS 0 //#define FASTLED_ALLOW_INTERRUPTS 0
//#define FASTLED_INTERRUPT_RETRY_COUNT 1
#include <FastLED.h> #include <FastLED.h>
#include "led.h" #include "led.h"

3
software/src/led.h

@ -1,7 +1,8 @@
#ifndef _LED_H #ifndef _LED_H
#define _LED_H #define _LED_H
#define FASTLED_ALLOW_INTERRUPTS 0 //#define FASTLED_ALLOW_INTERRUPTS 0
//#define FASTLED_INTERRUPT_RETRY_COUNT 1
#include <FastLED.h> #include <FastLED.h>
#include "hardware.h" #include "hardware.h"

14
software/src/main.cpp

@ -15,6 +15,8 @@
#include "led.h" #include "led.h"
#include "MyMQTT.h" #include "MyMQTT.h"
#define LED_BUILTIN 2
using namespace std; using namespace std;
volatile SemaphoreHandle_t xPreferencesSemaphore = xSemaphoreCreateMutex(); volatile SemaphoreHandle_t xPreferencesSemaphore = xSemaphoreCreateMutex();
@ -43,23 +45,25 @@ void setup()
led.setup(); led.setup();
wifi.connect(); wifi.connect();
mqtt.connect(); mqtt.connect();
delay(1000); // delay(1000);
shelf = new Shelf(8); shelf = new Shelf(8);
pusher.setup(); pusher.setup();
// webserver.start(); // webserver.start();
} }
void loop() void loop()
{/* {
Serial.print("Free Heap: ");
Serial.println(ESP.getFreeHeap());
// turn the LED on (HIGH is the voltage level) // turn the LED on (HIGH is the voltage level)
digitalWrite(LED_BUILTIN, HIGH); if (mqtt.connected()) digitalWrite(LED_BUILTIN, HIGH);
// wait for a second // wait for a second
delay(1000); delay(1000);
// turn the LED off by making the voltage LOW // turn the LED off by making the voltage LOW
digitalWrite(LED_BUILTIN, LOW); digitalWrite(LED_BUILTIN, LOW);
// wait for a second // wait for a second
delay(1000); delay(1000);
*/
delay(5000); //delay(5000);
} }

11
software/src/pusher.cpp

@ -14,13 +14,13 @@ Pusher::Pusher(void) {
void Pusher::setup(void) { void Pusher::setup(void) {
int channel = LEDC_TIMER_0; int channel = LEDC_TIMER_0;
ledcSetup(channel, 50, TIMER_WIDTH);
for(int pin : SERVO_PINS) { for(int pin : SERVO_PINS) {
if (xSemaphoreTake(xSemaphore, TIMEOUT) == pdTRUE) { if (xSemaphoreTake(xSemaphore, TIMEOUT) == pdTRUE) {
ledcSetup(channel, 50, TIMER_WIDTH); ledcAttachPin(pin, channel);
/* ledcAttachPin(pin, channel);
ledcWrite(channel, MAX_PULSE_WIDTH); ledcWrite(channel, MAX_PULSE_WIDTH);
delay(200); delay(500);
ledcDetachPin(pin);*/ ledcDetachPin(pin);
xSemaphoreGive(xSemaphore); xSemaphoreGive(xSemaphore);
Serial.print("configured pin "); Serial.print("configured pin ");
Serial.println(pin); Serial.println(pin);
@ -35,8 +35,7 @@ void Pusher::push(int num) {
if (channel >= LEDC_NUM_CHANNELS) { if (channel >= LEDC_NUM_CHANNELS) {
return; return;
} }
// ledcSetup(channel, 50, TIMER_WIDTH);
ledcSetup(channel, 50, TIMER_WIDTH);
ledcAttachPin(pin, channel); ledcAttachPin(pin, channel);
ledcWrite(channel, MIN_PULSE_WIDTH); ledcWrite(channel, MIN_PULSE_WIDTH);
delay(1000); delay(1000);

14
software/src/shelf.cpp

@ -25,7 +25,8 @@ void Shelf::reload(void) {
} }
} }
void Shelf::dispense(int num) { void Shelf::dispenseTask(void* parameters) {
Serial.println("dispensing");
item.at(num)->quantity--; item.at(num)->quantity--;
led.changeAnimation(1, 0); led.changeAnimation(1, 0);
pusher.push(num); pusher.push(num);
@ -33,4 +34,15 @@ void Shelf::dispense(int num) {
led.changeAnimation(2, 0); led.changeAnimation(2, 0);
delay(7000); delay(7000);
led.changeAnimation(0, 0); led.changeAnimation(0, 0);
vTaskDelete(NULL);
}
void Shelf::cTaskWrapper(void* parameters) {
static_cast<Shelf*>(parameters)->dispenseTask(NULL);
}
void Shelf::dispense(int num) {
this->num = num;
xTaskCreate(&cTaskWrapper, "dispenseTask", 1024, this, 0, &dispenseTaskHandle);
} }

4
software/src/shelf.h

@ -12,6 +12,10 @@ class Shelf {
void reload(void); void reload(void);
void dispense(int); void dispense(int);
private: private:
TaskHandle_t dispenseTaskHandle;
void dispenseTask(void*);
static void cTaskWrapper(void*);
int num;
}; };
#endif /* _SHELF_H */ #endif /* _SHELF_H */

7
software/src/wifi.cpp

@ -14,13 +14,18 @@ void Wifi::reconnectTask(void* parameters) {
if(wifiMulti.run() != WL_CONNECTED) { if(wifiMulti.run() != WL_CONNECTED) {
Serial.println("WiFi not connected!"); Serial.println("WiFi not connected!");
} }
vTaskDelay(3000 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
} }
Serial.println("WiFi disconnecting!"); Serial.println("WiFi disconnecting!");
WiFi.disconnect(); WiFi.disconnect();
vTaskDelete(NULL); vTaskDelete(NULL);
} }
bool Wifi::connected(void) {
if(wifiMulti.run() == WL_CONNECTED) return true;
else return false;
}
void Wifi::cTaskWrapper(void* parameters) { void Wifi::cTaskWrapper(void* parameters) {
static_cast<Wifi*>(parameters)->reconnectTask(NULL); static_cast<Wifi*>(parameters)->reconnectTask(NULL);
} }

1
software/src/wifi.h

@ -9,6 +9,7 @@ class Wifi {
Wifi(); Wifi();
void connect(); void connect();
void disconnect(); void disconnect();
bool connected();
private: private:
void reconnectTask(void*); void reconnectTask(void*);
static void cTaskWrapper(void*); static void cTaskWrapper(void*);

Loading…
Cancel
Save