Have you ever wanted to know if your light is on in your room? What if you are out of town and have forgotten if you turned off the living room lamp? This project allows you to see the light levels of a room from a simple webpage that updates every second.
VideoWiringTo begin, solder some headers to the ESP8266 Thing Dev board. Next, I connected the LDR to a 10k pulldown resistor and 3.3v. An LDR changes resistance based on the level of light that hits it. Then I used a jumper wire to connect the 10k resistor to the ADC pin.
The code for this device is pretty simple. I began by loading up the example sketch for a Hello World server. In order to make it state the light values and refresh automatically, I changed the handleRoot callback function to serve custom html code. Since I don't have an SD card, I couldn't use javascript for automatically refreshing the values. Thankfully, HTML provides a way to refresh the page at a set interval. I just used <meta http-equiv="refresh" content="1">
after the <p> tags. Don't forget to include your WiFi credentials in the attached sketch.
Now, just visit the IP address of the ESP8266 to view the page, in my case it was 192.168.0.41. Be sure to allow websites to refresh automatically in your browser settings. If you would like to view the page when you're not on the same network, such as on a phone using data, port forward to the IP address of the ESP8266. So instead of 192.168.0.41, for example, I would type MY_IP:THE_PORT_I_CHOSE_TO_FORWARD
. MY_IP
is your external IP address and THE_PORT_I_CHOSE_TO_FORWARD
is the port you set up in your router settings. And that's it! Now you can check if your lights are on or off without being in the room! Try making the device turn off a lamp if you click a button on the webpage for an added challenge!
Comments