Created a Blynk code to turn on or off a relay based on a value set using Blynk server.
if V0 = 1, the code will accept a 4 digit password to turn on the relay.
if V0 = 0 (or anything else), the code will accept any key presses in the key board to turn on the relay.
I am using ESP8266 to run this project.
The problem is if the internet goes down, nothing will work.
I just want any key pressed on the keypad to turn on the relay if there is no internet / server connectivity.
I tried many things, but most of the time it wont work or the board wont be able to connect to the Blynk server. Chat GPT couldn’t help even after hours of trying.
Following is a working code which accepts password 1234 followed by # if V0 is set to 1.
else if V0 = 0, any key is accepted as password. I need this function to be also able to work even if there is no internet. I hope someone will help.
#define BLYNK_TEMPLATE_ID "ccccccxxxxxxxcccccco"
#define BLYNK_TEMPLATE_NAME "NodeMCU"
#define BLYNK_DEVICE_NAME "NodeMCU"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
#include "BlynkEdgent.h"
#include <Keypad.h>
const byte numRows = 4;
const byte numCols = 4;
char keyMap[numRows][numCols] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[numRows] = {D1, D2, D3, D4};
byte colPins[numCols] = {D5, D6, D7, D8};
Keypad keypad = Keypad(makeKeymap(keyMap), rowPins, colPins, numRows, numCols);
const String password = "1234";
bool passwordCorrect = false;
const int relayPin = 16; // D0
bool relayActivated = false;
unsigned long relayActivationTime = 0;
const unsigned long relayDuration = 5000; // 5 seconds
bool acceptPassword = false; // Flag to accept or reject password
BLYNK_WRITE(V0)
{
int value = param.asInt();
Serial.print("V0 write: ");
Serial.println(value);
acceptPassword = (value == 1); // Update the flag based on the value written to V0
passwordCorrect = false; // Reset the password check after each attempt
}
void checkPassword()
{
char key = keypad.getKey();
if (key) {
Serial.print("Key pressed: ");
Serial.println(key);
if (acceptPassword) {
if (key == '#') {
if (passwordCorrect && !relayActivated) {
digitalWrite(relayPin, LOW); // Turn on the relay (set pin 16 - D0 - LOW) if password matches
relayActivationTime = millis(); // Record the relay activation time
relayActivated = true;
Serial.println("Relay turned ON");
} else {
Serial.println("Invalid password or relay already activated");
}
passwordCorrect = false; // Reset the password check after each attempt
} else {
if (key == password.charAt(0)) {
passwordCorrect = false;
} else if (passwordCorrect) {
passwordCorrect = false;
} else {
passwordCorrect = true;
}
}
} else {
digitalWrite(relayPin, LOW); // Turn on the relay (set pin 16 - D0 - LOW) for any keypress when acceptPassword is false
relayActivationTime = millis(); // Record the relay activation time
relayActivated = true;
Serial.println("Relay turned ON (No password required)");
}
}
// Check if it's time to turn off the relay
if (relayActivated && millis() - relayActivationTime >= relayDuration) {
digitalWrite(relayPin, HIGH); // Turn off the relay (set pin 16 - D0 - HIGH)
relayActivated = false;
Serial.println("Relay turned OFF");
}
}
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V0);
}
void setup()
{
Serial.begin(115200);
delay(100);
pinMode(relayPin, OUTPUT); // Initialize digital pin 16 (D0) as an output pin for the relay
digitalWrite(relayPin, HIGH); // Turn off the relay initially
BlynkEdgent.begin();
}
void loop()
{
BlynkEdgent.run();
checkPassword(); // Check keypad input
}