From 6cd948329003d774f56f144e7d64f1e050c52b8e Mon Sep 17 00:00:00 2001 From: Nis Wechselberg Date: Tue, 14 Mar 2017 00:52:46 +0100 Subject: [PATCH] Progress on mqtt connection for esp8266 rgb strips --- ESP8266-RGB-MQTT/ESP8266-RGB-MQTT.h.template | 12 ++ ESP8266-RGB-MQTT/ESP8266-RGB-MQTT.ino | 159 +++++++++++-------- 2 files changed, 108 insertions(+), 63 deletions(-) diff --git a/ESP8266-RGB-MQTT/ESP8266-RGB-MQTT.h.template b/ESP8266-RGB-MQTT/ESP8266-RGB-MQTT.h.template index f279001..acaa1de 100644 --- a/ESP8266-RGB-MQTT/ESP8266-RGB-MQTT.h.template +++ b/ESP8266-RGB-MQTT/ESP8266-RGB-MQTT.h.template @@ -1,3 +1,15 @@ // WiFi configuration const char* ssid = "http://kiel.freifunk.net/"; const char* password = ""; + +// MQTT Broker +const char* mqtt_server = "broker.mqtt-dashboard.com"; + +// Device identification +const char* mqtt_device_name = "esp8266client" + +// MQTT topics +const char* mqtt_topic_power = "esp8266client/power"; +const char* mqtt_topic_color = "esp8266client/color"; +const char* mqtt_topic_breathe = "esp8266client/breathe"; +const char* mqtt_topic_cycle = "esp8266client/cycle"; diff --git a/ESP8266-RGB-MQTT/ESP8266-RGB-MQTT.ino b/ESP8266-RGB-MQTT/ESP8266-RGB-MQTT.ino index e46f85f..f078df1 100644 --- a/ESP8266-RGB-MQTT/ESP8266-RGB-MQTT.ino +++ b/ESP8266-RGB-MQTT/ESP8266-RGB-MQTT.ino @@ -1,73 +1,100 @@ -/* - Basic ESP8266 MQTT example +/********************************************************************** + * 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. + **********************************************************************/ - This sketch demonstrates the capabilities of the pubsub library in combination - with the ESP8266 board/library. - - It connects to an MQTT server then: - - publishes "hello world" to the topic "outTopic" every two seconds - - subscribes to the topic "inTopic", printing out any messages - it receives. NB - it assumes the received payloads are strings not binary - - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led, - else switch it off - - It will reconnect to the server if the connection is lost using a blocking - reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to - achieve the same result without blocking the main loop. - - To install the ESP8266 board, (using Arduino 1.6.4+): - - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs": - http://arduino.esp8266.com/stable/package_esp8266com_index.json - - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266" - - Select your ESP8266 in "Tools -> Board" - -*/ +/********************************************************************** + * 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: + * - /power + * - /color + * - /breathing + * - /cycle + * Outgoing topics: + * + **********************************************************************/ #include #include +#include "ESP8266-RGB-MQTT.h" -// Update these with values suitable for your network. - -const char* ssid = "........"; -const char* password = "........"; -const char* mqtt_server = "broker.mqtt-dashboard.com"; - +// Global mqtt client object WiFiClient espClient; PubSubClient client(espClient); -long lastMsg = 0; -char msg[50]; -int value = 0; +// RGB strip pin configuration +const short redPin = 14; +const short greenPin = 16; +const short bluePin = 12; + + + +/* + * Initial setup for arduino + */ void setup() { - pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output + // Configure serial port Serial.begin(115200); + delay(10); + + // Prepare WiFi connection setup_wifi(); + + // Connect to mqtt broker client.setServer(mqtt_server, 1883); - client.setCallback(callback); + client.setCallback(incoming_mqtt); } +/* + * Prepares the wireless connection + */ void setup_wifi() { - - delay(10); - // We start by connecting to a WiFi network + // 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"); - Serial.println("IP address: "); + + // Print IP address to serial + Serial.print("My IP address: "); Serial.println(WiFi.localIP()); } -void callback(char* topic, byte* payload, unsigned int length) { +/* + * 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("] "); @@ -76,28 +103,38 @@ void callback(char* topic, byte* payload, unsigned int length) { } Serial.println(); - // Switch on the LED if an 1 was received as first character - if ((char)payload[0] == '1') { - digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level - // but actually the LED is on; this is because - // it is acive low on the ESP-01) - } else { - digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH + if (strcmp(topic, mqtt_topic_power) == 0) { + + } + if (strcmp(topic, mqtt_topic_color) == 0) { + + } + if (strcmp(topic, mqtt_topic_cycle) == 0) { + + } + if (strcmp(topic, mqtt_topic_breathe) == 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("ESP8266Client")) { + if (client.connect(mqtt_device)) { Serial.println("connected"); // Once connected, publish an announcement... - client.publish("outTopic", "hello world"); - // ... and resubscribe - client.subscribe("inTopic"); + // 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()); @@ -107,20 +144,16 @@ void reconnect() { } } } -void loop() { +void updateColor() { + +} + +void loop() { if (!client.connected()) { reconnect(); } client.loop(); - long now = millis(); - if (now - lastMsg > 2000) { - lastMsg = now; - ++value; - snprintf (msg, 75, "hello world #%ld", value); - Serial.print("Publish message: "); - Serial.println(msg); - client.publish("outTopic", msg); - } + updateColor(); }