Why does not green led turns on?

I just can’t understand why the “greenLED” not turns on when i turn the relay HIGH(HIGH is LOW)

Can someone give me a push here?

Here is the code:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

//Put WiFi cridentials and token here----------------------------------

char auth[] = "****";
char ssid[] = "****";
char pass[] = "****";

//---------------------------------------------------------------------

int coffeeSwitch = 5;
int yellowLED = 4;
int greenLED = 12;

int ledStatus;

BlynkTimer timer;

void setup() {
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600);
  pinMode(coffeeSwitch, OUTPUT);
  digitalWrite(coffeeSwitch, HIGH);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V1);
}

void coffeeOFF() {
  digitalWrite(coffeeSwitch, HIGH);
  Blynk.virtualWrite(V1, LOW);
}

BLYNK_WRITE(V1) {
  int pinValue = param.asInt();
  if (pinValue == 1)   {
    digitalWrite(coffeeSwitch, LOW);
    digitalWrite(yellowLED, HIGH);
    digitalWrite(greenLED, LOW);
    timer.setTimeout(5000L, coffeeOFF);
    ledStatus = 0;
  }
  if (pinValue == 0) {
    digitalWrite(coffeeSwitch, HIGH);
    digitalWrite(yellowLED, LOW);
    digitalWrite(greenLED, HIGH);
  }
}

void loop() {
  Blynk.run();
  timer.run();
}

Depending on what you mean by “i turn the relay”… If by switching the widget it should work, if by the timer function then you need to add a command to do so :slight_smile:

OH, I see now… in the timer function, you need to add in the sync command…

When i turn of the relay from the app, i can se a wery short blink from the greenLED. But it never turns on.

When the time is out and the relay shuts off with the timer nothing happens at all.

Can you show me what i must do here for getting this to work?

When i think about it, i only want the green led to light up when the relay shuts down automaticly with timer. When i press the button in app for turning the relay off manually, i do not wnt the greenLED to light up.

Actually, you only need this in your timer function to do the same job that manually pressing the button does…

void coffeeOFF() {
 Blynk.virtualWrite(V1, LOW);  // Sets the virtual pin state
 Blynk.syncVirtual(V1); // runs the vPin function to process the last set state
}

However, that will do everything the manual command does…

…including turning on or off the LED.

And this is cleaner if written like this… (yes, more coding shortcuts :wink: )

BLYNK_WRITE(V1) {
  if (param.asInt()){  // If vPin HIGH/1
    digitalWrite(coffeeSwitch, LOW);
    digitalWrite(yellowLED, HIGH);
    digitalWrite(greenLED, LOW);
    timer.setTimeout(5000L, coffeeOFF);
    ledStatus = 0;  // No idea what this is for?
  } else {  // If vPin LOW/0
    digitalWrite(coffeeSwitch, HIGH);
    digitalWrite(yellowLED, LOW);
    digitalWrite(greenLED, HIGH);
  }
}

Other than that, I am not seeing why your LED flickers… but then I would need to play with the code in my own IDE to troubleshoot that.

BTW, this action is what should happen when the ESP turns on/reboots, as that particular pin (D6 - GPIO12) will flash HIGH for 100ms…

Sounds strange when you say it does the same when manually toggling the button widget OFF (I am guessing it is in switch mode?)

I would add in some Serial.println("this thing happened") type statements so you can follow your code progress through the Serial monitor… best way to troubleshoot if what you think is happening… is or isn’t happening.

Strange…

I changed pin to D7 (GPIO 13) and same problem appears. The greenLED flicker one time and shuts off when i push the button in app to turn off relay or it turns off automaticly with timer.

This is the code i tried with:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

//Put WiFi cridentials and token here----------------------------------

char auth[] = "****";
char ssid[] = "****";
char pass[] = "****";

//---------------------------------------------------------------------

int coffeeSwitch = 5;
int yellowLED = 4;
int greenLED = 13;

int ledStatus;

BlynkTimer timer;

void setup() {
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600);
  pinMode(coffeeSwitch, OUTPUT);
  digitalWrite(coffeeSwitch, HIGH);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V1);
}

void coffeeOFF() {
 Blynk.virtualWrite(V1, LOW);  // Sets the virtual pin state
 Blynk.syncVirtual(V1); // runs the vPin function to process the last set state
}

BLYNK_WRITE(V1) {
  int pinValue = param.asInt();
  if (pinValue == 1)   {
    digitalWrite(coffeeSwitch, LOW);
    digitalWrite(yellowLED, HIGH);
    digitalWrite(greenLED, LOW);
    timer.setTimeout(5000L, coffeeOFF);
  }
  if (pinValue == 0) {
    digitalWrite(coffeeSwitch, HIGH);
    digitalWrite(yellowLED, LOW);
    digitalWrite(greenLED, HIGH);
  }
}

void loop() {
  Blynk.run();
  timer.run();
}

Set some print commands at key points and show your Serial Monitors output…

Pins 12, 13 & 14 all have the same brief ON pulse at boot… perhaps when you shut off the relay your ESP is actually rebooting??

Can you confirm this about your Button on V1

Yes, the button is in Switch mode :slight_smile:

I really do not know why this is not working.

I’m at work now so i can’t do anything now, but it’s attempting to try an Arduino Uno or Nano just for checking the code.

Possible rebooting is only thing I can think of that matches description of flashing LED then OFF issue… Serial Monitor output will confirm or deny that.

BTW, do the Yellow LED and relay turn ON and OFF as planned??

Yes, the yellow led and relay works fine and turns on/off.

Btw, i can see the yellow led and relay is unstable from time to time also. It works, but the yellow LED is flickering a little too sometimes.

Can bad connection make my Wemos to reboot?

Strange that it only the greenLED that does not work then…

Lots of things can cause reboots… power brownouts, code issues, possibly even coil feedback from the relay (but probably not that if you are using a relay board with inverting opto isolator chip) … but once you can, a few Serial prints and watching the Serial monitor will help show what is actually happening.

I see.

Have you tried the code?

Interesting if this code works other places. If so, i must be something with my connections or hardware related.

Most probably i know what the problem is now. I think i have made an rookie mistake.

I post the answer later today if thats true…

This shouldn’t be the problem but… Why do you need to fire the BLYNK_WRITE after it was just fired by the app?

Comment out the virtual sync and see if it behaves different.