Hi all,
Could I get some advice on why this Blynk project keeps going offline. I don’t think I am spamming the server.
The problem is, when it goes offline the interrupts are not captured. It leads me to wonder whether I should code to read high or low instead, in that case I think I need a new function which can be called by a timer - correct?
Some info on the project:
Wemos D1 mii
iOS Blynk app with plenty of credits
Two inputs from an alarm panel (alarm set state, and sounder on/off state)
One output to the alarm panel (close or open a link via relay, to set/unset alarm)
(see notes in codes for full descriptions)
Any help appreciated
//v001 - Wemos D1 mini - garage alarm
//
//Blynk app "Garage alarm"
//Blynk documentation and help https://docs.blynk.cc/
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = ""; //Enter the Auth code which was send by Blink
char ssid[] = ""; //Enter your WIFI Name
char pass[] = ""; //Enter your WIFI Password
const byte AlarmState = D5; // INPUT - is the alarm set, or unset/alarmed? (Set = LOW/0V and Unset/alarmed = HIGH/3.3V)
const byte SounderActive = D7; // INPUT - is the sounder on or off? (Sounder on = LOW/0.33V and Sounder off = HIGH/3.3V)
const byte SetAlarm = D6; // OUTPUT - set or unset the alarm (HIGH = unset the alarm, LOW = set the alarm)
//Setup Blynk virtual pin states
static unsigned long last_interrupt_time = 0;
bool LastVirtualButtonState = 0; // "0", "FALSE", "LOW' means exactly the same
void setup()
{
pinMode(AlarmState, INPUT); // is the alarm set, or unselt/alarmed? (Set = LOW/0V and Unset/alarmed = HIGH/3.3V)
pinMode(SounderActive, INPUT); //is the sounder on or off? (Sounder on = LOW/0.33V and Sounder off = HIGH/3.3V)
pinMode(SetAlarm, OUTPUT); // set or unset the alarm (HIGH = unset the alarm, LOW = set the alarm)
digitalWrite(SetAlarm, LOW); //ensures the alarm defaults to SET condition after power loss of Wemos
Blynk.begin(auth, ssid, pass); //connects to WiFi network, then connects to Blynk server
//read the current state of the alarm
int lastAlarmState = digitalRead(AlarmState); //reads state of the alarm i.e. set or unset
int lastSounderActive = digitalRead(SounderActive); // reads state of sounder i.e on or off
//write the current state to the Blynk app
Blynk.virtualWrite(V5, (!lastAlarmState * 255)); // writes set or unset state of alarm to Blynk virtual LED pin V5
Blynk.virtualWrite(V6, (!lastSounderActive * 255)); // writes sounder on or off state to Blynk virtual LED pin V6
//set up interupts to capture changes of state
attachInterrupt(digitalPinToInterrupt(AlarmState), updateAlarmState, CHANGE); // upon change of state - call the function updateAlarmState
attachInterrupt(digitalPinToInterrupt(SounderActive), updateSounderActive, CHANGE); // upon change of state - call the function updateSounderActive
}
void loop()
{
Blynk.run(); // This function should be called frequently to process incoming commands and perform housekeeping of Blynk connection.
}
void updateAlarmState() // a function called by a CHANGE interrupt, which write the input state to a Blynk virtual LED
{
Blynk.virtualWrite(V5, !digitalRead(AlarmState)*255); //Using ! to invert the write, to match the alarm panel logic
}
void updateSounderActive() // a function called by a CHANGE interrupt, which write the input state to a Blynk virtual LED
{
Blynk.virtualWrite(V6, !digitalRead(SounderActive)*255); //Using ! to invert the write, to match the alarm panel logic
}
// BLYNK_WRITE is a function called every time device gets an update of a Virtual Pin value from the server (or app): e.g. Blynk app virtual button is pressed
// Contains virtual button "latching" code
BLYNK_WRITE(V3)
{
int VirtualButtonState = param.asInt(); // assigning incoming value from pin V3 to a variable
if ((VirtualButtonState) && (!LastVirtualButtonState)) // "VirtualButtonState" is the Blynk virtual button current state |||||| this means same as "if ((VirtualButtonState == 1) && (LastVirtualButtonState == 0))"
//if V3 virtual button is still being pressed, the LastVirtualState is set to 1, and !LastVirtualState will therefore be 0. Hence 1 && 0 condition == 0 and therefore function will not be called.
{
digitalWrite(SetAlarm, !digitalRead(SetAlarm)); //writes the inverse value to the pin (booleon NOT operator )
Blynk.virtualWrite(V0, digitalRead(SetAlarm)*255); //writes the new state to the Blynk app LED at V0
}
LastVirtualButtonState = VirtualButtonState; // sets LastVirtualButtonState to the same as pinValue, so if pinValue (V5 button) is high, LastVirtualPinState gets set to high
}