Are the timers controlling digital or Virtual pns?
All the timers are controlling virtual pins.
What, if any, code is working in conjunction with the timers?
BLYNK_WRITE(V47)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
client.publish("Commands","Dawn Lights On");
Blynk.virtualWrite(V43, 1);
dawnLED.on();
}
if (pinValue == 0)
{
client.publish("Commands","Dawn Lights Off");
Blynk.virtualWrite(V43, 0);
dawnLED.off();
}
}
Have you confirmed that your ESP is loosing connection every so often… if so, how and could it be because of a timed action?
Yeah, my ESP has a MQTT message that it publishes on reconnect, every so often it loses connection… I’m guessing due to less than ideal wifi reception (this ESP happens to be in my garage, which is 50ft from my router through a concrete wall).
Supply info on phone type; What versions of App & Library; Are you using Local Server or Cloud; Show your code, etc.
Iphone 6s, IOS, app version 2.9.7 (1), Blynk library version 0.4.8.
Full code (far from optimized, I’m a novice at best):
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <PubSubClient.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
//Blynk Auth
char auth[] = "--";
char ssid1[] = "--";
char pass[] = "--";
//PubSub Stuff
const char* ssid = "--";
const char* password = "--";
const char* mqtt_server = "m12.--.com";
const int mqtt_port = --;
const char *mqtt_user = "--";
const char *mqtt_pass = "--";
const char *mqtt_client_name = "Garage32"; // Client connections cant have the same connection name
WiFiClient espClient;
PubSubClient client(espClient);
//Global Variables
const int openerPin = 27;
const int sensorPin = 26;
String previousTemp = "0";
int oldStatus = 0;
int masterAlarm = 0;
int scarlettAlarm = 0;
int garageAlarm = 0;
int gatesAlarm = 0;
int frontAlarm = 0;
int sliderAlarm = 0;
String currentTime = "0";
String currentDate = "0";
bool boot = true;
//Blynk Services
BlynkTimer timer;
WidgetLED frontLED(V35);
WidgetLED sliderLED(V36);
WidgetLED gatesLED(V37);
WidgetLED gateLED(V29);
WidgetLED garageLED(V32);
WidgetLED scarlettLED(V33);
WidgetLED masterLED(V34);
WidgetLED openerLED(V10);
WidgetLED timerLED(V38);
WidgetLED bedtimeLED(V30);
WidgetLED dawnLED(V48);
WidgetLED daylightLED(V49);
WidgetLED buffetLED(V50);
WidgetTerminal terminal(V0);
WidgetRTC rtc;
//Functions
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect()
{
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass)) {
Serial.println("connected");
// Once connected, publish an announcement...
if(boot == false)
{
client.publish("Device_Status", "Garage32 Reconnected");
}
if(boot == true)
{
client.publish("Device_Status", "Garage32 Rebooted");
boot = false;
}
// ... and resubscribe
client.subscribe("Current_Temperature");
client.subscribe("Pool_Mode");
client.subscribe("Door_Status");
client.subscribe("Device_Status");
client.subscribe("Aquarium_R");
client.subscribe("Aquarium_L");
Blynk.run();
rtc.begin();
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void clockUpdate()
{
if(minute() < 10 && second() < 10)
{
currentTime = String(hour()) + ":0" + minute() + ":0" + second();
}
else if(minute() < 10)
{
currentTime = String(hour()) + ":0" + minute() + ":" + second();
}
else if(second() < 10)
{
currentTime = String(hour()) + ":" + minute() + ":0" + second();
}
else
{
currentTime = String(hour()) + ":" + minute() + ":" + second();
}
currentDate = String(month()) + "/" + day();
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
String newTopic = topic;
Serial.print(topic);
Serial.print("] ");
payload[length] = '\0';
String newPayload = String((char *)payload);
Serial.println(newPayload);
Serial.println();
if (newTopic == "Current_Temperature")
{
Blynk.virtualWrite(V24, newPayload);
}
if (newTopic == "Aquarium_R")
{
Blynk.virtualWrite(V39, newPayload);
}
if (newTopic == "Aquarium_L")
{
Blynk.virtualWrite(V40, newPayload);
}
if (newTopic == "Pool_Mode")
{
if (newPayload == "SPA")
{
Blynk.virtualWrite(V22, 1);
Blynk.setProperty(V24, "label", "Current Mode: SPA");
}
if (newPayload == "POOL")
{
Blynk.virtualWrite(V22, 0);
Blynk.setProperty(V24, "label", "Current Mode: POOL");
}
}
if (newTopic == "Device_Status")
{
if (newPayload == "Garage 32 OK!")
{
Blynk.virtualWrite(51, currentTime);
}
if (newPayload == "Pool MCU OK!")
{
Blynk.virtualWrite(52, currentTime);
}
if (newPayload == "PatioMCU OK!")
{
Blynk.virtualWrite(53, currentTime);
}
if (newPayload == "Aquarium MCU OK!")
{
Blynk.virtualWrite(54, currentTime);
}
}
if (newTopic == "Door_Status")
{
terminal.print(currentDate);
terminal.print(" ");
terminal.print(currentTime);
terminal.print(" ");
terminal.println(newPayload);
terminal.flush();
if(newPayload == "Garage Open")
{
Blynk.virtualWrite(12, "Garage Door Open");
if(garageAlarm == 1)
{
Blynk.notify("The Garage Door Has Opened");
client.publish("Alarms","Garage Alarm");
}
}
if(newPayload == "Garage Closed")
{
Blynk.virtualWrite(12, "Garage Door Closed");
if(garageAlarm == 1)
{
Blynk.notify("The Garage Door Has Closed");
client.publish("Alarms","Garage Alarm");
}
}
if(newPayload == "Doorbell")
{
Blynk.notify("Doorbell!");
client.publish("Alarms","Doorbell");
}
if(newPayload == "Scarlett Open")
{
Blynk.virtualWrite(13, "Scarlett Door Open");
if(scarlettAlarm == 1)
{
Blynk.notify("Scarlett's Door Has Opened!");
client.publish("Alarms","Scarlett Alarm");
}
}
if(newPayload == "Scarlett Closed")
{
Blynk.virtualWrite(13, "Scarlett Door Closed");
if(scarlettAlarm == 1)
{
Blynk.notify("Scarlett's Door Has Closed!");
}
}
if(newPayload == "Master Open")
{
Blynk.virtualWrite(14, "Master Door Open");
if(masterAlarm == 1)
{
Blynk.notify("Master Door Has Opened!");
client.publish("Alarms","Master Alarm");
}
}
if(newPayload == "Master Closed")
{
Blynk.virtualWrite(14, "Master Door Closed");
if(masterAlarm == 1)
{
Blynk.notify("Master Door Has Closed!");
}
}
if(newPayload == "Exercise Open")
{
Blynk.virtualWrite(26, "Exercise Door Open");
}
if(newPayload == "Exercise Closed")
{
Blynk.virtualWrite(26, "Exercise Door Closed");
}
if(newPayload == "Zeier Gate Open")
{
Blynk.virtualWrite(15, "Zeier Gate Open");
Blynk.notify("Zeier Gate Open!");
if(gatesAlarm == 1)
{
client.publish("Alarms","Gate Alarm");
}
}
if(newPayload == "Zeier Gate Closed")
{
Blynk.virtualWrite(15, "Zeier Gate Closed");
Blynk.notify("Zeier Gate Has Closed!");
}
if(newPayload == "Equipment Gate Open")
{
Blynk.virtualWrite(23, "Equipment Gate Open");
Blynk.notify("Equipment Gate Open!");
if(gatesAlarm == 1)
{
client.publish("Alarms","Gate Alarm");
}
}
if(newPayload == "Equipment Gate Closed")
{
Blynk.virtualWrite(23, "Equipment Gate Closed");
Blynk.notify("Equipment Gate Has Closed!");
}
if(newPayload == "Front Door Open")
{
Blynk.virtualWrite(16, "Front Door Open");
if(frontAlarm == 1)
{
Blynk.notify("Front Door Open!");
client.publish("Alarms","Front Door Alarm");
}
}
if(newPayload == "Front Door Closed")
{
Blynk.virtualWrite(16, "Front Door Closed");
if(frontAlarm == 1)
{
Blynk.notify("Front Door Closed!");
}
}
if(newPayload == "Sliding Door Open")
{
Blynk.virtualWrite(17, "Sliding Door Open");
if(sliderAlarm == 1)
{
Blynk.notify("Sliding Door Open!");
client.publish("Alarms","Slider Alarm");
}
}
if(newPayload == "Sliding Door Closed")
{
Blynk.virtualWrite(17, "Sliding Door Closed");
if(sliderAlarm == 1)
{
Blynk.notify("Sliding Door Has Closed!");
}
}
}
}
//Timer Functions
void getGarageDoorState()
{
int newStatus = digitalRead(sensorPin);
if(newStatus != oldStatus && newStatus == 1)
{
client.publish("Door_Status","Garage Open");
oldStatus = newStatus;
}
if(newStatus != oldStatus && newStatus == 0)
{
client.publish("Door_Status","Garage Closed");
oldStatus = newStatus;
}
}
void checkIn()
{
client.publish("Device_Status","Garage 32 OK!");
}
void alarmStatus()
{
if(masterAlarm == 0)
{
masterLED.off();
Blynk.virtualWrite(V4, 0);
}
if(masterAlarm == 1)
{
masterLED.on();
Blynk.virtualWrite(V4, 1);
}
if(garageAlarm == 0)
{
garageLED.off();
Blynk.virtualWrite(V2, 0);
}
if(garageAlarm == 1)
{
garageLED.on();
Blynk.virtualWrite(V2, 1);
}
if(gatesAlarm == 0)
{
gateLED.off();
gatesLED.off();
Blynk.virtualWrite(V9, 0);
}
if(gatesAlarm == 1)
{
gateLED.on();
gatesLED.on();
Blynk.virtualWrite(V9, 1);
}
if(frontAlarm == 0)
{
frontLED.off();
Blynk.virtualWrite(V8, 0);
}
if(frontAlarm == 1)
{
frontLED.on();
Blynk.virtualWrite(V8, 1);
}
if(sliderAlarm == 0)
{
sliderLED.off();
Blynk.virtualWrite(V10, 0);
}
if(sliderAlarm == 1)
{
sliderLED.on();
Blynk.virtualWrite(V10, 1);
}
if(scarlettAlarm == 0)
{
scarlettLED.off();
Blynk.virtualWrite(V3, 0);
}
if(scarlettAlarm == 1)
{
scarlettLED.on();
Blynk.virtualWrite(V3, 1);
}
}
//Blynk Writes
BLYNK_WRITE(V20)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
if (! client.publish("Commands","Pool_Light"))
{
Blynk.virtualWrite(V25, "Command Failed");
delay(1000);
Blynk.virtualWrite(V25, " ");
}
else
{
Blynk.virtualWrite(V25, "Pool Light Sent");
delay(1000);
Blynk.virtualWrite(V25, " ");
}
}
}
BLYNK_WRITE(V21)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
if (! client.publish("Commands","Spa_Light"))
{
Blynk.virtualWrite(V25, "Command Failed");
delay(1000);
Blynk.virtualWrite(V25, " ");
}
else
{
Blynk.virtualWrite(V25, "Spa Light Sent");
delay(1000);
Blynk.virtualWrite(V25, " ");
}
}
}
BLYNK_WRITE(V22)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
if (! client.publish("Commands","SPA"))
{
Blynk.virtualWrite(V25, "Command Failed");
delay(1000);
Blynk.virtualWrite(V25, " ");
}
else
{
Blynk.virtualWrite(V25, "Mode: SPA Sent");
delay(1000);
Blynk.virtualWrite(V25, " ");
}
}
if (pinValue == 0)
{
if (! client.publish("Commands","POOL"))
{
Blynk.virtualWrite(V25, "Command Failed");
delay(1000);
Blynk.virtualWrite(V25, " ");
}
else
{
Blynk.virtualWrite(V25, "Mode: POOL Sent");
delay(1000);
Blynk.virtualWrite(V25, " ");
}
}
}
BLYNK_WRITE(V2)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1 && garageAlarm == 0)
{
garageAlarm = 1;
garageLED.on();
}
if (pinValue == 0 && garageAlarm == 1)
{
garageAlarm = 0;
garageLED.off();
}
}
BLYNK_WRITE(V8)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1 && frontAlarm == 0)
{
frontAlarm = 1;
frontLED.on();
}
if (pinValue == 0 && frontAlarm == 1)
{
frontAlarm = 0;
frontLED.off();
}
}
BLYNK_WRITE(V9)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1 && gatesAlarm == 0)
{
gatesAlarm = 1;
gatesLED.on();
gateLED.on();
}
if (pinValue == 0 && gatesAlarm == 1)
{
gatesAlarm = 0;
gatesLED.off();
gateLED.off();
}
}
BLYNK_WRITE(V3)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1 && scarlettAlarm == 0)
{
scarlettAlarm = 1;
scarlettLED.on();
}
if (pinValue == 0 && scarlettAlarm == 1)
{
scarlettAlarm = 0;
scarlettLED.off();
}
}
BLYNK_WRITE(V10)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1 && sliderAlarm == 0)
{
sliderAlarm = 1;
sliderLED.on();
}
if (pinValue == 0 && sliderAlarm == 1)
{
sliderAlarm = 0;
sliderLED.off();
}
}
BLYNK_WRITE(V4)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1 && masterAlarm == 0)
{
masterAlarm = 1;
masterLED.on();
}
if (pinValue == 0 && masterAlarm == 1)
{
masterAlarm = 0;
masterLED.off();
}
}
BLYNK_WRITE(V7)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
digitalWrite(openerPin, HIGH);
openerLED.on();
delay(1500);
digitalWrite(openerPin, LOW);
openerLED.off();
}
}
BLYNK_WRITE(V5)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
client.publish("Commands","Patio Light On");
}
if (pinValue == 0)
{
client.publish("Commands","Patio Light Off");
}
}
BLYNK_WRITE(V6)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
client.publish("Commands","Patio Fan On");
}
if (pinValue == 0)
{
client.publish("Commands","Patio Fan Off");
}
}
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
client.publish("Alarms","Clear Alarms");
}
}
BLYNK_WRITE(V18)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
masterAlarm = 0;
scarlettAlarm = 0;
garageAlarm = 0;
gatesAlarm = 0;
frontAlarm = 0;
sliderAlarm = 0;
}
}
BLYNK_WRITE(V28)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
gatesAlarm = 1;
frontAlarm = 1;
sliderAlarm = 1;
timerLED.on();
}
if (pinValue == 0)
{
gatesAlarm = 0;
frontAlarm = 0;
sliderAlarm = 0;
timerLED.off();
}
}
BLYNK_WRITE(V19)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
scarlettAlarm = 1;
bedtimeLED.on();
}
if (pinValue == 0)
{
scarlettAlarm = 0;
bedtimeLED.off();
}
}
BLYNK_WRITE(V42)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
client.publish("Commands","Daylight LED On");
}
if (pinValue == 0)
{
client.publish("Commands","Daylight LED Off");
}
}
BLYNK_WRITE(V43)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
client.publish("Commands","Dawn LED On");
}
if (pinValue == 0)
{
client.publish("Commands","Dawn LED Off");
}
}
BLYNK_WRITE(V44)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
client.publish("Commands","Filter On");
}
if (pinValue == 0)
{
client.publish("Commands","Filter Off");
}
}
BLYNK_WRITE(V47)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
client.publish("Commands","Dawn LED On");
Blynk.virtualWrite(V43, 1);
dawnLED.on();
}
if (pinValue == 0)
{
client.publish("Commands","Dawn LED Off");
Blynk.virtualWrite(V43, 0);
dawnLED.off();
}
}
BLYNK_WRITE(V46)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
client.publish("Commands","Daylight LED On");
Blynk.virtualWrite(V42, 1);
daylightLED.on();
}
if (pinValue == 0)
{
client.publish("Commands","Daylight LED Off");
Blynk.virtualWrite(V42, 0);
daylightLED.off();
}
}
BLYNK_WRITE(V31)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
client.publish("Commands","Buffet Light On");
}
if (pinValue == 0)
{
client.publish("Commands","Buffet Light Off");
}
}
BLYNK_WRITE(V41)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
client.publish("Commands","Buffet Light On");
Blynk.virtualWrite(V31, 1);
buffetLED.on();
}
if (pinValue == 0)
{
client.publish("Commands","Buffet Light Off");
Blynk.virtualWrite(V31, 0);
buffetLED.off();
}
}
BLYNK_WRITE(V11)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1)
{
Blynk.syncAll();
terminal.println("Updated");
terminal.flush();
}
}
BLYNK_APP_CONNECTED()
{
Blynk.syncAll();
Blynk.syncVirtual(V0);
client.publish("Device_Status", "App Connected");
}
void setup()
{
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 16552);
client.setCallback(callback);
ArduinoOTA.setHostname("garage32");
ArduinoOTA.begin();
Blynk.config(auth);
pinMode(sensorPin, INPUT);
pinMode(openerPin, OUTPUT);
setSyncInterval(60);
// timer setup in millis
timer.setInterval(1000, getGarageDoorState);
timer.setInterval(1000, alarmStatus);
timer.setInterval(60000, checkIn);
timer.setInterval(5000, clockUpdate);
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
ArduinoOTA.handle();
Blynk.run();
timer.run();
ArduinoOTA.handle();
}