Progress on mqtt connection for esp8266 rgb strips
This commit is contained in:
parent
57de764ed1
commit
6cd9483290
2 changed files with 108 additions and 63 deletions
|
@ -1,3 +1,15 @@
|
||||||
// WiFi configuration
|
// WiFi configuration
|
||||||
const char* ssid = "http://kiel.freifunk.net/";
|
const char* ssid = "http://kiel.freifunk.net/";
|
||||||
const char* password = "";
|
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";
|
||||||
|
|
|
@ -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.
|
* ESP8266-RGB-MQTT
|
||||||
|
*
|
||||||
It connects to an MQTT server then:
|
* Links the ESP8266 based RGB led strip to an mqtt broker.
|
||||||
- publishes "hello world" to the topic "outTopic" every two seconds
|
* The program uses multiple topics for incoming and outgoing messages.
|
||||||
- subscribes to the topic "inTopic", printing out any messages
|
* Incoming topics:
|
||||||
it receives. NB - it assumes the received payloads are strings not binary
|
* - <mqtt_device>/power
|
||||||
- If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
|
* - <mqtt_device>/color
|
||||||
else switch it off
|
* - <mqtt_device>/breathing
|
||||||
|
* - <mqtt_device>/cycle
|
||||||
It will reconnect to the server if the connection is lost using a blocking
|
* Outgoing topics:
|
||||||
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"
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
#include "ESP8266-RGB-MQTT.h"
|
||||||
|
|
||||||
// Update these with values suitable for your network.
|
// Global mqtt client object
|
||||||
|
|
||||||
const char* ssid = "........";
|
|
||||||
const char* password = "........";
|
|
||||||
const char* mqtt_server = "broker.mqtt-dashboard.com";
|
|
||||||
|
|
||||||
WiFiClient espClient;
|
WiFiClient espClient;
|
||||||
PubSubClient client(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() {
|
void setup() {
|
||||||
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
|
// Configure serial port
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
delay(10);
|
||||||
|
|
||||||
|
// Prepare WiFi connection
|
||||||
setup_wifi();
|
setup_wifi();
|
||||||
|
|
||||||
|
// Connect to mqtt broker
|
||||||
client.setServer(mqtt_server, 1883);
|
client.setServer(mqtt_server, 1883);
|
||||||
client.setCallback(callback);
|
client.setCallback(incoming_mqtt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Prepares the wireless connection
|
||||||
|
*/
|
||||||
void setup_wifi() {
|
void setup_wifi() {
|
||||||
|
// Connect to the WiFi as a client
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
|
||||||
delay(10);
|
// Do the connection
|
||||||
// We start by connecting to a WiFi network
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.print("Connecting to ");
|
Serial.print("Connecting to ");
|
||||||
Serial.println(ssid);
|
Serial.println(ssid);
|
||||||
|
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
Serial.println("IP address: ");
|
|
||||||
|
// Print IP address to serial
|
||||||
|
Serial.print("My IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
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("Message arrived [");
|
||||||
Serial.print(topic);
|
Serial.print(topic);
|
||||||
Serial.print("] ");
|
Serial.print("] ");
|
||||||
|
@ -76,28 +103,38 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
||||||
}
|
}
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
// Switch on the LED if an 1 was received as first character
|
if (strcmp(topic, mqtt_topic_power) == 0) {
|
||||||
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
|
if (strcmp(topic, mqtt_topic_color) == 0) {
|
||||||
// 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_cycle) == 0) {
|
||||||
|
|
||||||
|
}
|
||||||
|
if (strcmp(topic, mqtt_topic_breathe) == 0) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Blocking reconnect to the mqtt broker
|
||||||
|
*/
|
||||||
void reconnect() {
|
void reconnect() {
|
||||||
// Loop until we're reconnected
|
// Loop until we're reconnected
|
||||||
while (!client.connected()) {
|
while (!client.connected()) {
|
||||||
Serial.print("Attempting MQTT connection...");
|
Serial.print("Attempting MQTT connection...");
|
||||||
// Attempt to connect
|
// Attempt to connect
|
||||||
if (client.connect("ESP8266Client")) {
|
if (client.connect(mqtt_device)) {
|
||||||
Serial.println("connected");
|
Serial.println("connected");
|
||||||
// Once connected, publish an announcement...
|
// Once connected, publish an announcement...
|
||||||
client.publish("outTopic", "hello world");
|
// client.publish("outTopic", "hello world");
|
||||||
// ... and resubscribe
|
// subscribe to incoming topics
|
||||||
client.subscribe("inTopic");
|
client.subscribe(mqtt_topic_power);
|
||||||
|
client.subscribe(mqtt_topic_color);
|
||||||
|
client.subscribe(mqtt_topic_cycle);
|
||||||
|
client.subscribe(mqtt_topic_breathe);
|
||||||
} else {
|
} else {
|
||||||
Serial.print("failed, rc=");
|
Serial.print("failed, rc=");
|
||||||
Serial.print(client.state());
|
Serial.print(client.state());
|
||||||
|
@ -107,20 +144,16 @@ void reconnect() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void loop() {
|
|
||||||
|
|
||||||
|
void updateColor() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
if (!client.connected()) {
|
if (!client.connected()) {
|
||||||
reconnect();
|
reconnect();
|
||||||
}
|
}
|
||||||
client.loop();
|
client.loop();
|
||||||
|
|
||||||
long now = millis();
|
updateColor();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue