LIFX bulbs are great, but when you just need to turn them on, off or just change brightness, it can be not very comfortable, to do this simple task by app. So I decided to create much simpler way to do this. And then I came across NodeMCU, small development board similar to Arduino with integrated Wi-Fi, with cost of about $5. I'll describe here, what I've done to make it work.
Goal
I want to control chosen LIFX bulb without smartphone, just by waving my hand. Change intensity by moving towards/away from sensor.
This is what I achieved
How I've done it
Diagram of route from sensor to bulb.
Shopping list
- one LIFX bulb - $60
- Raspberry Pi 2/3 ~$35
- power supply (5V microUSB)
- NodeMCU v3 ~$5 or D1 mini ~$4
- power supply (5V microUSB) or battery
- HC-SR04 ultrasonic range sensor ~$1
- Dupont cables ~$1 or something else to connect it all together
- Wi-Fi router/AP
Setting up Raspberry Pi
I'll just assume you LIFX bulbs are already installed, nothing to write about there.
Of course you can use anything else than RPi, Even RPi A should do the job. Raspberry Pi will do the job of server between NodeMCU and LIFX. I'll skip OS installation, I've got xubuntu, anything will, where you can compile an install nginx will work.
lightsd - compile and configure
Now you need to download, compile and install lightsd service. This service will provide API to communicate with LIFX bulbs via json-rpc over local network, this will give you quick responses. Open terminal and go to folder where you want to download repository.
You'll need first to install git
, cmake
and libevent
.
sudo apt-get install git cmake libevent-dev
Then download and compile lightsd.
git clone https://github.com/lopter/lightsd.git
cd lightsd
cmake .
make install
This will take a while, and if make install
fails, run it with sudo
.
Now you should have lightsd
service ready. Next I've modified lightsd systemd service, to listen on port not only pipe, this I needed for nginx proxy.
You can see if lightsd is running and where is its daemon file by running
systemctl status lightsd
My location is /usr/local/lib/systemd/system/lightsd.service
, I modified it to look like this:
[Unit]
Description=LIFX WiFi smart bulbs control service
After=network.target
[Service]
Restart=on-success
RestartSec=10
User=root
PIDFile=/run/lightsd/lightsd.pid
ExecStart=/usr/local/bin/lightsd -t -u pi -s /run/lightsd/socket -c /run/lightsd/pipe -l localhost:8447
Restart=on-failure
[Install]
WantedBy=multi-user.target
Setting up nginx
This is optional, everything should work also without this step.
First I modified /etc/nginx/nginx.conf
, for easy addition of TCP streams. Add these lines
stream {
include /etc/nginx/conf.d/*.stream;
}
This will load extra configuation from /etc/nginx/conf.d
folder.
Next create file /etc/nginx/conf.d/lightsd.stream
:
server {
listen 8449;
proxy_pass 127.0.0.1:8447;
}
Restart lightsd and nginx.
systemctl daemon-reload
systemctl restart lightsd
systemctl restart nginx
NodeMCU wiring
Nothing complicated, just connect pins like this
HC-SR04 | NodeMCU |
---|---|
Vcc | VU |
Trig | D5 |
Echo | D6 |
GND | G |
Now you can connect NodeMCU with microUSB cable to your computer and flash latest stable nodemcu-firmware. You can get custom build from nodemcu-build.com. Choose master build and select these modules node
, file
, gpio
, wifi
, net
, tmr
, cjson
. You'll have to wait few minutes, till the build is completed. In the meantime you can prepare esptool.py for flashing firmware, ESPlorer for uploading files and get code for light control.
Flashing firmware
Clone esptool repository or just download esptool.py
from https://github.com/themadinventor/esptool.
git clone https://github.com/themadinventor/esptool.git
cd esptool
And follow installation instructions.
When you download firmware (float version), flash it, by something like this:
sudo ./esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash 0x00000 /PATH/TO/FIRMWARE-ROM.bin
nodemcu-lightsd configuration
Clone nodemcu-lightsd repository https://github.com/ra100/nodemcu-lightsd
git clone https://github.com/ra100/nodemcu-lightsd.git
Open file in src/config.lua.default
and change it to suit you needs, you'll find explanations in README.md
file. Then save it as config.lua
.
This is example:
SSID = 'MyNetwork'
PASSWORD = 'StrongPassword42'
LIGHT = 'Lifx1' -- light name
MINDIST = 0.2 -- in meters
MAXDIST = 0.5 -- in meters
MAXRANGE = 0.2 -- in meters
SERVER = '192.168.0.10'
IP = '192.168.0.11'
GATEWAYIP = '192.168.0.1'
PORT = 8449
TRIG = 5
ECHO = 6
RANGE = MAXDIST - MINDIST
REFRESH = 200
MEASURE_TIMER = 2
FADETIME = 500
DEBUG = false
I recommend setting static IP address, then connecting after restart is much faster, you can control lights just after under 5s after restart.
Uploading files
Download ESPlorer from http://esp8266.ru/esplorer-latest/?f=ESPlorer.zip. Extract it, navigate to extracted folder and run Esplorer.jar
, on linux by running
sudo java -jar ESPlorer.jar
Choose communication port where NodeMCU is connected, e.g. /dev/ttyUSB0
, set baud rate to 9600. And upload all .lua files via Upload button, you'll have to do it one by one. After last file is uploaded, reset module (with Reset button in ESPlorer or RST on NodeMCU module).
Now you are done. And here is another video
Bonus task
Print nice case for you NodeMCU and HC-SR04. I've used this model Ultrasonic Sensor Housing and added mount so I could fit it on headboard.
Links to code and tools
- nodemcu-lightsd https://github.com/ra100/nodemcu-lightsd
- lightsd https://github.com/lopter/lightsd
- esptool.py https://github.com/themadinventor/esptool
- ESPlorer http://esp8266.ru/esplorer