More light!
Charly's Column – Hue and Rasp Pi
Since his Trådfri Smarthome article over a year ago, sys admin columnist Charly has been receiving messages from readers with two questions: "Can you do that with the Philips' Hue system?" and "Can this also be done with a normal brightness sensor?" Yes and yes!
In a previous issue [1], I described how I control a smart light in my living room with Ikea's Trådfri system and Linux. The darker it gets, the more I turn up the lights. Around the same time, Martin Loschwitz then explained how he used a Raspberry Pi and a transmitter module to control Hue and other Zigbee devices [2].
Now I'm going to do it again, without the Zigbee module, but I expect to use a Hue bridge, which Martin's approach doesn't need, and because not everyone has a roof-top photovoltaic system, this time I focused on a simple brightness sensor.
Getting Started
First, I discovered the IP address of my Hue Bridge: 10.0.0.8. Then I pressed the button on the bridge and entered the following at the command line:
curl --data "{\"devicetype\": \"huelr\"}" http://10.0.0.8/api
The huelr
string (for "Hue living room") is freely selectable. I got some output like:
[{"success":{"username":"T3VGtGWmFUgJwHufxkudY1bizvqoZMhCSqu1ySwm"}}]
This unpronounceable string is an authentication string, which I have to specify with every call in the future. A (confusing) list of all connected devices is returned by the command:
curl --request GET 10.0.0.8/api/T3VGtGWmFUgJwHufxkudY1bizvqoZMhCSqu1ySwm
If you only need information about lights, you can append /lights
to the command or /lights/1
, /lights/2
, and so on for each lamp. The following command switches lamp 1 to medium brightness (value 125
):
curl --request PUT --data "{\"bri\":125}" 10.0.0.8/api/T3VGtGWmFUgJwHufxkudY1bizvqoZMhCSqu1ySwm/lights/1/state
For the brightness sensor, I used a $2.00 module named BH1750 (online, e.g., [3]). I connected this to the I2C bus of a Raspberry Pi (Figure 1). I used the Zero W model with WiFi – it has to communicate with the bridge.
Cat's Eyes
Raspbian comes with almost everything you need, with only a few packages to add:
sudo apt install build-essential wiringpi i2c-tools python-smbus
The program in Listing 1 reads the sensor in the program I named lux
:
cc lux.c -lwiringPi mv a.out lux && chmod 755 lux
Listing 1
lux.c
01 #include <wiringPiI2C.h> 02 #include <stdio.h> 03 int main (void) { 04 int handle = wiringPiI2CSetup(0x23); 05 wiringPiI2CWrite(handle,0x10); 06 sleep(1); 07 int word=wiringPiI2CReadReg16(handle,0x00); 08 int lux=((word & 0xff00)>>8) | ((word & 0x00ff)<<8); 09 printf("%d \n",lux); 10 return 0; 11 }
If you run lux
, you will see the current measured value for the photo sensor in lux units. Depending on this, you can now control your smart lighting.
Infos
- "Charly's Column: libcoap" by Charly Kühnast, Linux Pro Magazine, issue 202, September 2017, pg. 49: http://www.linuxpromagazine.com/Issues/2017/202/Enlightened-libcoap/(language)/eng-US
- "Controlling Zigbee Devices with the Raspberry Pi" by Martin Loschwitz, Linux-Magazin, September 2017, pg. 76: http://www.linux-magazin.de/ausgaben/2017/09/raspi-spricht-zigbee/ [in German]
- Debo BH1750: https://www.ebay.com/i/292393778070?chn=ps
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.