garage_door_opener.lua 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. -- garage_door_opener.lua
  2. -- Part of nodemcu-httpserver, example.
  3. -- Author: Marcos Kirsch
  4. local function pushTheButton(connection, pin)
  5. -- push the button!
  6. -- The hardware in this case is a Wemos D1 Pro with two relay shields.
  7. -- The first relay is controlled with D1.
  8. -- The second one was modified to be controlled with D2.
  9. gpio.write(pin, gpio.HIGH)
  10. gpio.mode(pin, gpio.OUTPUT, gpio.FLOAT)
  11. tmr.delay(300000) -- in microseconds
  12. gpio.mode(pin, gpio.INPUT, gpio.FLOAT)
  13. gpio.write(pin, gpio.LOW)
  14. -- Send back JSON response.
  15. connection:send("HTTP/1.0 200 OK\r\nContent-Type: application/json\r\nCache-Control: private, no-store\r\n\r\n")
  16. connection:send('{"error":0, "message":"OK"}')
  17. end
  18. return function (connection, req, args)
  19. print('Garage door button was pressed!', args.door)
  20. if args.door == "1" then pushTheButton(connection, 1)
  21. elseif args.door == "2" then pushTheButton(connection, 2)
  22. else
  23. connection:send("HTTP/1.0 400 OK\r\nContent-Type: application/json\r\nCache-Control: private, no-store\r\n\r\n")
  24. connection:send('{"error":-1, "message":"Bad door"}')
  25. end
  26. end