garage_door_opener.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  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. -- Note that the relays connected to the garage door opener are wired
  7. -- to close when the GPIO pin is low. This way they don't activate when
  8. -- the chip is reset and the GPIO pins are in input mode.
  9. gpio.write(pin, gpio.LOW)
  10. gpio.mode(pin, gpio.OUTPUT)
  11. gpio.write(pin, gpio.LOW)
  12. tmr.delay(300000) -- in microseconds
  13. gpio.write(pin, gpio.HIGH)
  14. gpio.mode(pin, gpio.INPUT)
  15. -- Send back JSON response.
  16. connection:send("HTTP/1.0 200 OK\r\nContent-Type: application/json\r\nCache-Control: private, no-store\r\n\r\n")
  17. coroutine.yield()
  18. connection:send('{"error":0, "message":"OK"}')
  19. end
  20. return function (connection, req, args)
  21. print('Garage door button was pressed!', args.door)
  22. if args.door == "1" then pushTheButton(connection, 1) -- GPIO1
  23. elseif args.door == "2" then pushTheButton(connection, 2) -- GPIO2
  24. else
  25. connection:send("HTTP/1.0 400 OK\r\nContent-Type: application/json\r\nCache-Control: private, no-store\r\n\r\n")
  26. coroutine.yield()
  27. connection:send('{"error":-1, "message":"Bad door"}')
  28. end
  29. end