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
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
||||
// ***************************************************************************
|
||||
|
|
|
@ -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,22 +117,45 @@ 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);
|
||||
}
|
||||
|
||||
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() {
|
||||
char temp[1500];
|
||||
int sec = millis() / 1000;
|
||||
|
@ -175,33 +199,10 @@ void handleNotFound() {
|
|||
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) {
|
||||
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());
|
||||
|
|
Loading…
Reference in a new issue