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();
}