So like many I am new to using blynk and the NodeMCU. I have been trying to use this like some as a garage door opener. I have looked at many tutorials and for the life of my I can not get it to work.
Using Blynk by itself works fine. I can press the buttons i made and it will turn on my relay. It is getting the relay to me momentary that is the problem for me. I see references to how the webhook are set up but no clear answer as i see it and that may just be my misunderstanding of it. Here is the code that i have found all over the place that I’m trying to use with no joy. I know I have to be missing something. Also included a pic of the webhook.
Using a NodeMCU V2.4.1
Relay connected to labeled pin D3
Any help would be greatly apritiated
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer timer;
///////////////////////////////////////////////////////////
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "8XXXXXXXXXX2";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXXXXXXXXX";
char pass[] = "XXXXXXXXX";
/////////////////////////////////////////////////////////////
int outputPin = D3;
int inputPin = V1;
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
//pasted code starts here
BLYNK_WRITE(V1) {
if (param.asInt()==1){ // act only on the HIGH and not the LOW of the momentary
digitalWrite(D3, !digitalRead(D3)); // invert pin state just once
timer.setTimeout(200L, [](){
delay(200);
digitalWrite(D3, !digitalRead(D3)); // then invert it back after 1000ms
Blynk.virtualWrite(V1, LOW);
});
}}
//pasted code ends here
void loop()
{
Blynk.run();
timer.run(); // missing this very important line to make the timer work
}
This is a bit confusing… you are setting a timeout for 200ms (nice use of the lambda BTW ) but then you delay the whole thing for 200ms (a delay stops all processes… probably even the timers) and on top of that your comments state wanting to wait one second, not 200ms… which I think is waaayyy to fast for a mechanical relay to properly function anyhow.
Change your timeout to 1000 and get rid of the delay!!
PS, while they can sometimes work, it is best to use the universal (GPIO) Arduino pin designation instead of the silkscreened Dx pins
BLYNK_WRITE(V1) {
if (param.asInt()==1){ // act only on the HIGH and not the LOW of the momentary
digitalWrite(D0, !digitalRead(D0)); // invert pin state just once
timer.setTimeout(1000L, [](){
digitalWrite(D0, !digitalRead(D0)); // then invert it back after 1000ms
Blynk.virtualWrite(V1, LOW);
});
}}
Neither GPIO0 (D3) or GPIO16 (D0) are good pins to use. GPIO4 (D2) or or 5 (D1) should be your first choices. Do t forget that when you change your code you also need to change the wiring!
In your void setup you’re not declaring the digital pin you’re using as an output pin using a Pinmode statement, and you’re not setting a default status for it. It depends if the relay you’re using is active LOW or active HIGH as to what you need to set the default status to so that the relay isn’t activated during normal (standby) operation.
Your IFTTT PUT statement is using a digital rather than virtually pin, and in your screenshot you’re writing a “0” to it. If pulling the pin LOW activates the relay then this is fine, but if the relay needs to be HIGH to operate then nothing will happen.
By using IFTTT on your Digital rather than Virtual pin, the code in BLYNK_WRITE(V1) won’t get called by the IFTTT command, it will simply pull whichever digital pin you’re using LOW and leave it that way.
I’d be inclined to switch to using V1 in your IFTTT call.
Ha! That’s what happens when I try writing stuff on my mobile phone, before my first coffee of the day!
I’ve corrected my pin numbering so that it’s now (hopefully) correct
First I want to say thanks to each of you that helped me through this. It is now working. Hear is the changed code as well a screen of the changes i made on the webhook in case anyone else runs into this and is looking for the answer.
Again, thanks so much!!
int outputPin = 4;
int inputPin = V1;
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
pinMode( 4, OUTPUT);
digitalWrite(4, HIGH);
}
BLYNK_WRITE(V1) {
if (param.asInt()==1){ // act only on the HIGH and not the LOW of the momentary
digitalWrite(4, !digitalRead(4)); // invert pin state just once
timer.setTimeout(2000L, [](){
digitalWrite(4, !digitalRead(4)); // then invert it back after 1000ms
Blynk.virtualWrite(V1, LOW);
});
}}
void loop()
{
Blynk.run();
timer.run(); // missing this very important line to make the timer work
}