forked from SamZorSec/Open-Home-Automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathha_mqtt_rgb_light.ino
254 lines (217 loc) · 7.82 KB
/
ha_mqtt_rgb_light.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
MQTT RGB Light for Home-Assistant - NodeMCU (ESP8266)
https://home-assistant.io/components/light.mqtt/
Libraries :
- ESP8266 core for Arduino : https://github.com/esp8266/Arduino
- PubSubClient : https://github.com/knolleary/pubsubclient
Sources :
- File > Examples > ES8266WiFi > WiFiClient
- File > Examples > PubSubClient > mqtt_auth
- File > Examples > PubSubClient > mqtt_esp8266
- http://forum.arduino.cc/index.php?topic=272862.0
Schematic :
- https://github.com/mertenats/open-home-automation/blob/master/ha_mqtt_rgb_light/Schematic.png
- LED leg 1 - Resistor 270 Ohms - D1/GPIO5
- LED leg 2 (longuest leg) - GND
- LED leg 3 - Resistor 270 Ohms - D2/GPIO4
- LED leg 4 - Resistor 270 Ohms - D3/GPIO0
Configuration (HA) :
light:
platform: mqtt
name: 'Office RGB light'
state_topic: 'office/rgb1/light/status'
command_topic: 'office/rgb1/light/switch'
brightness_state_topic: 'office/rgb1/brightness/status'
brightness_command_topic: 'office/rgb1/brightness/set'
rgb_state_topic: 'office/rgb1/rgb/status'
rgb_command_topic: 'office/rgb1/rgb/set'
brightness_scale: 100
optimistic: false
Samuel M. - v1.1 - 08.2016
If you like this example, please add a star! Thank you!
https://github.com/mertenats/open-home-automation
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define MQTT_VERSION MQTT_VERSION_3_1_1
// Wifi: SSID and password
const char* WIFI_SSID = "[Redacted]";
const char* WIFI_PASSWORD = "[Redacted]";
// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "office_rgb_light";
const PROGMEM char* MQTT_SERVER_IP = "[Redacted]";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "[Redacted]";
const PROGMEM char* MQTT_PASSWORD = "[Redacted]";
// MQTT: topics
// state
const PROGMEM char* MQTT_LIGHT_STATE_TOPIC = "office/rgb1/light/status";
const PROGMEM char* MQTT_LIGHT_COMMAND_TOPIC = "office/rgb1/light/switch";
// brightness
const PROGMEM char* MQTT_LIGHT_BRIGHTNESS_STATE_TOPIC = "office/rgb1/brightness/status";
const PROGMEM char* MQTT_LIGHT_BRIGHTNESS_COMMAND_TOPIC = "office/rgb1/brightness/set";
// colors (rgb)
const PROGMEM char* MQTT_LIGHT_RGB_STATE_TOPIC = "office/rgb1/rgb/status";
const PROGMEM char* MQTT_LIGHT_RGB_COMMAND_TOPIC = "office/rgb1/rgb/set";
// payloads by default (on/off)
const PROGMEM char* LIGHT_ON = "ON";
const PROGMEM char* LIGHT_OFF = "OFF";
// variables used to store the state, the brightness and the color of the light
boolean m_rgb_state = false;
uint8_t m_rgb_brightness = 100;
uint8_t m_rgb_red = 255;
uint8_t m_rgb_green = 255;
uint8_t m_rgb_blue = 255;
// pins used for the rgb led (PWM)
const PROGMEM uint8_t RGB_LIGHT_RED_PIN = 5;
const PROGMEM uint8_t RGB_LIGHT_GREEN_PIN = 0;
const PROGMEM uint8_t RGB_LIGHT_BLUE_PIN = 4;
// buffer used to send/receive data with MQTT
const uint8_t MSG_BUFFER_SIZE = 20;
char m_msg_buffer[MSG_BUFFER_SIZE];
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// function called to adapt the brightness and the color of the led
void setColor(uint8_t p_red, uint8_t p_green, uint8_t p_blue) {
// convert the brightness in % (0-100%) into a digital value (0-255)
uint8_t brightness = map(m_rgb_brightness, 0, 100, 0, 255);
analogWrite(RGB_LIGHT_RED_PIN, map(p_red, 0, 255, 0, brightness));
analogWrite(RGB_LIGHT_GREEN_PIN, map(p_green, 0, 255, 0, brightness));
analogWrite(RGB_LIGHT_BLUE_PIN, map(p_blue, 0, 255, 0, brightness));
}
// function called to publish the state of the led (on/off)
void publishRGBState() {
if (m_rgb_state) {
client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_ON, true);
} else {
client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_OFF, true);
}
}
// function called to publish the brightness of the led (0-100)
void publishRGBBrightness() {
snprintf(m_msg_buffer, MSG_BUFFER_SIZE, "%d", m_rgb_brightness);
client.publish(MQTT_LIGHT_BRIGHTNESS_STATE_TOPIC, m_msg_buffer, true);
}
// function called to publish the colors of the led (xx(x),xx(x),xx(x))
void publishRGBColor() {
snprintf(m_msg_buffer, MSG_BUFFER_SIZE, "%d,%d,%d", m_rgb_red, m_rgb_green, m_rgb_blue);
client.publish(MQTT_LIGHT_RGB_STATE_TOPIC, m_msg_buffer, true);
}
// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
// concat the payload into a string
String payload;
for (uint8_t i = 0; i < p_length; i++) {
payload.concat((char)p_payload[i]);
}
// handle message topic
if (String(MQTT_LIGHT_COMMAND_TOPIC).equals(p_topic)) {
// test if the payload is equal to "ON" or "OFF"
if (payload.equals(String(LIGHT_ON))) {
if (m_rgb_state != true) {
m_rgb_state = true;
setColor(m_rgb_red, m_rgb_green, m_rgb_blue);
publishRGBState();
}
} else if (payload.equals(String(LIGHT_OFF))) {
if (m_rgb_state != false) {
m_rgb_state = false;
setColor(0, 0, 0);
publishRGBState();
}
}
} else if (String(MQTT_LIGHT_BRIGHTNESS_COMMAND_TOPIC).equals(p_topic)) {
uint8_t brightness = payload.toInt();
if (brightness < 0 || brightness > 100) {
// do nothing...
return;
} else {
m_rgb_brightness = brightness;
setColor(m_rgb_red, m_rgb_green, m_rgb_blue);
publishRGBBrightness();
}
} else if (String(MQTT_LIGHT_RGB_COMMAND_TOPIC).equals(p_topic)) {
// get the position of the first and second commas
uint8_t firstIndex = payload.indexOf(',');
uint8_t lastIndex = payload.lastIndexOf(',');
uint8_t rgb_red = payload.substring(0, firstIndex).toInt();
if (rgb_red < 0 || rgb_red > 255) {
return;
} else {
m_rgb_red = rgb_red;
}
uint8_t rgb_green = payload.substring(firstIndex + 1, lastIndex).toInt();
if (rgb_green < 0 || rgb_green > 255) {
return;
} else {
m_rgb_green = rgb_green;
}
uint8_t rgb_blue = payload.substring(lastIndex + 1).toInt();
if (rgb_blue < 0 || rgb_blue > 255) {
return;
} else {
m_rgb_blue = rgb_blue;
}
setColor(m_rgb_red, m_rgb_green, m_rgb_blue);
publishRGBColor();
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("INFO: Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
Serial.println("INFO: connected");
// Once connected, publish an announcement...
// publish the initial values
publishRGBState();
publishRGBBrightness();
publishRGBColor();
// ... and resubscribe
client.subscribe(MQTT_LIGHT_COMMAND_TOPIC);
client.subscribe(MQTT_LIGHT_BRIGHTNESS_COMMAND_TOPIC);
client.subscribe(MQTT_LIGHT_RGB_COMMAND_TOPIC);
} else {
Serial.print("ERROR: failed, rc=");
Serial.print(client.state());
Serial.println("DEBUG: try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// init the serial
Serial.begin(115200);
// init the RGB led
pinMode(RGB_LIGHT_BLUE_PIN, OUTPUT);
pinMode(RGB_LIGHT_RED_PIN, OUTPUT);
pinMode(RGB_LIGHT_GREEN_PIN, OUTPUT);
analogWriteRange(255);
setColor(0, 0, 0);
// init the WiFi connection
Serial.println();
Serial.println();
Serial.print("INFO: Connecting to ");
WiFi.mode(WIFI_STA);
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("INFO: WiFi connected");
Serial.print("INFO: IP address: ");
Serial.println(WiFi.localIP());
// init the MQTT connection
client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}