Virtual Pin unexpectedly stopped working

Hi,

The code for my room on an ESP12E to control the light and my roller shutters worked for almost a year till today.
The roller shutters are still working (control them with direct pins) but my two relay switch (need it for the light) wont work anymore. it just wont change state when i press a button in the app. I did some testing and it seems to be that the pins that are reserved for the virtual pin are no more able to change state (even tried it manually by setting a button in the app digital for one of them). Maybe they ignore the pinMode output?

Thats the whole code:

#define BLYNK_PRINT Serial


    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <DHT.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 DHTPIN 2 
    #define DHTTYPE DHT11

    DHT dht(DHTPIN, DHTTYPE);
    BlynkTimer timer;

    int light = 0;



    void sendSensor()
    {
      float h = dht.readHumidity();
      float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

      if (isnan(h) || isnan(t)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
      }
      // You can send any value at any time.
      // Please don't send more that 10 values per second.
      Blynk.virtualWrite(V6, h);
      Blynk.virtualWrite(V7, t);
    }

    long unsigned int switchActivated;
    long unsigned int switchActivated2;


    void setup()
    {
      Serial.begin(115200);
      Blynk.begin(auth, ssid, pass);
        dht.begin();
    pinMode(2, OUTPUT); 
    pinMode(3, OUTPUT); 

      // Setup a function to be called every second
      timer.setInterval(1000L, sendSensor);
      
    }

    BLYNK_WRITE(V1) //Button Widget is writing to pin V
    {
      if (param.asInt()){
        switchActivated2 = millis(); 
        digitalWrite(16, HIGH);
        Blynk.virtualWrite(V1, HIGH);
        Blynk.virtualWrite(V5, 1023);
        Blynk.virtualWrite(V4, 0);
      }
    }

      BLYNK_WRITE(V2) //Button Widget is writing to pin V
    {
      if (param.asInt()){
        switchActivated = millis(); 
        digitalWrite(5, HIGH);
        Blynk.virtualWrite(V2, HIGH);
        Blynk.virtualWrite(V4, 1023);
        Blynk.virtualWrite(V5, 0);
      }
    }

      BLYNK_WRITE(V3) //Button Widget is writing to pin V
    {
      if (param.asInt()){

        if (light == 0) {
           digitalWrite(2, HIGH);
           digitalWrite(3, LOW);
           light = 1;
        }
         else  {
           digitalWrite(3, HIGH);
           digitalWrite(2, LOW);
           light = 0;
        }        
      }
    }


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

      
      if(switchActivated > 0 && switchActivated + 20000 < millis())
    {
      switchActivated = 0;
      digitalWrite(5, LOW);
       Blynk.virtualWrite(V2, LOW);
       
    }

     if(switchActivated2 > 0 && switchActivated2 + 20000 < millis())
    {
      switchActivated2 = 0;
      digitalWrite(16, LOW);
       Blynk.virtualWrite(V1, LOW);
       
    }
    }

Thats the code needed for the light:

  BLYNK_WRITE(V3) //Button Widget is writing to pin V
{
  if (param.asInt()){

    if (light == 0) {
       digitalWrite(2, HIGH);
       digitalWrite(3, LOW);
       light = 1;
    }
     else  {
       digitalWrite(3, HIGH);
       digitalWrite(2, LOW);
       light = 0;
    }        
  }
}

Maybe theres just a stupid mistake but it worked till today :frowning:

Sounds like your App updated, but you are still runing an old Library version… possibly even an old Local Server version if you are not using Cloud.

All three need to be constantly updated with each other over time.

I now started with a complete new sketch and tested everything on its own and i found out that the “light switch” with V3 only works when I set up the two digital pins as buttons in the app and toggle them once. After that it works completely fine till I restart the NoceMCU. How can I solve that?

#define BLYNK_PRINT Serial


#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[] = "";


long unsigned int switchActivated;
long unsigned int switchActivated2;

BlynkTimer timer;
int light = 0;

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

}


BLYNK_WRITE(V1)
{
  if (param.asInt()){
    switchActivated2 = millis(); 
    digitalWrite(5, HIGH);
    Blynk.virtualWrite(V1, HIGH);
    Blynk.virtualWrite(V5, 1023);
    Blynk.virtualWrite(V4, 0);
  }
}

  BLYNK_WRITE(V2) 
{
  if (param.asInt()){
    switchActivated = millis(); 
    digitalWrite(16, HIGH);
    Blynk.virtualWrite(V2, HIGH);
    Blynk.virtualWrite(V4, 1023);
    Blynk.virtualWrite(V5, 0);
  }
}

BLYNK_WRITE(V3) // Thats the code for the light switch, toggles between the two pins
{
  if (param.asInt()){

    if (light == 0) {
       digitalWrite(4, HIGH);
       digitalWrite(0, LOW);
       light = 1;
    }
     else  {
       digitalWrite(0, HIGH);
       digitalWrite(4, LOW);
       light = 0;
    }        
  }
}

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

  
  if(switchActivated > 0 && switchActivated + 20000 < millis())
{
  switchActivated = 0;
  digitalWrite(16, LOW);
   Blynk.virtualWrite(V2, LOW);
   
}

 if(switchActivated2 > 0 && switchActivated2 + 20000 < millis())
{
  switchActivated2 = 0;
  digitalWrite(5, LOW);
   Blynk.virtualWrite(V1, LOW);
   
}
}

Not sure I follow what you need solved? Perhaps setting up sync commands so that upon reboot the sketch retreve last known virtual pin settings?

An you still should have a pinMode() in setup for each digital pin used for reliable operation

ok thanks a lot will try that tomorrow

Everything works perfectly now. I dont know if it was the pinMode or Blynk syncAll, but it works.
Thanks!

1 Like

Hi,
Its been a while but recently the problem occured that my lights randomly turn on when the esp disconnects from the internet. Before I implemented Blynk.SyncAll there was no problem with that at all.
Now I come home and my Lights are turned on, this also happens in the night.
What can I do to solve that problem?