ESP8266 NodeMCU - Automated WiFi Night Lights, NTP and Weather Station

by jonr6846 in Circuits > Wireless

995 Views, 1 Favorites, 0 Comments

ESP8266 NodeMCU - Automated WiFi Night Lights, NTP and Weather Station

WeatherStation.png

Living in the UK, the length of daylight varies greatly between summer and winter. I have security lights that come on each evening but to ensure the lights only come on at dusk, the timers require frequent adjustment. Further, with BST/GMT daylight savings time, the timers need changing again.

So, how about automating the process by using a WiFi enabled NodeMCU with a Light Dependent Resistor (LDR) to trigger a mains relay at dusk, then turn the lights off each evening at 11pm?

But how will the NodeMCU know the time of day or if it's DST? - Simple, use the WiFi to connect to an internet based time source. (NTP Server)

Ok - but what if I want to control multiple light relays? - This can be done, use the NodeMCU to host a web page to display the time/night or day status and additional ESP8266 can read the web page and trigger the relay at the appropriate time.

Oh, as the LDR needs to be outside to see if it's getting dark, why not add a weather station to the web page? Yeah and get the web page to auto-update every 10 secs... coz you can.

This is real project scope slip ....but this is a solution!!

Supplies

1 off, NodeMcu Lua V3 ESP8266 CH340G ESP-12E WIFI Lua Internet Development Board Module

1 off Light Dependent Resistor (LDR) Photo Cell

1 off 10k 0.25w resistor

1 off BreadBoard and a few jumpers

1 or more ESP8266 relay board(s)

1 off Bosch BME280 Pressure, Temperature and Humidity Sensor

Adruino development environment

Set Up the Breadboard

breadboard.png

Plug the NodeMCU into the breadboard, along with the BME280 and LDR. One leg of the LDR goes to pin A0 on the NodeMCU, the other leg goes to the 3.3v pin. Insert the 10k resistor between the A0 pin and ground.

Next connect the +ve and ground from the NodeMCU to BME280, connect pin D1 to the SCL pin and D2 pin to SDA. Reference the picture for details.

Connect the NodeMCU to your computer via a USB to microUSB cable.

Load the Sketch .......

Start Arduino on your PC and load the attached sketch. Edit the file to insert the SSID and password for your network and compile the sketch. If any libraries are missing, these need to be added to your Arduino environment and if they are not obvious, take a look in GitHub. The code has been hacked around from other peoples projects to create the required functionality and I expect it could be optimised by someone brighter than me.

Once the sketch is loaded to the ModeMCU, the Arduino serial monitor will show the new host IP address.

Test Your New Weather Station

WeatherStation.png

Open a browser on the same LAN as the NodeMCU and enter the host IP address and press enter. - See attached picture of the "WiFi Weather Station". If the wrong timezone is displayed, take a look at the ezTime documentation and change the "myTZ.setLocation" value to the required location.

Cover the LRD and after a few seconds the "Day Light" setting with change to "Night Time".

Once all working package up the components in a breathable box and locate the device outside in a shaded dry location.

Create the WiFi Controlled Switch

IMG_2525.JPG
IMG_2526.JPG
IMG_2527.JPG

ESP8266 WiFi relay boards are freely available on well known auctions sites. The microcontroller is denoted as an ESP-01S and needs to be unplugged from the relay module to program it. This is achieved using a USB Serial interface. See the pictures.

Open the attached Arduino sketch and add you LAN SSID, LAN password and the IP address of the "WiFi Weather Station" we created earlier. Now load the sketch to your ESP8266 relay board.

The device is set to turn off at 11pm i.e. when the hour is greater than "22" (see the note in the sketch referring to 11pm)

So, this sketch reads the Weather Station host and when the day turns to night, the relay on the ESP8266 activates and and switches off again at 11pm. There is a code in the sketch to ensure the relay can't activate again until the next day.

Be VERY careful with anything connected to mains electricity and don't use this unless you know what you're doing. Don't complain to me if you get a mains shock or set your hair alight.

Alternate Board Layout for ESP8266

IMG_2528.JPG

I also had this version of the relay board which worked well - I removed the power circuit and replaced the blue mechanical relay with solid state logic relay. It runs off 5v so I could power it with phone charger.

NOTE:- the client reading the data from the Weather Station host uses simple strings - If I was clever I'd use JSON......