Close
0%
0%

Web-connected automatic sunset lamp

A web-connected ESP8266 controller to turn on a lamp at sunset. Useful if the wiring is far from a location where daylight may be sensed.

Similar projects worth following
I need to turn on a lamp at sunset, but the wiring box is deep inside my house. This means that it is impossible to use a traditional daylight sensor (Light Dependent Resistor or Photodiode). The solution is to use an ESP8266 to fetch the date/time from the internet over WiFi. Depending on a list of sunset times for each day of the year, the lamp is turned on and off at the desired times.

This project is based on a Lua program which uses a relay to control a lamp. The lamp is intended to turn on at sunset and turn off at sunrise, but the lamp can be configured with any desired on/off time once per day.

The current date and time are fetched every minute from google.com.

A text file (lamptimes.txt) contains a list of the turn-on and turn-off times for each day of the year. The user must edit this file to configure the required turn-on and turn-off times. A service such as aa.usno.navy.mil/data/docs/RS_OneYear.php can be used to find the sunset and sunrise times for your location.

All times are based on internet time (GMT). There is no attempt to convert the time to local time. Therefore all times in lamptimes.txt should be in GMT.

Note: The circuit diagram is capable of driving 2 relays, but I only used 1 relay for this project.

suntracker.lua

Lua program to read the daily turn-on and turn-off times. Compares these times to the current internet time to turn a lamp on or off.

text/x-lua - 3.82 kB - 06/27/2017 at 13:58

Download

lamptimes.txt

A text file which contains the turn-on and turn-off times for each day of the year.

text/plain - 6.49 kB - 06/28/2017 at 11:41

Download

init.lua

Automatically executed at power-on. Waits for 30 seconds before launching the main program (suntracker.lua).

x-lua - 324.00 bytes - 06/15/2017 at 14:29

Download

Circuit.png

Circuit diagram to show how an ESP-01 is connected to a pair of relays.

Portable Network Graphics (PNG) - 70.39 kB - 06/30/2017 at 12:15

Preview

Relay.jpg

An example of the relay board that I use.

JPEG Image - 53.33 kB - 06/30/2017 at 12:15

Preview

View all 6 files

  • 1 × ESP8266 Any type of ESP8266 can be used, but I use the ESP-01 (see for example dx.com/p/448981)
  • 1 × 5-volt Relay Make sure that the relay supports the voltage and current for the lamp. I use the 250-volt 10-Amp relay dx.com/p/448813
  • 1 × 3.3-volt power supply Anything in the range of 2.5 to 4.0 volts is acceptable.
  • 1 × 10K resistor Anything in the range 1K to 10K is acceptable.
  • 1 × WiFi network with internet access.

View all 7 components

  • Simple circuit to allow ESP-01 to control a pair of relays

    bredman06/30/2017 at 12:30 0 comments

    This project will use a circuit that I often use. It allows an ESP-01 to control a pair of relays.

    Some issues to note

    • The ESP-01 and the relay board are driven from a 4-volt supply. Even though the ESP-01 is rated for maximum 3.6v and the relay board is rated at 5v, I find that they both behave well at 4v. This means that I only need a single power supply.
    • The ESP-01 has two GPIO pins available for use, but these must be pulled up with resistors to boot into run mode. This is why the two resistors are shown in the circuit.
    • I actually only need one relay, but I have loads of these 2-relay boards. So it's not worth my while to buy a single-relay board. If you wish to use a single-relay board, you can remove the resistor R2 and connect GPIO-2 directly to 4v.
    • The duplicated GND and 4v for the relay are for an odd reason. I have a selection of relay boards, and they randomly mix up the ground and power pins. So I have allowed for both combinations to be connected without crossing wires.

  • Software is updated to add more flexibility

    bredman06/27/2017 at 14:06 0 comments

    I have been flooded with requests for different variants of this project. Some people want to follow a sunrise/sunset schedule. Some want to turn the lamp off in the middle of the night. Some want a different type of schedule completely.

    To answer all of these requirements, I have moved away from the idea of following sunset and sunrise. Now I have only the concept of turning the lamp on once per day and turning the lamp off once per day.

    To do this, I have defined a file (lamptimes.txt) which lists the daily on-time and off-time, for example

    14 Mar 1837 0651
    15 Mar 1839 0648
    16 Mar 1840 0646

    This means that the user can define any type of desired schedule, as long as it is only required once per day.

    Next step is to tackle the hardware. Shouldn't be too difficult !!!

  • New revision of software works better offline

    bredman06/16/2017 at 12:40 0 comments

    The software is updated to remove the need to fetch the sunset time from sunrise-sunset.org. This means that the software will continue to run for years in the future, even if this API is removed.

    The software now reads a file (sunset.txt) to find the sunset time for each day. The current date/time is fetched from google.com. Let's hope that google.com will still exist in years to come!

  • First version of software is now complete

    bredman06/15/2017 at 14:25 0 comments

    The first version of the software is now complete and has been tested.

    The software has 3 main functions...

    • Fetch the current time, sunset time, and solar noon time from the internet.
    • Calculate how long until the next sunset and next solar midnight.
    • Turn on the lamp at sunset and turn it off at solar midnight.

    The most important aspect of the software is that it should continue to work even if internet connectivity is lost. Every 10 minutes, the times are refreshed from the internet. Even if this fails, the internal clock will continue to run, and the cycle will be repeated every 24 hours until the refresh from the internet is successful.

    After a power failure, the program will wait for 30 seconds (init.lua) to allow internet conntivity to be restored. The the main program (suntracker.lua) is launched. The lamp defaults to ON, and will remain ON until a valid shedule has been downloaded from the internet.

    Next step is to tackle the hardware...

View all 4 project logs

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates