ESP8266 Blynk Garage door & climate sensor, wifi, phone app
A garage door and a climate sensor, which is connected to wifi. It sends phone notifications when the door opens or closes.
Components and supplies
1
DHT22 Temperature Sensor
1
Reed Switch, SPST-NO
1
Adafruit HUZZAH ESP8266 Breakout
1
Jumper wires (generic)
1
Cable, USB to TTL Level
Tools and machines
1
Soldering iron (generic)
Apps and platforms
1
Blynk
1
Arduino IDE
Project description
Code
Adafruit_ESP8266_Reed_DHT22_Blynk_Notify
arduino
1/************************************************************** 2 * * 3 * Magnetic switch & DHT 22 output to blynk by David K 4 * 5 * Reed switch is connected to pin 12 via input pullup 6 * DHT sensor is connected to pin 13 7 * 8 * Blynk App project setup: 9 * LED widged attched to V4 10 * Value Display widget attached to V5 11 * Value Display widget attached to V6 12 * 13 * 14 * 15 **************************************************************/ 16 17#define BLYNK_PRINT Serial // Comment this out to disable prints and save space 18#include <SPI.h> 19#include <ESP8266WiFi.h> 20#include <BlynkSimpleEsp8266.h> 21#include <DHT.h> 22 23// You should get Auth Token in the Blynk App. 24// Go to the Project Settings (nut icon). 25char auth[] = "***************************"; 26 27// Your WiFi credentials. 28// Set password to "" for open networks. 29char ssid[] = "**********"; 30char pass[] = "*********"; 31 32int StatMagSwitch = 0; // switch status setting to 0 or LOW initially, door closed 33int PreviousStatus = 0; // this variable holds previous door status, 0: closed, 1: open 34 35#define MAGPIN 12 // pin for magnetic switch 36#define DHTPIN 13 // pin DHT sensor 37#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321 38 39BlynkTimer timer; // Initializing blynk 40DHT dht(DHTPIN, DHTTYPE); // Initializing DHT sensor 41 42WidgetLED ledDoor(V4); //Setting led widget as virtual pin 4 first 43// This function sends Arduino's up time every second to Virtual Pins (4,5,6). 44// In the app, Widget's reading frequency should be set to PUSH. This means 45// that you define how often to send data to Blynk App 46 47void sendReadings() 48{ int DoorChanged = 0; // this variable will check whether door has changed, 1 if changed 49 50 StatMagSwitch = digitalRead(MAGPIN); // Reading Magnetic switch status 51 52 if (StatMagSwitch != PreviousStatus) 53 { 54 DoorChanged = 1; 55 } 56 57 if (StatMagSwitch==1 && DoorChanged==1) 58 { 59 ledDoor.off(); //turning off if magnet is far away, i.e. Switch is HIGH 60 Blynk.notify("Garage door just closed"); 61 } 62 63 if (StatMagSwitch==0 && DoorChanged==1) 64 { 65 ledDoor.on(); //turning on if magnet is close, i.e. Switch is LOW 66 Blynk.notify("Garage door just opened"); 67 } 68 PreviousStatus = StatMagSwitch; 69 70 //Reading DHT sensor readings 71 float h = dht.readHumidity(); //Reading Humidity 72 float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit 73 74 if (isnan(h) || isnan(t)) { 75 Serial.println("Failed to read from DHT sensor!"); 76 return; 77 } 78 79 Blynk.virtualWrite(V5, h); //V5 is for Humidity 80 Blynk.virtualWrite(V6, t); //V6 is for Temperature 81} 82 83void setup() 84{ 85 Serial.begin(9600); // See the connection status in Serial Monitor 86 pinMode(MAGPIN, INPUT_PULLUP); // Defining magnetic switch pin as input_pullup 87 dht.begin(); 88 Blynk.begin(auth, ssid, pass); 89 90 // Setup a function to be called every second 91 timer.setInterval(1000L, sendReadings); 92} 93 94void loop() 95{ 96 Blynk.run(); // Initiates Blynk 97 timer.run(); // Initiates SimpleTimer 98}
Adafruit_ESP8266_Reed_DHT22_Blynk_Notify
arduino
1/************************************************************** 2 * 3 * 4 * Magnetic switch & DHT 22 output to blynk by David K 5 * 6 * Reed 7 switch is connected to pin 12 via input pullup 8 * DHT sensor is connected to 9 pin 13 10 * 11 * Blynk App project setup: 12 * LED widged attched to V4 13 14 * Value Display widget attached to V5 15 * Value Display widget attached to 16 V6 17 * 18 * 19 * 20 **************************************************************/ 21 22#define 23 BLYNK_PRINT Serial // Comment this out to disable prints and save space 24#include 25 <SPI.h> 26#include <ESP8266WiFi.h> 27#include <BlynkSimpleEsp8266.h> 28#include 29 <DHT.h> 30 31// You should get Auth Token in the Blynk App. 32// Go to the Project 33 Settings (nut icon). 34char auth[] = "***************************"; 35 36// 37 Your WiFi credentials. 38// Set password to "" for open networks. 39char ssid[] 40 = "**********"; 41char pass[] = "*********"; 42 43int StatMagSwitch = 0; 44 // switch status setting to 0 or LOW initially, door closed 45int PreviousStatus 46 = 0; // this variable holds previous door status, 0: closed, 1: open 47 48#define 49 MAGPIN 12 // pin for magnetic switch 50#define DHTPIN 13 // pin 51 DHT sensor 52#define DHTTYPE DHT22 // DHT 22, AM2302, AM2321 53 54BlynkTimer 55 timer; // Initializing blynk 56DHT dht(DHTPIN, DHTTYPE); // Initializing 57 DHT sensor 58 59WidgetLED ledDoor(V4); //Setting led widget as virtual pin 60 4 first 61// This function sends Arduino's up time every second to Virtual Pins 62 (4,5,6). 63// In the app, Widget's reading frequency should be set to PUSH. This 64 means 65// that you define how often to send data to Blynk App 66 67void sendReadings() 68{ 69 int DoorChanged = 0; // this variable will check whether door has changed, 1 if 70 changed 71 72 StatMagSwitch = digitalRead(MAGPIN); // Reading Magnetic switch 73 status 74 75 if (StatMagSwitch != PreviousStatus) 76 { 77 DoorChanged = 78 1; 79 } 80 81 if (StatMagSwitch==1 && DoorChanged==1) 82 { 83 ledDoor.off(); 84 //turning off if magnet is far away, i.e. Switch is HIGH 85 Blynk.notify("Garage 86 door just closed"); 87 } 88 89 if (StatMagSwitch==0 && DoorChanged==1) 90 91 { 92 ledDoor.on(); //turning on if magnet is close, i.e. Switch is LOW 93 94 Blynk.notify("Garage door just opened"); 95 } 96 PreviousStatus = StatMagSwitch; 97 98 99 //Reading DHT sensor readings 100 float h = dht.readHumidity(); //Reading Humidity 101 102 float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit 103 104 105 if (isnan(h) || isnan(t)) { 106 Serial.println("Failed to read from DHT sensor!"); 107 108 return; 109 } 110 111 Blynk.virtualWrite(V5, h); //V5 is for Humidity 112 113 Blynk.virtualWrite(V6, t); //V6 is for Temperature 114} 115 116void setup() 117{ 118 119 Serial.begin(9600); // See the connection status in Serial Monitor 120 pinMode(MAGPIN, 121 INPUT_PULLUP); // Defining magnetic switch pin as input_pullup 122 dht.begin(); 123 124 Blynk.begin(auth, ssid, pass); 125 126 // Setup a function to be called every 127 second 128 timer.setInterval(1000L, sendReadings); 129} 130 131void loop() 132{ 133 134 Blynk.run(); // Initiates Blynk 135 timer.run(); // Initiates SimpleTimer 136}
Downloadable files
Schematic with annotations
This is how you can replicate the schematic
Schematic with annotations

Comments
Only logged in users can leave comments