NodeMCU + LIFX cover
Wed, 03/09/2016 - 15:30
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](http://www.lifx.com/products/color-1000) bulb - $60 - [Raspberry Pi 2/3](https://www.pi-supply.com/product/raspberry-pi-3-model-b-newest-version/?v=928568b84963) ~$35 - power supply (5V microUSB) - [NodeMCU v3](http://www.aliexpress.com/af/nodemcu-v3.html?ltype=wholesale&d=y&origin=n&isViewCP=y&catId=0&initiative_id=SB_20160310035331&SearchText=nodemcu+v3) ~$5 or [D1 mini](http://www.aliexpress.com/store/product/D1-mini-Mini-NodeMcu-4M-bytes-Lua-WIFI-Internet-of-Things-development-board-based-ESP8266/1331105_32529101036.htm) ~$4 - power supply (5V microUSB) or battery - [HC-SR04 ultrasonic range sensor](http://www.aliexpress.com/af/hc%25252dsr04.html?ltype=wholesale&d=y&origin=n&isViewCP=y&catId=0&initiative_id=SB_20160310035753&SearchText=hc-sr04) ~$1 - [Dupont cables](http://www.aliexpress.com/af/dupont-cable.html?ltype=wholesale&d=y&origin=n&isViewCP=y&catId=0&initiative_id=AS_20160310035754&SearchText=dupont+cable) ~$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`. ```bash sudo apt-get install git cmake libevent-dev ``` Then download and compile lightsd. ```bash 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 ```bash systemctl status lightsd ``` My location is `/usr/local/lib/systemd/system/lightsd.service`, I modified it to look like this: ```ini [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 ```nginx 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`: ```nginx server { listen 8449; proxy_pass 127.0.0.1:8447; } ``` Restart lightsd and nginx. ```bash 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](http://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](https://github.com/themadinventor/esptool). ```bash 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: ```bash 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](https://github.com/ra100/nodemcu-lightsd) ```bash 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: ```lua 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](http://esp8266.ru/esplorer-latest/?f=ESPlorer.zip). Extract it, navigate to extracted folder and run `Esplorer.jar`, on linux by running ```bash 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](http://www.thingiverse.com/thing:1170335) and added mount so I could fit it on headboard. #### Links to code and tools - nodemcu-lightsd [https://github.com/ra100/nodemcu-lightsd](https://github.com/ra100/nodemcu-lightsd) - lightsd [https://github.com/lopter/lightsd](https://github.com/lopter/lightsd) - esptool.py [https://github.com/themadinventor/esptool](https://github.com/themadinventor/esptool) - ESPlorer [http://esp8266.ru/esplorer](http://esp8266.ru/esplorer)

Gallery