Light sensor switch

30.1.2019

This project shows how to turn an ordinary Christmas lights into a smart one. The basic idea is to automatically turn the lights on in the evening and turn them off in the morning.  This task can be easily handled by a light sensor (LDR), but if it is connected to an esp8266, it can offer some additional manipulations like turn off during night hours or operate remotely with a help of the Blynk App.

Features:

Components Required:

Operation basics:


Christmas lights are switched ON and OFF by a light sensor (LDR) according to illuminance. The LDR is connected to an analog (ADC) pin.

Please note that analog pin can be powered by a maximum of 1V, therefore resistor divider should be calculated properly.



Here I am demonstrating a two different ways of controlling the Christmas lights.

The first version doesn’t include Blynk App so the system is not interactive remotely. In this case, the system can operate completely offline. You can set desired illuminance threshold in the code and/or set it manually over the trimmer. Therefore a timer and time synchronization can be disabled in the sketch. However, the ESP goes online for 10 minutes each time when it is plugged into a power socket so you can easily update a new sketch within this period by using OTA. To update OTA, ESP-12 or higher version is required. How to updating OTA please read here. Onboard led indicate different states during connecting to AP so you can find how to manipulate onboard led within the code.

Furthermore, if there is a need for a Christmas lights to be additionally switched off during the night (e.g. from  0 am to 4 am when no one observes), a timer is included into the code. The timer is synchronized from NTP server by connecting ESP to an AP. In this case the ESP goes online only for time synchronization and return to offline mode after that. If connection to AP fails, the timer will add a compensation time which was lost when unsuccessfully trying to connect to AP. To shorten synchronization time, firstly we ping NTP server. If NTP server is not reachable than time synchronization is skipped. Synchronization is done once per day.

The second version includes Blynk App. Here the ESP is connected to AP all the time so interaction is possible anytime. This version includes almost all the features from first version, additionally the Blynk App is used so you can control Christmas lights remotely from your mobile phone over WiFi or mobile network. Here you can enable or disable the whole system, set a threshold, set a night timer off and observe the system state. There is also a code for a ThingSpeak channel which enables to observe sun conditions captured from the LDR sensor on the widget added to the smartphone. Timer and  NTP synchronization is not needed in this case since Blynk App cares for this.

Library:

NTP code and library: https://github.com/SensorsIot/NTPtimeESP
Find nearest NTP server: http://www.pool.ntp.org/zone/europe
Ping library: https://github.com/dancol90/ESP8266Ping
You will also find links to the libraries within the sketch. 

Blynk:


You can clone this project by scanning the QR code. QR code can be found in project gallery at the bottom of this page.

ThingSpeak:


Essential operating data are uploaded to private ThingSpeak channel to be easily monitored on the IoT ThingSpeak Monitor widget placed on my smart phone. Therefore I don't need to launch Blynk App each time only to check the data. The data are uploaded each 10 minutes.

Project gallery & downloads:
Project gallery & downloads:
esp8266 light sensor switch
esp8266 light sensor switch
esp8266 light sensor switch
esp8266_light_sensor_switch
esp8266_light_sensor_switch
esp8266_light_sensor_switch
esp8266_light_sensor_switch
Any comments please leave on YouTube.

Turn lights on at night - turn off at day

Turn off in the night for a certain time

Cynchronized with NTP time

New parameters uploading OTA

Operate remotely with mobile phone & Blynk App

Works offline / online

esp8266-12

AC/DC 220V to 3.3V

LDR 10k

Trimmer 1k or R 470

R 1k

Q BC547

D 1N4001

Relay 3V

Turn lights on at night - turn off at day

Turn off over the night for a certain time

Cynchronized with NTP time

New parameters upload OTA

Operate remotely with mobile phone & Blynk App

Works offline / online