Dynamic topic names

This commit is contained in:
Tobias Blum 2017-08-06 18:11:01 +02:00
parent b71834bbb2
commit 90578a4b33
3 changed files with 75 additions and 68 deletions

View file

@ -120,7 +120,7 @@ void setup() {
// set builtin led pin as output
pinMode(BUILTIN_LED, OUTPUT);
// start ticker with 0.5 because we start in AP mode and try to connect
ticker.attach(0.6, tick);
ticker.attach(0.5, tick);
// ***************************************************************************
// Setup: Neopixel
@ -207,6 +207,9 @@ void setup() {
// Configure MQTT
// ***************************************************************************
#ifdef ENABLE_MQTT
String(String(HOSTNAME) + "/in").toCharArray(mqtt_intopic, 32);
String(String(HOSTNAME) + "/out").toCharArray(mqtt_outtopic, 32);
mqtt_client.setServer(mqtt_server, 1883);
mqtt_client.setCallback(mqtt_callback);
#endif

View file

@ -3,16 +3,16 @@
#define NUMLEDS 24 // Number of leds in the strip
#define HOSTNAME "ESP8266_VORONOI" // Friedly hostname
const char HOSTNAME[] = "ESP8266_VORONOI"; // Friedly hostname
#define ENABLE_OTA // If defined, enable Arduino OTA code.
#define ENABLE_MQTT // If defined, enable MQTT client code.
#define MQTT_MAX_PACKET_SIZE 4096
#ifdef ENABLE_MQTT
const char mqtt_intopic[] = "inTopic";
const char mqtt_outtopic[] = "outTopic";
const char mqtt_server[] = "raspberrypi2";
#define MQTT_MAX_PACKET_SIZE 256
char mqtt_intopic[strlen(HOSTNAME) + 3]; // Topic in will be: <HOSTNAME>/in
char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: <HOSTNAME>/out
const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker
#endif
// ***************************************************************************

View file

@ -38,20 +38,21 @@ void getArgs() {
}
void handleSetMainColor(uint8_t * payload) {
// ***************************************************************************
// Handler functions for WS and MQTT
// ***************************************************************************
void handleSetMainColor(uint8_t * mypayload) {
// decode rgb data
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
uint32_t rgb = (uint32_t) strtol((const char *) &mypayload[1], NULL, 16);
main_color.red = ((rgb >> 16) & 0xFF);
main_color.green = ((rgb >> 8) & 0xFF);
main_color.blue = ((rgb >> 0) & 0xFF);
strip.setColor(main_color.red, main_color.green, main_color.blue);
}
void handleSetAllMode(uint8_t * payload) {
void handleSetAllMode(uint8_t * mypayload) {
// decode rgb data
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
uint32_t rgb = (uint32_t) strtol((const char *) &mypayload[1], NULL, 16);
main_color.red = ((rgb >> 16) & 0xFF);
main_color.green = ((rgb >> 8) & 0xFF);
@ -66,9 +67,9 @@ void handleSetAllMode(uint8_t * payload) {
mode = ALL;
}
void handleSetSingleLED(uint8_t * payload) {
void handleSetSingleLED(uint8_t * mypayload) {
// decode led index
uint64_t rgb = (uint64_t) strtol((const char *) &payload[1], NULL, 16);
uint64_t rgb = (uint64_t) strtol((const char *) &mypayload[1], NULL, 16);
uint8_t led = ((rgb >> 24) & 0xFF);
if (led < strip.numPixels()) {
@ -116,65 +117,14 @@ void handleSetNamedMode(String str_mode) {
}
}
void handleSetWS2812FXMode(uint8_t * payload) {
void handleSetWS2812FXMode(uint8_t * mypayload) {
mode = HOLD;
uint8_t ws2812fx_mode = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
uint8_t ws2812fx_mode = (uint8_t) strtol((const char *) &mypayload[1], NULL, 10);
ws2812fx_mode = constrain(ws2812fx_mode, 0, 255);
strip.setColor(main_color.red, main_color.green, main_color.blue);
strip.setMode(ws2812fx_mode);
}
void handleMinimalUpload() {
char temp[1500];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf ( temp, 1500,
"<!DOCTYPE html>\
<html>\
<head>\
<title>ESP8266 Upload</title>\
<meta charset=\"utf-8\">\
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
</head>\
<body>\
<form action=\"/edit\" method=\"post\" enctype=\"multipart/form-data\">\
<input type=\"file\" name=\"data\">\
<input type=\"text\" name=\"path\" value=\"/\">\
<button>Upload</button>\
</form>\
</body>\
</html>",
hr, min % 60, sec % 60
);
server.send ( 200, "text/html", temp );
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for ( uint8_t i = 0; i < server.args(); i++ ) {
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
}
server.send ( 404, "text/plain", message );
}
char* listStatusJSON() {
char json[255];
snprintf(json, sizeof(json), "{\"mode\":%d, \"ws2812fx_mode\":%d, \"ws2812fx_mode_name\":\"%s\", \"speed\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}", mode, strip.getMode(), strip.getModeName(strip.getMode()), ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue);
@ -202,6 +152,57 @@ void getModesJSON() {
server.send ( 200, "application/json", listModesJSON() );
}
// ***************************************************************************
// HTTP request handlers
// ***************************************************************************
void handleMinimalUpload() {
char temp[1500];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf ( temp, 1500,
"<!DOCTYPE html>\
<html>\
<head>\
<title>ESP8266 Upload</title>\
<meta charset=\"utf-8\">\
<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
</head>\
<body>\
<form action=\"/edit\" method=\"post\" enctype=\"multipart/form-data\">\
<input type=\"file\" name=\"data\">\
<input type=\"text\" name=\"path\" value=\"/\">\
<button>Upload</button>\
</form>\
</body>\
</html>",
hr, min % 60, sec % 60
);
server.send ( 200, "text/html", temp );
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += ( server.method() == HTTP_GET ) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for ( uint8_t i = 0; i < server.args(); i++ ) {
message += " " + server.argName ( i ) + ": " + server.arg ( i ) + "\n";
}
server.send ( 404, "text/plain", message );
}
// ***************************************************************************
// WS request handlers
// ***************************************************************************
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
switch (type) {
case WStype_DISCONNECTED:
@ -381,7 +382,7 @@ void checkForRequests() {
}
// / ==> Set WS2812 mode.
if ((char)payload[0] == '/') {
if (payload[0] == '/') {
handleSetWS2812FXMode(payload);
DBG_OUTPUT_PORT.printf("MQTT: Set WS2812 mode [%s]\n", payload);
mqtt_client.publish(mqtt_outtopic, "OK");
@ -402,6 +403,9 @@ void checkForRequests() {
mqtt_client.publish(mqtt_outtopic, message);
// ... and resubscribe
mqtt_client.subscribe(mqtt_intopic);
DBG_OUTPUT_PORT.printf("MQTT topic in: %s\n", mqtt_intopic);
DBG_OUTPUT_PORT.printf("MQTT topic out: %s\n", mqtt_outtopic);
} else {
DBG_OUTPUT_PORT.print("failed, rc=");
DBG_OUTPUT_PORT.print(mqtt_client.state());