Hello,
An update, and a fairly good one for a novice like me.
I have ended up with the below code, which seems to do exactly what I want, and together with IFTTT, I can now use “OK Google” to activate the 4 different relays exactly how I want.
I went further and set up my smartwatch with an HTTP request and can now open my gate - quite happy with that.
But I do have some questions,
-
What happens in the event of network disconnection mid-way between a set of actions? How can I be sure the virtual pins are always in sync with the Blynk dashboard.
-
If an HTTP request has been carried out, is there a snippet of code somewhere that can alert me to that via the Blynk app? - but only for HTTP requests such as the ok google/IFTTT requests and not the actions through the apps dashboard?
-
Is there the possibility for any other reason that the code could stop - and potentially keeping my relay always on?
-
The OTA function - I am struggling to find a good and relevant tutorial for this on the NodeMCU. I found one which initially sets up the SSID/pass but then I thought that was doubling up with Blynk’s own Blynk.begin(auth, ssid, pass,
etc - is this a conflict and do I need to remove the SSID, pass from the Blynk line?
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "auth";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ssid";
char pass[] = "pass";
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass, IPAddress(45,55,96,146), 80);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
pinMode(4, OUTPUT); // Initialise NodeMCU D2 as an output pin
pinMode(12, OUTPUT); // Initialise NodeMCU D6 as an output pin
pinMode(13, OUTPUT); // Initialise NodeMCU D7 as an output pin
pinMode(15, OUTPUT); // Initialise NodeMCU D8 as an output pin
Blynk.syncVirtual(V2); // will cause BLYNK_WRITE(V2) to be executed
Blynk.syncVirtual(V6); // will cause BLYNK_WRITE(V6) to be executed
Blynk.syncVirtual(V7); // will cause BLYNK_WRITE(V7) to be executed
Blynk.syncVirtual(V8); // will cause BLYNK_WRITE(V8) to be executed
}
BLYNK_WRITE(V2) // Executes when the value of virtual pin 2 changes
{
if(param.asInt() == 1)
{
// execute this code if the switch widget is now ON
digitalWrite(4,HIGH); // Set NodeMCU D2 to HIGH
timer.setTimeout(1000L, []() { // Wait 1s then...
digitalWrite(4,LOW); // Set NodeMCU D2 to LOW
Blynk.virtualWrite(V2,0); // Turn the widget attached to V2 Off
}); // END of Lambda Function
}
else
{
// execute this code if the switch widget is now OFF
digitalWrite(4,LOW); // Set NodeMCU D2 to LOW
}
}
BLYNK_WRITE(V6) // Executes when the value of virtual pin 6 changes
{
if(param.asInt() == 1)
{
// execute this code if the switch widget is now ON
digitalWrite(12,HIGH); // Set NodeMCU D6 to HIGH
timer.setTimeout(1000L, []() { // Wait 1s then...
digitalWrite(12,LOW); // Set NodeMCU D6 to LOW
Blynk.virtualWrite(V6,0); // Turn the widget attached to V2 Off
}); // END of Lambda Function
}
else
{
// execute this code if the switch widget is now OFF
digitalWrite(12,LOW); // Set NodeMCU D6 to LOW
}
}
BLYNK_WRITE(V7) // Executes when the value of virtual pin 7 changes
{
if(param.asInt() == 1)
{
// execute this code if the switch widget is now ON
digitalWrite(13,HIGH); // Set NodeMCU D7 to HIGH
timer.setTimeout(1000L, []() { // Wait 1s then...
digitalWrite(13,LOW); // Set NodeMCU D7 to LOW
Blynk.virtualWrite(V7,0); // Turn the widget attached to V7 Off
}); // END of Lambda Function
}
else
{
// execute this code if the switch widget is now OFF
digitalWrite(13,LOW); // Set NodeMCU D7 to LOW
}
}
BLYNK_WRITE(V8) // Executes when the value of virtual pin 8 changes
{
if(param.asInt() == 1)
{
// execute this code if the switch widget is now ON
digitalWrite(15,HIGH); // Set NodeMCU D8 to HIGH
timer.setTimeout(1000L, []() { // Wait 1s then...
digitalWrite(15,LOW); // Set NodeMCU D8 to LOW
Blynk.virtualWrite(V8,0); // Turn the widget attached to V8 Off
}); // END of Lambda Function
}
else
{
// execute this code if the switch widget is now OFF
digitalWrite(15,LOW); // Set NodeMCU D8 to LOW
}
}
void loop()
{
Blynk.run();
timer.run();
}