-
Notifications
You must be signed in to change notification settings - Fork 3
/
BasicOTAFastLEDHumidMQTT.ino
237 lines (210 loc) · 6.27 KB
/
BasicOTAFastLEDHumidMQTT.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
/*
* Message format is "position,red,green,blue" like:
*
* 3,23,87,12 (Turns on LED 3 with mostly blue tone)
* 0,255,0,0 (Turns on LED 0 with bright red)
* 2,0,0,0 (Turns LED 2 totally off)
* 2,255,255,255 (Turns LED 2 on full white)
*
* Position starts at 0
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include "FastLED.h"
#include "DHT.h"
/* ***************************************************** */
/* WiFi settings */
const char* ssid = "xxxx"; // Put your WiFi SSID here
const char* password = "xxxx"; // Put your WiFi password here
/* FreePixel LED settings */
#define NUM_LEDS 4 // How many leds in your strip?
// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the WS2801 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN D2
#define CLOCK_PIN D1
uint32_t ledPosition = 0;
/* RGB values are set in the MQTT callback and then referenced when LED colour is set */
int red = 0;
int green = 0;
int blue = 0;
volatile bool updateLights = false;
CRGB leds[NUM_LEDS]; // Define the array of leds
/* Humidity sensor settings */
#define DHTPIN D4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
unsigned long timeLater = 0;
/* MQTT Settings */
String ledTopic = "/kitchen/statusleds"; // MQTT topic
String humidTopic = "/kitchenfloor/humidity"; // MQTT topic
String tempTopic = "/kitchenfloor/temperature"; // MQTT topic
IPAddress broker(192,168,1,111); // Address of the MQTT broker
#define BUFFER_SIZE 100
#define CLIENT_ID "client-cf3fff"
/* ***************************************************** */
/**
* MQTT callback to process messages
*/
void callback(const MQTT::Publish& pub)
{
if (pub.has_stream())
{
uint8_t buf[BUFFER_SIZE];
int read;
while (read = pub.payload_stream()->read(buf, BUFFER_SIZE))
{
Serial.write(buf, read);
}
pub.payload_stream()->stop();
Serial.println("");
} else
Serial.print("Message: ");
Serial.println(pub.payload_string());
String values = pub.payload_string();
int c1 = pub.payload_string().indexOf(',');
int c2 = pub.payload_string().indexOf(',',c1+1);
int c3 = pub.payload_string().indexOf(',',c2+1);
ledPosition = pub.payload_string().toInt(); // Global
red = pub.payload_string().substring(c1+1).toInt(); // Global
green = pub.payload_string().substring(c2+1).toInt(); // Global
blue = pub.payload_string().substring(c3+1).toInt(); // Global
updateLights = true;
}
/**
* Relies on 4 global variables being set: "ledPosition", "red", "green", and "blue"
*/
void setColor()
{
if (updateLights)
{
updateLights = false;
if(ledPosition <= NUM_LEDS)
{
leds[ledPosition].r = red;
leds[ledPosition].g = green;
leds[ledPosition].b = blue;
FastLED.show();
}
}
}
WiFiClient wificlient;
PubSubClient client(wificlient, broker);
/**
* Setup
*/
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("WiFi begun");
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println("Proceeding");
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
// ArduinoOTA.setHostname("myesp8266");
// No authentication by default
// ArduinoOTA.setPassword((const char *)"123");
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR ) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR ) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR ) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
// Scan red, then green, then blue across the LEDs
for(int i = 0; i <= NUM_LEDS; i++)
{
leds[i] = CRGB::Red;
FastLED.show();
delay(200);
leds[i] = CRGB::Black;
FastLED.show();
}
for(int i = 0; i <= NUM_LEDS; i++)
{
leds[i] = CRGB::Green;
FastLED.show();
delay(200);
leds[i] = CRGB::Black;
FastLED.show();
}
for(int i = 0; i <= NUM_LEDS; i++)
{
leds[i] = CRGB::Blue;
FastLED.show();
delay(200);
leds[i] = CRGB::Black;
FastLED.show();
}
}
/**
* Main
*/
void loop() {
ArduinoOTA.handle();
if (WiFi.status() != WL_CONNECTED)
{
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
return;
Serial.println("WiFi connected");
}
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
if (client.connect(CLIENT_ID)) {
client.set_callback(callback);
client.subscribe(ledTopic);
}
}
}
unsigned long timeNow = millis();
if (timeNow >= timeLater) {
timeLater = timeNow + 60000;
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Below from stackxchange.com
char tempC[10];
dtostrf(temperature,1,2,tempC);
char relH[10];
dtostrf(humidity,1,2,relH);
client.publish(tempTopic, tempC);
//client.publish("test/humid", temperature);
client.publish(humidTopic, relH);
Serial.print("T: ");
Serial.print(temperature, DEC);
Serial.print("C H:");
Serial.print(humidity, DEC);
Serial.println("%");
}
if (client.connected())
client.loop();
setColor();
}