From 82d5df44bb438d1dcf5e03c4ec67e63a226298a3 Mon Sep 17 00:00:00 2001 From: Corban Mailloux Date: Sun, 10 Dec 2017 15:35:23 -0500 Subject: [PATCH] Use the white pin for brightness. --- mqtt_esp8266/config-sample.h | 26 ++++++++------- mqtt_esp8266/mqtt_esp8266.ino | 60 ++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/mqtt_esp8266/config-sample.h b/mqtt_esp8266/config-sample.h index 1b826c0..fdab441 100644 --- a/mqtt_esp8266/config-sample.h +++ b/mqtt_esp8266/config-sample.h @@ -5,22 +5,24 @@ * You can then upload the code using the Arduino IDE. */ -// Define the strip type -enum strip { BRIGHTNESS // only one color - , RGB // RGB LEDs - , RGBW // RGB LEDs with an extra white LED per LED - }; -#define CONFIG_STRIP RGB +// Leave this here. These are the choices for CONFIG_STRIP below. +enum strip { + BRIGHTNESS, // only one color + RGB, // RGB LEDs + RGBW // RGB LEDs with an extra white LED per LED +}; + +#define CONFIG_STRIP RGB // Choose one of the options from above. // Pins -// In case of BRIGHTNESS: only RED is used +// In case of BRIGHTNESS: only WHITE is used // In case of RGB(W): red, green, blue(, white) is used -// All values need to be present, if they are not needed, just write any value, +// All values need to be present, if they are not needed, set to -1, // it will be ignored. -#define CONFIG_PIN_RED 0 -#define CONFIG_PIN_GREEN 2 // only needed for RGB(W) strips -#define CONFIG_PIN_BLUE 3 // only needed for RGB(W) strips -#define CONFIG_PIN_WHITE 4 // only needed for RGBW strips +#define CONFIG_PIN_RED 0 // For RGB(W) +#define CONFIG_PIN_GREEN 2 // For RGB(W) +#define CONFIG_PIN_BLUE 3 // For RGB(W) +#define CONFIG_PIN_WHITE -1 // For BRIGHTNESS and RGBW // WiFi #define CONFIG_WIFI_SSID "{WIFI-SSID}" diff --git a/mqtt_esp8266/mqtt_esp8266.ino b/mqtt_esp8266/mqtt_esp8266.ino index 2f00c56..589aade 100644 --- a/mqtt_esp8266/mqtt_esp8266.ino +++ b/mqtt_esp8266/mqtt_esp8266.ino @@ -17,8 +17,8 @@ // http://pubsubclient.knolleary.net/ #include -const bool rgb = CONFIG_STRIP == RGB || CONFIG_STRIP == RGBW; -const bool rgbw = CONFIG_STRIP == RGBW; +const bool rgb = (CONFIG_STRIP == RGB) || (CONFIG_STRIP == RGBW); +const bool includeWhite = (CONFIG_STRIP == BRIGHTNESS) || (CONFIG_STRIP == RGBW); const bool debug_mode = CONFIG_DEBUG; const bool led_invert = CONFIG_INVERT_LED_LOGIC; @@ -99,13 +99,13 @@ WiFiClient espClient; PubSubClient client(espClient); void setup() { - pinMode(redPin, OUTPUT); if (rgb) { + pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); - if (rgbw) { - pinMode(whitePin, OUTPUT); - } + } + if (includeWhite) { + pinMode(whitePin, OUTPUT); } pinMode(txPin, OUTPUT); @@ -153,7 +153,7 @@ void setup_wifi() { "state": "ON" } - SAMPLE PAYLOAD (RGB): + SAMPLE PAYLOAD (RGBW): { "brightness": 120, "color": { @@ -252,7 +252,7 @@ bool processJson(char* message) { flashBlue = blue; } - if (rgbw && root.containsKey("white_value")) { + if (includeWhite && root.containsKey("white_value")) { flashWhite = root["white_value"]; } else { @@ -294,7 +294,7 @@ bool processJson(char* message) { blue = root["color"]["b"]; } - if (rgbw && root.containsKey("white_value")) { + if (includeWhite && root.containsKey("white_value")) { white = root["white_value"]; } @@ -328,7 +328,7 @@ void sendState() { root["brightness"] = brightness; - if (rgbw) { + if (includeWhite) { root["white_value"] = white; } @@ -376,36 +376,44 @@ void setColor(int inR, int inG, int inB, int inW) { inW = (255 - inW); } - analogWrite(redPin, inR); if (rgb) { + analogWrite(redPin, inR); analogWrite(greenPin, inG); analogWrite(bluePin, inB); - if (rgbw) { - analogWrite(whitePin, inW); - } } - Serial.println("Setting LEDs:"); - Serial.print(rgb ? "r: " : ""); - Serial.print(inR); - if (rgb) { - Serial.print(", g: "); - Serial.print(inG); - Serial.print(", b: "); - Serial.print(inB); - if (rgbw) { - Serial.print(", w: "); + if (includeWhite) { + analogWrite(whitePin, inW); + } + + if (debug_mode) { + Serial.print("Setting LEDs: {"); + if (rgb) { + Serial.print("r: "); + Serial.print(inR); + Serial.print(" , g: "); + Serial.print(inG); + Serial.print(" , b: "); + Serial.print(inB); + } + + if (includeWhite) { + if (rgb) { + Serial.print(", "); + } + Serial.print("w: "); Serial.print(inW); } + + Serial.println("}"); } - Serial.println(); } void loop() { - if (!client.connected()) { reconnect(); } + client.loop(); if (flash) {