2017-03-14 00:52:46 +01:00
|
|
|
/**********************************************************************
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Nis Wechselberg
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
* ESP8266-RGB-MQTT
|
2017-03-14 23:10:00 +01:00
|
|
|
*
|
2017-03-14 00:52:46 +01:00
|
|
|
* Links the ESP8266 based RGB led strip to an mqtt broker.
|
|
|
|
* The program uses multiple topics for incoming and outgoing messages.
|
|
|
|
* Incoming topics:
|
|
|
|
* - <mqtt_device>/power
|
|
|
|
* - <mqtt_device>/color
|
|
|
|
* - <mqtt_device>/breathing
|
|
|
|
* - <mqtt_device>/cycle
|
|
|
|
* Outgoing topics:
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2017-03-13 08:30:48 +01:00
|
|
|
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <PubSubClient.h>
|
2017-03-24 12:50:31 +01:00
|
|
|
#include "ESP8266-MQTT-RGBControl.h"
|
2017-03-13 08:30:48 +01:00
|
|
|
|
2017-03-14 00:52:46 +01:00
|
|
|
// Global mqtt client object
|
2017-03-13 08:30:48 +01:00
|
|
|
WiFiClient espClient;
|
|
|
|
PubSubClient client(espClient);
|
|
|
|
|
2017-03-14 00:52:46 +01:00
|
|
|
// RGB strip pin configuration
|
2017-03-14 23:10:00 +01:00
|
|
|
const unsigned char pin[3] = {14,16,12};
|
|
|
|
|
|
|
|
// Color values for the pwm outputs
|
|
|
|
unsigned char rgbColor[3];
|
|
|
|
|
|
|
|
// Scaling factors for color calibration
|
|
|
|
const float calibration[3] = {1.0, 1.0, 0.7};
|
|
|
|
|
|
|
|
// Flagss to determine if all necessary information
|
|
|
|
// has been gathered from mqtt broker
|
|
|
|
unsigned char isColorSet = 0;
|
|
|
|
unsigned char isCycleSet = 0;
|
|
|
|
unsigned char isBreatheSet = 0;
|
|
|
|
unsigned char isPowerSet = 0;
|
|
|
|
|
|
|
|
// Scaling factors for different color adjustments
|
|
|
|
unsigned char powerFactor = 0;
|
|
|
|
unsigned char doColorCycle = 0;
|
|
|
|
unsigned char doBreathe = 0;
|
|
|
|
float breathScale = 0.999;
|
|
|
|
float breathAdjustment = -0.001;
|
2017-03-14 00:52:46 +01:00
|
|
|
|
2017-03-14 23:10:00 +01:00
|
|
|
// Global scaling for pwm, due to pwm scale 0..1023
|
|
|
|
const float globalScale = 3.0;
|
2017-03-14 00:52:46 +01:00
|
|
|
|
2017-03-14 23:10:00 +01:00
|
|
|
// Pointer to the current decreasing color
|
|
|
|
unsigned char decColor = 0;
|
2017-03-14 00:52:46 +01:00
|
|
|
|
2017-03-14 23:10:00 +01:00
|
|
|
// Timestamp for last color update
|
|
|
|
unsigned long lastColorUpdate = 0;
|
2017-03-14 00:52:46 +01:00
|
|
|
/*
|
|
|
|
* Initial setup for arduino
|
|
|
|
*/
|
2017-03-13 08:30:48 +01:00
|
|
|
void setup() {
|
2017-03-14 00:52:46 +01:00
|
|
|
// Configure serial port
|
2017-03-13 08:30:48 +01:00
|
|
|
Serial.begin(115200);
|
2017-03-14 00:52:46 +01:00
|
|
|
delay(10);
|
2017-03-14 23:10:00 +01:00
|
|
|
|
2017-03-14 00:52:46 +01:00
|
|
|
// Prepare WiFi connection
|
2017-03-13 08:30:48 +01:00
|
|
|
setup_wifi();
|
2017-03-14 00:52:46 +01:00
|
|
|
|
|
|
|
// Connect to mqtt broker
|
2017-03-13 08:30:48 +01:00
|
|
|
client.setServer(mqtt_server, 1883);
|
2017-03-14 00:52:46 +01:00
|
|
|
client.setCallback(incoming_mqtt);
|
2017-03-13 08:30:48 +01:00
|
|
|
}
|
|
|
|
|
2017-03-14 00:52:46 +01:00
|
|
|
/*
|
|
|
|
* Prepares the wireless connection
|
|
|
|
*/
|
2017-03-13 08:30:48 +01:00
|
|
|
void setup_wifi() {
|
2017-03-14 00:52:46 +01:00
|
|
|
// Connect to the WiFi as a client
|
|
|
|
WiFi.mode(WIFI_STA);
|
2017-03-14 23:10:00 +01:00
|
|
|
|
2017-03-14 00:52:46 +01:00
|
|
|
// Do the connection
|
2017-03-13 08:30:48 +01:00
|
|
|
Serial.println();
|
|
|
|
Serial.print("Connecting to ");
|
|
|
|
Serial.println(ssid);
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
delay(500);
|
|
|
|
Serial.print(".");
|
|
|
|
}
|
|
|
|
Serial.println("");
|
|
|
|
Serial.println("WiFi connected");
|
2017-03-14 23:10:00 +01:00
|
|
|
|
2017-03-14 00:52:46 +01:00
|
|
|
// Print IP address to serial
|
|
|
|
Serial.print("My IP address: ");
|
2017-03-13 08:30:48 +01:00
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
}
|
|
|
|
|
2017-03-14 00:52:46 +01:00
|
|
|
/*
|
|
|
|
* Callback method for incoming mqtt messages
|
|
|
|
*/
|
|
|
|
void incoming_mqtt(char* topic, byte* payload, unsigned int length) {
|
2017-03-13 08:30:48 +01:00
|
|
|
Serial.print("Message arrived [");
|
|
|
|
Serial.print(topic);
|
|
|
|
Serial.print("] ");
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
Serial.print((char)payload[i]);
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
|
2017-03-14 00:52:46 +01:00
|
|
|
if (strcmp(topic, mqtt_topic_power) == 0) {
|
2017-03-14 23:10:00 +01:00
|
|
|
isPowerSet = 1;
|
|
|
|
if (length == 1) {
|
|
|
|
Serial.println("Setting power factor");
|
|
|
|
if ((char)payload[0] == '1') {
|
|
|
|
powerFactor = 1;
|
|
|
|
} else {
|
|
|
|
powerFactor = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 00:52:46 +01:00
|
|
|
}
|
|
|
|
if (strcmp(topic, mqtt_topic_color) == 0) {
|
2017-03-14 23:10:00 +01:00
|
|
|
isColorSet = 1;
|
|
|
|
if (length == 7 && (char)payload[0] == '#') {
|
|
|
|
Serial.println("Setting color");
|
|
|
|
// Parse hex input to color values
|
|
|
|
unsigned char i = 0;
|
|
|
|
unsigned char colorValue;
|
|
|
|
while (i < 6) {
|
|
|
|
// Reset parsing container at start of color
|
|
|
|
if (i % 2 == 0) {
|
|
|
|
colorValue = 0;
|
|
|
|
}
|
|
|
|
// Grab current char
|
|
|
|
char data = (char) payload[i+1];
|
|
|
|
char value = 0;
|
|
|
|
if (data >= '0' && data <= '9') {
|
|
|
|
value = data - '0';
|
|
|
|
}
|
|
|
|
if (data >= 'A' && data <= 'F') {
|
|
|
|
value = data - 'A';
|
|
|
|
value += 10;
|
|
|
|
}
|
|
|
|
if (data >= 'a' && data <= 'f') {
|
|
|
|
value = data;
|
|
|
|
value += 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i % 2 == 0) {
|
|
|
|
// First hex digit, shift value to the left
|
|
|
|
value = value << 4;
|
|
|
|
}
|
|
|
|
// Add parsed value to accumulator
|
|
|
|
colorValue += value;
|
2017-03-14 00:52:46 +01:00
|
|
|
|
2017-03-14 23:10:00 +01:00
|
|
|
if (i % 2 == 1) {
|
|
|
|
// Write colorValue to rgbValue
|
|
|
|
rgbColor[i/2] = colorValue;
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 00:52:46 +01:00
|
|
|
}
|
|
|
|
if (strcmp(topic, mqtt_topic_cycle) == 0) {
|
2017-03-14 23:10:00 +01:00
|
|
|
isCycleSet = 1;
|
|
|
|
if (length == 1) {
|
|
|
|
Serial.println("Setting color cycling");
|
|
|
|
if ((char)payload[0] == '1') {
|
|
|
|
if (!doColorCycle) {
|
|
|
|
doColorCycle = 1;
|
|
|
|
// Reset decreasing color
|
|
|
|
decColor = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (doColorCycle) {
|
|
|
|
doColorCycle = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 00:52:46 +01:00
|
|
|
}
|
|
|
|
if (strcmp(topic, mqtt_topic_breathe) == 0) {
|
2017-03-14 23:10:00 +01:00
|
|
|
isBreatheSet = 1;
|
|
|
|
if (length == 1) {
|
|
|
|
Serial.println("Setting color breathing");
|
|
|
|
if ((char)payload[0] == '1') {
|
|
|
|
if (!doBreathe) {
|
|
|
|
doBreathe = 1;
|
|
|
|
breathScale = 0.999;
|
|
|
|
breathAdjustment = -0.001;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (doBreathe) {
|
|
|
|
doBreathe = 0;
|
|
|
|
breathScale = 1.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-13 08:30:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 00:52:46 +01:00
|
|
|
/*
|
|
|
|
* Blocking reconnect to the mqtt broker
|
|
|
|
*/
|
2017-03-13 08:30:48 +01:00
|
|
|
void reconnect() {
|
|
|
|
// Loop until we're reconnected
|
|
|
|
while (!client.connected()) {
|
|
|
|
Serial.print("Attempting MQTT connection...");
|
|
|
|
// Attempt to connect
|
2017-03-14 00:52:46 +01:00
|
|
|
if (client.connect(mqtt_device)) {
|
2017-03-13 08:30:48 +01:00
|
|
|
Serial.println("connected");
|
|
|
|
// Once connected, publish an announcement...
|
2017-03-14 00:52:46 +01:00
|
|
|
// client.publish("outTopic", "hello world");
|
|
|
|
// subscribe to incoming topics
|
|
|
|
client.subscribe(mqtt_topic_power);
|
|
|
|
client.subscribe(mqtt_topic_color);
|
|
|
|
client.subscribe(mqtt_topic_cycle);
|
|
|
|
client.subscribe(mqtt_topic_breathe);
|
2017-03-13 08:30:48 +01:00
|
|
|
} else {
|
|
|
|
Serial.print("failed, rc=");
|
|
|
|
Serial.print(client.state());
|
|
|
|
Serial.println(" try again in 5 seconds");
|
|
|
|
// Wait 5 seconds before retrying
|
|
|
|
delay(5000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 23:10:00 +01:00
|
|
|
void updateLight() {
|
|
|
|
if (doColorCycle) {
|
|
|
|
updateColorCycling();
|
|
|
|
}
|
|
|
|
if (doBreathe) {
|
|
|
|
updateBreathing();
|
|
|
|
}
|
|
|
|
setColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateColorCycling() {
|
|
|
|
if (rgbColor[0] == 0 && rgbColor[1] == 0 && rgbColor[2] == 0) {
|
|
|
|
rgbColor[0] = 255;
|
|
|
|
decColor = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char incColor = (decColor + 1) % 3;
|
|
|
|
if (rgbColor[decColor] > 0) {
|
|
|
|
rgbColor[decColor] -= 1;
|
|
|
|
}
|
|
|
|
if (rgbColor[incColor] < 255) {
|
|
|
|
rgbColor[incColor] += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rgbColor[incColor] == 255 && rgbColor[decColor] == 0) {
|
|
|
|
decColor = incColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateBreathing() {
|
|
|
|
breathScale += breathAdjustment;
|
|
|
|
|
|
|
|
if (breathScale <= 0.001 || breathScale >= 0.999) {
|
|
|
|
breathAdjustment = 0 - breathAdjustment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setColor() {
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
analogWrite(pin[i], rgbColor[i] * powerFactor * globalScale * breathScale * calibration[i]);
|
|
|
|
}
|
2017-03-14 00:52:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2017-03-13 08:30:48 +01:00
|
|
|
if (!client.connected()) {
|
|
|
|
reconnect();
|
|
|
|
}
|
|
|
|
client.loop();
|
|
|
|
|
2017-03-14 23:10:00 +01:00
|
|
|
if (isPowerSet & isColorSet & isCycleSet & isBreatheSet) {
|
|
|
|
unsigned long now = millis();
|
|
|
|
if (now - lastColorUpdate > 30) {
|
|
|
|
lastColorUpdate = now;
|
|
|
|
updateLight();
|
|
|
|
}
|
|
|
|
}
|
2017-03-13 08:30:48 +01:00
|
|
|
}
|