Automatic sunset on and sunrise off relay switch without internet connection.
How it’s work, basically its need only 4 thing longitude,latitude and timezone and the most important time.
You need only ds3231 rtc module,esp8266 and relay.
And the most amazing thing no need of reprogramming for wifi credentials or adjust rtc module just plug your module and mcu automatic sync with NTP server.
Note:
1.NTP server sync only occur when you reinsert battery in rtc module.
2.No need of Blynk or internet.
3.No need of wifi router.
Library:
Code:
#include <Arduino.h>
#include <WiFiManager.h>
WiFiManager wifiManager;
#include <WiFiUdp.h>
WiFiUDP ntpUDP;
#include <NTPClient.h>
NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 60000);
#include "RTClib.h"
RTC_DS3231 rtc;
#include <sunset.h>
SunSet sun;
#define TIMEZONE 5.5 //india timezone +5:30
#define LATITUDE 28.7041
#define LONGITUDE 77.1025
void autoOnOff()
{
DateTime now = rtc.now();
sun.setCurrentDate(now.year(), now.month(), now.day());
int sunrise = sun.calcSunrise();
int sunset = sun.calcSunset();
int c_time = now.hour() * 60 + now.minute();
if (c_time >= sunrise && c_time <= sunset)
{
digitalWrite(D7, HIGH);
}
else
{
digitalWrite(D7, LOW);
}
Serial.print(c_time);
Serial.print(":");
Serial.print(sunrise);
Serial.print(":");
Serial.print(sunset);
Serial.println(" ");
}
void setup()
{
digitalWrite(D7, HIGH);
pinMode(D7, OUTPUT);
pinMode(D5, INPUT_PULLUP);
Serial.begin(115200);
rtc.begin();
if (rtc.lostPower() && digitalRead(D5) == LOW)
{
wifiManager.autoConnect("Wemos-322AC1");
Serial.println("getting time from server");
timeClient.begin();
timeClient.update();
rtc.adjust(timeClient.getEpochTime());
Serial.println("time adjusted");
}
sun.setPosition(LATITUDE, LONGITUDE, TIMEZONE);
}
void loop()
{
autoOnOff();
delay(10000);
}