Can't control digital pin unless i have an additional button

so, progress! Now the problem I’m having is that there seems to be no way to control the D1 pin indirectly. In my case, I need to turn D1 on ONLY IF a variable is >2. This would be controlled by a button that toggles V2. The code I have written seems enough to do the task, but I found out that it only works if I have another button hooked up to D1, and even if I don’t use it, the main task works perfectly. Here’s the stuff:

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

int nivel = 0;
int relState = 0;

char ssid[]   = "xxx";
char pass[]   = "xxx";
char auth[]   = "xxx";
char server[] = "blynk-cloud.com";

BlynkTimer timer;
WidgetLED led1(V4);

unsigned int myServerTimeout  =  3500;  //  3.5s server connection timeout (SCT)
unsigned int myWiFiTimeout    =  3200;  //  3.2s WiFi connection timeout   (WCT)
unsigned int functionInterval =  7500;  //  7.5s function call frequency   (FCF)
unsigned int blynkInterval    = 10000;  // 25.0s check server frequency    (CSF)

void setup()
{
  pinMode(D2, INPUT_PULLUP);
  pinMode(D5, INPUT_PULLUP);
  pinMode(D6, INPUT_PULLUP);
  pinMode(D7, INPUT_PULLUP);

  Serial.begin(115200);
  Serial.println();
  if (WiFi.status() == 6) {
    Serial.println("\tWiFi not connected yet.");
  }
  timer.setInterval(functionInterval, myfunction);// run some function at intervals per functionInterval
  timer.setInterval(blynkInterval, checkBlynk);   // check connection to server per blynkInterval
  unsigned long startWiFi = millis();
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    if (millis() > startWiFi + myWiFiTimeout) {
      Serial.println("\tCheck the WiFi router. ");
      break;
    }
  }
  Blynk.config(auth, server);
  checkBlynk();
  timer.setInterval(500L, checkNiv);
}

void myfunction() {
  Serial.println("\tLook, no Blynk  block.");
  if (WiFi.status() == 3) {
    Serial.println("\tWiFi still  connected.");
  }
  if (Blynk.connected()) {
    Blynk.virtualWrite(V11, millis() / 1000);
  }
}

void checkBlynk() {
  if (WiFi.status() == WL_CONNECTED)
  {
    unsigned long startConnecting = millis();
    while (!Blynk.connected()) {
      Blynk.connect();
      if (millis() > startConnecting + myServerTimeout) {
        Serial.print("Unable to connect to server. ");
        break;
      }
    }
  }
  if (WiFi.status() != 3) {
    Serial.print("\tNo WiFi. ");
  }
  Serial.printf("\tChecking again in %is.\n", blynkInterval / 1000);
  Serial.println();
}
void checkNiv() {
  if (! digitalRead(D7)) {
    Blynk.virtualWrite(2, 4);
    nivel = 4;
  } else if (! digitalRead(D6)) {
    Blynk.virtualWrite(2, 3);
    nivel = 3;
  } else if (! digitalRead(D5)) {
    Blynk.virtualWrite(2, 2);
    nivel = 2;
  } else if (! digitalRead(D2)) {
    Blynk.virtualWrite(2, 1);
    nivel = 1;
  } else {
    Blynk.virtualWrite(2, 0);
    nivel = 0;
  };  Serial.print("Nivel: ");
  Serial.println(nivel);
  if (nivel > 2 && relState == 1) {
    digitalWrite(D1, HIGH);
    led1.on();
  } else {
    digitalWrite(D1, LOW) ;
    led1.off();
  };
}

BLYNK_WRITE(V3)
{
  Serial.println("Received V3");
  relState = param.asInt();
}

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

}

Also just a little bit of info: There’s an LED (in Blynk) as an indicator of the relay state. If there’s no button controlling D1, the LED lights up as if the relay pin was on (which unfortunately is not the case)

It’s like the digitalWrite(D1, HIGH) is completely bypassed…

I honestly do not fully know what you are doing in your code :woozy_face:… but I did notice that you do not have a pinMode(D1, OUTPUT) perhaps that might help?

I have determined it best to NOT mix virtual pins and direct (Digital/Analog) pin references in the App… yes it can work, but I just found it … quirky.

And due to my only using vPins, i also add this into my code just to make sure my pin designations are not conflicting with the library’s ability for direct pin control.

#define BLYNK_NO_BUILTIN  // Disable Apps built-in analog & digital pin operations

I see, it’s surely that. I started with a simple button that toggled D1 from Blynk, then got rid of that and added other things… but i definitely forgot to define d1 as an output

This code measures water level via 4 different electrodes, then turns off a pump if the level is below 3, nothing else. I have to be unable to turn it on if the level is below 3, hence the quirky V3 usage. idk

and lo and behold, it works! Thanks.

1 Like