├── .gitattributes ├── .DS_Store ├── Rainmaker ├── .DS_Store ├── debug_custom.json ├── debug.cfg └── Rainmaker.ino ├── Home Assistant ├── ifan02.h └── aio.yaml └── Blynk └── Blynk.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techiesms/All-in-One-Home-Automation-V3/main/.DS_Store -------------------------------------------------------------------------------- /Rainmaker/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/techiesms/All-in-One-Home-Automation-V3/main/Rainmaker/.DS_Store -------------------------------------------------------------------------------- /Rainmaker/debug_custom.json: -------------------------------------------------------------------------------- 1 | { 2 | "name":"Arduino on ESP32", 3 | "toolchainPrefix":"xtensa-esp32-elf", 4 | "svdFile":"debug.svd", 5 | "request":"attach", 6 | "postAttachCommands":[ 7 | "set remote hardware-watchpoint-limit 2", 8 | "monitor reset halt", 9 | "monitor gdb_sync", 10 | "thb setup", 11 | "c" 12 | ], 13 | "overrideRestartCommands":[ 14 | "monitor reset halt", 15 | "monitor gdb_sync", 16 | "thb setup", 17 | "c" 18 | ] 19 | } -------------------------------------------------------------------------------- /Rainmaker/debug.cfg: -------------------------------------------------------------------------------- 1 | # SPDX-License-Identifier: GPL-2.0-or-later 2 | # 3 | # Example OpenOCD configuration file for ESP32-WROVER-KIT board. 4 | # 5 | # For example, OpenOCD can be started for ESP32 debugging on 6 | # 7 | # openocd -f board/esp32-wrover-kit-3.3v.cfg 8 | # 9 | 10 | # Source the JTAG interface configuration file 11 | source [find interface/ftdi/esp32_devkitj_v1.cfg] 12 | set ESP32_FLASH_VOLTAGE 3.3 13 | # Source the ESP32 configuration file 14 | source [find target/esp32.cfg] 15 | -------------------------------------------------------------------------------- /Home Assistant/ifan02.h: -------------------------------------------------------------------------------- 1 | #include "esphome.h" 2 | 3 | using namespace esphome; 4 | 5 | class IFan02Output : public Component, public FloatOutput { 6 | public: 7 | void write_state(float state) override 8 | { 9 | if(state < 0.2) 10 | { 11 | digitalWrite(21, LOW); 12 | digitalWrite(19, LOW); 13 | digitalWrite(18, LOW); 14 | } 15 | else if (state < 0.4) { 16 | // OFF 17 | digitalWrite(21, LOW); 18 | digitalWrite(19, LOW); 19 | digitalWrite(18, LOW); 20 | delay(1000); 21 | digitalWrite(21, HIGH); 22 | digitalWrite(19, LOW); 23 | digitalWrite(18, LOW); 24 | } else if (state < 0.6) { 25 | // low speed 26 | digitalWrite(21, LOW); 27 | digitalWrite(19, LOW); 28 | digitalWrite(18, LOW); 29 | delay(1000); 30 | digitalWrite(21, LOW); 31 | digitalWrite(19, HIGH); 32 | digitalWrite(18, LOW); 33 | } else if (state < 0.8) { 34 | // medium speed 35 | digitalWrite(21, LOW); 36 | digitalWrite(19, LOW); 37 | digitalWrite(18, LOW); 38 | delay(1000); 39 | digitalWrite(21, HIGH); 40 | digitalWrite(19, HIGH); 41 | digitalWrite(18, LOW); 42 | } else { 43 | // high speed 44 | digitalWrite(21, LOW); 45 | digitalWrite(19, LOW); 46 | digitalWrite(18, LOW); 47 | delay(1000); 48 | digitalWrite(21, LOW); 49 | digitalWrite(19, LOW); 50 | digitalWrite(18, HIGH); 51 | } 52 | } 53 | }; -------------------------------------------------------------------------------- /Home Assistant/aio.yaml: -------------------------------------------------------------------------------- 1 | esphome: 2 | name: esp-fan 3 | includes: ifan02.h 4 | 5 | esp32: 6 | board: esp32doit-devkit-v1 7 | framework: 8 | type: arduino 9 | 10 | # Enable logging 11 | logger: 12 | 13 | # Enable Home Assistant API 14 | api: 15 | encryption: 16 | key: "tXfMjq3L0HQrXSqi5oWOG5NFVo20arn1m7lGs9GJKL4=" 17 | 18 | ota: 19 | password: "989776e5dd4b4024d2346ed0c38de407" 20 | 21 | wifi: 22 | ssid: !secret wifi_ssid 23 | password: !secret wifi_password 24 | 25 | # Enable fallback hotspot (captive portal) in case wifi connection fails 26 | ap: 27 | ssid: "Esp-Fan Fallback Hotspot" 28 | password: "oLR184xFSxja" 29 | 30 | captive_portal: 31 | 32 | binary_sensor: 33 | 34 | - platform: gpio 35 | pin: GPIO32 36 | name: "Light1" 37 | device_class: light 38 | on_press: 39 | then: 40 | - light.turn_off: light1 41 | on_release: 42 | then: 43 | - light.turn_on: light1 44 | 45 | - platform: gpio 46 | pin: GPIO35 47 | name: "Light2" 48 | device_class: light 49 | on_press: 50 | then: 51 | - light.turn_off: light2 52 | on_release: 53 | then: 54 | - light.turn_on: light2 55 | 56 | - platform: gpio 57 | pin: GPIO34 58 | name: "Light3" 59 | device_class: light 60 | on_press: 61 | then: 62 | - light.turn_off: light3 63 | on_release: 64 | then: 65 | - light.turn_on: light3 66 | 67 | - platform: gpio 68 | pin: GPIO39 69 | name: "Light4" 70 | device_class: light 71 | on_press: 72 | then: 73 | - light.turn_off: light4 74 | on_release: 75 | then: 76 | - light.turn_on: light4 77 | 78 | - platform: gpio 79 | pin: GPIO33 80 | name: "FanSw" 81 | device_class: light 82 | on_press: 83 | then: 84 | - fan.turn_off: 85 | id: ifan02_fan 86 | on_release: 87 | then: 88 | - fan.turn_on: 89 | id: ifan02_fan 90 | speed: 1 91 | 92 | - platform: gpio 93 | pin: GPIO27 94 | name: "fan speed1" 95 | device_class: power 96 | id: fan_speed1 97 | on_release: 98 | then: 99 | - fan.turn_on: 100 | id: ifan02_fan 101 | speed: 1 102 | 103 | 104 | - platform: gpio 105 | pin: GPIO14 106 | name: "fan speed2" 107 | device_class: power 108 | id: fan_speed2 109 | on_release: 110 | then: 111 | - fan.turn_on: 112 | id: ifan02_fan 113 | speed: 2 114 | 115 | - platform: gpio 116 | pin: GPIO13 117 | name: "fan speed4" 118 | device_class: power 119 | id: fan_speed4 120 | on_release: 121 | then: 122 | - fan.turn_on: 123 | id: ifan02_fan 124 | speed: 4 125 | 126 | light: 127 | - platform: binary 128 | name: "Light1" 129 | output: light_output1 130 | id: light1 131 | 132 | - platform: binary 133 | name: "Light2" 134 | output: light_output2 135 | id: light2 136 | 137 | - platform: binary 138 | name: "Light3" 139 | output: light_output3 140 | id: light3 141 | 142 | - platform: binary 143 | name: "Light4" 144 | output: light_output4 145 | id: light4 146 | 147 | output: 148 | - id: light_output1 149 | platform: gpio 150 | pin: 25 151 | # inverted: true 152 | 153 | - id: light_output2 154 | platform: gpio 155 | pin: 26 156 | # inverted: true 157 | 158 | - id: light_output3 159 | platform: gpio 160 | pin: 4 161 | # inverted: true 162 | 163 | - id: light_output4 164 | platform: gpio 165 | pin: 22 166 | # inverted: true 167 | 168 | - platform: custom 169 | type: float 170 | outputs: 171 | id: fanoutput 172 | lambda: |- 173 | auto ifan02_fan = new IFan02Output(); 174 | App.register_component(ifan02_fan); 175 | return {ifan02_fan}; 176 | 177 | switch: 178 | - platform: template 179 | id: update_fan_speed 180 | optimistic: true 181 | turn_on_action: 182 | then: 183 | - delay: 200ms 184 | - if: 185 | condition: 186 | and: 187 | - switch.is_off: fan_relay1 188 | - switch.is_off: fan_relay2 189 | - switch.is_off: fan_relay3 190 | then: 191 | - fan.turn_off: ifan02_fan 192 | - if: 193 | condition: 194 | and: 195 | - switch.is_on: fan_relay1 196 | - switch.is_off: fan_relay2 197 | - switch.is_off: fan_relay3 198 | then: 199 | - fan.turn_on: 200 | id: ifan02_fan 201 | speed: 1 202 | - if: 203 | condition: 204 | and: 205 | - switch.is_off: fan_relay1 206 | - switch.is_on: fan_relay2 207 | - switch.is_off: fan_relay3 208 | then: 209 | - fan.turn_on: 210 | id: ifan02_fan 211 | speed: 2 212 | - if: 213 | condition: 214 | and: 215 | - switch.is_on: fan_relay1 216 | - switch.is_on: fan_relay2 217 | - switch.is_off: fan_relay3 218 | then: 219 | - fan.turn_on: 220 | id: ifan02_fan 221 | speed: 3 222 | - if: 223 | condition: 224 | and: 225 | - switch.is_off: fan_relay1 226 | - switch.is_off: fan_relay2 227 | - switch.is_on: fan_relay3 228 | then: 229 | - fan.turn_on: 230 | id: ifan02_fan 231 | speed: 4 232 | - switch.turn_off: update_fan_speed 233 | 234 | - platform: gpio 235 | pin: GPIO21 236 | id: fan_relay1 237 | 238 | - platform: gpio 239 | pin: GPIO19 240 | id: fan_relay2 241 | 242 | - platform: gpio 243 | pin: GPIO18 244 | id: fan_relay3 245 | 246 | sensor: 247 | - platform: dht 248 | pin: GPIO16 249 | temperature: 250 | name: "Living Room Temperature" 251 | humidity: 252 | name: "Living Room Humidity" 253 | update_interval: 60s 254 | 255 | fan: 256 | - platform: speed 257 | output: fanoutput 258 | id: ifan02_fan 259 | speed_count: 4 260 | name: "iFan02 Fan" -------------------------------------------------------------------------------- /Blynk/Blynk.ino: -------------------------------------------------------------------------------- 1 | 2 | #define BLYNK_PRINT Serial 3 | 4 | #define BLYNK_TEMPLATE_ID "TEMPLATE_ID" 5 | #define BLYNK_TEMPLATE_NAME "TEMPELATE_NAME" 6 | #define BLYNK_AUTH_TOKEN "AUTH_TOKEN" 7 | 8 | 9 | #include 10 | #include 11 | #include 12 | #include // https://github.com/Arduino-IRremote/Arduino-IRremote (3.6.1) 13 | #include // https://github.com/bxparks/AceButton (1.9.2) 14 | #include // https://github.com/adafruit/DHT-sensor-library (1.4.3) 15 | 16 | BlynkTimer timer; // Timer to check Blynk Connectivity 17 | 18 | using namespace ace_button; 19 | 20 | //#define BLYNK_DEBUG 21 | 22 | #define APP_DEBUG 23 | 24 | // IR Remote Code for Lights 25 | #define IR_Relay1 0x1FE50AF 26 | #define IR_Relay2 0x1FED827 27 | #define IR_Relay3 0x1FEF807 28 | #define IR_Relay4 0x1FE30CF 29 | #define IR_Relay_All_Off 0x1FE48B7 30 | #define IR_Relay_All_On 0x1FE7887 31 | 32 | // IR Remote Code for Fan 33 | #define IR_Speed_Up 0x1FE609F 34 | #define IR_Speed_Dw 0x1FEA05F 35 | #define IR_Fan_off 0x1FE10EF 36 | #define IR_Fan_on 0x1FE906F 37 | 38 | // Define Relay Pins 39 | #define r1 25 40 | #define r2 26 41 | #define r3 4 42 | #define r4 22 43 | 44 | // Define Switch Pins 45 | #define switch1 32 46 | #define switch2 35 47 | #define switch3 34 48 | #define switch4 39 49 | #define fan_switch 33 50 | 51 | // Pins of Fan Regulator Knob 52 | #define s1 27 53 | #define s2 14 54 | #define s3 23 55 | #define s4 13 56 | 57 | // Pins of Relay (Fan Speed Control) 58 | #define Speed1 21 59 | #define Speed2 19 60 | #define Speed4 18 61 | 62 | // Switch Flags 63 | bool SWITCH5_FLAG = 1; 64 | bool SWITCH6_FLAG = 1; 65 | bool SWITCH7_FLAG = 1; 66 | bool SWITCH8_FLAG = 1; 67 | 68 | // Default Relay State 69 | bool relay1 = LOW; 70 | bool relay2 = LOW; 71 | bool relay3 = LOW; 72 | bool relay4 = LOW; 73 | 74 | // Relay State 75 | bool switch_state_ch1 = LOW; 76 | bool switch_state_ch2 = LOW; 77 | bool switch_state_ch3 = LOW; 78 | bool switch_state_ch4 = LOW; 79 | 80 | // Flags for Fan Speed 81 | bool speed1_flag = 1; 82 | bool speed2_flag = 1; 83 | bool speed3_flag = 1; 84 | bool speed4_flag = 1; 85 | bool speed0_flag = 1; 86 | 87 | int curr_speed = 0; 88 | 89 | #define DEBUG_SW 1 90 | 91 | #define irPin 17 // IR sensor pin 92 | #define DHTPIN 16 93 | 94 | float temperature_value = 0; 95 | float humidity_value = 0; 96 | 97 | 98 | DHT dht(DHTPIN, DHT11); 99 | IRrecv irrecv(irPin); 100 | decode_results results; 101 | 102 | // Your WiFi credentials. 103 | // Set password to "" for open networks. 104 | char ssid[] = "SmS_jiofi"; 105 | char pass[] = "sms123458956"; 106 | 107 | 108 | ButtonConfig config1; 109 | AceButton button1(&config1); 110 | ButtonConfig config2; 111 | AceButton button2(&config2); 112 | ButtonConfig config3; 113 | AceButton button3(&config3); 114 | ButtonConfig config4; 115 | AceButton button4(&config4); 116 | ButtonConfig config5; 117 | AceButton button5(&config5); 118 | 119 | void handleEvent1(AceButton*, uint8_t, uint8_t); 120 | void handleEvent2(AceButton*, uint8_t, uint8_t); 121 | void handleEvent3(AceButton*, uint8_t, uint8_t); 122 | void handleEvent4(AceButton*, uint8_t, uint8_t); 123 | void handleEvent5(AceButton*, uint8_t, uint8_t); 124 | 125 | void send_sensor(); 126 | 127 | BLYNK_WRITE(V0) 128 | { 129 | int fan_speed = param.asInt(); // assigning incoming value from pin V1 to a variable 130 | if (fan_speed == 0) 131 | { 132 | speed_0(); 133 | } 134 | if (fan_speed == 1) 135 | { 136 | speed_1(); 137 | } 138 | if (fan_speed == 2) 139 | { 140 | speed_2(); 141 | } 142 | if (fan_speed == 3) 143 | { 144 | speed_3(); 145 | } 146 | if (fan_speed == 4) 147 | { 148 | speed_4(); 149 | } 150 | } 151 | 152 | BLYNK_WRITE(V1) 153 | { 154 | int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable 155 | digitalWrite(r1, pinValue); 156 | // process received value 157 | } 158 | 159 | BLYNK_WRITE(V2) 160 | { 161 | int pinValue = param.asInt(); // assigning incoming value from pin V2 to a variable 162 | digitalWrite(r2, pinValue); 163 | // process received value 164 | } 165 | 166 | BLYNK_WRITE(V3) 167 | { 168 | int pinValue = param.asInt(); // assigning incoming value from pin V3 to a variable 169 | digitalWrite(r3, pinValue); 170 | // process received value 171 | } 172 | 173 | BLYNK_WRITE(V4) 174 | { 175 | int pinValue = param.asInt(); // assigning incoming value from pin V4 to a variable 176 | digitalWrite(r4, pinValue); 177 | // process received value 178 | } 179 | 180 | void setup() 181 | { 182 | // Debug console 183 | Serial.begin(115200); 184 | irrecv.enableIRIn(); // Enabling IR sensor 185 | 186 | pinMode(switch1, INPUT_PULLUP); 187 | pinMode(switch2, INPUT_PULLUP); 188 | pinMode(switch3, INPUT_PULLUP); 189 | pinMode(switch4, INPUT_PULLUP); 190 | pinMode(fan_switch, INPUT_PULLUP); 191 | 192 | 193 | pinMode(s1, INPUT_PULLUP); 194 | pinMode(s2, INPUT_PULLUP); 195 | pinMode(s3, INPUT_PULLUP); 196 | pinMode(s4, INPUT_PULLUP); 197 | 198 | 199 | pinMode(r1, OUTPUT); 200 | pinMode(r2, OUTPUT); 201 | pinMode(r3, OUTPUT); 202 | pinMode(r4, OUTPUT); 203 | pinMode(Speed1, OUTPUT); 204 | pinMode(Speed2, OUTPUT); 205 | pinMode(Speed4, OUTPUT); 206 | 207 | digitalWrite(r1, 0); 208 | digitalWrite(r2, 0); 209 | digitalWrite(r3, 0); 210 | digitalWrite(r4, 0); 211 | 212 | config1.setEventHandler(button1Handler); 213 | config2.setEventHandler(button2Handler); 214 | config3.setEventHandler(button3Handler); 215 | config4.setEventHandler(button4Handler); 216 | config5.setEventHandler(button5Handler); 217 | 218 | button1.init(switch1); 219 | button2.init(switch2); 220 | button3.init(switch3); 221 | button4.init(switch4); 222 | button5.init(fan_switch); 223 | 224 | WiFi.begin(ssid, pass); 225 | dht.begin(); 226 | timer.setInterval(3000L, checkBlynk); // check if connected to Blynk server every 3 seconds 227 | 228 | Blynk.config(BLYNK_AUTH_TOKEN);//, ssid, pass); 229 | } 230 | 231 | void loop() 232 | { 233 | if (WiFi.status() != WL_CONNECTED) 234 | { 235 | if (DEBUG_SW) Serial.println("WiFi Not Connected"); 236 | } 237 | else 238 | { 239 | if (DEBUG_SW) Serial.println("WiFi Connected"); 240 | Blynk.run(); 241 | } 242 | button1.check(); 243 | button2.check(); 244 | button3.check(); 245 | button4.check(); 246 | button5.check(); 247 | fan(); 248 | ir_remote(); 249 | 250 | timer.run(); // Initiates SimpleTimer 251 | 252 | 253 | // put your main code here, to run repeatedly: 254 | } 255 | 256 | 257 | 258 | void ir_remote() { 259 | if (DEBUG_SW)Serial.println("Inside IR REMOTE"); 260 | if (irrecv.decode(&results)) { 261 | if (DEBUG_SW)Serial.println(results.value, HEX); //print the HEX code 262 | switch (results.value) { 263 | case IR_Relay1: 264 | switch_state_ch1 = !switch_state_ch1; 265 | digitalWrite(r1, switch_state_ch1); 266 | if (DEBUG_SW)Serial.println("RELAY1 ON"); 267 | Blynk.virtualWrite(V1, switch_state_ch1); 268 | delay(100); 269 | break; 270 | case IR_Relay2: 271 | switch_state_ch2 = !switch_state_ch2; 272 | digitalWrite(r2, switch_state_ch2); 273 | Blynk.virtualWrite(V2, switch_state_ch2); 274 | delay(100); 275 | break; 276 | case IR_Relay3: 277 | switch_state_ch3 = !switch_state_ch3; 278 | digitalWrite(r3, switch_state_ch3); 279 | Blynk.virtualWrite(V3, switch_state_ch3); 280 | delay(100); 281 | break; 282 | case IR_Relay4: 283 | switch_state_ch4 = !switch_state_ch4; 284 | digitalWrite(r4, switch_state_ch4); 285 | Blynk.virtualWrite(V4, switch_state_ch4); 286 | delay(100); 287 | break; 288 | case IR_Relay_All_Off: 289 | All_Lights_Off(); 290 | break; 291 | case IR_Relay_All_On: 292 | All_Lights_On(); 293 | break; 294 | case IR_Fan_on: 295 | if (curr_speed == 0) 296 | { 297 | speed_0 (); 298 | } 299 | else if (curr_speed == 1) 300 | { 301 | speed_1(); 302 | } 303 | else if (curr_speed == 2) 304 | { 305 | speed_2(); 306 | } 307 | else if (curr_speed == 3) 308 | { 309 | speed_3(); 310 | } 311 | else if (curr_speed == 4) 312 | { 313 | speed_4(); 314 | } 315 | else 316 | {} 317 | break; 318 | case IR_Fan_off: 319 | speed_0(); 320 | break; 321 | case IR_Speed_Up: 322 | if (curr_speed == 1) 323 | { 324 | speed_2(); 325 | } 326 | else if (curr_speed == 2) 327 | { 328 | speed_3(); 329 | } 330 | else if (curr_speed == 3) 331 | { 332 | speed_4(); 333 | } 334 | else if (curr_speed == 4) 335 | { 336 | //Do nothing 337 | } 338 | else {} 339 | 340 | break; 341 | case IR_Speed_Dw: 342 | if (curr_speed == 1) 343 | { 344 | //Do nothing 345 | } 346 | if (curr_speed == 2) 347 | { 348 | speed_1(); 349 | } 350 | if (curr_speed == 3) 351 | { 352 | speed_2(); 353 | } 354 | if (curr_speed == 4) 355 | { 356 | speed_3(); 357 | } 358 | else 359 | {} 360 | 361 | break; 362 | default : break; 363 | } 364 | irrecv.resume(); 365 | } 366 | } 367 | 368 | void All_Lights_Off() 369 | { 370 | switch_state_ch1 = 0; 371 | digitalWrite(r1, LOW); 372 | Blynk.virtualWrite(V1, LOW); 373 | 374 | switch_state_ch2 = 0; 375 | digitalWrite(r2, LOW); 376 | Blynk.virtualWrite(V2, LOW); 377 | 378 | switch_state_ch3 = 0; 379 | digitalWrite(r3, LOW); 380 | Blynk.virtualWrite(V3, LOW); 381 | 382 | switch_state_ch4 = 0; 383 | digitalWrite(r4, LOW); 384 | Blynk.virtualWrite(V4, LOW); 385 | 386 | } 387 | 388 | void All_Lights_On() 389 | { 390 | switch_state_ch1 = 1; 391 | digitalWrite(r1, HIGH); 392 | Blynk.virtualWrite(V1, HIGH); 393 | 394 | switch_state_ch2 = 1; 395 | digitalWrite(r2, HIGH); 396 | Blynk.virtualWrite(V2, HIGH); 397 | 398 | switch_state_ch3 = 1; 399 | digitalWrite(r3, HIGH); 400 | Blynk.virtualWrite(V3, HIGH); 401 | 402 | switch_state_ch4 = 1; 403 | digitalWrite(r4, HIGH); 404 | Blynk.virtualWrite(V4, HIGH); 405 | 406 | } 407 | 408 | 409 | void checkBlynk() 410 | { 411 | bool isconnected = Blynk.connected(); 412 | if (isconnected == false) 413 | { 414 | if (DEBUG_SW)Serial.println("Blynk Not Connected"); 415 | } 416 | if (isconnected == true) 417 | { 418 | if (DEBUG_SW)Serial.println("Blynk Connected"); 419 | send_sensor(); 420 | } 421 | } 422 | 423 | 424 | 425 | 426 | //functions for defineing manual switch 427 | 428 | void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 429 | if (DEBUG_SW)Serial.println("EVENT1"); 430 | switch (eventType) { 431 | case AceButton::kEventPressed: 432 | if (DEBUG_SW)Serial.println("kEventPressed"); 433 | Blynk.virtualWrite(V1, HIGH); 434 | digitalWrite(r1, HIGH); 435 | break; 436 | case AceButton::kEventReleased: 437 | if (DEBUG_SW)Serial.println("kEventReleased"); 438 | Blynk.virtualWrite(V1, LOW); 439 | digitalWrite(r1, LOW); 440 | break; 441 | } 442 | } 443 | void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 444 | if (DEBUG_SW)Serial.println("EVENT2"); 445 | //EspalexaDevice* d2 = espalexa.getDevice(1); 446 | switch (eventType) { 447 | case AceButton::kEventPressed: 448 | if (DEBUG_SW)Serial.println("kEventPressed"); 449 | Blynk.virtualWrite(V2, HIGH); 450 | digitalWrite(r2, HIGH); 451 | break; 452 | case AceButton::kEventReleased: 453 | if (DEBUG_SW)Serial.println("kEventReleased"); 454 | Blynk.virtualWrite(V2, LOW); 455 | digitalWrite(r2, LOW); 456 | break; 457 | } 458 | } 459 | void button3Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 460 | if (DEBUG_SW)Serial.println("EVENT3"); 461 | //EspalexaDevice* d3 = espalexa.getDevice(2); 462 | switch (eventType) { 463 | case AceButton::kEventPressed: 464 | if (DEBUG_SW)Serial.println("kEventPressed"); 465 | Blynk.virtualWrite(V3, HIGH); 466 | digitalWrite(r3, HIGH); 467 | break; 468 | case AceButton::kEventReleased: 469 | if (DEBUG_SW)Serial.println("kEventReleased"); 470 | Blynk.virtualWrite(V3, LOW); 471 | digitalWrite(r3, LOW); 472 | break; 473 | } 474 | } 475 | void button4Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 476 | if (DEBUG_SW)Serial.println("EVENT4"); 477 | //EspalexaDevice* d4 = espalexa.getDevice(3); 478 | switch (eventType) { 479 | case AceButton::kEventPressed: 480 | if (DEBUG_SW)Serial.println("kEventPressed"); 481 | Blynk.virtualWrite(V4, HIGH); 482 | digitalWrite(r4, HIGH); 483 | break; 484 | case AceButton::kEventReleased: 485 | if (DEBUG_SW)Serial.println("kEventReleased"); 486 | Blynk.virtualWrite(V4, LOW); 487 | digitalWrite(r4, LOW); 488 | break; 489 | } 490 | } 491 | 492 | void button5Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 493 | if (DEBUG_SW)Serial.println("EVENT5"); 494 | switch (eventType) { 495 | case AceButton::kEventPressed: 496 | if (DEBUG_SW)Serial.println("kEventPressed"); 497 | if (curr_speed == 0) 498 | { 499 | speed_0(); 500 | } 501 | if (curr_speed == 1) 502 | { 503 | speed_1(); 504 | } 505 | if (curr_speed == 2) 506 | { 507 | speed_2(); 508 | } 509 | if (curr_speed == 3) 510 | { 511 | speed_3(); 512 | } 513 | if (curr_speed == 4) 514 | { 515 | speed_4(); 516 | } 517 | break; 518 | case AceButton::kEventReleased: 519 | if (DEBUG_SW)Serial.println("kEventReleased"); 520 | digitalWrite(Speed1, LOW); 521 | digitalWrite(Speed2, LOW); 522 | digitalWrite(Speed4, LOW); 523 | curr_speed = 0; 524 | Blynk.virtualWrite(V0, curr_speed); 525 | break; 526 | } 527 | } 528 | 529 | void fan() 530 | { 531 | if ( digitalRead(s1) == LOW && speed1_flag == 1) 532 | { 533 | speed_1(); 534 | speed1_flag = 0; 535 | speed2_flag = 1; 536 | speed3_flag = 1; 537 | speed4_flag = 1; 538 | speed0_flag = 1; 539 | 540 | 541 | } 542 | if (digitalRead(s2) == LOW && digitalRead(s3) == HIGH && speed2_flag == 1) 543 | { 544 | speed_2(); 545 | speed1_flag = 1; 546 | speed2_flag = 0; 547 | speed3_flag = 1; 548 | speed4_flag = 1; 549 | speed0_flag = 1; 550 | 551 | } 552 | if (digitalRead(s2) == LOW && digitalRead(s3) == LOW && speed3_flag == 1) 553 | { 554 | speed_3(); 555 | speed1_flag = 1; 556 | speed2_flag = 1; 557 | speed3_flag = 0; 558 | speed4_flag = 1; 559 | speed0_flag = 1; 560 | } 561 | if (digitalRead(s4) == LOW && speed4_flag == 1) 562 | { 563 | speed_4(); 564 | speed1_flag = 1; 565 | speed2_flag = 1; 566 | speed3_flag = 1; 567 | speed4_flag = 0; 568 | speed0_flag = 1; 569 | } 570 | if (digitalRead(s1) == HIGH && digitalRead(s2) == HIGH && digitalRead(s3) == HIGH && digitalRead(s4) == HIGH && speed0_flag == 1) 571 | { 572 | speed_0(); 573 | speed1_flag = 1; 574 | speed2_flag = 1; 575 | speed3_flag = 1; 576 | speed4_flag = 1; 577 | speed0_flag = 0; 578 | } 579 | 580 | } 581 | 582 | 583 | //functions for defineing of speeds 584 | 585 | void speed_0() 586 | { 587 | //All Relays Off - Fan at speed 0 588 | if (DEBUG_SW)Serial.println("SPEED 0"); 589 | curr_speed = 0; 590 | Blynk.virtualWrite(V0, curr_speed); 591 | digitalWrite(Speed1, LOW); 592 | digitalWrite(Speed2, LOW); 593 | digitalWrite(Speed4, LOW); 594 | 595 | } 596 | 597 | void speed_1() 598 | { 599 | //Speed1 Relay On - Fan at speed 1 600 | if (DEBUG_SW)Serial.println("SPEED 1"); 601 | curr_speed = 1; 602 | Blynk.virtualWrite(V0, curr_speed); 603 | digitalWrite(Speed1, LOW); 604 | digitalWrite(Speed2, LOW); 605 | digitalWrite(Speed4, LOW); 606 | delay(1000); 607 | digitalWrite(Speed1, HIGH); 608 | } 609 | 610 | void speed_2() 611 | { 612 | //Speed2 Relay On - Fan at speed 2 613 | if (DEBUG_SW)Serial.println("SPEED 2"); 614 | curr_speed = 2; 615 | Blynk.virtualWrite(V0, curr_speed); 616 | digitalWrite(Speed1, LOW); 617 | digitalWrite(Speed2, LOW); 618 | digitalWrite(Speed4, LOW); 619 | delay(1000); 620 | digitalWrite(Speed2, HIGH); 621 | } 622 | 623 | void speed_3() 624 | { 625 | //Speed1 & Speed2 Relays On - Fan at speed 3 626 | if (DEBUG_SW)Serial.println("SPEED 3"); 627 | curr_speed = 3; 628 | Blynk.virtualWrite(V0, curr_speed); 629 | digitalWrite(Speed1, LOW); 630 | digitalWrite(Speed2, LOW); 631 | digitalWrite(Speed4, LOW); 632 | delay(1000); 633 | digitalWrite(Speed1, HIGH); 634 | digitalWrite(Speed2, HIGH); 635 | 636 | } 637 | 638 | void speed_4() 639 | { 640 | //Speed4 Relay On - Fan at speed 4 641 | if (DEBUG_SW)Serial.println("SPEED 4"); 642 | curr_speed = 4; 643 | Blynk.virtualWrite(V0, curr_speed); 644 | digitalWrite(Speed1, LOW); 645 | digitalWrite(Speed2, LOW); 646 | digitalWrite(Speed4, LOW); 647 | delay(1000); 648 | digitalWrite(Speed4, HIGH); 649 | } 650 | 651 | void send_sensor() { 652 | 653 | float h = dht.readHumidity(); 654 | float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit 655 | 656 | if (isnan(h) || isnan(t)) { 657 | if (DEBUG_SW)Serial.println("Failed to read from DHT sensor!"); 658 | return; 659 | } 660 | else 661 | { 662 | humidity_value = h; 663 | temperature_value = t; 664 | if (DEBUG_SW)Serial.print("Temperature - "); if (DEBUG_SW)Serial.println(t); 665 | if (DEBUG_SW)Serial.print("Humidity - "); if (DEBUG_SW)Serial.println(h); 666 | Blynk.virtualWrite(V5, temperature_value); 667 | Blynk.virtualWrite(V6, humidity_value); 668 | } 669 | } 670 | -------------------------------------------------------------------------------- /Rainmaker/Rainmaker.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This is the code for the project, 3 | All in One Home Automation project using ESP RainMaker made using ESP32 (Version 3) 4 | 5 | This code was written by Sachin Soni for techiesms YouTube channel 6 | 7 | This code is provided free for project purpose and fair use only. 8 | Do mail us if you want to use it commercially 9 | techiesms@gmail.com 10 | 11 | Copyrighted © by techiesms 12 | 13 | Watch out it's complete tutorial on our YouTube channel 14 | https://www.YouTube.com/techiesms 15 | */ 16 | 17 | #include "RMaker.h" 18 | #include "WiFi.h" 19 | #include "WiFiProv.h" 20 | #include // https://github.com/Arduino-IRremote/Arduino-IRremote (3.6.1) 21 | #include // https://github.com/adafruit/DHT-sensor-library (1.4.3) 22 | #include // https://github.com/kiryanenko/SimpleTimer (1.0.0) 23 | #include // https://github.com/bxparks/AceButton (1.9.2) 24 | #include 25 | Preferences pref; 26 | 27 | // Relay State 28 | bool toggleState_1 = LOW; //Define integer to remember the toggle state for relay 1 29 | bool toggleState_2 = LOW; //Define integer to remember the toggle state for relay 2 30 | bool toggleState_3 = LOW; //Define integer to remember the toggle state for relay 3 31 | bool toggleState_4 = LOW; //Define integer to remember the toggle state for relay 4 32 | int FanState = 0 ; 33 | 34 | using namespace ace_button; 35 | 36 | // BLE Credentials 37 | const char *service_name = "PROV_techiesms"; //name of node in BLE 38 | const char *pop = "Techiesms123"; //password 39 | 40 | // for Turning On and Off the Serial Monitor 41 | #define DEBUG_SW 1 42 | 43 | // By Default all the Relays will be in OFF State 44 | #define DEFAULT_RELAY_STATE false 45 | 46 | // define the Node Name 47 | char nodeName[] = "All in One Home Automation"; 48 | 49 | // GPIO for Relay (Appliance Control) 50 | static uint8_t relay1 = 25; 51 | static uint8_t relay2 = 26; 52 | static uint8_t relay3 = 4; 53 | static uint8_t relay4 = 22; 54 | 55 | // GPIO for Relay (Fan Speed Control) 56 | static uint8_t Speed1 = 21; 57 | static uint8_t Speed2 = 19; 58 | static uint8_t Speed4 = 18; 59 | 60 | // GPIO for switch 61 | static uint8_t switch1 = 32; 62 | static uint8_t switch2 = 35; 63 | static uint8_t switch3 = 34; 64 | static uint8_t switch4 = 39; 65 | 66 | // GPIO for Fan Regulator Knob 67 | static uint8_t fan_switch = 33; 68 | static uint8_t s1 = 27; 69 | static uint8_t s2 = 14; 70 | static uint8_t s3 = 23; 71 | static uint8_t s4 = 13; 72 | 73 | static uint8_t gpio_reset = 0; // Reset Pin 74 | static uint8_t IR_SENS = 17; // IR Receiver Pin 75 | static uint8_t DHTPIN = 16; // DHT11 Pin 76 | 77 | // Flags for Fan Speed 78 | bool speed1_flag = 1; 79 | bool speed2_flag = 1; 80 | bool speed3_flag = 1; 81 | bool speed4_flag = 1; 82 | bool speed0_flag = 1; 83 | 84 | // Name of the device shown in the App 85 | char Device1[] = "light1"; 86 | char Device2[] = "light2"; 87 | char Device3[] = "light3"; 88 | char Device4[] = "light4"; 89 | 90 | // IR Remote Code for Lights 91 | #define IR_Relay1 0x1FE50AF 92 | #define IR_Relay2 0x1FED827 93 | #define IR_Relay3 0x1FEF807 94 | #define IR_Relay4 0x1FE30CF 95 | #define IR_Relay_All_Off 0x1FE48B7 96 | #define IR_Relay_All_On 0x1FE7887 97 | 98 | // IR Remote Code for Fan 99 | #define IR_Speed_Up 0x1FE609F 100 | #define IR_Speed_Dw 0x1FEA05F 101 | #define IR_Fan_off 0x1FE10EF 102 | #define IR_Fan_on 0x1FE906F 103 | 104 | // Relay State 105 | bool switch_state_ch1 = LOW; 106 | bool switch_state_ch2 = LOW; 107 | bool switch_state_ch3 = LOW; 108 | bool switch_state_ch4 = LOW; 109 | 110 | // Declaring & Setting Deafult value in all variables 111 | float temperature_value = 0; 112 | float humidity_value = 0; 113 | int Slider_Value = 0; 114 | int curr_speed = 0; 115 | bool fan_power = 0; 116 | 117 | static Switch my_switch1("light1", &relay1); 118 | static Switch my_switch2("light2", &relay2); 119 | static Switch my_switch3("light3", &relay3); 120 | static Switch my_switch4("light4", &relay4); 121 | static Fan my_fan("Fan"); 122 | 123 | static TemperatureSensor temperature("Temperature"); 124 | static TemperatureSensor humidity("Humidity"); 125 | 126 | ButtonConfig config1; 127 | AceButton button1(&config1); 128 | ButtonConfig config2; 129 | AceButton button2(&config2); 130 | ButtonConfig config3; 131 | AceButton button3(&config3); 132 | ButtonConfig config4; 133 | AceButton button4(&config4); 134 | ButtonConfig config5; 135 | AceButton button5(&config5); 136 | 137 | void handleEvent1(AceButton*, uint8_t, uint8_t); 138 | void handleEvent2(AceButton*, uint8_t, uint8_t); 139 | void handleEvent3(AceButton*, uint8_t, uint8_t); 140 | void handleEvent4(AceButton*, uint8_t, uint8_t); 141 | void handleEvent5(AceButton*, uint8_t, uint8_t); 142 | 143 | DHT dht(DHTPIN, DHT11); 144 | IRrecv irrecv(IR_SENS); 145 | decode_results results; 146 | SimpleTimer Timer; 147 | 148 | void sysProvEvent(arduino_event_t *sys_event) 149 | { 150 | switch (sys_event->event_id) 151 | { 152 | case ARDUINO_EVENT_PROV_START: 153 | if (DEBUG_SW)if (DEBUG_SW)Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop); 154 | printQR(service_name, pop, "ble"); 155 | break; 156 | case ARDUINO_EVENT_WIFI_STA_GOT_IP: 157 | if (DEBUG_SW)if (DEBUG_SW)Serial.print("\nConnected IP address : "); 158 | if (DEBUG_SW)if (DEBUG_SW)Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr)); 159 | break; 160 | case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: 161 | if (DEBUG_SW)if (DEBUG_SW)Serial.println("\nDisconnected. Connecting to the AP again... "); 162 | break; 163 | case ARDUINO_EVENT_PROV_CRED_RECV: 164 | if (DEBUG_SW)if (DEBUG_SW)Serial.println("\nReceived Wi-Fi credentials"); 165 | if (DEBUG_SW)if (DEBUG_SW)Serial.print("\tSSID : "); 166 | if (DEBUG_SW)if (DEBUG_SW)Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid); 167 | if (DEBUG_SW)if (DEBUG_SW)Serial.print("\tPassword : "); 168 | if (DEBUG_SW)if (DEBUG_SW)Serial.println((char const *) sys_event->event_info.prov_cred_recv.password); 169 | break; 170 | case ARDUINO_EVENT_PROV_INIT: 171 | wifi_prov_mgr_disable_auto_stop(10000); 172 | break; 173 | case ARDUINO_EVENT_PROV_CRED_SUCCESS: 174 | if (DEBUG_SW)if (DEBUG_SW)Serial.println("Stopping Provisioning!!!"); 175 | wifi_prov_mgr_stop_provisioning(); 176 | break; 177 | } 178 | } 179 | 180 | void write_callback(Device *device, Param *param, const param_val_t val, void *priv_data, write_ctx_t *ctx) 181 | { 182 | const char *device_name = device->getDeviceName(); 183 | const char *param_name = param->getParamName(); 184 | 185 | if (strcmp(device_name, "Fan") == 0) 186 | { 187 | if (strcmp(param_name, "Power") == 0) 188 | { 189 | if (DEBUG_SW)if (DEBUG_SW)Serial.printf("Received Fan power = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name); 190 | fan_power = val.val.b; 191 | if (fan_power) { 192 | if (curr_speed == 0) 193 | { 194 | speed_0(); 195 | } 196 | if (curr_speed == 1) 197 | { 198 | speed_1(); 199 | } 200 | if (curr_speed == 2) 201 | { 202 | speed_2(); 203 | } 204 | if (curr_speed == 3) 205 | { 206 | speed_3(); 207 | } 208 | if (curr_speed == 4) 209 | { 210 | speed_4(); 211 | } 212 | } 213 | else 214 | speed_0(); 215 | param->updateAndReport(val); 216 | } 217 | if (strcmp(param_name, "My_Speed") == 0) 218 | { 219 | if (DEBUG_SW)if (DEBUG_SW)Serial.printf("\nReceived value = %d for %s - %s\n", val.val.i, device_name, param_name); 220 | int Slider_Value = val.val.i; 221 | if (Slider_Value == 1) 222 | { 223 | speed_1(); 224 | } 225 | if (Slider_Value == 2) 226 | { 227 | speed_2(); 228 | } 229 | if (Slider_Value == 3) 230 | { 231 | speed_3(); 232 | } 233 | if (Slider_Value == 4) 234 | { 235 | speed_4(); 236 | } 237 | if (Slider_Value == 0) 238 | { 239 | speed_0(); 240 | } 241 | param->updateAndReport(val); 242 | } 243 | } 244 | 245 | if (strcmp(device_name, Device1) == 0) 246 | { 247 | if (DEBUG_SW)if (DEBUG_SW)Serial.printf("Switch value_1 = %s\n", val.val.b ? "true" : "false"); 248 | 249 | if (strcmp(param_name, "Power") == 0) 250 | { 251 | if (DEBUG_SW)if (DEBUG_SW)Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name); 252 | switch_state_ch1 = val.val.b; 253 | (switch_state_ch1 == false) ? digitalWrite(relay1, LOW) : digitalWrite(relay1, HIGH); 254 | pref.putBool("Relay1", switch_state_ch1); 255 | param->updateAndReport(val); 256 | } 257 | 258 | } else if (strcmp(device_name, Device2) == 0) { 259 | 260 | if (DEBUG_SW)if (DEBUG_SW)Serial.printf("SSwitch value_2 = %s\n", val.val.b ? "true" : "false"); 261 | 262 | if (strcmp(param_name, "Power") == 0) { 263 | if (DEBUG_SW)if (DEBUG_SW)Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name); 264 | switch_state_ch2 = val.val.b; 265 | (switch_state_ch2 == false) ? digitalWrite(relay2, LOW) : digitalWrite(relay2, HIGH); 266 | pref.putBool("Relay2", switch_state_ch2); 267 | param->updateAndReport(val); 268 | } 269 | 270 | } else if (strcmp(device_name, Device3) == 0) { 271 | 272 | if (DEBUG_SW)if (DEBUG_SW)Serial.printf("Switch value_3 = %s\n", val.val.b ? "true" : "false"); 273 | 274 | if (strcmp(param_name, "Power") == 0) { 275 | if (DEBUG_SW)if (DEBUG_SW)Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name); 276 | switch_state_ch3 = val.val.b; 277 | (switch_state_ch3 == false) ? digitalWrite(relay3, LOW) : digitalWrite(relay3, HIGH); 278 | pref.putBool("Relay3", switch_state_ch3); 279 | param->updateAndReport(val); 280 | } 281 | 282 | } else if (strcmp(device_name, Device4) == 0) 283 | { 284 | 285 | if (DEBUG_SW)if (DEBUG_SW)Serial.printf("Switch value_4 = %s\n", val.val.b ? "true" : "false"); 286 | 287 | if (strcmp(param_name, "Power") == 0) { 288 | if (DEBUG_SW)if (DEBUG_SW)Serial.printf("Received value = %s for %s - %s\n", val.val.b ? "true" : "false", device_name, param_name); 289 | switch_state_ch4 = val.val.b; 290 | (switch_state_ch4 == false) ? digitalWrite(relay4, LOW) : digitalWrite(relay4, HIGH); 291 | pref.putBool("Relay4", switch_state_ch4); 292 | param->updateAndReport(val); 293 | } 294 | 295 | } 296 | } 297 | 298 | void readSensor() { 299 | 300 | float h = dht.readHumidity(); 301 | float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit 302 | 303 | if (isnan(h) || isnan(t)) { 304 | if (DEBUG_SW)Serial.println("Failed to read from DHT sensor!"); 305 | return; 306 | } 307 | else 308 | { 309 | humidity_value = h; 310 | temperature_value = t; 311 | if (DEBUG_SW)Serial.print("Temperature - "); if (DEBUG_SW)Serial.println(t); 312 | if (DEBUG_SW)Serial.print("Humidity - "); if (DEBUG_SW)Serial.println(h); 313 | } 314 | } 315 | 316 | void sendSensor() 317 | { 318 | readSensor(); 319 | temperature.updateAndReportParam("Temperature", temperature_value); 320 | humidity.updateAndReportParam("Temperature", humidity_value); 321 | } 322 | 323 | 324 | // This function will recall the last state 325 | void getRelayState() 326 | { 327 | toggleState_1 = pref.getBool("Relay1", 0); 328 | Serial.print("Last State Relay1 - "); Serial.println(toggleState_1); 329 | digitalWrite(relay1, toggleState_1); 330 | my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_1); 331 | delay(200); 332 | toggleState_2 = pref.getBool("Relay2", 0); 333 | Serial.print("Last State Relay2- "); Serial.println(toggleState_2); 334 | digitalWrite(relay2, toggleState_2); 335 | my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_2); 336 | delay(200); 337 | toggleState_3 = pref.getBool("Relay3", 0); 338 | Serial.print("Last State Relay3- "); Serial.println(toggleState_3); 339 | digitalWrite(relay3, toggleState_3); 340 | my_switch3.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_3); 341 | delay(200); 342 | toggleState_4 = pref.getBool("Relay4", 0); 343 | Serial.print("Last State Relay4- "); Serial.println(toggleState_4); 344 | digitalWrite(relay4, toggleState_4); 345 | my_switch3.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, toggleState_4); 346 | delay(200); 347 | FanState = pref.getInt("Fan", 0); 348 | Serial.print("Last State Fan- "); Serial.println(FanState); 349 | 350 | if (FanState == 0) 351 | speed_0(); 352 | else if (FanState == 1) 353 | speed_1(); 354 | else if (FanState == 2) 355 | speed_2(); 356 | else if (FanState == 3) 357 | speed_3(); 358 | else if (FanState == 4) 359 | speed_4(); 360 | else 361 | {} 362 | delay(200); 363 | 364 | } 365 | 366 | void ir_remote() { 367 | if (DEBUG_SW)Serial.println("Inside IR REMOTE"); 368 | if (irrecv.decode(&results)) { 369 | if (DEBUG_SW)Serial.println(results.value, HEX); //print the HEX code 370 | switch (results.value) { 371 | case IR_Relay1: 372 | switch_state_ch1 = !switch_state_ch1; 373 | digitalWrite(relay1, switch_state_ch1); 374 | if (DEBUG_SW)Serial.println("RELAY1 ON"); 375 | my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch1); 376 | pref.putBool("Relay1", switch_state_ch1); 377 | delay(100); 378 | break; 379 | case IR_Relay2: 380 | switch_state_ch2 = !switch_state_ch2; 381 | digitalWrite(relay2, switch_state_ch2); 382 | my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch2); 383 | pref.putBool("Relay2", switch_state_ch2); 384 | delay(100); 385 | break; 386 | case IR_Relay3: 387 | switch_state_ch3 = !switch_state_ch3; 388 | digitalWrite(relay3, switch_state_ch3); 389 | my_switch3.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch3); 390 | pref.putBool("Relay3", switch_state_ch3); 391 | delay(100); 392 | break; 393 | case IR_Relay4: 394 | switch_state_ch4 = !switch_state_ch4; 395 | digitalWrite(relay4, switch_state_ch4); 396 | my_switch4.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch4); 397 | pref.putBool("Relay4", switch_state_ch4); 398 | delay(100); 399 | break; 400 | case IR_Relay_All_Off: 401 | All_Lights_Off(); 402 | break; 403 | case IR_Relay_All_On: 404 | All_Lights_On(); 405 | break; 406 | case IR_Fan_on: 407 | if (curr_speed == 0) 408 | { 409 | speed_0 (); 410 | } 411 | else if (curr_speed == 1) 412 | { 413 | speed_1(); 414 | } 415 | else if (curr_speed == 2) 416 | { 417 | speed_2(); 418 | } 419 | else if (curr_speed == 3) 420 | { 421 | speed_3(); 422 | } 423 | else if (curr_speed == 4) 424 | { 425 | speed_4(); 426 | } 427 | else 428 | {} 429 | break; 430 | case IR_Fan_off: 431 | speed_0(); 432 | break; 433 | case IR_Speed_Up: 434 | if (curr_speed == 1) 435 | { 436 | speed_2(); 437 | } 438 | else if (curr_speed == 2) 439 | { 440 | speed_3(); 441 | } 442 | else if (curr_speed == 3) 443 | { 444 | speed_4(); 445 | } 446 | else if (curr_speed == 4) 447 | { 448 | //Do nothing 449 | } 450 | else {} 451 | 452 | break; 453 | case IR_Speed_Dw: 454 | if (curr_speed == 1) 455 | { 456 | //Do nothing 457 | } 458 | if (curr_speed == 2) 459 | { 460 | speed_1(); 461 | } 462 | if (curr_speed == 3) 463 | { 464 | speed_2(); 465 | } 466 | if (curr_speed == 4) 467 | { 468 | speed_3(); 469 | } 470 | else 471 | {} 472 | 473 | break; 474 | default : break; 475 | } 476 | irrecv.resume(); 477 | } 478 | } 479 | 480 | void All_Lights_Off() 481 | { 482 | switch_state_ch1 = 0; 483 | digitalWrite(relay1, LOW); 484 | my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch1); 485 | pref.putBool("Relay1", switch_state_ch1); 486 | 487 | switch_state_ch2 = 0; 488 | digitalWrite(relay2, LOW); 489 | my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch2); 490 | pref.putBool("Relay2", switch_state_ch2); 491 | 492 | switch_state_ch3 = 0; 493 | digitalWrite(relay3, LOW); 494 | my_switch3.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch3); 495 | pref.putBool("Relay3", switch_state_ch3); 496 | 497 | switch_state_ch4 = 0; 498 | digitalWrite(relay4, LOW); 499 | my_switch4.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch4); 500 | pref.putBool("Relay4", switch_state_ch4); 501 | 502 | } 503 | 504 | void All_Lights_On() 505 | { 506 | switch_state_ch1 = 1; 507 | digitalWrite(relay1, HIGH); 508 | my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch1); 509 | pref.putBool("Relay1", switch_state_ch1); 510 | 511 | switch_state_ch2 = 1; 512 | digitalWrite(relay2, HIGH); 513 | my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch2); 514 | pref.putBool("Relay2", switch_state_ch2); 515 | 516 | switch_state_ch3 = 1; 517 | digitalWrite(relay3, HIGH); 518 | my_switch3.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch3); 519 | pref.putBool("Relay3", switch_state_ch3); 520 | 521 | switch_state_ch4 = 1; 522 | digitalWrite(relay4, HIGH); 523 | my_switch4.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch4); 524 | pref.putBool("Relay4", switch_state_ch4); 525 | 526 | } 527 | 528 | void setup() 529 | { 530 | 531 | if (DEBUG_SW)Serial.begin(115200); 532 | pref.begin("Relay_State", false); 533 | // Set the Relays GPIOs as output mode 534 | pinMode(relay1, OUTPUT); 535 | pinMode(relay2, OUTPUT); 536 | pinMode(relay3, OUTPUT); 537 | pinMode(relay4, OUTPUT); 538 | 539 | //Turning All Relays Off by default 540 | digitalWrite(relay1, LOW); 541 | digitalWrite(relay2, LOW); 542 | digitalWrite(relay3, LOW); 543 | digitalWrite(relay4, LOW); 544 | 545 | // Set the Relays GPIOs as output mode 546 | pinMode(Speed1, OUTPUT); 547 | pinMode(Speed2, OUTPUT); 548 | pinMode(Speed4, OUTPUT); 549 | 550 | //Turning All Relays Off by default 551 | digitalWrite(Speed1, LOW); 552 | digitalWrite(Speed2, LOW); 553 | digitalWrite(Speed4, LOW); 554 | 555 | // Configure the input GPIOs 556 | pinMode(switch1, INPUT_PULLUP); 557 | pinMode(switch2, INPUT_PULLUP); 558 | pinMode(switch3, INPUT_PULLUP); 559 | pinMode(switch4, INPUT_PULLUP); 560 | 561 | pinMode(fan_switch, INPUT_PULLUP); 562 | pinMode(s1, INPUT_PULLUP); 563 | pinMode(s2, INPUT_PULLUP); 564 | pinMode(s3, INPUT_PULLUP); 565 | pinMode(s4, INPUT_PULLUP); 566 | 567 | pinMode(gpio_reset, INPUT); 568 | 569 | config1.setEventHandler(button1Handler); 570 | config2.setEventHandler(button2Handler); 571 | config3.setEventHandler(button3Handler); 572 | config4.setEventHandler(button4Handler); 573 | config5.setEventHandler(button5Handler); 574 | 575 | button1.init(switch1); 576 | button2.init(switch2); 577 | button3.init(switch3); 578 | button4.init(switch4); 579 | button5.init(fan_switch); 580 | 581 | irrecv.enableIRIn(); // Enabling IR sensor 582 | dht.begin(); // Enabling DHT sensor 583 | 584 | Node my_node; 585 | my_node = RMaker.initNode(nodeName); 586 | 587 | //Custom Fan device 588 | my_fan.addCb(write_callback); 589 | Param speed("My_Speed",ESP_RMAKER_PARAM_RANGE , value(0), PROP_FLAG_READ | PROP_FLAG_WRITE); 590 | speed.addBounds(value(0), value(4), value(1)); 591 | speed.addUIType(ESP_RMAKER_UI_SLIDER); 592 | my_fan.addParam(speed); 593 | my_node.addDevice(my_fan); 594 | my_fan.updateAndReportParam("My_Speed", 0); 595 | my_fan.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, fan_power); 596 | delay(500); 597 | 598 | my_switch1.addCb(write_callback); 599 | my_node.addDevice(my_switch1); 600 | my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch1); 601 | delay(500); 602 | 603 | my_switch2.addCb(write_callback); 604 | my_node.addDevice(my_switch2); 605 | my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch2); 606 | delay(500); 607 | 608 | my_switch3.addCb(write_callback); 609 | my_node.addDevice(my_switch3); 610 | my_switch3.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch3); 611 | delay(500); 612 | 613 | my_switch4.addCb(write_callback); 614 | my_node.addDevice(my_switch4); 615 | my_switch4.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch4); 616 | delay(500); 617 | 618 | 619 | my_node.addDevice(temperature); 620 | my_node.addDevice(humidity); 621 | 622 | 623 | 624 | Timer.setInterval(30000); 625 | 626 | //This is optional 627 | RMaker.enableOTA(OTA_USING_PARAMS); 628 | //If you want to enable scheduling, set time zone for your region using setTimeZone(). 629 | //The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html 630 | // RMaker.setTimeZone("Asia/Shanghai"); 631 | // Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone 632 | RMaker.enableTZService(); 633 | RMaker.enableSchedule(); 634 | 635 | if (DEBUG_SW)Serial.printf("\nStarting ESP-RainMaker\n"); 636 | RMaker.start(); 637 | 638 | WiFi.onEvent(sysProvEvent); 639 | #if CONFIG_IDF_TARGET_ESP32 640 | WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name); 641 | #else 642 | WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name); 643 | #endif 644 | 645 | 646 | getRelayState(); // Get the last state of Relays 647 | 648 | my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false); 649 | my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false); 650 | my_switch3.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false); 651 | my_switch4.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, false); 652 | 653 | 654 | } 655 | 656 | void loop() 657 | { 658 | button1.check(); 659 | button2.check(); 660 | button3.check(); 661 | button4.check(); 662 | button5.check(); 663 | fan(); 664 | 665 | // Read GPIO0 (external button to gpio_reset device 666 | if (digitalRead(gpio_reset) == LOW) { 667 | //Push button pressed 668 | if (DEBUG_SW)Serial.printf("reset Button Pressed!\n"); 669 | // Key debounce handling 670 | delay(100); 671 | int startTime = millis(); 672 | while (digitalRead(gpio_reset) == LOW) delay(50); 673 | int endTime = millis(); 674 | 675 | if ((endTime - startTime) > 5000) { 676 | // If key pressed for more than 5secs, reset all 677 | if (DEBUG_SW)Serial.printf("reset to factory.\n"); 678 | RMakerFactoryReset(2); 679 | } 680 | } 681 | delay(100); 682 | 683 | if (WiFi.status() != WL_CONNECTED) 684 | { 685 | if (DEBUG_SW)Serial.println("WiFi Not Connected"); 686 | 687 | } 688 | else 689 | { 690 | if (DEBUG_SW)Serial.println("WiFi Connected"); 691 | if (Timer.isReady()) { 692 | if (DEBUG_SW)Serial.println("Sending Sensor Data"); 693 | sendSensor(); 694 | Timer.reset(); // Reset a second timer 695 | } 696 | } 697 | 698 | ir_remote(); //IR remote Control 699 | } 700 | 701 | //functions for defineing manual switch 702 | 703 | void button1Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 704 | if (DEBUG_SW)Serial.println("EVENT1"); 705 | //EspalexaDevice* d1 = espalexa.getDevice(0); 706 | switch (eventType) { 707 | case AceButton::kEventPressed: 708 | if (DEBUG_SW)Serial.println("kEventPressed"); 709 | switch_state_ch1 = true; 710 | my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch1); 711 | digitalWrite(relay1, HIGH); 712 | pref.putBool("Relay1", switch_state_ch1); 713 | break; 714 | case AceButton::kEventReleased: 715 | if (DEBUG_SW)Serial.println("kEventReleased"); 716 | switch_state_ch1 = false; 717 | my_switch1.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch1); 718 | digitalWrite(relay1, LOW); 719 | pref.putBool("Relay1", switch_state_ch1); 720 | break; 721 | } 722 | } 723 | void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 724 | if (DEBUG_SW)Serial.println("EVENT2"); 725 | //EspalexaDevice* d2 = espalexa.getDevice(1); 726 | switch (eventType) { 727 | case AceButton::kEventPressed: 728 | if (DEBUG_SW)Serial.println("kEventPressed"); 729 | switch_state_ch2 = true; 730 | my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch2); 731 | digitalWrite(relay2, HIGH); 732 | pref.putBool("Relay2", switch_state_ch2); 733 | break; 734 | case AceButton::kEventReleased: 735 | if (DEBUG_SW)Serial.println("kEventReleased"); 736 | switch_state_ch2 = false; 737 | my_switch2.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch2); 738 | digitalWrite(relay2, LOW); 739 | pref.putBool("Relay2", switch_state_ch2); 740 | break; 741 | } 742 | } 743 | void button3Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 744 | if (DEBUG_SW)Serial.println("EVENT3"); 745 | //EspalexaDevice* d3 = espalexa.getDevice(2); 746 | switch (eventType) { 747 | case AceButton::kEventPressed: 748 | if (DEBUG_SW)Serial.println("kEventPressed"); 749 | switch_state_ch3 = true; 750 | my_switch3.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch3); 751 | digitalWrite(relay3, HIGH); 752 | pref.putBool("Relay3", switch_state_ch3); 753 | break; 754 | case AceButton::kEventReleased: 755 | if (DEBUG_SW)Serial.println("kEventReleased"); 756 | switch_state_ch3 = false; 757 | my_switch3.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch3); 758 | digitalWrite(relay3, LOW); 759 | pref.putBool("Relay3", switch_state_ch3); 760 | break; 761 | } 762 | } 763 | void button4Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 764 | if (DEBUG_SW)Serial.println("EVENT4"); 765 | //EspalexaDevice* d4 = espalexa.getDevice(3); 766 | switch (eventType) { 767 | case AceButton::kEventPressed: 768 | if (DEBUG_SW)Serial.println("kEventPressed"); 769 | switch_state_ch4 = true; 770 | my_switch4.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch4); 771 | digitalWrite(relay4, HIGH); 772 | pref.putBool("Relay4", switch_state_ch4); 773 | break; 774 | case AceButton::kEventReleased: 775 | if (DEBUG_SW)Serial.println("kEventReleased"); 776 | switch_state_ch4 = false; 777 | my_switch4.updateAndReportParam(ESP_RMAKER_DEF_POWER_NAME, switch_state_ch4); 778 | digitalWrite(relay4, LOW); 779 | pref.putBool("Relay4", switch_state_ch4); 780 | break; 781 | } 782 | } 783 | 784 | void button5Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) { 785 | if (DEBUG_SW)Serial.println("EVENT5"); 786 | switch (eventType) { 787 | case AceButton::kEventPressed: 788 | if (DEBUG_SW)Serial.println("kEventPressed"); 789 | if (curr_speed == 0) 790 | { 791 | speed_0(); 792 | } 793 | if (curr_speed == 1) 794 | { 795 | speed_1(); 796 | } 797 | if (curr_speed == 2) 798 | { 799 | speed_2(); 800 | } 801 | if (curr_speed == 3) 802 | { 803 | speed_3(); 804 | } 805 | if (curr_speed == 4) 806 | { 807 | speed_4(); 808 | } 809 | break; 810 | case AceButton::kEventReleased: 811 | if (DEBUG_SW)Serial.println("kEventReleased"); 812 | digitalWrite(Speed1, LOW); 813 | digitalWrite(Speed2, LOW); 814 | digitalWrite(Speed4, LOW); 815 | fan_power = 0; 816 | my_fan.updateAndReportParam("My_Speed", 0); 817 | my_fan.updateAndReportParam("Power", fan_power); 818 | break; 819 | } 820 | } 821 | 822 | 823 | //functions for defineing of speeds 824 | 825 | void speed_0() 826 | { 827 | //All Relays Off - Fan at speed 0 828 | if (DEBUG_SW)Serial.println("SPEED 0"); 829 | fan_power = 0; 830 | my_fan.updateAndReportParam("My_Speed", 0); 831 | my_fan.updateAndReportParam("Power", fan_power); 832 | digitalWrite(Speed1, LOW); 833 | digitalWrite(Speed2, LOW); 834 | digitalWrite(Speed4, LOW); 835 | pref.putInt("Fan", curr_speed); 836 | 837 | } 838 | 839 | void speed_1() 840 | { 841 | //Speed1 Relay On - Fan at speed 1 842 | if (DEBUG_SW)Serial.println("SPEED 1"); 843 | curr_speed = 1; 844 | fan_power = 1; 845 | my_fan.updateAndReportParam("My_Speed", 1); 846 | my_fan.updateAndReportParam("Power", fan_power); 847 | digitalWrite(Speed1, LOW); 848 | digitalWrite(Speed2, LOW); 849 | digitalWrite(Speed4, LOW); 850 | delay(1000); 851 | digitalWrite(Speed1, HIGH); 852 | pref.putInt("Fan", curr_speed); 853 | } 854 | 855 | void speed_2() 856 | { 857 | //Speed2 Relay On - Fan at speed 2 858 | if (DEBUG_SW)Serial.println("SPEED 2"); 859 | curr_speed = 2; 860 | fan_power = 1; 861 | my_fan.updateAndReportParam("My_Speed", 2); 862 | my_fan.updateAndReportParam("Power", fan_power); 863 | digitalWrite(Speed1, LOW); 864 | digitalWrite(Speed2, LOW); 865 | digitalWrite(Speed4, LOW); 866 | delay(1000); 867 | digitalWrite(Speed2, HIGH); 868 | pref.putInt("Fan", curr_speed); 869 | } 870 | 871 | void speed_3() 872 | { 873 | //Speed1 & Speed2 Relays On - Fan at speed 3 874 | if (DEBUG_SW)Serial.println("SPEED 3"); 875 | curr_speed = 3; 876 | fan_power = 1; 877 | my_fan.updateAndReportParam("My_Speed", 3); 878 | my_fan.updateAndReportParam("Power", fan_power); 879 | digitalWrite(Speed1, LOW); 880 | digitalWrite(Speed2, LOW); 881 | digitalWrite(Speed4, LOW); 882 | delay(1000); 883 | digitalWrite(Speed1, HIGH); 884 | digitalWrite(Speed2, HIGH); 885 | pref.putInt("Fan", curr_speed); 886 | 887 | } 888 | 889 | void speed_4() 890 | { 891 | //Speed4 Relay On - Fan at speed 4 892 | if (DEBUG_SW)Serial.println("SPEED 4"); 893 | curr_speed = 4; 894 | fan_power = 1; 895 | my_fan.updateAndReportParam("My_Speed", 4); 896 | my_fan.updateAndReportParam("Power", fan_power); 897 | digitalWrite(Speed1, LOW); 898 | digitalWrite(Speed2, LOW); 899 | digitalWrite(Speed4, LOW); 900 | delay(1000); 901 | digitalWrite(Speed4, HIGH); 902 | pref.putInt("Fan", curr_speed); 903 | } 904 | 905 | 906 | // function for controling fan using regulator 907 | 908 | void fan() 909 | { 910 | if ( digitalRead(s1) == LOW && speed1_flag == 1) 911 | { 912 | speed_1(); 913 | speed1_flag = 0; 914 | speed2_flag = 1; 915 | speed3_flag = 1; 916 | speed4_flag = 1; 917 | speed0_flag = 1; 918 | 919 | 920 | } 921 | if (digitalRead(s2) == LOW && digitalRead(s3) == HIGH && speed2_flag == 1) 922 | { 923 | speed_2(); 924 | speed1_flag = 1; 925 | speed2_flag = 0; 926 | speed3_flag = 1; 927 | speed4_flag = 1; 928 | speed0_flag = 1; 929 | 930 | } 931 | if (digitalRead(s2) == LOW && digitalRead(s3) == LOW && speed3_flag == 1) 932 | { 933 | speed_3(); 934 | speed1_flag = 1; 935 | speed2_flag = 1; 936 | speed3_flag = 0; 937 | speed4_flag = 1; 938 | speed0_flag = 1; 939 | } 940 | if (digitalRead(s4) == LOW && speed4_flag == 1) 941 | { 942 | speed_4(); 943 | speed1_flag = 1; 944 | speed2_flag = 1; 945 | speed3_flag = 1; 946 | speed4_flag = 0; 947 | speed0_flag = 1; 948 | } 949 | if (digitalRead(s1) == HIGH && digitalRead(s2) == HIGH && digitalRead(s3) == HIGH && digitalRead(s4) == HIGH && speed0_flag == 1) 950 | { 951 | speed_0(); 952 | speed1_flag = 1; 953 | speed2_flag = 1; 954 | speed3_flag = 1; 955 | speed4_flag = 1; 956 | speed0_flag = 0; 957 | } 958 | 959 | } 960 | --------------------------------------------------------------------------------