Before creating the topic
- Search forum for similar topics
- Check http://docs.blynk.cc and http://help.blynk.cc/
- Add details :
• Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
• Smartphone OS (iOS or Android) + version
• Blynk server or local server
• Blynk Library version
• Add your sketch code.Code should be formatted as example below.
Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.
Ok…the weird think is that I can remove all the reference to Blynk and the code compiles without errors. If I add the blynk definitions in it throws
“‘class NTPClient’ has no member named ‘getFormattedDate’; did you mean ‘getFormattedTime’?”
I have updated the correct NTPClient that contains the getFormattedDate() Method in the library but that didn’t help. Any help would be appreciated. I really need the current date and time for this project.
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPLTpm9mxiz"
#define BLYNK_TEMPLATE_NAME "ZoneControllerTemplate"
#define BLYNK_AUTH_TOKEN "bR_vqWl0tKq20IyA5Qk0DEjMMxjOB8xv"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include "WiFi.h"
#include "WiFiClient.h"
#include "Dusk2Dawn.h"
#include <Time.h>
#include "RTClib.h"
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <BlynkSimpleEsp32.h>
int dawnTime = 0;
int duskTime = 0;
int doorOpenTime = 0;
int doorCloseTime = 0;
int wateringCyclesDone = 0;
int manualWateringTime = 0;
int manualWateringTimeLeft = 0;
int lastMin = 0;
String dawnTimeStr = "";
String duskTimeStr = "";
String doorOpenTimeStr = "";
String doorCloseTimeStr = "";
int iMonth = 2;
int iDay = 16;
int iYear = 2020;
int iHour = 2;
int iMin = 16;
int iSec = 0;
const int ledPin = 13;// the number of the LED pin
int ledState = LOW;
const int ON = LOW;
const int OFF = LOW;
boolean daylightSavingsTime = false;
boolean dstAdjusted = false;
boolean springDstAdjusted = false;
boolean fallDstAdjusted = false;
boolean doorClosed = false;
boolean doorOpen = false;
boolean heaterOn = false;
boolean heaterManualOn = false;
boolean heaterOff = true;
boolean fenceOn = false;
boolean fenceOff = true;
boolean fenceManualOn = false;
boolean circulation = false;
boolean dawnDuskTimesUpdated = false;
double tempF = 0.0;
RTC_DS3231 rtc;
float temperature = 0.0;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String currentTime = "";
BlynkTimer timer;
//char auth[] = "OCVpPCNgNSSGEyAA3YTjYfC1JH-c6mcY";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Variables to save date and time
String formattedDate;
String dateStamp;
String dayStamp;
String mthStamp;
String yearStamp;
String timeStamp;
const int RELAY1 = 18;
const int RELAY2 = 19;
const int RELAY3 = 27;
const int RELAY4 = 33;
const int RELAY5 = 15;
const int RELAY6 = 32;
const int RELAY7 = 14;
const int RELAY8 = 5;
void myTimerEvent()
{
}
int getDawnTime(int month, int day, int year) {
Dusk2Dawn tomballTx(30.136307, -95.683519, -6);
return tomballTx.sunrise(year, month, day, daylightSavingsTime);
}
int getDuskTime(int month, int day, int year) {
Dusk2Dawn tomballTx(30.136307, -95.683519, -6);
return tomballTx.sunset(year, month, day, daylightSavingsTime);
}
void setTime(int year, int month, int day, int hour, int min, int sec) {
rtc.adjust(DateTime(year, month, day, hour, min, sec));
}
void openDoor() {
digitalWrite(RELAY1, LOW);
doorOpen = true;
doorClosed = false;
Serial.print("Door is opening");
delay(5000);
ledState = HIGH;
digitalWrite(ledPin, ledState);
digitalWrite(RELAY1, HIGH);
dawnDuskTimesUpdated = false;
}
void closeDoor() {
digitalWrite(RELAY2, LOW);
doorClosed = true;
doorOpen = false;
delay(5000);
Serial.print("Door Closed");
ledState = LOW;
digitalWrite(ledPin, ledState);
digitalWrite(RELAY2, HIGH);
dawnDuskTimesUpdated = false;
}
void turnFenceOn() {
fenceOn = true;
fenceOff = false;
Serial.print("Electric Fence is On");
digitalWrite(RELAY8, LOW);
}
void turnFenceOff() {
fenceOff = true;
fenceOn = false;
Serial.print("Electric Fence is Off");
digitalWrite(RELAY8, HIGH);
}
void turnHeaterOn() {
heaterOn = true;
heaterOff = false;
Serial.print("Heater is On");
digitalWrite(RELAY6, HIGH);
digitalWrite(RELAY7, HIGH);
}
void turnHeaterOff() {
heaterOn = false;
heaterOff = true;
Serial.print("Heater is Off");
digitalWrite(RELAY6, HIGH);
digitalWrite(RELAY7, HIGH);
}
void circPumpOn() {
circulation = true;
Serial.print("Circ Pump is On");
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY4, LOW);
digitalWrite(RELAY5, LOW);
}
void circPumpOff() {
circulation = false;
Serial.print("Circ Pump is Off");
digitalWrite(RELAY3, HIGH);
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
}
void getDawnDuskTimes() {
if (WiFi.status() == WL_CONNECTED) {
dawnTime = getDawnTime(iMonth, iDay, iYear);
doorOpenTime = dawnTime - 30;
duskTime = getDuskTime(iMonth, iDay, iYear);
doorCloseTime = duskTime + 60;
}
Serial.println(dawnTime); // 418
Serial.println(duskTime); // 1004
Serial.println(doorOpenTime); // 418
Serial.println(doorCloseTime); // 1004
char time[6];
Dusk2Dawn::min2str(time, dawnTime);
dawnTimeStr = time;
Serial.println("The Sun rises at ");
Serial.println(time); // 06:58
char time2[] = "00:00";
Dusk2Dawn::min2str(time2, duskTime);
duskTimeStr = time2;
Serial.println("The Sun sets at ");
Serial.println(time2); // 16:53
Serial.println("");
char odTime[6];
Dusk2Dawn::min2str(odTime, doorOpenTime);
doorOpenTimeStr = odTime;
Serial.println("Door opens at ");
Serial.println(odTime); // 06:58
char cdTime2[] = "00:00";
Dusk2Dawn::min2str(cdTime2, doorCloseTime);
doorCloseTimeStr = cdTime2;
Serial.println("Door closes at ");
Serial.println(cdTime2); // 16:53
Serial.println("");
dawnDuskTimesUpdated = true;
}
BLYNK_WRITE(V0)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1) {
openDoor();
}
}
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1) {
closeDoor();
}
}
BLYNK_WRITE(V2)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1) {
turnHeaterOn();
heaterManualOn = true;
}
if (pinValue == 0) {
turnHeaterOff();
}
}
BLYNK_WRITE(V14)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1) {
turnFenceOn();
}
if (pinValue == 0) {
turnFenceOff();
}
}
BLYNK_WRITE(V3)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1) {
circPumpOn();
}
if (pinValue == 0) {
circPumpOff();
manualWateringTime = 0;
manualWateringTimeLeft = 0;
}
}
BLYNK_WRITE(V4)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue != 0) {
manualWateringTime = pinValue;
}
}
BLYNK_WRITE(V16)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
if (pinValue == 1) {
daylightSavingsTime = true;
dstAdjusted = false;
}
if (pinValue == 0) {
daylightSavingsTime = false;
dstAdjusted = false;
}
}
void setup() {
Serial.begin(115200);
int count = 0;
// Set pinMode for RELAYs 1-4 to OUTPUT
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
pinMode(RELAY5, OUTPUT);
pinMode(RELAY6, OUTPUT);
pinMode(RELAY7, OUTPUT);
pinMode(RELAY8, OUTPUT);
digitalWrite(RELAY1, HIGH);
digitalWrite(RELAY2, HIGH);
digitalWrite(RELAY3, HIGH);
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
digitalWrite(RELAY6, HIGH);
digitalWrite(RELAY7, HIGH);
digitalWrite(RELAY8, HIGH);
pinMode(ledPin, OUTPUT);
Serial.print("Attempting WiFi connection");
WiFi.reconnect(); //or reconnect they work about the same
while (WiFi.status() != WL_CONNECTED) {
if (count > 9) break;
Serial.print(".");
Serial.println("Connecting...." );
Serial.println(count);
WiFi.begin();
delay (2000);
count++;
}
if (WiFi.status() != WL_CONNECTED) {
WiFi.mode(WIFI_AP_STA);
WiFi.beginSmartConfig();
Serial.println("Waiting for SmartConfig.");
while (!WiFi.smartConfigDone()) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("SmartConfig done.");
Serial.println("Waiting for WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("WiFi Connected.\nIP Address: ");
Serial.println(WiFi.localIP());
}
Serial.println("WiFi Connected.\nIP Address: ");
Serial.println(WiFi.localIP());
Blynk.begin(BLYNK_AUTH_TOKEN, WiFi.SSID().c_str(), WiFi.psk().c_str());
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// You can also specify server:
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(-21600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
while (!timeClient.update()) {
timeClient.forceUpdate();
}
// The formattedDate comes with the following format:
// 2018-05-28T16:00:13Z
// We need to extract date and time
formattedDate = timeClient.getFormattedDate();
// Extract date
int splitT = formattedDate.indexOf("T");
dateStamp = formattedDate.substring(0, splitT);
// Extract year
yearStamp = dateStamp.substring(0, 4);
iYear = yearStamp.toInt();
// Extract month
mthStamp = dateStamp.substring(5, 7);
iMonth = mthStamp.toInt();
// Extract day
dayStamp = formattedDate.substring(8, 10);
iDay = dayStamp.toInt();
Serial.print("DATE: ");
Serial.print(iMonth);
Serial.print("-");
Serial.print(iDay);
Serial.print("-");
Serial.print(iYear);
Serial.println("");
getDawnDuskTimes();
// Extract time
timeStamp = formattedDate.substring(splitT + 1, formattedDate.length() - 1);
delay(1000);
// Extract time
iHour = timeStamp.substring(0, 2).toInt();
iMin = timeStamp.substring(3, 5).toInt();
iSec = timeStamp.substring(7, 9).toInt();
Serial.print("Time: ");
Serial.print(iHour);
Serial.print(":");
Serial.print(iMin);
Serial.print(":");
Serial.print(iSec);
Serial.println("");
Serial.println("");
Serial.println(timeStamp);
setTime(iYear, iMonth, iDay, iHour, iMin, iSec);
}
void loop(){
if (daylightSavingsTime && fallDstAdjusted && !dstAdjusted){
iHour = iHour -1;
setTime(iYear, iMonth, iDay, iHour, iMin, iSec);
springDstAdjusted = true;
fallDstAdjusted = false;
dstAdjusted = true;
getDawnDuskTimes();
}
if (!daylightSavingsTime && springDstAdjusted && !dstAdjusted){
iHour = iHour +1;
setTime(iYear, iMonth, iDay, iHour, iMin, iSec);
springDstAdjusted = false;
fallDstAdjusted = true;
dstAdjusted = true;
getDawnDuskTimes();
}
iMin = currentTime.substring(3, 5).toInt();
//digitalWrite(V9, HIGH);
if (doorOpen) {
Blynk.virtualWrite(V9, 1023);
}
if (doorClosed) {
Blynk.virtualWrite(V9, 0);
}
if (circulation) {
Blynk.virtualWrite(V10, 1023);
} else {
Blynk.virtualWrite(V10, 0);
}
if (heaterOn) {
Blynk.virtualWrite(V11, 1023);
} else {
Blynk.virtualWrite(V11, 0);
}
if (fenceOn) {
Blynk.virtualWrite(V15, 1023);
} else {
Blynk.virtualWrite(V15, 0);
}
Blynk.virtualWrite(V12, doorOpenTimeStr);
Blynk.virtualWrite(V13, doorCloseTimeStr);
Blynk.virtualWrite(V5, dawnTimeStr);
Blynk.virtualWrite(V6, duskTimeStr);
Blynk.virtualWrite(V7, currentTime);
Blynk.virtualWrite(V8, tempF);
Blynk.virtualWrite(V14, manualWateringTimeLeft);
Blynk.virtualWrite(V15, wateringCyclesDone);
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
char buf1[] = "hh:mm";
currentTime = now.toString(buf1);
Serial.println(currentTime);
Serial.println(dawnTimeStr);
Serial.println(duskTimeStr);
if (doorOpenTimeStr.equals(currentTime) && doorOpen == false) {
Serial.println("It is dawn - open door");
openDoor();
}
if (doorCloseTimeStr.equals(currentTime) && doorClosed == false) {
Serial.println("It is dusk - closed door");
closeDoor();
}
if (currentTime.equals("00:00") && dawnDuskTimesUpdated == false) {
Serial.println("It's midnight update dawn & dusk times");
getDawnDuskTimes();
wateringCyclesDone = 0;
}
if (currentTime.equals("06:00") && circulation == false) {
Serial.println("Turning Circ Pump On");
circPumpOn();
wateringCyclesDone += 1;
}
if (currentTime.equals("06:30") && circulation == true) {
Serial.println("Turning Circ Pump Off");
circPumpOff();
}
if (currentTime.equals("14:00") && circulation == false) {
Serial.println("Turning Circ Pump On");
circPumpOn();
wateringCyclesDone += 1;
}
if (currentTime.equals("14:30") && circulation == true) {
Serial.println("Turning Circ Pump Off");
circPumpOff();
}
if (currentTime.equals("22:00") && circulation == false) {
Serial.println("Turning Circ Pump On");
circPumpOn();
wateringCyclesDone += 1;
}
if (currentTime.equals("22:30") && circulation == true) {
Serial.println("Turning Circ Pump Off");
circPumpOff();
}
if (manualWateringTime != 0.0 && circulation == false) {
Serial.println("Manual watering started");
lastMin = iMin;
manualWateringTimeLeft = manualWateringTime;
circPumpOn();
}
if (manualWateringTime != 0.0 && circulation == true) {
if (lastMin != iMin) {
manualWateringTimeLeft--;
lastMin = iMin;
}
if (manualWateringTimeLeft == -1) {
manualWateringTime = 0.0;
manualWateringTimeLeft = 0.0;
Serial.println("Manual watering finished");
circPumpOff();
}
}
temperature = (rtc.getTemperature()); //read temperature
tempF = (((temperature * 1.8) + 32.0) - 10.0);
Serial.print(tempF); //print temperature as a float
Serial.println(" F");
if (tempF < 40.0 && heaterOff) {
turnHeaterOn();
}
if (tempF > 45.0 && heaterOn && !heaterManualOn) {
turnHeaterOff();
}
if (duskTimeStr.equals(currentTime) && fenceOn == false) {
Serial.println("It is dusk - energise fence");
turnFenceOn();
}
if (dawnTimeStr.equals(currentTime) && fenceOff == false) {
Serial.println("It is dawn - de-energise fence");
turnFenceOff();
}
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
}