#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include <SimpleTimer.h>
#include "SparkFunHTU21D.h"
#include <SparkFunTSL2561.h>
#include "BlynkBoard_settings.h"
HTU21D thSense;
bool scanI2C(uint8_t address);
// Wifi connections
char BlynkAuth[] = "xxxxxxxxx";
char WiFiNetwork[] = "xxxxxxxxxxx";
char WiFiPassword[] = "xxxxxxxxx";
WidgetLCD lcd(V10); //LCD widget on V10
WidgetLED led3(V3); // LED widget in Blynk App on v3
WidgetLED led4(V4); // LED widget in Blynk App on v4
WidgetTerminal terminal(V21);
SimpleTimer timer;
#define DOOR_STATE_VIRTUAL V25
#define DOOR_SWITCH_PIN 13
#define ACTIVATION 12
uint8_t lastSwitchState = 255;
long distancia;
long tiempo;
void setup()
{
Serial.begin(9600);
Blynk.begin(BlynkAuth, WiFiNetwork, WiFiPassword); //data to connect
pinMode(16, OUTPUT); /*pin 16 ultrasonic output*/
pinMode(15, INPUT); /*pin 15 receive ultrasonic signal*/
pinMode(12, OUTPUT); //open door
while (Blynk.connect() == false) {
// Wait until connected
}
}
BLYNK_READ(DOOR_STATE_VIRTUAL)
{
uint8_t switchState = digitalRead(DOOR_SWITCH_PIN); // Read the door switch pin
// Pin 16 is pulled low internally. If the switch (and door) is open,
// pin 16 is LOW. If the switch is closed (door too), pin 16 is HIGH.
// LOW = open
// HIGH = closed
if (switchState && distancia < 10)
{
Blynk.virtualWrite(DOOR_STATE_VIRTUAL, "Close"); // Update virtual variable
digitalWrite(12, HIGH);
}
else
{
Blynk.virtualWrite(DOOR_STATE_VIRTUAL, "Open");
digitalWrite(12, LOW);
}
if (lastSwitchState != switchState) // If the state has changed
{
if (switchState) // If the switch is closed (door shut)
{
terminal.println("Door closed." );
terminal.flush();
}
else
{
terminal.println("Door open.");
terminal.flush();
}
}
lastSwitchState = switchState;
}
void loop()
{
digitalWrite(16,LOW); /* sensor*/
delayMicroseconds(5);
digitalWrite(16, HIGH); /* send ultrasonic signal*/
delayMicroseconds(10);
tiempo=pulseIn(15, HIGH);
distancia= int(0.017*tiempo); /*how to calculate the distance*/
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(0, 0, "Distance"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
lcd.print(0, 1, distancia);
lcd.print(10,1, "cm");
delay(1000);
Blynk.run();
timer.run();
}
Comments