2

I am working on a garden timer project, I connect to WiFi once and if its successful show time from NTPClient or else show time from RTC.

I have been done the above, and it works in the sense that if I turn off the WiFi while the program is running, it switches to the RTC and back to NTPClient when WiFi is available again.

PROBLEM- If the WiFi is not available when the board boots, the program just waits for WiFi where I want it to use RTC.

Hardware-ESP8266 based NodeMCU, I2C RTC, I2C 16x2 LCD.

Software-Blynk

My code:-

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <DS3231.h>
#include <Wire.h>
#include "DHT.h"
#include "LiquidCrystal_PCF8574.h"

#define BLYNK_PRINT Serial #define DHT_PIN_DATA 12 // LCD definitions #define LCD_ADDRESS 0x27 #define LCD_ROWS 2 #define LCD_COLUMNS 16 #define SCROLL_DELAY 150 #define BACKLIGHT 25 #define DHTTYPE DHT22

// Auth code and wifi credentials here char auth[] = "ymri80lFL9_xjZppphf6WIRHtQ1kqEWOt9zBy"; char ssid[] = "Hotspot-82DB"; char pass[] = "qqnbkbbbk"; int autoSch; int waterNow; int morTime; int afterTime; int postNoon; int eveTime; int schTime; int minStep; int interval; int count; int flag = 0; int soilHum = A0; int waterItr; int checkRun; int waterItr1; int oneTime; bool h12Flag; bool pmFlag;

unsigned long previousMillis = 0; byte wifi[] = { B00001, B00001, B00101, B00101, B00101, B10101, B10101, B10101,

};

DHT dht(DHT_PIN_DATA, DHTTYPE); LiquidCrystal_PCF8574 lcdI2C; WiFiUDP ntpUDP; DS3231 Clock; NTPClient timeClient(ntpUDP, "in.pool.ntp.org", 19800, 600000);

BLYNK_CONNECTED() { Blynk.syncAll(); }

