Dynamic topic names
This commit is contained in:
parent
b71834bbb2
commit
90578a4b33
3 changed files with 75 additions and 68 deletions
|
@ -120,7 +120,7 @@ void setup() {
|
||||||
// set builtin led pin as output
|
// set builtin led pin as output
|
||||||
pinMode(BUILTIN_LED, OUTPUT);
|
pinMode(BUILTIN_LED, OUTPUT);
|
||||||
// start ticker with 0.5 because we start in AP mode and try to connect
|
// 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
|
// Setup: Neopixel
|
||||||
|
@ -207,6 +207,9 @@ void setup() {
|
||||||
// Configure MQTT
|
// Configure MQTT
|
||||||
// ***************************************************************************
|
// ***************************************************************************
|
||||||
#ifdef ENABLE_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.setServer(mqtt_server, 1883);
|
||||||
mqtt_client.setCallback(mqtt_callback);
|
mqtt_client.setCallback(mqtt_callback);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -3,16 +3,16 @@
|
||||||
#define NUMLEDS 24 // Number of leds in the strip
|
#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_OTA // If defined, enable Arduino OTA code.
|
||||||
|
|
||||||
#define ENABLE_MQTT // If defined, enable MQTT client code.
|
#define ENABLE_MQTT // If defined, enable MQTT client code.
|
||||||
#define MQTT_MAX_PACKET_SIZE 4096
|
|
||||||
#ifdef ENABLE_MQTT
|
#ifdef ENABLE_MQTT
|
||||||
const char mqtt_intopic[] = "inTopic";
|
#define MQTT_MAX_PACKET_SIZE 256
|
||||||
const char mqtt_outtopic[] = "outTopic";
|
char mqtt_intopic[strlen(HOSTNAME) + 3]; // Topic in will be: <HOSTNAME>/in
|
||||||
const char mqtt_server[] = "raspberrypi2";
|
char mqtt_outtopic[strlen(HOSTNAME) + 4]; // Topic out will be: <HOSTNAME>/out
|
||||||
|
const char mqtt_server[] = "raspberrypi2"; // Hostname of the MQTT broker
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// ***************************************************************************
|
// ***************************************************************************
|
||||||
|
|
|
@ -38,20 +38,21 @@ void getArgs() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ***************************************************************************
|
||||||
|
// Handler functions for WS and MQTT
|
||||||
void handleSetMainColor(uint8_t * payload) {
|
// ***************************************************************************
|
||||||
|
void handleSetMainColor(uint8_t * mypayload) {
|
||||||
// decode rgb data
|
// 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.red = ((rgb >> 16) & 0xFF);
|
||||||
main_color.green = ((rgb >> 8) & 0xFF);
|
main_color.green = ((rgb >> 8) & 0xFF);
|
||||||
main_color.blue = ((rgb >> 0) & 0xFF);
|
main_color.blue = ((rgb >> 0) & 0xFF);
|
||||||
strip.setColor(main_color.red, main_color.green, main_color.blue);
|
strip.setColor(main_color.red, main_color.green, main_color.blue);
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleSetAllMode(uint8_t * payload) {
|
void handleSetAllMode(uint8_t * mypayload) {
|
||||||
// decode rgb data
|
// 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.red = ((rgb >> 16) & 0xFF);
|
||||||
main_color.green = ((rgb >> 8) & 0xFF);
|
main_color.green = ((rgb >> 8) & 0xFF);
|
||||||
|
@ -66,9 +67,9 @@ void handleSetAllMode(uint8_t * payload) {
|
||||||
mode = ALL;
|
mode = ALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleSetSingleLED(uint8_t * payload) {
|
void handleSetSingleLED(uint8_t * mypayload) {
|
||||||
// decode led index
|
// 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);
|
uint8_t led = ((rgb >> 24) & 0xFF);
|
||||||
if (led < strip.numPixels()) {
|
if (led < strip.numPixels()) {
|
||||||
|
@ -116,22 +117,45 @@ void handleSetNamedMode(String str_mode) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleSetWS2812FXMode(uint8_t * payload) {
|
void handleSetWS2812FXMode(uint8_t * mypayload) {
|
||||||
mode = HOLD;
|
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);
|
ws2812fx_mode = constrain(ws2812fx_mode, 0, 255);
|
||||||
strip.setColor(main_color.red, main_color.green, main_color.blue);
|
strip.setColor(main_color.red, main_color.green, main_color.blue);
|
||||||
strip.setMode(ws2812fx_mode);
|
strip.setMode(ws2812fx_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
void getStatusJSON() {
|
||||||
|
server.send ( 200, "application/json", listStatusJSON() );
|
||||||
|
}
|
||||||
|
|
||||||
|
String listModesJSON() {
|
||||||
|
String modes = "[";
|
||||||
|
for(uint8_t i=0; i < strip.getModeCount(); i++) {
|
||||||
|
modes += "{\"mode\":";
|
||||||
|
modes += i;
|
||||||
|
modes += ", \"name\":\"";
|
||||||
|
modes += strip.getModeName(i);
|
||||||
|
modes += "\"},";
|
||||||
|
}
|
||||||
|
modes += "{}]";
|
||||||
|
return modes;
|
||||||
|
}
|
||||||
|
|
||||||
|
void getModesJSON() {
|
||||||
|
server.send ( 200, "application/json", listModesJSON() );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ***************************************************************************
|
||||||
|
// HTTP request handlers
|
||||||
|
// ***************************************************************************
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void handleMinimalUpload() {
|
void handleMinimalUpload() {
|
||||||
char temp[1500];
|
char temp[1500];
|
||||||
int sec = millis() / 1000;
|
int sec = millis() / 1000;
|
||||||
|
@ -175,33 +199,10 @@ void handleNotFound() {
|
||||||
server.send ( 404, "text/plain", message );
|
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);
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
|
|
||||||
void getStatusJSON() {
|
|
||||||
server.send ( 200, "application/json", listStatusJSON() );
|
|
||||||
}
|
|
||||||
|
|
||||||
String listModesJSON() {
|
|
||||||
String modes = "[";
|
|
||||||
for(uint8_t i=0; i < strip.getModeCount(); i++) {
|
|
||||||
modes += "{\"mode\":";
|
|
||||||
modes += i;
|
|
||||||
modes += ", \"name\":\"";
|
|
||||||
modes += strip.getModeName(i);
|
|
||||||
modes += "\"},";
|
|
||||||
}
|
|
||||||
modes += "{}]";
|
|
||||||
return modes;
|
|
||||||
}
|
|
||||||
|
|
||||||
void getModesJSON() {
|
|
||||||
server.send ( 200, "application/json", listModesJSON() );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// ***************************************************************************
|
||||||
|
// WS request handlers
|
||||||
|
// ***************************************************************************
|
||||||
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case WStype_DISCONNECTED:
|
case WStype_DISCONNECTED:
|
||||||
|
@ -381,7 +382,7 @@ void checkForRequests() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// / ==> Set WS2812 mode.
|
// / ==> Set WS2812 mode.
|
||||||
if ((char)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, "OK");
|
||||||
|
@ -402,6 +403,9 @@ void checkForRequests() {
|
||||||
mqtt_client.publish(mqtt_outtopic, message);
|
mqtt_client.publish(mqtt_outtopic, message);
|
||||||
// ... and resubscribe
|
// ... and resubscribe
|
||||||
mqtt_client.subscribe(mqtt_intopic);
|
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 {
|
} else {
|
||||||
DBG_OUTPUT_PORT.print("failed, rc=");
|
DBG_OUTPUT_PORT.print("failed, rc=");
|
||||||
DBG_OUTPUT_PORT.print(mqtt_client.state());
|
DBG_OUTPUT_PORT.print(mqtt_client.state());
|
||||||
|
|
Loading…
Reference in a new issue