HuzzahCrickitIOT-ESP8266.ino
1 // SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries 2 // 3 // SPDX-License-Identifier: MIT 4 5 #include <ESP8266WiFi.h> 6 #include <PubSubClient.h> 7 #include "config.h" 8 #include <Adafruit_Crickit.h> 9 #include <Adafruit_NeoPixel.h> 10 #include <seesaw_servo.h> 11 #include <seesaw_motor.h> 12 13 #define NEOPIX_PIN (13) /* Neopixel pin */ 14 #define NEOPIX_NUMBER_OF_PIXELS (7) 15 #define LUX CRICKIT_SIGNAL1 16 #define PIR CRICKIT_SIGNAL3 17 #define DOOR CRICKIT_SIGNAL5 18 #define chip_name "CrickitOIT_1" 19 20 Adafruit_Crickit crickit; 21 seesaw_Servo myservo(&crickit); // create servo object to control a servo 22 seesaw_Motor motor_a(&crickit); 23 Adafruit_NeoPixel strip(NEOPIX_NUMBER_OF_PIXELS, NEOPIX_PIN, NEO_GRB + NEO_KHZ800); 24 25 //****************************** MQTT TOPICS 26 27 //***** Door Lock 28 #define MQTTlock "house/lock" 29 //***** Window Fan 30 #define MQTTfan "house/fan" 31 #define MQTTfanSpeed "house/fan/speed" 32 //***** RGB LED 1 33 #define MQTTled1 "house/led/one" 34 #define MQTTled1Bright "house/led/one/brightness" 35 #define MQTTled1Color "house/led/one/color" 36 //***** RGB LED 2 37 #define MQTTled2 "house/led/two" 38 #define MQTTled2Bright "house/led/two/brightness" 39 #define MQTTled2Color "house/led/two/color" 40 //***** RGB LED 3 41 #define MQTTled3 "house/led/three" 42 #define MQTTled3Bright "house/led/three/brightness" 43 #define MQTTled3Color "house/led/three/color" 44 //***** RGB LED 4 45 #define MQTTled4 "house/led/four" 46 #define MQTTled4Bright "house/led/four/brightness" 47 #define MQTTled4Color "house/led/four/color" 48 //***** RGB LED 5 49 #define MQTTled5 "house/led/five" 50 #define MQTTled5Bright "house/led/five/brightness" 51 #define MQTTled5Color "house/led/five/color" 52 //***** Light Level Sensor 53 #define MQTTlux "house/lux" 54 //***** Temperature and Humidity Sensor 55 #define MQTTtemp "house/temperature" 56 #define MQTThumid "house/humidity" 57 //***** Motion Sensor 58 #define MQTTpir "house/motion" 59 //***** Door Sensor 60 #define MQTTdoor "house/door" 61 62 //****************************** Connection Settings 63 WiFiClient espClient; 64 PubSubClient client(espClient); 65 long lastMsg = 0; 66 char msg[50]; 67 int value = 0; 68 char message_buff[100]; 69 70 //****************************** RGB LEDs 71 int R[5]; 72 int G[5]; 73 int B[5]; 74 bool LEDstate[5]; 75 76 //****************************** Sensor Smoothing 77 const int numReadings = 30; 78 int lux_R[numReadings]; // the readings from the lux input 79 int temp_R[numReadings]; // the readings from the temperature input 80 int humid_R[numReadings]; // the readings from the humidity input 81 int readIndex = 0; // the index of the current reading 82 int total = 0; // the running total 83 int Lux_A = 0; // the average 84 85 /****************************** Define Global Veriables ***************************************/ 86 87 88 void setup() { 89 Serial.begin(115200); 90 setup_wifi(); 91 client.setServer(mqtt_server, 1883); 92 client.setCallback(callback); 93 94 if(!crickit.begin()){ 95 Serial.println("ERROR!"); 96 while(1); 97 } 98 else Serial.println("Crickit started"); 99 100 crickit.pinMode(LUX, INPUT); 101 crickit.pinMode(PIR, INPUT_PULLUP); 102 crickit.pinMode(DOOR, INPUT_PULLUP); 103 104 myservo.attach(CRICKIT_SERVO1); // attaches the servo to CRICKIT_SERVO1 pin 105 motor_a.attach(CRICKIT_MOTOR_A1, CRICKIT_MOTOR_A2); 106 strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) 107 strip.show(); // Initialize all pixels to 'off' 108 //strip.setPixelColor(1, strip.Color(0, 0, 255)); 109 for(uint16_t i=0; i<5; i++) { 110 R[i] = 255; 111 G[i] = 255; 112 B[i] = 255; 113 } 114 for (int thisReading = 0; thisReading < numReadings; thisReading++) { 115 lux_R[thisReading] = 0; 116 } 117 } 118 119 void setup_wifi() { 120 121 122 delay(10); 123 // We start by connecting to a WiFi network 124 Serial.println(); 125 Serial.print("Connecting to "); 126 Serial.println(ssid); 127 128 WiFi.begin(ssid, password); 129 130 while (WiFi.status() != WL_CONNECTED) { 131 delay(500); 132 Serial.print("."); 133 } 134 135 Serial.println(""); 136 Serial.println("WiFi connected"); 137 Serial.println("IP address: "); 138 Serial.println(WiFi.localIP()); 139 140 } 141 142 void callback(char* topic, byte* payload, unsigned int length) { 143 int i = 0; 144 char message_buff[100]; 145 String StrPayload; 146 Serial.print("Message arrived ["); 147 Serial.print(topic); 148 Serial.print("] "); 149 for (i = 0; i < length; i++) { 150 message_buff[i] = payload[i]; 151 } 152 message_buff[i] = '\0'; 153 StrPayload = String(message_buff); 154 int IntPayload = StrPayload.toInt(); 155 Serial.print(StrPayload); 156 157 if (String(topic) == MQTTlock) { 158 if (StrPayload == "UNLOCK") { 159 myservo.write(180); 160 } 161 if (StrPayload == "LOCK") { 162 myservo.write(0); 163 } 164 } 165 166 if (String(topic) == MQTTfan) { 167 if (StrPayload == "OFF") { 168 motor_a.throttle(0); 169 } 170 if (StrPayload == "ON") { 171 motor_a.throttle(1); 172 } 173 } 174 175 if (String(topic) == MQTTfanSpeed) { 176 if (StrPayload == "low") { 177 motor_a.throttle(0.4); 178 } 179 if (StrPayload == "medium") { 180 motor_a.throttle(0.6); 181 } 182 if (StrPayload == "high") { 183 motor_a.throttle(1); 184 } 185 } 186 187 //.................. Light 1 ......................// 188 if (String(topic) == MQTTled1) { 189 if (StrPayload == "OFF") { 190 LEDstate[0] = false; 191 strip.setPixelColor(0, strip.Color(0, 0, 0)); 192 } 193 if (StrPayload == "ON") { 194 if (!LEDstate[0]) { 195 strip.setPixelColor(0, strip.Color(R[0], G[0], B[0])); 196 LEDstate[0] = true; 197 } 198 } 199 } 200 if (String(topic) == MQTTled1Bright) { 201 int r = map(R[0], 0, 255, 0, IntPayload); 202 int g = map(G[0], 0, 255, 0, IntPayload); 203 int b = map(B[0], 0, 255, 0, IntPayload); 204 strip.setPixelColor(0, strip.Color(r, g, b)); 205 } 206 if (String(topic) == MQTTled1Color) { 207 R[0] = StrPayload.substring(0, StrPayload.indexOf(',')).toInt(); 208 G[0] = StrPayload.substring(StrPayload.indexOf(',') + 1, StrPayload.lastIndexOf(',')).toInt(); 209 B[0] = StrPayload.substring(StrPayload.lastIndexOf(',') + 1).toInt(); 210 strip.setPixelColor(0, strip.Color(R[0], G[0], B[0])); 211 } 212 strip.show(); 213 //.................. Light 2 ......................// 214 if (String(topic) == MQTTled2) { 215 if (StrPayload == "OFF") { 216 LEDstate[1] = false; 217 strip.setPixelColor(1, strip.Color(0, 0, 0)); 218 } 219 if (StrPayload == "ON") { 220 if (!LEDstate[1]) { 221 strip.setPixelColor(1, strip.Color(R[1], G[1], B[1])); 222 LEDstate[1] = true; 223 } 224 } 225 } 226 if (String(topic) == MQTTled2Bright) { 227 int r = map(R[1], 0, 255, 0, IntPayload); 228 int g = map(G[1], 0, 255, 0, IntPayload); 229 int b = map(B[1], 0, 255, 0, IntPayload); 230 strip.setPixelColor(1, strip.Color(r, g, b)); 231 } 232 if (String(topic) == MQTTled2Color) { 233 R[1] = StrPayload.substring(0, StrPayload.indexOf(',')).toInt(); 234 G[1] = StrPayload.substring(StrPayload.indexOf(',') + 1, StrPayload.lastIndexOf(',')).toInt(); 235 B[1] = StrPayload.substring(StrPayload.lastIndexOf(',') + 1).toInt(); 236 strip.setPixelColor(1, strip.Color(R[1], G[1], B[1])); 237 } 238 strip.show(); 239 240 //.................. Light 3 ......................// 241 if (String(topic) == MQTTled3) { 242 if (StrPayload == "OFF") { 243 LEDstate[2] = false; 244 strip.setPixelColor(2, strip.Color(0, 0, 0)); 245 } 246 if (StrPayload == "ON") { 247 if (!LEDstate[2]) { 248 strip.setPixelColor(2, strip.Color(R[2], G[2], B[2])); 249 LEDstate[2] = true; 250 } 251 } 252 } 253 if (String(topic) == MQTTled3Bright) { 254 int r = map(R[2], 0, 255, 0, IntPayload); 255 int g = map(G[2], 0, 255, 0, IntPayload); 256 int b = map(B[2], 0, 255, 0, IntPayload); 257 strip.setPixelColor(2, strip.Color(r, g, b)); 258 } 259 if (String(topic) == MQTTled3Color) { 260 R[2] = StrPayload.substring(0, StrPayload.indexOf(',')).toInt(); 261 G[2] = StrPayload.substring(StrPayload.indexOf(',') + 1, StrPayload.lastIndexOf(',')).toInt(); 262 B[2] = StrPayload.substring(StrPayload.lastIndexOf(',') + 1).toInt(); 263 strip.setPixelColor(2, strip.Color(R[2], G[2], B[2])); 264 } 265 strip.show(); 266 267 //.................. Light 4 ......................// 268 if (String(topic) == MQTTled4) { 269 if (StrPayload == "OFF") { 270 LEDstate[3] = false; 271 strip.setPixelColor(3, strip.Color(0, 0, 0)); 272 } 273 if (StrPayload == "ON") { 274 if (!LEDstate[3]) { 275 strip.setPixelColor(3, strip.Color(R[3], G[3], B[3])); 276 LEDstate[3] = true; 277 } 278 } 279 } 280 if (String(topic) == MQTTled4Bright) { 281 int r = map(R[3], 0, 255, 0, IntPayload); 282 int g = map(G[3], 0, 255, 0, IntPayload); 283 int b = map(B[3], 0, 255, 0, IntPayload); 284 strip.setPixelColor(3, strip.Color(r, g, b)); 285 } 286 if (String(topic) == MQTTled4Color) { 287 R[3] = StrPayload.substring(0, StrPayload.indexOf(',')).toInt(); 288 G[3] = StrPayload.substring(StrPayload.indexOf(',') + 1, StrPayload.lastIndexOf(',')).toInt(); 289 B[3] = StrPayload.substring(StrPayload.lastIndexOf(',') + 1).toInt(); 290 strip.setPixelColor(3, strip.Color(R[3], G[3], B[3])); 291 } 292 strip.show(); 293 294 //.................. Light 5 ......................// 295 if (String(topic) == MQTTled5) { 296 if (StrPayload == "OFF") { 297 LEDstate[4] = false; 298 strip.setPixelColor(4, strip.Color(0, 0, 0)); 299 } 300 if (StrPayload == "ON") { 301 if (!LEDstate[4]) { 302 strip.setPixelColor(4, strip.Color(R[4], G[4], B[4])); 303 LEDstate[4] = true; 304 } 305 } 306 } 307 if (String(topic) == MQTTled5Bright) { 308 int r = map(R[4], 0, 255, 0, IntPayload); 309 int g = map(G[4], 0, 255, 0, IntPayload); 310 int b = map(B[4], 0, 255, 0, IntPayload); 311 strip.setPixelColor(4, strip.Color(r, g, b)); 312 } 313 if (String(topic) == MQTTled5Color) { 314 R[4] = StrPayload.substring(0, StrPayload.indexOf(',')).toInt(); 315 G[4] = StrPayload.substring(StrPayload.indexOf(',') + 1, StrPayload.lastIndexOf(',')).toInt(); 316 B[4] = StrPayload.substring(StrPayload.lastIndexOf(',') + 1).toInt(); 317 strip.setPixelColor(4, strip.Color(R[4], G[4], B[4])); 318 } 319 strip.show(); 320 Serial.println(); 321 } 322 323 void reconnect() { 324 // Loop until we're reconnected 325 while (!client.connected()) { 326 Serial.print("Attempting MQTT connection..."); 327 // Attempt to connect 328 if (client.connect(chip_name, mqtt_user, mqtt_password)) { 329 Serial.println("connected"); 330 // Once connected, publish an announcement... 331 client.publish(MQTTlock, "LOCK"); 332 // client.publish(MQTTfan, "OFF"); 333 // client.publish(MQTTled1Color, "255,255,255"); 334 // client.publish(MQTTled1, "OFF"); 335 // client.publish(MQTTled2Color, "255,255,255"); 336 // client.publish(MQTTled2, "OFF"); 337 // client.publish(MQTTled3Color, "255,255,255"); 338 // client.publish(MQTTled3, "OFF"); 339 // client.publish(MQTTled4Color, "255,255,255"); 340 // client.publish(MQTTled4, "OFF"); 341 // client.publish(MQTTled5Color, "255,255,255"); 342 // client.publish(MQTTled5, "OFF"); 343 // ... and resubscribe 344 client.subscribe(MQTTlock); 345 client.subscribe(MQTTfan); 346 client.subscribe(MQTTfanSpeed); 347 client.subscribe(MQTTled1); 348 client.subscribe(MQTTled1Bright); 349 client.subscribe(MQTTled1Color); 350 client.subscribe(MQTTled2); 351 client.subscribe(MQTTled2Bright); 352 client.subscribe(MQTTled2Color); 353 client.subscribe(MQTTled3); 354 client.subscribe(MQTTled3Bright); 355 client.subscribe(MQTTled3Color); 356 client.subscribe(MQTTled4); 357 client.subscribe(MQTTled4Bright); 358 client.subscribe(MQTTled4Color); 359 client.subscribe(MQTTled5); 360 client.subscribe(MQTTled5Bright); 361 client.subscribe(MQTTled5Color); 362 363 } else { 364 Serial.print("failed, rc="); 365 Serial.print(client.state()); 366 Serial.println(" try again in 5 seconds"); 367 // Wait 5 seconds before retrying 368 delay(5000); 369 } 370 } 371 } 372 373 // Fill the dots one after the other with a color 374 void colorWipe(uint32_t c, uint8_t wait) { 375 for(uint16_t i=0; i<NEOPIX_NUMBER_OF_PIXELS; i++) { 376 strip.setPixelColor(i, c); 377 strip.show(); 378 delay(wait); 379 } 380 } 381 382 void loop() { 383 384 if (!client.connected()) { 385 reconnect(); 386 } 387 388 //************** Lux Smoothing 389 390 total = total - lux_R[readIndex]; // subtract the last reading 391 lux_R[readIndex] = crickit.analogRead(LUX); // read from the sensor 392 total = total + lux_R[readIndex]; // add the reading to the total 393 readIndex = readIndex + 1; // advance to the next position in the array 394 395 if (readIndex >= numReadings) { // if we're at the end of the array... 396 readIndex = 0; // ...wrap around to the beginning 397 Lux_A = total / numReadings; // calculate the average 398 399 //int Lux = map(lightLux, 290, 590, 10, 960); 400 Serial.print("Lux = "); 401 Serial.println(Lux_A); 402 403 snprintf (msg, 75, "%ld", Lux_A); 404 Serial.println(msg); 405 client.publish(MQTTlux, msg); 406 407 delay(10); 408 if (crickit.digitalRead(PIR)){ 409 Serial.println("Motion Sensor = MOVE"); 410 client.publish(MQTTpir, "MOVE"); 411 }else{ 412 Serial.println("Motion Sensor = STILL"); 413 client.publish(MQTTpir, "STILL"); 414 } 415 delay(10); 416 if (crickit.digitalRead(DOOR)){ 417 Serial.println("Door = OPEN"); 418 client.publish(MQTTdoor, "OPEN"); 419 }else{ 420 Serial.println("Door = CLOSED"); 421 client.publish(MQTTdoor, "CLOSED"); 422 } 423 } 424 delay(10); // delay in between reads for stability 425 client.loop(); 426 }