Control LIFX light brightness with NodeMCU, hc-sr04 range sensor and lightsd server.
This is clone of https://github.com/ra100/nodemcu-lightsd with lightsd switched for lifx rest server.
-
LIFX bulbs
-
NodeMCU module
- firmware = 1.5.4 - float, with modules: node, file, gpio, wifi, net, http, tmr - http://nodemcu-build.com/
-
HC-SR04 ultrasonic sensor
-
LIFX REST server - https://github.com/ra100/lifx-rest-web
config.lua.default
- default configuration, rename toconfig.lua
and change settingscontrol.lua
- main code to bridge sensor and serverhcsr04.lua
- modified lua module from https://github.com/sza2/node_hcsr04init.lua
- init script to compile .lua to .lc and startwifi.lua
andcontrol.lc
lifx.lua
- module for communication withlifx-rest
server via http callswifi.lua
- connects to WiFi network configured inconfig.lua
-
Connect HC-SR04 to NodeMCU. Default pins are: TRIG - 5, ECHO - 6, Gnd - GND, VCC - VU (you need 5V).
-
Edit configuration in
config.lua.default
and rename it toconfig.lua
. Configuration options:SSID
[required] - network SSIDPASSWORD
[required] - network passwordLIGHT
[required] - name of light you want to controlMINDIST
[required] - distance below which light should turn off [in meters]MAXDIST
[required] - distance where light brightness should turn to 100% [in meters]MAXRANGE
[required] - range above MAXDIST where sensor should detect movement (and set brightness to 100%) [in meters]BASEURL
[required] - full url where to find APIhttp://10.0.0.5:3000/api/v1
IP
[optional] - IP address if you don't want to let DHCP assign address, makes reconnect much fasterGATEWAYIP
[optinoal] - if you set IP, set also gatewayTRIG
[required] - pin number where you connecttrig
wire from hcsr04ECHO
[required] - pin number where you connectecho
wire from hcsr04REFRESH
[required] - how often should script check for distance
-
Upload files to NodeMCU.
-
Restart.
-
Wave your hand in front of range sensor.
Init script connect to wifi and checks if connection is established. Then creates TCP connection to lightsd server. After that distance measurement starts. When is measured distance in range (from 0 to MAXDIST + MAXRANGE) value is calculated 0 - 100 which represents brightness level. That is sent to lightsd server via jsonrpc. Before first value change, current light state is requested and saved (to preserve hue, saturation, warmth). If light is Off, next command is to turn the light On and after that brightness value changes. New brightness value can not be sent before confirmation packet from lightsd is received. While NodeMCU is communicating with lightsd server, distance measurement is on hold, this prevents sending too much requests to server and responses which could exhaust memory.
- optimize memory usage
- light tags/groups support
GNU GPLv3