garage_door_opener.lua 1.2 KB

123456789101112131415161718192021222324252627282930
  1. -- garage_door_opener.lua
  2. -- Part of nodemcu-httpserver, example.
  3. -- Author: Marcos Kirsch
  4. local function pushTheButton(connection, pin)
  5. -- Redirect the user back to the static page where the garage door opener buttons are.
  6. connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\Cache-Control: private, no-store\r\n\r\n")
  7. connection:send('<script type="text/javascript">window.location.replace("/garage_door_opener.html");</script>')
  8. -- push the button!
  9. -- Note that the relays connected to the garage door opener are wired
  10. -- to close when the GPIO pin is low. This way they don't activate when
  11. -- the chip is reset and the GPIO pins are in input mode.
  12. gpio.write(pin, gpio.LOW)
  13. gpio.mode(pin, gpio.OUTPUT)
  14. gpio.write(pin, gpio.LOW)
  15. tmr.delay(300000) -- in microseconds
  16. gpio.write(pin, gpio.HIGH)
  17. gpio.mode(pin, gpio.INPUT)
  18. end
  19. return function (connection, args)
  20. print('Garage door button was pressed!')
  21. print('Door', args.door)
  22. if args.door == "1" then pushTheButton(connection, 3) -- GPIO0
  23. elseif args.door == "2" then pushTheButton(connection, 4) -- GPIO2
  24. else dofile("httpserver-error.lc")(connection, {code = 400}) end -- Bad Request
  25. end