Good evening,
I’ve watched Part 2, 3, 4 and I see there are many interesting things to learn. The man who lives learning!
Regarding the question:
One thing we will need to know is whether the relay you’re using is active HIGH or LOW, and which GPIO pin you plan to connect it to
The relay used will be LOW, when it will receive the command
for opening the door will go into HIGH.
and GIPO 12 will be for relay control.
The old code used would be:
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h> // servo library
#ifdef DEBUG_ENABLED
#define BLYNK_PRINT Serial
#define BB_DEBUG(msg) {\
Serial.print("[" + String(millis()) + "] "); \
Serial.println(msg); }
#else
#define BB_DEBUG(msg)
#endif
////////////////////
// Blynk Settings //
////////////////////
char BlynkAuth[] = "a78c0645e3274129a832ffa2da999510"; // cloud
char WiFiNetwork[] = "Michele";
char WiFiPassword[] = "jenniferlopez";
///////////////////////
// Hardware Settings //
///////////////////////
#define LED_PIN 5
#define SERVO_PIN 12
#define DOOR_STATE_VIRTUAL V25
#define SERIAL_VIRTUAL V21
#define LOCK_BUTTON_PIN 0
#define BUTTON_VIRTUAL V26
Servo servo1;
int pos;
void unlock() {
if (pos == 0) {
pos = 15;
servo1.write(pos);
delay(20);
}
}
void lock() {
if (pos == 15) {
pos = 0;
servo1.write(pos);
delay(20);
}
}
void toggle() {
if (pos == 0) {
pos = 15;
} else {
pos = 0;
}
servo1.write(pos);
delay(20);
}
/* 12 12 12 12 12 12 12 12 12 12 12 12 12
12 Experiment 12: Terminal 12
12 Widget(s): 12
12 - Terminal: V21, On, On 12
12 12 12 12 12 12 12 12 12 12 12 12 12 */
String emailAddress = "";
String boardName = "Blynk Lock";
WidgetTerminal terminal(SERIAL_VIRTUAL);
BLYNK_WRITE(SERIAL_VIRTUAL)
{
String incoming = param.asStr();
Serial.println(incoming);
if (incoming.charAt(0) == '!')
{
String emailAdd = incoming.substring(1, incoming.length());
for (int i=0; i<emailAdd.length(); i++)
{
if (emailAdd.charAt(i) == ' ')
emailAdd.remove(i, 1);
}
terminal.println("Your email is:" + emailAdd + ".");
emailAddress = emailAdd;
terminal.flush();
}
if (incoming.charAt(0) == '$')
{
String newName = incoming.substring(1, incoming.length());
boardName = newName;
terminal.println("Board name set to: " + boardName + ".");
terminal.flush();
}
}
/* 14 14 14 14 14 14 14 14 14 14 14 14 14
14 Experiment 14: Push 14
14 Widget(s): 14
14 - Push: Off, On (iPhone) 14
14 - Value: DoorState, V25, 1sec 14
14 - Button: PushEnable, V26, Switch 14
14 14 14 14 14 14 14 14 14 14 14 14 14 */
#define DOOR_SWITCH_PIN 16
#define NOTIFICATION_LIMIT 60000
unsigned long lastDoorSwitchNotification = 0;
uint8_t lastSwitchState = 255;
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) {
Blynk.virtualWrite(DOOR_STATE_VIRTUAL, "Close"); // Update virtual variable
digitalWrite(LED_PIN, 0);
}
else {
Blynk.virtualWrite(DOOR_STATE_VIRTUAL, "Open");
digitalWrite(LED_PIN, 255);
}
if (lastSwitchState != switchState) // If the state has changed
{
if (switchState) // If the switch is closed (door shut)
{
BB_DEBUG("Notified closed.");
if (lastDoorSwitchNotification && (lastDoorSwitchNotification + NOTIFICATION_LIMIT > millis()))
{
int timeLeft = (lastDoorSwitchNotification + NOTIFICATION_LIMIT - millis()) / 1000;
BB_DEBUG("Can't notify for " + String(timeLeft) + "s");
terminal.println("Door closed. Can't notify for " + String(timeLeft) + "s");
terminal.flush();
}
else
{
Blynk.notify("Door closed\r\nFrom: " + boardName + "\r\n[" + String(millis()) + "]");
terminal.println("Door closed!");
terminal.flush();
lastDoorSwitchNotification = millis();
}
}
else
{
BB_DEBUG("Notified opened.");
// Send the notification
if (lastDoorSwitchNotification && (lastDoorSwitchNotification + NOTIFICATION_LIMIT > millis()))
{
int timeLeft = (lastDoorSwitchNotification + NOTIFICATION_LIMIT - millis()) / 1000;
BB_DEBUG("Can't notify for " + String(timeLeft) + "s");
terminal.println("Door open. Can't notify for " + String(timeLeft) + "s");
terminal.flush();
}
else
{
Blynk.notify("Door open\r\nFrom: " + boardName + "\r\n[" + String(millis()) + "]");
terminal.println("Door opened!");
terminal.flush();
lastDoorSwitchNotification = millis();
}
}
lastSwitchState = switchState;
}
}
BLYNK_WRITE(V26)
{
int pinValue = param.asInt(); // assigning incoming value from pin V26 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("V26 Button value is: ");
Serial.println(pinValue);
toggle();
}
void setup()
{
// Initialize hardware
Serial.begin(9600); // Serial
pinMode(LED_PIN, OUTPUT); // LED output
// Set up the pin 16 door switch input:
pinMode(DOOR_SWITCH_PIN, INPUT_PULLDOWN_16);
lastSwitchState = digitalRead(DOOR_SWITCH_PIN);
// Initialize Blynk
Blynk.begin(BlynkAuth, WiFiNetwork, WiFiPassword);
// Blynk.begin(BlynkAuth, WiFiNetwork, WiFiPassword, IPAddress(192,168,1,4));
servo1.attach(SERVO_PIN);
pos = 15;
servo1.write(pos);
}
void loop()
{
// Execute Blynk.run() as often as possible during the loop
Blynk.run();
}
Buttons used in the application:
I asked but did not answer. Is it normal that the application running in the background does not receive sound notifications? These only appear when I enter the application!
Thank you.