Automatic sunset/sunrise on/off switch without internet

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

Nice and useful project buddy.

Thanks for sharing.

Can you please share the circuit diagram of the project. It would be really helpful if you shared the details.

Its a post from 2021. I doubt he is active in the forum.

You can google, ESP8266 with RTC on google and find many circuits and codes.

1 Like

If you follow the link for the RTC library it will tell you how to connect the hardware RTC.

The sketch doesn’t provide any Blynk connectivity though, and needs quite a bit of work to make it suitable to use with Blynk.

Pete.

1 Like

Thank you.
Hope it will work…