main.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 jsonify(payload)
  20. open_brace, close_brace = string.find(payload, '^{.*}')
  21. return cjson.decode(string.sub(payload, open_brace, close_brace))
  22. end
  23. function on_change()
  24. debug_message('on_change')
  25. --[[
  26. Since one of the legs in our 3-legged OAUTH is a peg leg,
  27. we opt to refresh on every request. ESPs can barely keep time anyway.
  28. ]]
  29. http.post(
  30. 'https://api.lyft.com/oauth/token',
  31. 'Content-Type: application/json\r\n'
  32. .. 'Authorization: Basic ' .. config.base64_auth .. '\r\n',
  33. '{"grant_type": "refresh_token", "refresh_token": "' .. config.refresh_token .. '"}',
  34. function(code, data)
  35. debug_message('token refresh status code: ' .. (code or 'nil'))
  36. debug_message('token refresh resp data: ' .. (data or 'nil'))
  37. debug_message('Requesting ride')
  38. http.post(
  39. 'https://api.lyft.com/v1/rides',
  40. 'Authorization: Bearer ' .. 'SANDBOX-gAAAAABW0edPpkkTOzW3w0vo5DE-bgHGD53Hz-kehlw-FWMQw72YqmbOPYlenvnEtRLg-Ru025J0zHhPR443buu6Wr6-ipcChddXUfgeeYZUu5ZFZpjicEiwdFiBl0Y6lklBVQTMtc2BiosI9nC2SFkAuL28PSIAQpgCISDKNdYopoaQjg5nYGIQ210isODJ7o451tkeUJE5vv2Pmq-nxfKlTQK5LPwFvQ-MHUBRHZezdPBtXA0uQ-xamxPuz7eWbNUJqbeVsqZlsVxcN21YYC5I4nolkuUlnmfAVfMFJcam6DrQOVVKz6E=' .. '\r\n'
  41. .. 'Content-Type: application/json\r\n',
  42. '{"ride_type" : "lyft", "origin" : {"lat" : 37.804427, "lng" : -122.429473 } }',
  43. function(code, data)
  44. debug_message('ride request status code: ' .. (code or 'nil'))
  45. debug_message('ride request resp data: ' .. (data or 'nil'))
  46. debug_message("Sending IFTTT notification")
  47. http.get(
  48. 'https://maker.ifttt.com/trigger/lyftoff/with/key/' .. config.ifttt_event_key,
  49. nil,
  50. function(code, data)
  51. debug_message('ifttt status code: ' .. (code or 'nil'))
  52. debug_message('ifttt resp data: ' .. (data or 'nil'))
  53. print("LYFT OFF!")
  54. end
  55. )
  56. end
  57. )
  58. end
  59. )
  60. end
  61. function start_server()
  62. debug_message('server start')
  63. debug_message(srv)
  64. if srv then
  65. srv = nil
  66. end
  67. srv = net.createServer(net.TCP, 30)
  68. srv:listen(80, connect)
  69. debug_message(srv)
  70. end
  71. function stop_server()
  72. debug_message('server stop')
  73. debug_message(srv)
  74. if srv then
  75. srv:close()
  76. srv = nil
  77. end
  78. debug_message(srv)
  79. end
  80. function connect(sock)
  81. sock:on('receive', function(sck, payload)
  82. conn:send('HTTP/1.1 200 OK\r\n\r\n' .. 'Hello world')
  83. end)
  84. sock:on('send', function(sck)
  85. sck:close()
  86. end)
  87. end
  88. function on_start()
  89. debug_message('on_start')
  90. debug_message('on_start: reading config')
  91. file.open('config.json')
  92. config = cjson.decode(file.read())
  93. file.close()
  94. debug_message('on_start: enable pwm')
  95. pwm.setup(pwm_pin, pwm_freq, 0)
  96. pwm.start(pwm_pin)
  97. debug_message('on_start: connecting')
  98. wifi.sta.config(config.ssid, config.pwd)
  99. end
  100. function pwm_fadein()
  101. local brightness = pwm.getduty(pwm_pin)
  102. if brightness >= pwm_max_bright then
  103. tmr.unregister(pwm_timer)
  104. else
  105. pwm.setduty(pwm_pin, brightness + 1)
  106. tmr.alarm(pwm_timer, 2, tmr.ALARM_SINGLE, pwm_fadein)
  107. end
  108. end
  109. function pwm_fadeout()
  110. local brightness = pwm.getduty(pwm_pin)
  111. if brightness <= 0 then
  112. tmr.unregister(pwm_timer)
  113. else
  114. pwm.setduty(pwm_pin, brightness - 3)
  115. tmr.alarm(pwm_timer, 2, tmr.ALARM_SINGLE, pwm_fadeout)
  116. end
  117. end
  118. on_start()
  119. start_server()
  120. gpio.mode(button_pin, gpio.INT)
  121. gpio.trig(button_pin, 'down', debounce(on_change))