├── settings.h ├── ESP8266-Remote-Blynk-Switch.ino └── README.md /settings.h: -------------------------------------------------------------------------------- 1 | /************************************************************** 2 | * 3 | * Settings 4 | * 5 | **************************************************************/ 6 | /* 7 | Blynk Auth Code 8 | */ 9 | #define AUTH "b9af72dccede40e195093c149688c4fb" 10 | #define AUTH_GATE "ae5eab51641343209ae3c2b139ef6e0b" 11 | /* 12 | Over The Air Hostname. 13 | */ 14 | #define OTA_HOSTNAME "REMOTE-SWITCH-FRONT" 15 | /* 16 | Local Blynk Server Settings (uncomment to use local server and set address) 17 | */ 18 | #define LOCAL_SERVER IPAddress(192, 168, 1, 2) 19 | /* 20 | Hardware Pins 21 | */ 22 | #define SWITCH_PIN 14 // D5 23 | /* 24 | Virtual Pins - Base 25 | */ 26 | #define vPIN_BUTTON_TIMEOUT V0 27 | #define vPIN_BUTTON_MANUAL V1 28 | #define vPIN_TIME V2 29 | #define vPIN_LED V3 30 | #define vPIN_TIMEOUT V4 31 | #define vPIN_INFO V5 32 | #define vPIN_BRIDGE_GATE V17 33 | -------------------------------------------------------------------------------- /ESP8266-Remote-Blynk-Switch.ino: -------------------------------------------------------------------------------- 1 | #define BLYNK_PRINT Serial 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "settings.h" 7 | #include 8 | 9 | int switchState, timer1, switchDelay; 10 | SimpleTimer timer; 11 | WidgetBridge gate(vPIN_BRIDGE_GATE); 12 | 13 | BLYNK_CONNECTED() { 14 | gate.setAuthToken("ae5eab51641343209ae3c2b139ef6e0b"); 15 | } 16 | 17 | BLYNK_WRITE(vPIN_BUTTON_TIMEOUT) { // remote delay switch 18 | if (digitalRead(SWITCH_PIN)) { 19 | Switch_ON(); 20 | timer.setTimeout(switchDelay, Switch_OFF); 21 | } 22 | } 23 | 24 | BLYNK_WRITE(vPIN_BUTTON_MANUAL) { // manual button 25 | Switch_Toggle(param.asInt()); 26 | } 27 | 28 | BLYNK_WRITE(vPIN_TIME) { // timer switch 29 | if (param.asInt()) { 30 | Switch_ON(); 31 | gate.virtualWrite(vPIN_BRIDGE_GATE, param.asInt()); 32 | } else { 33 | Switch_OFF(); 34 | } 35 | } 36 | 37 | BLYNK_WRITE(vPIN_TIMEOUT) { 38 | switchDelay = param[0].asInt() * 60000; 39 | Blynk.syncVirtual(vPIN_BUTTON_TIMEOUT); 40 | } 41 | 42 | void Switch_OFF() { 43 | digitalWrite(SWITCH_PIN, 1); 44 | Blynk.virtualWrite(vPIN_BUTTON_MANUAL, 0); 45 | Blynk.virtualWrite(vPIN_LED, 0); 46 | } 47 | 48 | void Switch_ON() { 49 | digitalWrite(SWITCH_PIN, 0); 50 | Blynk.virtualWrite(vPIN_BUTTON_MANUAL, 1); 51 | Blynk.virtualWrite(vPIN_LED, 255); 52 | } 53 | 54 | void Switch_Toggle(bool state) { 55 | digitalWrite(SWITCH_PIN, !state); 56 | switchState = digitalRead(SWITCH_PIN); 57 | if (!switchState) switchState = 255; 58 | Blynk.virtualWrite(vPIN_LED, switchState); 59 | } 60 | 61 | void setup() { 62 | WiFi.mode(WIFI_STA); 63 | Serial.begin(115200); 64 | #ifdef LOCAL_SERVER 65 | Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS, LOCAL_SERVER); 66 | #else 67 | Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS); 68 | #endif 69 | while (Blynk.connect() == false) {} 70 | ArduinoOTA.setHostname(OTA_HOSTNAME); 71 | ArduinoOTA.begin(); 72 | pinMode(SWITCH_PIN, OUTPUT); 73 | digitalWrite(SWITCH_PIN, HIGH); 74 | switchDelay = 60000; 75 | Blynk.syncVirtual(vPIN_TIMEOUT, vPIN_TIME); 76 | timer.setInterval(2000L, []() { 77 | Blynk.setProperty(vPIN_INFO, "label", String("WIFI: ") + String(map(WiFi.RSSI(), -105, -40, 0, 100)) + String("% (") + WiFi.RSSI() + String("dB)")); 78 | }); 79 | } 80 | 81 | void loop() { 82 | Blynk.run(); 83 | ArduinoOTA.handle(); 84 | timer.run(); 85 | } 86 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP8266-Remote-Blynk-Switch 2 | A simple AC switch using an ESP8266 with Blynk mobile app interface. 3 | 4 | You could use this to control ANY AC or DC load up to the rated value of your relay. 5 | 6 | I have it controlling my garden lights, and even have my DIY ESP8266 Gate Counter and Logger triggering the lights when the gate is opened so you can see where you're going at night. 7 | 8 | You could use it to turn on a coffee maker (soo cliche), turn on the TV when you're not home, or use it to turn off and on lights. Anything really! 9 | 10 | I hacked it in to a 4way power board so I could turn on and off everything plugged in to it at once (again, good for mulitple sets of garden lights). 11 | 12 | Feel free to make pull requests if you wish to help develop it. 13 | 14 | There is also a support thread on the Blynk community forums: http://community.blynk.cc/t/mini-esp8266-relay-controlled-ac-power-strip-sonoff-duplicate/9549 15 | 16 | ![Blynk Dash](http://i.imgur.com/Ez9L1wl.png?1) 17 | 18 | ## Hardware 19 | 20 | * [ESP8266 Mini Dev Board (WeMos clone or other)](https://www.aliexpress.com/wholesale?catId=0&initiative_id=SB_20170114172728&SearchText=esp8266+mini) 21 | * [5VDC Relay Module (with 3.3V level shifter)](https://www.aliexpress.com/wholesale?catId=0&initiative_id=SB_20170114172728&SearchText=5vdc+relay+module) 22 | * Small project case 23 | 24 | ### Software 25 | 26 | * [Blynk](http://www.blynk.cc/) Mobile App ([iOS](https://itunes.apple.com/us/app/blynk-iot-for-arduino-rpi/id808760481?mt=8) & [Android](https://play.google.com/store/apps/details?id=cc.blynk&hl=en)) 27 | * Arduino IDE 1.6.9+ 28 | * The project sketch INO 29 | 30 | ## Wiring 31 | 32 | ![Wiring diagram](http://community.blynk.cc/uploads/default/original/2X/6/6d62d1d80a1afce405fe832198adab7e5745addc.png) 33 | 34 | ## Setup 35 | 36 | ### Libraries 37 | 38 | * Blynk Library 39 | * ArduinoOTA 40 | * SimpleTimer 41 | 42 | ### Tutorial 43 | 44 | * Open the Blynk mobile app and create a new project by scanning the following QR code 45 | 46 | ![Project QR Code](http://i.imgur.com/BWQowpv.jpg?1) 47 | 48 | * Send yourself the generated auth code 49 | * Paste your auth code in to the sketch 50 | 51 | ```cpp 52 | char auth[] = "xxxxx"; 53 | ``` 54 | 55 | * Enter your wifi SSID and PASS 56 | 57 | ```cpp 58 | char ssid[] = "xxxxx"; 59 | char pass[] = "xxxxx"; 60 | ``` 61 | 62 | * Upload the sketch to your ESP8266 63 | * Load the Blynk project and hit the PLAY button 64 | 65 | ## Usage 66 | 67 | The dash consists of a few buttons, LED, timer and a slider. 68 | 69 | You have manual control via the MANUAL button. ON and OFF state. 70 | 71 | You can also trigger the device using the TRIGGER TIMEOUT button. It will activate the relay, then deactivate it after the period set by the slider. 72 | 73 | The LED just shows the real time state of the relay. 74 | 75 | There is also a timer widget which you can use to have the relay turn on/off at a certain time. 76 | 77 | # Deveoping Further 78 | 79 | If you're just using this with manual control, then you can easily remove parts of the sketch as well as the dash widgets from the Blynk app. 80 | 81 | If you wish to toggle the relay from another ESP or hardware, then you will need to set up a Blynk Bridge between the two hardwares and make the call to V0. 82 | 83 | ```cpp 84 | bridge.virtualWrite(0,HIGH); 85 | ``` 86 | 87 | Using the new multi-device features of the app, you could use the single Blynk dash to control as many remote switches around your home. This is great because as you can see in the first image, I have set it up to control mulitple zones around my house all from the same dash. 88 | 89 | If you run out of room, you could just add the TABS widget and build on another app page. 90 | 91 | # Build Gallery 92 | 93 | ![ESP8266 Build Galelry](http://community.blynk.cc/uploads/default/optimized/2X/8/8f7701daff5ca8326e2b4d4ca5b00236416349c4_1_375x500.JPG) 94 | ![ESP8266 Build Galelry](http://community.blynk.cc/uploads/default/optimized/2X/0/0e1dc57962373873cefb44b95db958285e3a398d_1_375x500.JPG) 95 | ![ESP8266 Build Galelry](http://community.blynk.cc/uploads/default/optimized/2X/7/717696bb7d3a9b3b7dc6595b14c568734cedcea1_1_666x500.JPG) 96 | ![ESP8266 Build Galelry](http://community.blynk.cc/uploads/default/optimized/2X/7/7cdd16547c66c6ed552c4d14438eec676bac01f6_1_375x500.JPG) 97 | ![ESP8266 Build Galelry](http://community.blynk.cc/uploads/default/optimized/2X/2/2f3b14c6db2c63e738f3edcd52ca27b747951f56_1_666x500.JPG) 98 | ![ESP8266 Build Galelry](http://community.blynk.cc/uploads/default/optimized/2X/2/287da7c6946c8b4d26353d92db5e9f6c090cfb7b_1_666x500.JPG) 99 | ![ESP8266 Build Galelry](http://community.blynk.cc/uploads/default/optimized/2X/5/549b1aab15cb4bea90ef729491a0f5685d92aa8d_1_666x500.JPG) 100 | ![ESP8266 Build Galelry](http://community.blynk.cc/uploads/default/optimized/2X/c/c9866e2a4d6d535b710050e4a52d49867b36a582_1_666x500.JPG) 101 | ![ESP8266 Build Galelry](http://community.blynk.cc/uploads/default/optimized/2X/8/85712b86b53a622474ae118b4f5020ab4ef647e3_1_666x500.JPG) 102 | --------------------------------------------------------------------------------