main.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. print("starting main.lua")
  2. local srv = nil
  3. local button_pin = 6
  4. local pwm_pin = 1
  5. local pwm_timer = 1
  6. local pwm_freq = 500
  7. local pwm_max_bright = 1023
  8. local config = nil -- sensitive data loaded at runtime
  9. function debounce (func)
  10. local last = 0
  11. local delay = 200000
  12. return function (...)
  13. local now = tmr.now()
  14. if now - last < delay then return end
  15. last = now
  16. return func(...)
  17. end
  18. end
  19. function on_change ()
  20. print('LYFT OFF!')
  21. --[[
  22. Since one of the legs in our 3-legged OAUTH is a peg leg,
  23. we opt to refresh on every request. ESPs can't keep time anyway.
  24. ]]
  25. http.post(
  26. 'https://api.lyft.com/oauth/token',
  27. 'Content-Type: application/json\r\n'
  28. .. 'Authorization: Basic ' .. config.base64_auth .. '\r\n',
  29. '{"grant_type": "refresh_token", "refresh_token": "' .. config.refresh_token .. '"}',
  30. function(code, data)
  31. debug_message('refresh status code: ' .. (code or 'nil'))
  32. debug_message('refresh resp data: ' .. (data or 'nil'))
  33. open_brace, close_brace = string.find(data, '^{.*}')
  34. json_data = cjson.decode(string.sub(data, open_brace, close_brace))
  35. for k,v in pairs(json_data) do print(k, v) end
  36. http.get(
  37. 'https://maker.ifttt.com/trigger/lyftoff/with/key/' .. config.ifttt_event_key,
  38. nil,
  39. function(code, data)
  40. debug_message('status code: ' .. (code or 'nil'))
  41. debug_message('resp data: ' .. (data or 'nil'))
  42. end
  43. )
  44. end
  45. )
  46. end
  47. function start_server()
  48. debug_message('server start')
  49. debug_message(srv)
  50. if srv then
  51. srv = nil
  52. end
  53. srv = net.createServer(net.TCP, 30)
  54. srv:listen(80, connect)
  55. debug_message(srv)
  56. end
  57. function stop_server()
  58. debug_message('server stop')
  59. debug_message(srv)
  60. if srv then
  61. srv:close()
  62. srv = nil
  63. end
  64. debug_message(srv)
  65. end
  66. function connect(sock)
  67. sock:on('receive', function(sck, payload)
  68. conn:send('HTTP/1.1 200 OK\r\n\r\n' .. 'Hello world')
  69. end)
  70. sock:on('send', function(sck)
  71. sck:close()
  72. end)
  73. end
  74. function on_start()
  75. debug_message('on_start')
  76. debug_message('on_start: reading config')
  77. file.open('config.json')
  78. config = cjson.decode(file.read())
  79. file.close()
  80. debug_message('on_start: enable pwm')
  81. pwm.setup(pwm_pin, pwm_freq, 0)
  82. pwm.start(pwm_pin)
  83. debug_message('on_start: connecting')
  84. wifi.sta.config(config.ssid, config.pwd)
  85. end
  86. function pwm_fadein()
  87. local brightness = pwm.getduty(pwm_pin)
  88. if brightness >= pwm_max_bright then
  89. tmr.unregister(pwm_timer)
  90. else
  91. pwm.setduty(pwm_pin, brightness + 1)
  92. tmr.alarm(pwm_timer, 2, tmr.ALARM_SINGLE, pwm_fadein)
  93. end
  94. end
  95. function pwm_fadeout()
  96. local brightness = pwm.getduty(pwm_pin)
  97. if brightness <= 0 then
  98. tmr.unregister(pwm_timer)
  99. else
  100. pwm.setduty(pwm_pin, brightness - 3)
  101. tmr.alarm(pwm_timer, 2, tmr.ALARM_SINGLE, pwm_fadeout)
  102. end
  103. end
  104. on_start()
  105. start_server()
  106. gpio.mode(button_pin, gpio.INT)
  107. gpio.trig(button_pin, 'both', debounce(on_change))