Bugfix: payload parsing

This commit is contained in:
Tobias Blum 2017-08-06 21:20:16 +02:00
parent 90578a4b33
commit c160b910d9
2 changed files with 19 additions and 21 deletions

View file

@ -10,9 +10,10 @@ const char HOSTNAME[] = "ESP8266_VORONOI"; // Friedly hostname
#define ENABLE_MQTT // If defined, enable MQTT client code. #define ENABLE_MQTT // If defined, enable MQTT client code.
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
#define MQTT_MAX_PACKET_SIZE 256 #define MQTT_MAX_PACKET_SIZE 256
char mqtt_intopic[strlen(HOSTNAME) + 3]; // Topic in will be: <HOSTNAME>/in char mqtt_intopic[strlen(HOSTNAME) + 3]; // Topic in will be: <HOSTNAME>/in
char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: <HOSTNAME>/out char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: <HOSTNAME>/out
const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker
const char mqtt_clientid[] = "ESP8266Client"; // MQTT ClientID
#endif #endif
// *************************************************************************** // ***************************************************************************

View file

@ -306,20 +306,17 @@ void checkForRequests() {
// MQTT callback / connection handler // MQTT callback / connection handler
// *************************************************************************** // ***************************************************************************
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
void mqtt_callback(char* topic, byte* payload, unsigned int length) { void mqtt_callback(char* topic, byte* payload_in, unsigned int length) {
DBG_OUTPUT_PORT.print("Message arrived ["); uint8_t * payload = (uint8_t *)malloc(length + 1);
DBG_OUTPUT_PORT.print(topic); memcpy(payload, payload_in, length);
DBG_OUTPUT_PORT.print("] "); payload[length] = NULL;
for (int i = 0; i < length; i++) { DBG_OUTPUT_PORT.printf("MQTT: Message arrived [%s]\n", payload);
DBG_OUTPUT_PORT.print((char)payload[i]);
}
DBG_OUTPUT_PORT.println();
// # ==> Set main color // # ==> Set main color
if (payload[0] == '#') { if (payload[0] == '#') {
handleSetMainColor(payload); handleSetMainColor(payload);
DBG_OUTPUT_PORT.printf("MQTT: Set main color to [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); DBG_OUTPUT_PORT.printf("MQTT: Set main color to [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue);
mqtt_client.publish(mqtt_outtopic, "OK"); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
} }
// ? ==> Set speed // ? ==> Set speed
@ -328,7 +325,7 @@ void checkForRequests() {
ws2812fx_speed = constrain(d, 0, 255); ws2812fx_speed = constrain(d, 0, 255);
strip.setSpeed(ws2812fx_speed); strip.setSpeed(ws2812fx_speed);
DBG_OUTPUT_PORT.printf("MQTT: Set speed to [%u]\n", ws2812fx_speed); DBG_OUTPUT_PORT.printf("MQTT: Set speed to [%u]\n", ws2812fx_speed);
mqtt_client.publish(mqtt_outtopic, "OK"); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
} }
// % ==> Set brightness // % ==> Set brightness
@ -337,21 +334,21 @@ void checkForRequests() {
brightness = constrain(b, 0, 255); brightness = constrain(b, 0, 255);
strip.setBrightness(brightness); strip.setBrightness(brightness);
DBG_OUTPUT_PORT.printf("MQTT: Set brightness to [%u]\n", brightness); DBG_OUTPUT_PORT.printf("MQTT: Set brightness to [%u]\n", brightness);
mqtt_client.publish(mqtt_outtopic, "OK"); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
} }
// * ==> Set main color and light all LEDs (Shortcut) // * ==> Set main color and light all LEDs (Shortcut)
if (payload[0] == '*') { if (payload[0] == '*') {
handleSetAllMode(payload); handleSetAllMode(payload);
DBG_OUTPUT_PORT.printf("MQTT: Set main color and light all LEDs [%s]\n", payload); DBG_OUTPUT_PORT.printf("MQTT: Set main color and light all LEDs [%s]\n", payload);
mqtt_client.publish(mqtt_outtopic, "OK"); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
} }
// ! ==> Set single LED in given color // ! ==> Set single LED in given color
if (payload[0] == '!') { if (payload[0] == '!') {
handleSetSingleLED(payload); handleSetSingleLED(payload);
DBG_OUTPUT_PORT.printf("MQTT: Set single LED in given color [%s]\n", payload); DBG_OUTPUT_PORT.printf("MQTT: Set single LED in given color [%s]\n", payload);
mqtt_client.publish(mqtt_outtopic, "OK"); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
} }
// = ==> Activate named mode // = ==> Activate named mode
@ -359,7 +356,7 @@ void checkForRequests() {
String str_mode = String((char *) &payload[0]); String str_mode = String((char *) &payload[0]);
handleSetNamedMode(str_mode); handleSetNamedMode(str_mode);
DBG_OUTPUT_PORT.printf("MQTT: Activate named mode [%s]\n", payload); DBG_OUTPUT_PORT.printf("MQTT: Activate named mode [%s]\n", payload);
mqtt_client.publish(mqtt_outtopic, "OK"); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
} }
// $ ==> Get status Info. // $ ==> Get status Info.
@ -385,17 +382,17 @@ void checkForRequests() {
if (payload[0] == '/') { if (payload[0] == '/') {
handleSetWS2812FXMode(payload); handleSetWS2812FXMode(payload);
DBG_OUTPUT_PORT.printf("MQTT: Set WS2812 mode [%s]\n", payload); DBG_OUTPUT_PORT.printf("MQTT: Set WS2812 mode [%s]\n", payload);
mqtt_client.publish(mqtt_outtopic, "OK"); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str());
} }
} }
void mqtt_reconnect() { void mqtt_reconnect() {
// Loop until we're reconnected // Loop until we're reconnected
while (!mqtt_client.connected()) { while (!mqtt_client.connected()) {
DBG_OUTPUT_PORT.print("Attempting MQTT connection..."); DBG_OUTPUT_PORT.print("Attempting MQTT connection... ");
// Attempt to connect // Attempt to connect
if (mqtt_client.connect("ESP8266Client")) { if (mqtt_client.connect(mqtt_clientid)) {
DBG_OUTPUT_PORT.println("connected"); DBG_OUTPUT_PORT.println("connected!");
// Once connected, publish an announcement... // Once connected, publish an announcement...
char * message = new char[18 + strlen(HOSTNAME) + 1]; char * message = new char[18 + strlen(HOSTNAME) + 1];
strcpy(message, "McLighting ready: "); strcpy(message, "McLighting ready: ");