Arduino/ESP8266-RGB-MQTT/ESP8266-RGB-MQTT.ino

160 lines
4.1 KiB
Arduino
Raw Normal View History

/**********************************************************************
* 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 <PubSubClient.h>
#include "ESP8266-RGB-MQTT.h"
// Global mqtt client object
WiFiClient espClient;
PubSubClient client(espClient);
// RGB strip pin configuration
const short redPin = 14;
const short greenPin = 16;
const short bluePin = 12;
/*
* 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());
}
/*
* 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) {
}
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(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 updateColor() {
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
updateColor();
}