I wanted to control light bulbs based on a measurement made by my Arduino. An Arduino total light sensor seemed the most useful to trigger lighting so that is what I built and talk about how to do in this post. The Arduino light sensor triggers a Philips Hue light bulb via a Linux computer and a Hue bridge. The code I used is in this GitHub repository; check out my website for more info.
######################################################
TL;DRGet an Arduino with a light sensor that can send data over your network, and some light bulbs that connect to the network. See my GitHub repository for inspiration and toggle your lights with changes in sensor data.
See this repository for the Python-Hue interface I used.
See this repository for direct control of Philips Hue bulbs via an Arduino.
######################################################
How it worksThe light sensor is a BH1750 from Adafruit, it is shown below. It is a small sensor that measures the total light in lux, the SI unit for light.
The system works by measuring the light via an Arduino, sending the light value over MQTT to my Linux computer/server which then sends commands to my Philips Hue lights based on the Arduino-measured light value. I talk about how to do the Arduino - MQTT - Computer setup in a previous post.
The setup is fairly straightforward to configure. The BH1750 Light Sensor has great support libraries that enable it to nearly work out of the box after connecting it to the Arduino correctly. I am using an existing Linux computer I am using as a media server to receive the data via MQTT. I have the White Ambiance Philips Hue Lights which have a nice API for sending commands to. Python code works great for collecting the MQTT data and sending the Hue commands.
Sketch of steps- Connect a light sensor to a Internet-connected Arduino
To measure the light via Arduino, this is my code:
bool avail = BH1750.begin(BH1750_TO_GROUND);
BH1750.start();
lux=BH1750.getLux(); // waits until a conversion finished
Serial.print("BH1750 status:");
Serial.println(avail);
Serial.print("The current lumens are:");
Serial.println(lux);
- Send light sensor data to a second computer (I used MQTT)
- Use code on second computer to send Hue commands
To change the state of the Hue lightbulbs, I used the phue library. In code, this looks similar to:
import datetime
from phue import Bridge
def send_hueUpdate(lumVal):
"""Update hue lights according to light value and time of day"""
# Specify times of day to limit hue updates
now = datetime.datetime.now()
todayLate = now.replace(hour=23, minute=0, second=0, microsecond=0)
todayEarly = now.replace(hour=6, minute=30, second=0, microsecond=0)
# Set up Hue bridge connection
b = Bridge(HueIP)
b.get_api()
# Check if luminosity and time meet conditions to turn on
if (float(lumVal) < 10) and (now < todayLate) and (now > todayEarly):
print('Sending Hue Update to turn on')
b.set_light('Table','on',True)
else:
print('Sending Hue Update to turn off')
b.set_light('Table','on',False)
This function is called when "lumVal" measuring the luminosity is received from the Arduino light sensor over MQTT. Follow the initial example in the phue library to make sure you properly connect to your Hue bridge by pressing the center button and running the initial Hue bridge code within 30 seconds. You have to authorize usage with the Hue bridge before sending commands.
It's important to note that you can send commands to Hue bulbs directly from an Arduino. I already had a computer set up to receive Arduino data which is why I did this project this way.
Final ProductI am keeping track of the sensor values in a dashboard in Grafana as shown below. Whenever the light sensor reads a value below the yellow line, I turn on the Hue bulbs.
It works! The code/sensor turned on my table light bulb when it got too dark inside. In the future other Arduino sensors can be used to do this too.
Comments
Please log in or sign up to comment.