Arduino/ESP8266-MQTT-RGBControl/ESP8266-MQTT-RGBControl.ino
2017-09-26 01:11:50 +02:00

339 lines
8.6 KiB
C++

/**********************************************************************
* 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
*
* 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:
*
**********************************************************************/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include "ESP8266-MQTT-RGBControl.h"
// Global mqtt client object
WiFiClient espClient;
PubSubClient client(espClient);
// RGB strip pin configuration
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;
// Global scaling for pwm, due to pwm scale 0..1023
const float globalScale = 3.0;
// Pointer to the current decreasing color
unsigned char decColor = 0;
// Timestamp for last color update
unsigned long lastColorUpdate = 0;
// Timestamp for last publish
unsigned long lastMsg = 0;
unsigned long publishInterval = 5000;
// Message buffer
char msg[50];
/*
* Initial setup for arduino
*/
void setup() {
// Configure serial port
Serial.begin(115200);
delay(10);
// Prepare WiFi connection
setup_wifi();
// Connect to mqtt broker
client.setServer(mqtt_server, 1883);
client.setCallback(incoming_mqtt);
}
/*
* Prepares the wireless connection
*/
void setup_wifi() {
// Connect to the WiFi as a client
WiFi.mode(WIFI_STA);
// Do the connection
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");
// Print IP address to serial
Serial.print("My IP address: ");
Serial.println(WiFi.localIP());
ArduinoOTA.setHostname(ota_hostname);
ArduinoOTA.setPassword(ota_hostname);
ArduinoOTA.begin();
}
/*
* Callback method for incoming mqtt messages
*/
void incoming_mqtt(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if (strcmp(topic, mqtt_topic_power) == 0) {
isPowerSet = 1;
if (length == 1) {
Serial.println("Setting power factor");
if ((char)payload[0] == '1') {
powerFactor = 1;
} else {
powerFactor = 0;
}
}
}
if (strcmp(topic, mqtt_topic_color) == 0) {
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;
if (i % 2 == 1) {
// Write colorValue to rgbValue
rgbColor[i/2] = colorValue;
}
i += 1;
}
}
}
if (strcmp(topic, mqtt_topic_cycle) == 0) {
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;
}
}
}
}
if (strcmp(topic, mqtt_topic_breathe) == 0) {
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;
}
}
}
}
}
/*
* Blocking reconnect to the mqtt broker
*/
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_device)) {
Serial.println("connected");
// Once connected, publish an announcement...
// 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);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
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]);
}
}
void publishState() {
snprintf(msg, 49, "%d", powerFactor);
client.publish(mqtt_topic_powerState, msg);
snprintf(msg, 49, "#%02X%02X%02X", rgbColor[0], rgbColor[1], rgbColor[2]);
client.publish(mqtt_topic_colorState, msg);
snprintf(msg, 49, "%d", doBreathe);
client.publish(mqtt_topic_breatheState, msg);
snprintf(msg, 49, "%d", doColorCycle);
client.publish(mqtt_topic_cycleState, msg);
}
void loop() {
// Check OTA update
ArduinoOTA.handle();
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long now = millis();
if (isPowerSet & isColorSet & isCycleSet & isBreatheSet) {
if (now - lastColorUpdate > 30) {
lastColorUpdate = now;
updateLight();
}
}
if (now - lastMsg > publishInterval) {
lastMsg = now;
publishState();
}
}