main.lua 4.0 KB

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