I am new to Blynk and am having a little trouble with my setup. I have uploaded the wiring drawing of my two ESP8266 (NodeMCUs) and will include the code as well. I have done something wrong somewhere. If anyone can provide help or at least another set of eyes to help me solve this it would be great. My idea is to use the same auth code for both devices and use just one project screen to control both sets of lights on outlets. The manual control is working fine on the single relay unit. The dual relay unit I think I am getting some feedback on my wiring or something as it shuts off if I have something plugged in. The timer function used to work, but does not seem to want to work now as well on either one. I will post the code below. Also in the picture I have included the Blynk app icons and what ports they are assigned to as well. Thank you very much for any help that is provided.
//ESP8266 #1 Script
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SPI.h>
//int enableTimer = HIGH;
int timer1Running = LOW;
int manualOffice = LOW;
SimpleTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Code";
void checkTimer();
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, "SID", "Pass");
timer.setInterval(2000, checkTimer);
pinMode(14, OUTPUT);
digitalWrite(14, LOW);
}
/*BLYNK_WRITE(9){
enableTimer = param.asInt();
}*/
BLYNK_WRITE(10){
timer1Running = param.asInt();
}
BLYNK_WRITE(11){
manualOffice = param.asInt();
}
BLYNK_WRITE(14)
{
BLYNK_LOG("Timer Value (V14): %s", param.asStr());
}
void loop()
{
Blynk.run();
timer.run(); // call function
}
void checkTimer()
{
// If timer is running or if manual override is on turn lights on
if(((timer1Running == 1)) || (manualOffice == 1))
//if(((enableTimer == 1) && (timer1Running == 1)) || (manualOffice == 1))
{
// Timer is running, so lights on!
digitalWrite(14, HIGH); //
Blynk.virtualWrite(19, 1023); // Turns Virtual LED on to signify on Phone that the light was indeed turned on.
}
else
{
digitalWrite(14, LOW); //
Blynk.virtualWrite(19, 0); // Turns Virtual LED off to signify on Phone that the light was indeed turned off.
}
}
/**************************************************************************************************************
//ESP8266 #2 Script
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
int enableTimer = HIGH;
int timer2Running = LOW;
int manualLight1 = LOW;
int manualLight2 = LOW;
SimpleTimer timer;
char auth[] = "Code";
void checkTimer();
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, "SID", "Pass");
timer.setInterval(2000, checkTimer);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
}
BLYNK_WRITE(3){
enableTimer = param.asInt();
}
BLYNK_WRITE(30){
timer2Running = param.asInt();
}
BLYNK_WRITE(25){
manualLight1 = param.asInt();
}
BLYNK_WRITE(16){
manualLight2 = param.asInt();
}
BLYNK_WRITE(4)
{
BLYNK_LOG("Timer Value (V4): %s", param.asStr());
}
void loop()
{
Blynk.run();
timer.run(); // call function
}
void checkTimer()
{
// If timer is running or if manual override is on turn lights on
if(((timer2Running == 1)) || (manualLight1 == 1))
{
// Timer is running, so lights on!
digitalWrite(4, HIGH); //
//digitalWrite(5, HIGH);
Blynk.virtualWrite(20, 1023); // Turns Virtual LED on to signify on Phone that the light was indeed turned on.
}
else
{
digitalWrite(4, LOW);
//digitalWrite(5, LOW);
Blynk.virtualWrite(20, 0); // Turns Virtual LED off to signify on Phone that the light was indeed turned off.
}
// If timer is running or if manual override is on turn lights on
if(((timer2Running == 1)) || (manualLight2 == 1))
{
digitalWrite(5, HIGH);
Blynk.virtualWrite(21, 1023); // Turns Virtual LED on to signify on Phone that the light was indeed turned on.
}
else
{
digitalWrite(5, LOW);//
Blynk.virtualWrite(21, 0); // Turns Virtual LED off to signify on Phone that the light was indeed turned off.
}
}
I am using a different brand relay board, but I found that the 3.3v output from the ESPs pins did not provide enough power. I had to use a level shift to get supply the relay board with 5v trigger inputs. Good luck!
Thanks for the comment, if you notice I am using a level shift up to 5.2V on the top drawing. On the bottom drawing, I am using a 5.2 V USB cord and the Nodemcu board provides the 5.2V on the bottom left pin to the relays. I am able to do the manual buttons in blink just fine. It just seems that something with the timer code is not working correctly. What is really strange, is that the timer works on one of the boards as of this morning but not on the other one.
I also modified my wiring this morning on the top drawing because I believe I am getting some feedback from energizing the relays. I removed the two voltage converters from the circuit and am powering the NodeMCU via USB instead. It seems to be working as expected but I now cant have just one plug coming out of the receptacle like I wanted. I am trying to make this system only have one power plug come out and allow control of two outlets.
The drawing shows you supplying the Vin on each board with 5v. However the ESP only outputs 3.3v to the relay trigger pins. In my experience, I had unreliable relay actions with 3.3v trigger even though I was supplying 5v to the relay board’s Vin. I had to put in a level shifter in between the relay board and the ESP output pins.
Vin also happens to be Vout when using USB. I measured the voltage and it is 5V. My problem isn’t getting the relays to work. I can control them great with the manual on off buttons in Blynk. I also was able to control them fine with the timer although flaky before but has stopped recently on one of the bottom drawing.
I’ll see about doing it later today, I am at work so I can’t right now. It is wired as the pictures show. It is the top one that I have had the issues with. I am thinking that I need to insert either diodes or resistors to limit the direction of the electricity.
Yes the Relays do, but I think I might be getting some feedback on the power lines that are used to power the NodeMCU. This is what Script 2 looks like wired up currently with a working timer as of this morning without the voltage converters.
I forgot to take a picture last night. Does anyone see any problem with using this code instead of what I posted earlier? I am at work and cannot test it as of right now.
#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
int timer1Running = LOW;
int timer2Running = LOW;
char auth[] = "Auth";
void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
Blynk.begin(auth, "SID", "Pass");
pinMode(0, OUTPUT);
digitalWrite(0, LOW);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
BLYNK_WRITE(V3)
{
/* Widget button set to turn on/off on GPIO0
You'll get HIGH/1 at startTime and LOW/0 at stopTime.
this method will be triggered every day
until you remove widget or stop project or
clean stop/start fields of widget
*/
BLYNK_LOG("Got a value: %s", param.asStr());
timer1Running = param.asInt();
if((timer1Running == 1))
{
// Timer is running, so lights on!
digitalWrite(0, HIGH); //
Blynk.virtualWrite(13, 1023);
}
else
{
digitalWrite(0, LOW);
Blynk.virtualWrite(13, 0);
}
}
BLYNK_WRITE(V7)
{
/* Widget button set to turn on/off on GPIO13
You'll get HIGH/1 at startTime and LOW/0 at stopTime.
this method will be triggered every day
until you remove widget or stop project or
clean stop/start fields of widget
*/
BLYNK_LOG("Got a value: %s", param.asStr());
timer2Running = param.asInt();
if((timer2Running == 1))
{
// Timer is running, so lights on!
digitalWrite(13, HIGH); //
Blynk.virtualWrite(17, 1023);
}
else
{
digitalWrite(13, LOW);
Blynk.virtualWrite(17, 0);
}
}
void loop()
{
Blynk.run();
}
OK, so I changed it even more possibly cleaned up the problem from the very first part of code. Please check this and see if you see anything. Thank you all.
#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
int timer1Running = LOW;
int timer1Manual = LOW;
int timer2Running = LOW;
int timer2Manual = LOW;
char auth[] = "Auth";
void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
Blynk.begin(auth, "SID", "Pass");
pinMode(0, OUTPUT);
digitalWrite(0, LOW);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
BLYNK_WRITE(V3)
{
/* Widget button set to turn on/off on GPIO0
You'll get HIGH/1 at startTime and LOW/0 at stopTime.
this method will be triggered every day
until you remove widget or stop project or
clean stop/start fields out of widget
*/
BLYNK_LOG("Got a value: %s", param.asStr());
timer1Running = param.asInt();
/*
if((timer1Running == 1))
{
// Timer is running, so lights on!
digitalWrite(0, HIGH); //
Blynk.virtualWrite(23, 1023);
}
else
{
digitalWrite(0, LOW);
Blynk.virtualWrite(23, 0);
}
*/
}
BLYNK_WRITE(V13)
{
//Manualy Turn on light
timer1Manual = param.asInt();
}
BLYNK_WRITE(V7)
{
/* Widget button set to turn on/off on GPIO13
You'll get HIGH/1 at startTime and LOW/0 at stopTime.
this method will be triggered every day
until you remove widget or stop project or
clean stop/start fields out of widget
*/
BLYNK_LOG("Got a value: %s", param.asStr());
timer2Running = param.asInt();
/*
if((timer2Running == 1))
{
// Timer is running, so lights on!
digitalWrite(13, HIGH); //
Blynk.virtualWrite(27, 1023);
}
else
{
digitalWrite(13, LOW);
Blynk.virtualWrite(27, 0);
}
*/
}
BLYNK_WRITE(V17)
{
//Manualy Turn on light
timer2Manual = param.asInt();
}
void loop()
{
Blynk.run();
timer1();
timer2();
}
void timer1()
{
if((timer1Running == 1) || (timer1Manual == 1))
{
// Timer is running and enabled or manual turned on, so lights on!
digitalWrite(0, HIGH);
Blynk.virtualWrite(23, 1023); // Turns Virtual LED widget on currently assigned to Virtual Pin 23
}
else
{
digitalWrite(0, LOW); // Turns lights off
Blynk.virtualWrite(23, 0); // Turns virtual LED widget on currently assigned to Virtual Pin 23
}
}
void timer2()
{
if((timer2Running == 1) || (timer2Manual == 1))
{
// Timer is running and enabled or manual turned on, so lights on!
digitalWrite(13, HIGH);
Blynk.virtualWrite(27, 1023); // Turns Virtual LED widget on currently assigned to Virtual Pin 27
}
else
{
digitalWrite(13, LOW); // Turns lights off
Blynk.virtualWrite(27, 0); // Turns virtual LED widget on currently assigned to Virtual Pin 27
}
}
Look in your function your “if” statement goes through and you are flooding server every loop because Blynk.virtualWrite() would happen on every loop. Rather than that you should switch something ON and thats it stop switching something that is already switched. How is it done? Through callbacks etc. Easiest solution here? Wrap both timers in one func like so and add bool value of shouldSwitch if it’s true you switch virtualPin and GPIO:
void timer1()
{
if (shouldSwitch) {
if((timer1Running) || (timer1Manual))
{
// Timer is running and enabled or manual turned on, so lights on!
digitalWrite(0, HIGH);
Blynk.virtualWrite(23, 1023); // Turns Virtual LED widget on currently assigned to Virtual Pin 23
}
else
{
digitalWrite(0, LOW); // Turns lights off
Blynk.virtualWrite(23, 0); // Turns virtual LED widget on currently assigned to Virtual Pin 23
}
if((timer2Running) || (timer2Manual))
{
// Timer is running and enabled or manual turned on, so lights on!
digitalWrite(13, HIGH);
Blynk.virtualWrite(27, 1023); // Turns Virtual LED widget on currently assigned to Virtual Pin 27
}
else
{
digitalWrite(13, LOW); // Turns lights off
Blynk.virtualWrite(27, 0); // Turns virtual LED widget on currently assigned to Virtual Pin 27
} shouldSwitch = false;
}
}
Ok next thing obviously only put timer1() inside loop(), now on every BLYNK_WRITE() add shouldSwitch = true;
Get rid of “if” inside your BLYNK_WRITE(V7) and BLYNK_WRITE(V3) and this will work.