I am trying to switch the relay off/on with the ESP8266 NodeMCU via a wifi connection.
The issue is whenever i power on the NodeMCU the relay switch turns on as well. So i want the relay switch to be turned off whenever i power the esp on or when i press the reset button. Here is the code.
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
// 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);
}
void loop()
{
Blynk.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
}
As I said, you’d need to modify your code.
Something like this should get you going…
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
#define relay_pin 5 // Relay pin connected to GPIO5 (Pin D1) - change as required
BLYNK_WRITE(V1) // callback function that is triggered automatically when the widget attached to pin V1 changes
{
int widget_value = param.asInt(); // capture the value from the widget
if (widget_value = 1)
{
digitalWrite(relay_pin,HIGH); // Turn the relay on
}
else
{
digitalWrite(relay_pin,LOW); // Turn the relay off
}
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(relay_pin,OUTPUT);
digitalWrite(relay_pin,HIGH);
Blynk.begin(auth, ssid, pass);
// 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);
}
void loop()
{
Blynk.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
}
The code assumes that your relay is attached to GPIO1 (D5) and that you have a button widget (in Switch mode) attached to pin V1.
I think his problem come from the choice of the mcu pinout.
Remember, some pins are high at MCU start !
He has to use safe pins.
Blynk V2 is on the way