nodeMCU flow control for webserver with motion sensor
Posted: Wed Nov 11, 2015 8:33 pm
I'm using an esp8266-03 to make a light switch with a web interface and a motion sensor. I have nodeMCU running on the esp8266 and I modified the
nodemcu_firmware/lua_examples/http_server.lua
file to serve a page that turns 4 pins (each connected to a separate relay) on and off. So I have
wifi.setmode(wifi.STATION);
wifi.sta.config(SSID, SSID_PASSWORD);
wifi.sta.autoconnect(1);
wait_for_wifi_conn();
svr=net.createServer(net.TCP,30);
svr:listen(80,connect);
and a callback function that gets called when a client connects to the esp module, reads the state of the pins and serves the web page. That part is working beautifully.
Now I want to add a motion sensor (with a photo cell) to the whole thing. So I'll be using a new pin to read input from the motion sensor. How do I actually do that? Read the pin in an infinite loop, and sleep for e.g. 100 ms between reads? How would this interfere with srv:listen()? On a real computer, with an operating system, these two (the web server and the motion sensor loop) should be in different threads. But on the esp, what's the right way to do this?
Thanks!
nodemcu_firmware/lua_examples/http_server.lua
file to serve a page that turns 4 pins (each connected to a separate relay) on and off. So I have
wifi.setmode(wifi.STATION);
wifi.sta.config(SSID, SSID_PASSWORD);
wifi.sta.autoconnect(1);
wait_for_wifi_conn();
svr=net.createServer(net.TCP,30);
svr:listen(80,connect);
and a callback function that gets called when a client connects to the esp module, reads the state of the pins and serves the web page. That part is working beautifully.
Now I want to add a motion sensor (with a photo cell) to the whole thing. So I'll be using a new pin to read input from the motion sensor. How do I actually do that? Read the pin in an infinite loop, and sleep for e.g. 100 ms between reads? How would this interfere with srv:listen()? On a real computer, with an operating system, these two (the web server and the motion sensor loop) should be in different threads. But on the esp, what's the right way to do this?
Thanks!