-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt-h801.ino
253 lines (206 loc) · 5.75 KB
/
mqtt-h801.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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "default_settings.h"
#include "Webserver.h"
#include "Settings.h"
#include "Led.h"
#include "ColorConverter.h"
#define MQTT_PORT 1883
// RGB FET
#define RED_PIN 15
#define GREEN_PIN 13
#define BLUE_PIN 12
// W FET
#define W1_PIN 14
#define W2_PIN 4
#define GREEN_LEDPIN 5
#define RED_LEDPIN 1
#define CONNECTION_CHECK_INTERVAL 10 // In seconds
extern "C" {
#include "user_interface.h"
}
WiFiClient wifiClient;
PubSubClient *mqttClient;
Webserver *webserver;
Settings *settings;
unsigned long lastConnectionCheckTime = 0;
unsigned int brightness = 0;
boolean on = false;
boolean mqttInitialized = false;
Led red(RED_PIN);
Led green(GREEN_PIN);
Led blue(BLUE_PIN);
//Led white1(W1_PIN);
//Led white2(W2_PIN);
RgbColor rgb = {0, 0, 0};
bool connectToWifi();
void connectToMqtt();
void mqttCallback(const MQTT::Publish&);
void turnOffLights();
void turnOnLights();
void setBrightness(int);
void setRgb(RgbColor rgbColor);
void checkConnection();
RgbColor strToRgb(String rgbString);
String rgbToStr(RgbColor rgb);
void setup() {
Serial1.begin(115200);
Serial1.print("Starting up");
red.setup();
green.setup();
blue.setup();
//white1.setup();
//white2.setup();
pinMode(GREEN_LEDPIN, OUTPUT);
pinMode(RED_LEDPIN, OUTPUT);
digitalWrite(RED_LEDPIN, HIGH);
settings = new Settings;
webserver = new Webserver(settings);
if (!settings->load()) {
Serial1.println("Initializing settings");
#ifdef DEFAULT_SETTINGS
settings->setName(NAME);
settings->setWifiSSID(WIFI_SSID);
settings->setWifiPassword(WIFI_PASS);
settings->setMQTTServer(MQTT_SERVER);
settings->setMQTTTopic(MQTT_TOPIC);
#endif
settings->save();
}
mqttClient = new PubSubClient(wifiClient, settings->getMQTTServer(), MQTT_PORT);
mqttClient->set_callback(mqttCallback);
if (connectToWifi()) {
connectToMqtt();
}
}
void loop() {
mqttClient->loop();
webserver->loop();
if (millis () > lastConnectionCheckTime + 1000 * CONNECTION_CHECK_INTERVAL) {
checkConnection();
}
red.update();
green.update();
blue.update();
//white1.update();
//white2.update();
}
bool connectToWifi() {
Serial1.println("Connecting to Wifi");
int retries = 10;
WiFi.begin(settings->getWifiSSID().c_str(), settings->getWifiPassword().c_str());
while ((WiFi.status() != WL_CONNECTED) && retries--) {
delay(500);
Serial1.print(" .");
}
if (WiFi.status() == WL_CONNECTED) {
Serial1.print("IP address: ");
Serial1.println(WiFi.localIP());
return true;
}
Serial1.println("Failed to connect to Wifi");
return false;
}
void connectToMqtt() {
Serial1.println("Connecting to MQTT");
int retries = 10;
while (!mqttClient->connect(MQTT::Connect(settings->getName()).set_keepalive(90)) && retries--) {
Serial1.print(" .");
delay(1000);
}
if(mqttClient->connected()) {
Serial1.println("Connected to MQTT");
mqttClient->subscribe(settings->getMQTTTopic() + "/switch");
mqttClient->subscribe(settings->getMQTTTopic() + "/brightness/set");
mqttClient->subscribe(settings->getMQTTTopic() + "/rgb/set");
digitalWrite(RED_LEDPIN, LOW);
digitalWrite(GREEN_LEDPIN, HIGH);
mqttInitialized = true;
}
}
void mqttCallback(const MQTT::Publish& pub) {
Serial1.print("MQTT topic: ");
Serial1.println(pub.topic());
Serial1.print("MQTT message: ");
Serial1.println(pub.payload_string());
if (pub.topic() == settings->getMQTTTopic() + "/brightness/set") {
setBrightness(pub.payload_string().toInt());
mqttClient->publish(MQTT::Publish(settings->getMQTTTopic() + "/brightness/status", pub.payload_string()).set_retain(1).set_qos(1));
return;
}
if (pub.topic() == settings->getMQTTTopic() + "/rgb/set") {
setRgb(strToRgb(pub.payload_string()));
mqttClient->publish(MQTT::Publish(settings->getMQTTTopic() + "/rgb/status", pub.payload_string()).set_retain(1).set_qos(1));
return;
}
if (pub.payload_string() == "on") {
turnOnLights();
}
if (pub.payload_string() == "off") {
turnOffLights();
}
if (mqttInitialized) {
mqttClient->publish(MQTT::Publish(settings->getMQTTTopic() + "/status", pub.payload_string()).set_retain(1).set_qos(1));
}
}
void turnOnLights() {
if (on) {
return;
}
Serial1.println("Turning lights on");
on = true;
//white1.set(brightness);
//white2.set(brightness);
setRgb(rgb);
}
void turnOffLights() {
if (!on) {
return;
}
Serial1.println("Turning lights off");
on = false;
//white1.set(0);
//white2.set(0);
red.set(0);
green.set(0);
blue.set(0);
}
void setBrightness(int newBrightness) {
Serial1.println("Setting brightness to " + String(newBrightness));
brightness = newBrightness;
//white1.set(brightness);
//white2.set(brightness);
HsvColor hsv = RgbToHsv(rgb);
hsv.v = newBrightness;
rgb = HsvToRgb(hsv);
setRgb(rgb);
}
void setRgb(RgbColor rgbColor) {
rgb = rgbColor;
red.set(rgb.r);
green.set(rgb.g);
blue.set(rgb.b);
HsvColor hsv = RgbToHsv(rgb);
brightness = hsv.v;
if (mqttInitialized) {
mqttClient->publish(MQTT::Publish(settings->getMQTTTopic() + "/brightness/status", String(brightness)).set_retain(1).set_qos(1));
mqttClient->publish(MQTT::Publish(settings->getMQTTTopic() + "/rgb/status", rgbToStr(rgb)).set_retain(1).set_qos(1));
}
}
RgbColor strToRgb(String rgbString) {
int r, g, b = 0;
sscanf(rgbString.c_str(), "%d,%d,%d", &r, &g, &b);
return {r, g, b};
}
String rgbToStr(RgbColor rgb) {
return String(rgb.r) + "," + String(rgb.g) + "," + String(rgb.b);
}
void checkConnection() {
if (WiFi.status() != WL_CONNECTED) {
connectToWifi();
}
if (WiFi.status() == WL_CONNECTED && !mqttClient->connected()) {
connectToMqtt();
}
lastConnectionCheckTime = millis();
}