Here’s the full code
// ** ---------------- Constants --------------- **
#define IRON_LED 13
#define IRON_POWER 5
#define VIBRATIONS 6
#define IS_FLAT 7
// ** ---------------- Blynk Constants and libraries -------------- **
#define BLYNK_TEMPLATE_ID "TMPL6o_AtNPvi"
#define BLYNK_TEMPLATE_NAME "Electric Iron Notifications"
#define BLYNK_AUTH_TOKEN "my_auth"
#define BLYNK_PRINT Serial
#define ESP8266_BAUD 9600
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(8, 9); // RX, TX
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
// ** ----------------- Blynk token and wifi credentials ------------- **
char auth[] = "my_auth";
char ssid[] = "my_ssid";
char pass[] = "my_pass";
// Timer constants
const unsigned long TIMER_DURATION_FLAT = 3000; // 3000 milliseconds = 3 seconds
const unsigned long TIMER_DURATION_STAND = 6000; // 60000 milliseconds = 60 seconds
// ** ---------------- Variables --------------- **
int IRON_POW = 0;
int VIBR = 0;
int FLAT = 0;
int tracker = 0;
unsigned long previousMillis = 0;
bool isFlat = false;
unsigned long flatStartTime = 0;
// Serial monitor output flag
bool stateChanged = false;
// Serial monitor constants for previous states
int prev_1 = 0;
int prev_2 = 0;
int prev_3 = 0;
// ** ---------------- END --------------- **
int IronState()
{
//pinMode(IRON_LED, OUTPUT);
pinMode(IRON_POWER, INPUT_PULLUP);
pinMode(VIBRATIONS, INPUT_PULLUP);
pinMode(IS_FLAT, INPUT_PULLUP);
// Read state of rocker switch
IRON_POW = digitalRead(IRON_POWER);
VIBR = digitalRead(VIBRATIONS);
FLAT = digitalRead(IS_FLAT);
if (IRON_POW == HIGH) {
tracker = 0;
}
// * ----------------- IRON is ON, CONDITIONS -------------- *
else{
// * --------------------- USER USING IT ------------------ *
if (IRON_POW == LOW && VIBR == LOW && FLAT == LOW){
tracker = 1;
}
// * -----------------------IRON is ON, NOT Using It, It's Flat ------------ *
else if ((IRON_POW == LOW && VIBR == HIGH && FLAT == LOW) && (prev_1 == LOW && prev_2 == LOW && prev_3 == LOW)){
tracker = 2;
}
// * ----------------- IRON is ON, NOT Using It, It's Standing ------------- *
else if ((IRON_POW == LOW && FLAT == HIGH) && (prev_1 == LOW && prev_2 == LOW && prev_3 == LOW)){
tracker = 3;
}
}
return tracker;
}
// ** ---------- New Function for TimerEvent (Notifications) ------------ **
void TimerEvent()
{
//Every second I will read the LED output state, if it's
tracker = IronState();
if (tracker == 0){
Blynk.logEvent("electric_iron", "Iron is OFF");
Serial.println("Iron is OFF.");
Blynk.virtualWrite(V0, 0);
digitalWrite(IRON_LED, LOW);
}
else if (tracker == 1){
Blynk.logEvent("electric_iron", "Someone's using the Iron...");
Serial.println("Iron is ON.");
Blynk.virtualWrite(V0, 1);
digitalWrite(IRON_LED, HIGH);
}
else if (tracker == 2){
Blynk.logEvent("electric_iron", "Iron flat and ON! Turning off in 3 seconds...");
Serial.println("Iron on, 3 seconds off");
Blynk.virtualWrite(V0, 1);
digitalWrite(IRON_LED, HIGH);
timer.setTimeout(3000L, []()
{
// When timer completes, turn off everything
Blynk.logEvent("electric_iron", "Iron turned off after 3 seconds...");
Blynk.virtualWrite(V0, 0);
digitalWrite(IRON_LED, LOW);
Serial.println("Iron turned off after 3 seconds...");
});
}
else if (tracker == 3){
Blynk.logEvent("electric_iron", "Iron idle and ON! Turning off in 6 seconds...");
Serial.println("Iron on, 6 seconds off");
Blynk.virtualWrite(V0, 1);
digitalWrite(IRON_LED, HIGH);
timer.setTimeout(6000L, []()
{
// When timer completes, turn off everything
Blynk.logEvent("electric_iron", "Iron turned off after 6 seconds...");
Blynk.virtualWrite(V0, 0);
digitalWrite(IRON_LED, LOW);
Serial.println("Iron turned off after 6 seconds...");
});
}
}
void setup() {
// put your setup code here, to run once:
pinMode(IRON_LED, OUTPUT);
pinMode(IRON_POWER, INPUT_PULLUP);
pinMode(VIBRATIONS, INPUT_PULLUP);
pinMode(IS_FLAT, INPUT_PULLUP);
Serial.begin(9600);
// Setting ESP8266 Baud Rate
EspSerial.begin(ESP8266_BAUD);
//delay(10);
Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass);
// Calling every second
timer.setInterval(1000L, TimerEvent);
}
void loop() {
// Run Blynk
Blynk.run();
timer.run();
} // void loop end