BLYNK_WRITE(V0) { autoSch = param.asInt(); // assigning incoming value from pin V1 to a variable }

BLYNK_WRITE(V1) { morTime = param.asInt(); // assigning incoming value from pin V1 to a variable }

BLYNK_WRITE(V2) { afterTime = param.asInt(); // assigning incoming value from pin V1 to a variable }

BLYNK_WRITE(V6) { postNoon = param.asInt(); // assigning incoming value from pin V1 to a variable }

BLYNK_WRITE(V3) { eveTime = param.asInt(); // assigning incoming value from pin V1 to a variable }

BLYNK_WRITE(V10) { waterNow = param.asInt(); // assigning incoming value from pin V1 to a variable }

BLYNK_WRITE(V4) { schTime = param.asInt(); // assigning incoming value from pin V1 to a variable }

BLYNK_WRITE(V7) { minStep = param.asInt(); // assigning incoming value from pin V1 to a variable }

void setup() { Wire.begin(); dht.begin(); lcdI2C.begin(LCD_COLUMNS, LCD_ROWS, LCD_ADDRESS, BACKLIGHT); pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); pinMode(14, OUTPUT); pinMode(15, OUTPUT); digitalWrite(14, LOW); digitalWrite(15, LOW); Blynk.virtualWrite(V5, 0); Blynk.virtualWrite(V8, 0); pinMode(0, INPUT_PULLUP); Serial.begin(9600); Blynk.begin(auth, ssid, pass); timeClient.begin(); /* while ( WiFi.status() != WL_CONNECTED ) { delay ( 500 ); Serial.print ( "." ); } */

Blynk.syncAll(); lcdI2C.createChar(0, wifi); }

void loop() { Blynk.run(); time_run(); lcd(); butPress(); butCheck(); /if (waterNow != 1) { Blynk.virtualWrite(V10, 0); digitalWrite(14, LOW); Blynk.virtualWrite(V5, 0); Blynk.virtualWrite(V8, minStep 60); }/ if (waterNow == 1) { if (waterItr < minStep 60) { waterOn(); waterItr++; delay(500); } else { waterOff(); }

} else if (autoSch == 0) { auto_run1(); waterItr = 0; } else if (autoSch == 1) { sch_run2(); waterItr = 0; } else { //Blynk.virtualWrite(V10, 0); waterItr = 0; digitalWrite(14, LOW); Blynk.virtualWrite(V5, 0); } Blynk.syncAll();

}

void butPress() { if (digitalRead(0) == LOW) { delay (100); if (flag == 0) { flag = 1; } else if (flag == 1) { flag = 0; waterItr1 = 0; digitalWrite(15, LOW); Blynk.virtualWrite(V5, 0); } } }

void butCheck() { if (flag == 1) { digitalWrite(15, HIGH); Blynk.virtualWrite(V5, 255); lcdI2C.selectLine(2); lcdI2C.print("Wtr B Now : "); lcdI2C.print(600 - waterItr1); if (waterItr1 < 600) { waterItr1++; delay(500); } else { waterItr1 = 0; flag = 0; digitalWrite(15, LOW); Blynk.virtualWrite(V5, 0); } } }

void lcd() { if (oneTime == 0){

oneTime=0;
}

//lcdI2C.clear(); lcdI2C.setCursor(0, 0);

if (WiFi.status() == WL_CONNECTED) { timeClient.update(); lcdI2C.print("T:"); lcdI2C.print(timeClient.getFormattedTime()); Clock.setHour(timeClient.getHours()); Clock.setMinute(timeClient.getMinutes()); Clock.setSecond(timeClient.getSeconds()); } else { lcdI2C.print("R:"); lcdI2C.print(Clock.getHour(h12Flag, pmFlag)); lcdI2C.print(":"); lcdI2C.print(Clock.getMinute()); lcdI2C.print(":"); lcdI2C.print(Clock.getSecond()); } if (autoSch == 0) { lcdI2C.setCursor(10, 0); lcdI2C.print(" Auto "); } else { lcdI2C.print(" Sch "); }

if (WiFi.status() == WL_CONNECTED) { lcdI2C.setCursor(15, 0); lcdI2C.write(0); }

if (digitalRead(14) == 1) { lcdI2C.selectLine(2); lcdI2C.print("Wtr Now "); } else { if (int(timeClient.getHours()) < 6 && morTime == 1 && autoSch == 0) { lcdI2C.selectLine(2); lcdI2C.print("Auto Wtr at: 6a "); } else if (int(timeClient.getHours()) < 12 && afterTime == 1 && autoSch == 0) { lcdI2C.selectLine(2); lcdI2C.print("Auto Wtr at: 12n "); } else if (int(timeClient.getHours()) < 14 && postNoon == 1 && autoSch == 0) { lcdI2C.selectLine(2); lcdI2C.print("Auto Wtr at:14pn "); } else if (int(timeClient.getHours()) < 18 && eveTime == 1 && autoSch == 0) { lcdI2C.selectLine(2); lcdI2C.print("Auto Wtr at: 18e "); } else if (int(timeClient.getHours()) >= 18 && morTime == 1 && autoSch == 0) { lcdI2C.selectLine(2); lcdI2C.print("Auto Wtr at: 6a "); } else if (int(timeClient.getHours()) >= 6 && morTime == 1 && autoSch == 0) { lcdI2C.selectLine(2); lcdI2C.print("Auto Wtr at: 6a "); } else if (int(timeClient.getHours()) >= 12 && afterTime == 1 && autoSch == 0) { lcdI2C.selectLine(2); lcdI2C.print("Auto Wtr at: 12a "); } else if (int(timeClient.getHours()) >= 14 && postNoon == 1 && autoSch == 0) { lcdI2C.selectLine(2); lcdI2C.print("Auto Wtr at: 14a "); } else if (int(timeClient.getHours()) >= 18 && eveTime == 1 && autoSch == 0) { lcdI2C.selectLine(2); lcdI2C.print("Auto Wtr at: 18a "); } else { if (autoSch == 0) { lcdI2C.selectLine(2); lcdI2C.print(" Switch to Sch "); } else { lcdI2C.selectLine(2); lcdI2C.print(" Schedule mode "); } } } }

void waterOn() { lcdI2C.setCursor(0, 0); digitalWrite(14, HIGH); Blynk.virtualWrite(V5, 255); lcdI2C.selectLine(2); lcdI2C.print("Wtr Now :"); lcdI2C.print((minStep * 60) - waterItr); Blynk.virtualWrite(V8, (minStep * 60) - waterItr); Blynk.syncAll(); }

void waterOff() { Blynk.virtualWrite(V10, 0); digitalWrite(14, LOW); Blynk.virtualWrite(V5, 0); Blynk.virtualWrite(V8, 0); waterItr = 0; Blynk.syncAll(); }

void sch_run2() { if (schTime == 1) { digitalWrite(14, HIGH); Blynk.virtualWrite(V5, 255); checkRun = 1; Blynk.syncAll(); } else if (schTime == 0) { checkRun = 0; if (checkRun == 0) { digitalWrite(14, LOW); Blynk.virtualWrite(V5, 0); Blynk.syncAll(); } } }

void time_run() { Blynk.virtualWrite(V11, int(timeClient.getHours())); Blynk.virtualWrite(V12, int(timeClient.getMinutes())); Blynk.virtualWrite(V13, float(dht.readTempC())); Blynk.virtualWrite(V14, float(dht.readHumidity())); }

void auto_run1() { if (morTime == 1 && String(timeClient.getFormattedTime()) > "06:00:00" && String(timeClient.getFormattedTime()) < "06:20:00") { digitalWrite(14, HIGH); Blynk.virtualWrite(V5, 255); checkRun = 1; } else if (afterTime == 1 && String(timeClient.getFormattedTime()) > "12:00:00" && String(timeClient.getFormattedTime()) < "12:20:00") { digitalWrite(14, HIGH); Blynk.virtualWrite(V5, 255); checkRun = 1; } else if (postNoon == 1 && String(timeClient.getFormattedTime()) > "14:00:00" && String(timeClient.getFormattedTime()) < "14:20:00") { digitalWrite(14, HIGH); Blynk.virtualWrite(V5, 255); checkRun = 1; } else if (eveTime == 1 && String(timeClient.getFormattedTime()) > "11:49:00" && String(timeClient.getFormattedTime()) < "11:50:00") { digitalWrite(14, HIGH); Blynk.virtualWrite(V5, 255); checkRun = 1; } else { //(String(timeClient.getFormattedTime() == "06:20:00" || "12:20:00" || "14:20:00" || "13:48:00")) //waterNowrun(); checkRun = 0; if (checkRun == 0) { //Blynk.virtualWrite(V10, 0); digitalWrite(14, LOW); Blynk.virtualWrite(V5, 0); Blynk.virtualWrite(V8, 0); Blynk.syncAll(); } } }

JRobert
  • 15,407
  • 3
  • 24
  • 51
Coder9390
  • 512
  • 1
  • 7
  • 25

1 Answers1

2

Instead of using blynk.begin() use WiFi.begin() and blynk.config() to achieve non-blocking code

void setup(){
//  Blynk.begin(auth, ssid, pass); don't use this
  WiFi.begin(ssid, pass);
  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }
  Blynk.config(auth);
}
Juraj
  • 18,264
  • 4
  • 31
  • 49
Coder9390
  • 512
  • 1
  • 7
  • 25