Help with digitalWrite function

Hi!

I think I have some problems with using digitalWrite function.

My code:

// --------- LIBRARIES -------------
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266_SSL.h>


// ----------- VARIABLES ---------------
// Set Blynk token.
char auth[] = "xxx";

// Define SSID and Password for a WiFi network
char ssid[] = "xxx";
char pass[] = "xxx";


// Define PINs which are connected to a faucet relay
byte faucet1_pin = 5;


// --------- FUNCTIONS --------------
void setup()
{
  // Debug console
  Serial.begin(115200);
  delay(10);

  // preparing GPIOs
  pinMode(faucet1_pin, OUTPUT);
  digitalWrite(faucet1_pin, LOW);

  // Connect to the WiFI and Blynk server
  Blynk.begin(auth, ssid, pass);
}

BLYNK_WRITE(V1) {
  TimeInputParam t(param);

  // Process start time
  if (t.hasStartTime())
  {
    Serial.println(String("Start: ") +
                   t.getStartHour() + ":" +
                   t.getStartMinute() + ":" +
                   t.getStartSecond());
    digitalWrite(faucet1_pin, HIGH);
  }
  else if (t.isStartSunrise())
  {
    Serial.println("Start at sunrise");
    digitalWrite(faucet1_pin, HIGH);
  }
  else if (t.isStartSunset())
  {
    Serial.println("Start at sunset");
    digitalWrite(faucet1_pin, HIGH);
  }
  else
  {
    // Do nothing
  }

  // Process stop time
  if (t.hasStopTime())
  {
    Serial.println(String("Stop: ") +
                   t.getStopHour() + ":" +
                   t.getStopMinute() + ":" +
                   t.getStopSecond());
    digitalWrite(faucet1_pin, LOW);
  }
  else if (t.isStopSunrise())
  {
    Serial.println("Stop at sunrise");
    digitalWrite(faucet1_pin, LOW);
  }
  else if (t.isStopSunset())
  {
    Serial.println("Stop at sunset");
    digitalWrite(faucet1_pin, LOW);
  }
  else
  {
    // Do nothing: no stop time was set
  }

  // Process timezone
  // Timezone is already added to start/stop time

  Serial.println(String("Time zone: ") + t.getTZ());

  // Get timezone offset (in seconds)
  Serial.println(String("Time zone offset: ") + t.getTZ_Offset());
  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
  for (int i = 1; i <= 7; i++) {
    if (t.isWeekdaySelected(i)) {
      Serial.println(String("Day ") + i + " is selected");
    }
  }
}

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

And I have two problems:

  1. My NodeMCU doesn’t connect to Wi-Fi when I try to prepare pin state in the setup() function. Everything is working fine when I comment that code.
  // preparing GPIOs
  pinMode(faucet1_pin, OUTPUT);
  digitalWrite(faucet1_pin, LOW);
  1. Nothing happens when I set a schedule in a Time input widget, but I expect to work BLYNK_WRITE(V1)

I suspect the problem is in using digitalWrite function with the wrong way, but I don’t understand what exactly I’ve done wrong.

I suspect that your NodeMCU is actually connecting to WiFi. If you all this line at the very top of your sketch, you’ll be able to see much more info:

#define BLYNK_PRINT Serial

I think that maybe you don’t understand how the Time Input widget works. Whenever you change the start/stop times in the widget the server will send a full set of new info to your device, via the virtual pin (V1 in this case).
This will contain a start time and a stop time, so your V1 code will process both of these, setting your faucet1_pin HIGH then immediately LOW again.

The widget does nothing else. It’s up to you to take this information, and compare it with the information from the RTC widget on a regular basis, to see if the start time, stop time, active days etc have been met and to take appropriate action.

Pete.