garage_door_opener.lua 1.2 KB

1234567891011121314151617181920212223242526272829303132
  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. connection:send('{"error":0, "message":"OK"}')
  18. end
  19. return function (connection, req, args)
  20. print('Garage door button was pressed!', args.door)
  21. if args.door == "1" then pushTheButton(connection, 1) -- GPIO1
  22. elseif args.door == "2" then pushTheButton(connection, 2) -- GPIO2
  23. else
  24. connection:send("HTTP/1.0 400 OK\r\nContent-Type: application/json\r\nCache-Control: private, no-store\r\n\r\n")
  25. connection:send('{"error":-1, "message":"Bad door"}')
  26. end
  27. end