├── .gitattributes ├── README.md └── ESPALEXA_All_in_One └── ESPALEXA_All_in_One.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # All-in-One-Home-Automation-using-ESPAlexa 2 | 3 | This code will help you with Controlling your Home Appliances using Alexa Smart speaker, Alexa Mobile Application, Manual Switches & Manual Fan Regulator along with their Real-time feedback on Alexa Mobile Application. 4 | 5 | This code is based on ESPAlexa & Acebutton Library whose link is attached below. 6 | 7 | ESPAlexa Library - https://github.com/Aircoookie/Espalexa 8 | 9 | Acebutton Library - https://github.com/bxparks/AceButton 10 | 11 | This code is tried and tested with 12 | - Arduino IDE version 1.8.19 13 | - ESP32 board packages version 2.0.1, 14 | - ESPAlexa Library version 2.7.0, 15 | - AceButton Library version 1.9.1 16 | 17 | To know how to make this project, we also made a complete video tutorial of it and also shown the demo of the project installed in our studio. 18 | 19 | https://youtu.be/sB8JhWnzkTU 20 | -------------------------------------------------------------------------------- /ESPALEXA_All_in_One/ESPALEXA_All_in_One.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * This is the code for the project, 4 | * Controlling UNLIMITED Appliances using ALEXA for FREE 5 | * YouTube Video Link - https://youtu.be/sB8JhWnzkTU 6 | * 7 | * 8 | * This code is tried and tested with 9 | * - Arduino IDE version 1.8.19 10 | * - ESP32 board packages version 2.0.1, 11 | * - ESPAlexa Library version 2.7.0, 12 | * - AceButton Library version 1.9.1 13 | * 14 | * In case the code doesn't work, try upgrading/downgrading the verison of above mentioned Libraries. 15 | * 16 | */ 17 | 18 | #define DEBUG_SW 1 19 | 20 | // libraries 21 | #include 22 | #include // https://github.com/Aircoookie/Espalexa 23 | #include // https://github.com/bxparks/AceButton 24 | 25 | using namespace ace_button; 26 | 27 | Espalexa espalexa; 28 | 29 | // Pins of Relay (Appliance Control) 30 | #define R1 15 31 | #define R2 2 32 | #define R3 4 33 | #define R4 22 34 | 35 | // Pins of Relay (Fan Speed Control) 36 | #define Speed1 21 37 | #define Speed2 19 38 | #define Speed4 18 39 | 40 | // Pins of Switches 41 | #define S1 32 42 | #define S2 35 43 | #define S3 34 44 | #define S4 39 45 | 46 | // Pins of Fan Regulator Knob 47 | #define F1 27 48 | #define F2 14 49 | #define F3 12 50 | #define F4 13 51 | 52 | // Flags for Fan Regulator Knob 53 | bool speed0_flag = 1; 54 | bool speed1_flag = 1; 55 | bool speed2_flag = 1; 56 | bool speed3_flag = 1; 57 | bool speed4_flag = 1; 58 | 59 | // WiFi Credentials 60 | const char* ssid = "SmS_jiofi"; 61 | const char* password = "sms123458956"; 62 | 63 | // device names 64 | String Appliance1 = "Light 1"; 65 | String Appliance2 = "Light 2"; 66 | String Appliance3 = "Light 3"; 67 | String Appliance4 = "Light 4"; 68 | String Fan = "Fan"; 69 | 70 | 71 | boolean connectWifi(); 72 | 73 | //callback functions 74 | void firstLightChanged(uint8_t brightness); 75 | void secondLightChanged(uint8_t brightness); 76 | void thirdLightChanged(uint8_t brightness); 77 | void fourthLightChanged(uint8_t brightness); 78 | void fanChanged(uint8_t brightness); 79 | 80 | 81 | ButtonConfig config1; 82 | AceButton button1(&config1); 83 | ButtonConfig config2; 84 | AceButton button2(&config2); 85 | ButtonConfig config3; 86 | AceButton button3(&config3); 87 | ButtonConfig config4; 88 | AceButton button4(&config4); 89 | 90 | void handleEvent1(AceButton*, uint8_t, uint8_t); 91 | void handleEvent2(AceButton*, uint8_t, uint8_t); 92 | void handleEvent3(AceButton*, uint8_t, uint8_t); 93 | void handleEvent4(AceButton*, uint8_t, uint8_t); 94 | 95 | boolean wifiConnected = false; 96 | 97 | //our callback functions 98 | void firstLightChanged(uint8_t brightness) 99 | { 100 | 101 | if (brightness == 255) 102 | { 103 | digitalWrite(R1, HIGH); 104 | Serial.println("Device1 ON"); 105 | } 106 | else 107 | { 108 | digitalWrite(R1, LOW); 109 | Serial.println("Device1 OFF"); 110 | } 111 | } 112 | 113 | void secondLightChanged(uint8_t brightness) 114 | { 115 | 116 | if (brightness == 255) 117 | { 118 | digitalWrite(R2, HIGH); 119 | Serial.println("Device2 ON"); 120 | } 121 | else 122 | { 123 | digitalWrite(R2, LOW); 124 | Serial.println("Device2 OFF"); 125 | } 126 | } 127 | 128 | void thirdLightChanged(uint8_t brightness) 129 | { 130 | 131 | if (brightness == 255) 132 | { 133 | digitalWrite(R3, HIGH); 134 | Serial.println("Device3 ON"); 135 | } 136 | else 137 | { 138 | digitalWrite(R3, LOW); 139 | Serial.println("Device3 OFF"); 140 | } 141 | } 142 | 143 | void fourthLightChanged(uint8_t brightness) 144 | { 145 | if (brightness == 255) 146 | { 147 | digitalWrite(R4, HIGH); 148 | Serial.println("Device4 ON"); 149 | } 150 | else 151 | { 152 | digitalWrite(R4, LOW); 153 | Serial.println("Device4 OFF"); 154 | } 155 | } 156 | 157 | void fanChanged(uint8_t brightness) 158 | { 159 | int percent = map(brightness, 0, 255, 0, 4); 160 | 161 | if (percent == 0) 162 | speed0(); 163 | if (percent == 1) 164 | speed1(); 165 | if (percent == 2) 166 | speed2(); 167 | if (percent == 3) 168 | speed3(); 169 | if (percent == 4) 170 | speed4(); 171 | 172 | } 173 | 174 | // connect to wifi – returns true if successful or false if not 175 | boolean connectWifi() 176 | { 177 | boolean state = true; 178 | int i = 0; 179 | 180 | WiFi.mode(WIFI_STA); 181 | WiFi.begin(ssid, password); 182 | Serial.println(""); 183 | Serial.println("Connecting to WiFi"); 184 | 185 | // Wait for connection 186 | Serial.print("Connecting..."); 187 | while (WiFi.status() != WL_CONNECTED) { 188 | delay(500); 189 | Serial.print("."); 190 | if (i > 20) { 191 | state = false; break; 192 | } 193 | i++; 194 | } 195 | Serial.println(""); 196 | if (state) { 197 | Serial.print("Connected to "); 198 | Serial.println(ssid); 199 | Serial.print("IP address: "); 200 | Serial.println(WiFi.localIP()); 201 | } 202 | else { 203 | Serial.println("Connection failed."); 204 | } 205 | return state; 206 | } 207 | 208 | void ESPAlexa_Devices() 209 | { 210 | // Define your devices here. 211 | espalexa.addDevice(Appliance1, firstLightChanged); 212 | espalexa.addDevice(Appliance2, secondLightChanged); 213 | espalexa.addDevice(Appliance3, thirdLightChanged); 214 | espalexa.addDevice(Appliance4, fourthLightChanged); 215 | espalexa.addDevice(Fan, fanChanged); 216 | 217 | espalexa.begin(); 218 | } 219 | 220 | void setup() 221 | { 222 | Serial.begin(115200); 223 | 224 | pinMode(R1, OUTPUT); 225 | pinMode(R2, OUTPUT); 226 | pinMode(R3, OUTPUT); 227 | pinMode(R4, OUTPUT); 228 | 229 | digitalWrite(R1, LOW); 230 | digitalWrite(R2, LOW); 231 | digitalWrite(R3, LOW); 232 | digitalWrite(R4, LOW); 233 | 234 | pinMode(Speed1, OUTPUT); 235 | pinMode(Speed2, OUTPUT); 236 | pinMode(Speed4, OUTPUT); 237 | 238 | pinMode(S1, INPUT_PULLUP); 239 | pinMode(S2, INPUT_PULLUP); 240 | pinMode(S3, INPUT_PULLUP); 241 | pinMode(S4, INPUT_PULLUP); 242 | 243 | pinMode(F1, INPUT_PULLUP); 244 | pinMode(F2, INPUT_PULLUP); 245 | pinMode(F3, INPUT_PULLUP); 246 | pinMode(F4, INPUT_PULLUP); 247 | 248 | 249 | config1.setEventHandler(button1Handler); 250 | config2.setEventHandler(button2Handler); 251 | config3.setEventHandler(button3Handler); 252 | config4.setEventHandler(button4Handler); 253 | 254 | 255 | button1.init(S1); 256 | button2.init(S2); 257 | button3.init(S3); 258 | button4.init(S4); 259 | 260 | 261 | 262 | // Initialise wifi connection 263 | wifiConnected = connectWifi(); 264 | 265 | if (wifiConnected) 266 | { 267 | ESPAlexa_Devices(); 268 | } 269 | else 270 | { 271 | Serial.println("wifi not connected!!!"); 272 | delay(1000); 273 | } 274 | } 275 | 276 | void loop() 277 | { 278 | if (WiFi.status() != WL_CONNECTED) 279 | { 280 | if (DEBUG_SW)Serial.print("Not Connected "); 281 | } 282 | else 283 | { 284 | if (DEBUG_SW)Serial.print("Connected "); 285 | if (wifiConnected) 286 | { 287 | espalexa.loop(); 288 | delay(1); 289 | } 290 | else 291 | { 292 | wifiConnected = connectWifi(); 293 | if (wifiConnected) 294 | { 295 | ESPAlexa_Devices(); 296 | } 297 | } 298 | } 299 | 300 | button1.check(); 301 | button2.check(); 302 | button3.check(); 303 | button4.check(); 304 | fan_switch_check(); 305 | 306 | } 307 | 308 | void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 309 | Serial.println("EVENT1"); 310 | EspalexaDevice* d1 = espalexa.getDevice(0); 311 | switch (eventType) { 312 | case AceButton::kEventPressed: 313 | Serial.println("kEventPressed"); 314 | d1->setPercent(100); //This is used for feedback 315 | digitalWrite(R1, HIGH); 316 | break; 317 | case AceButton::kEventReleased: 318 | Serial.println("kEventReleased"); 319 | d1->setPercent(0); 320 | digitalWrite(R1, LOW); 321 | break; 322 | } 323 | } 324 | 325 | void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 326 | Serial.println("EVENT2"); 327 | EspalexaDevice* d2 = espalexa.getDevice(1); 328 | switch (eventType) { 329 | case AceButton::kEventPressed: 330 | Serial.println("kEventPressed"); 331 | d2->setPercent(100); 332 | digitalWrite(R2, HIGH); 333 | break; 334 | case AceButton::kEventReleased: 335 | Serial.println("kEventReleased"); 336 | d2->setPercent(0); 337 | digitalWrite(R2, LOW); 338 | break; 339 | } 340 | } 341 | 342 | void button3Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 343 | Serial.println("EVENT3"); 344 | EspalexaDevice* d3 = espalexa.getDevice(2); 345 | switch (eventType) { 346 | case AceButton::kEventPressed: 347 | Serial.println("kEventPressed"); 348 | d3->setPercent(100); 349 | digitalWrite(R3, HIGH); 350 | break; 351 | case AceButton::kEventReleased: 352 | Serial.println("kEventReleased"); 353 | d3->setPercent(0); 354 | digitalWrite(R3, LOW); 355 | break; 356 | } 357 | } 358 | 359 | void button4Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 360 | Serial.println("EVENT4"); 361 | EspalexaDevice* d4 = espalexa.getDevice(3); 362 | switch (eventType) { 363 | case AceButton::kEventPressed: 364 | Serial.println("kEventPressed"); 365 | d4->setPercent(100); 366 | digitalWrite(R4, HIGH); 367 | break; 368 | case AceButton::kEventReleased: 369 | Serial.println("kEventReleased"); 370 | d4->setPercent(0); 371 | digitalWrite(R4, LOW); 372 | break; 373 | } 374 | } 375 | 376 | void fan_switch_check() 377 | { 378 | if (digitalRead(F1) == LOW && speed1_flag == 1) 379 | { 380 | EspalexaDevice* d5 = espalexa.getDevice(4); 381 | d5->setPercent(25); 382 | speed1(); 383 | speed1_flag = 0; 384 | speed2_flag = 1; 385 | speed3_flag = 1; 386 | speed4_flag = 1; 387 | speed0_flag = 1; 388 | 389 | 390 | } 391 | if (digitalRead(F2) == LOW && digitalRead(F3) == HIGH && speed2_flag == 1) 392 | { 393 | EspalexaDevice* d5 = espalexa.getDevice(4); 394 | d5->setPercent(50); 395 | speed2(); 396 | speed1_flag = 1; 397 | speed2_flag = 0; 398 | speed3_flag = 1; 399 | speed4_flag = 1; 400 | speed0_flag = 1; 401 | 402 | } 403 | if (digitalRead(F2) == LOW && digitalRead(F3) == LOW && speed3_flag == 1) 404 | { 405 | EspalexaDevice* d5 = espalexa.getDevice(4); 406 | d5->setPercent(75); 407 | speed3(); 408 | speed1_flag = 1; 409 | speed2_flag = 1; 410 | speed3_flag = 0; 411 | speed4_flag = 1; 412 | speed0_flag = 1; 413 | } 414 | if (digitalRead(F4) == LOW && speed4_flag == 1) 415 | { 416 | EspalexaDevice* d5 = espalexa.getDevice(4); 417 | d5->setPercent(100); 418 | speed4(); 419 | speed1_flag = 1; 420 | speed2_flag = 1; 421 | speed3_flag = 1; 422 | speed4_flag = 0; 423 | speed0_flag = 1; 424 | } 425 | if (digitalRead(F1) == HIGH && digitalRead(F2) == HIGH && digitalRead(F3) == HIGH && digitalRead(F4) == HIGH && speed0_flag == 1) 426 | { 427 | EspalexaDevice* d5 = espalexa.getDevice(4); 428 | d5->setPercent(0); 429 | speed0(); 430 | speed1_flag = 1; 431 | speed2_flag = 1; 432 | speed3_flag = 1; 433 | speed4_flag = 1; 434 | speed0_flag = 0; 435 | } 436 | } 437 | 438 | // Fan Speed Control 439 | 440 | void speed0() 441 | { 442 | //All Relays Off - Fan at speed 0 443 | if (DEBUG_SW)Serial.println("SPEED 0"); 444 | digitalWrite(Speed1, LOW); 445 | digitalWrite(Speed2, LOW); 446 | digitalWrite(Speed4, LOW); 447 | 448 | } 449 | 450 | void speed1() 451 | { 452 | //Speed1 Relay On - Fan at speed 1 453 | if (DEBUG_SW)Serial.println("SPEED 1"); 454 | digitalWrite(Speed1, LOW); 455 | digitalWrite(Speed2, LOW); 456 | digitalWrite(Speed4, LOW); 457 | delay(1000); 458 | digitalWrite(Speed1, HIGH); 459 | } 460 | 461 | void speed2() 462 | { 463 | //Speed2 Relay On - Fan at speed 2 464 | if (DEBUG_SW)Serial.println("SPEED 2"); 465 | digitalWrite(Speed1, LOW); 466 | digitalWrite(Speed2, LOW); 467 | digitalWrite(Speed4, LOW); 468 | delay(1000); 469 | digitalWrite(Speed2, HIGH); 470 | } 471 | 472 | void speed3() 473 | { 474 | //Speed1 & Speed2 Relays On - Fan at speed 3 475 | if (DEBUG_SW)Serial.println("SPEED 3"); 476 | digitalWrite(Speed1, LOW); 477 | digitalWrite(Speed2, LOW); 478 | digitalWrite(Speed4, LOW); 479 | delay(1000); 480 | digitalWrite(Speed1, HIGH); 481 | digitalWrite(Speed2, HIGH); 482 | 483 | } 484 | 485 | void speed4() 486 | { 487 | //Speed4 Relay On - Fan at speed 4 488 | if (DEBUG_SW)Serial.println("SPEED 4"); 489 | digitalWrite(Speed1, LOW); 490 | digitalWrite(Speed2, LOW); 491 | digitalWrite(Speed4, LOW); 492 | delay(1000); 493 | digitalWrite(Speed4, HIGH); 494 | } 495 | --------------------------------------------------------------------------------