main.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 onChange ()
  20. print('LYFT OFF!')
  21. http.post(
  22. 'https://api.lyft.com/oauth/token',
  23. 'Content-Type: application/json',
  24. '{"grant_type": "refresh_token", "refresh_token": "' .. config.refresh_token .. '"}',
  25. function(code, data)
  26. debug_message('refresh status code: ' .. (code or 'nil'))
  27. debug_message('refresh resp data: ' .. (data or 'nil'))
  28. openBrace, closeBrace = string.find(data, '^{.*}')
  29. json_data = cjson.decode(string.sub(data, openBrace, closeBrace))
  30. for k,v in pairs(json_data) do print(k, v) end
  31. http.get(
  32. 'https://maker.ifttt.com/trigger/lyftoff/with/key/' .. config.ifttt_event_key,
  33. nil,
  34. function(code, data)
  35. debug_message('status code: ' .. (code or 'nil'))
  36. debug_message('resp data: ' .. (data or 'nil'))
  37. end
  38. )
  39. end
  40. )
  41. end
  42. function startServer()
  43. debug_message('server start')
  44. debug_message(srv)
  45. if srv then
  46. srv = nil
  47. end
  48. srv = net.createServer(net.TCP, 30)
  49. srv:listen(80, connect)
  50. debug_message(srv)
  51. end
  52. function stopServer()
  53. debug_message('server stop')
  54. debug_message(srv)
  55. if srv then
  56. srv:close()
  57. srv = nil
  58. end
  59. debug_message(srv)
  60. end
  61. function connect(sock)
  62. sock:on('receive', function(sck, payload)
  63. conn:send('HTTP/1.1 200 OK\r\n\r\n' .. 'Hello world')
  64. end)
  65. sock:on('send', function(sck)
  66. sck:close()
  67. end)
  68. end
  69. function onStart()
  70. debug_message('onStart')
  71. debug_message('onStart: reading config')
  72. file.open('config.json')
  73. config = cjson.decode(file.read())
  74. file.close()
  75. debug_message('onStart: enable pwm')
  76. pwm.setup(pwm_pin, pwm_freq, 0)
  77. pwm.start(pwm_pin)
  78. debug_message('onStart: connecting')
  79. wifi.sta.config(config.ssid, config.pwd)
  80. end
  81. function pwm_fadein()
  82. local brightness = pwm.getduty(pwm_pin)
  83. if brightness >= pwm_max_bright then
  84. tmr.unregister(pwm_timer)
  85. else
  86. pwm.setduty(pwm_pin, brightness + 1)
  87. tmr.alarm(pwm_timer, 2, tmr.ALARM_SINGLE, pwm_fadein)
  88. end
  89. end
  90. function pwm_fadeout()
  91. local brightness = pwm.getduty(pwm_pin)
  92. if brightness <= 0 then
  93. tmr.unregister(pwm_timer)
  94. else
  95. pwm.setduty(pwm_pin, brightness - 3)
  96. tmr.alarm(pwm_timer, 2, tmr.ALARM_SINGLE, pwm_fadeout)
  97. end
  98. end
  99. onStart()
  100. startServer()
  101. gpio.mode(button_pin, gpio.INT)
  102. gpio.trig(button_pin, 'both', debounce(onChange))