main.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 .. '"}\r\n\r\n',
  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. -- json_data = jsonify(data)
  39. debug_message('Requesting ride')
  40. http.post(
  41. 'https://api.lyft.com/v1/rides',
  42. 'Authorization: Bearer ' .. 'SANDBOX-gAAAAABW0mvDUr9fc7Vgw6GF7Jv7ps5KRWw0sqd4jrg-p-tetlI5iL2ulbctnlV6y58B2mvOUrR7Huk13kQ2uhqrwV9vfK_BLTQ7vLynJSD0Y9Uw1UgpIyZeDV2q0RQ7Sxtf0-qTPgaXQyFLmUZDW-teuE6KTjv6srJ2naEMGe-QtAJnpLdrnw9ZDsB0RJwpK9Heq6Vbt3j4aXvXtacvprZ1S1oU8JIZ7IWbcoTVu4GkcvxFq6OFvRauUR1nTorQOk49h-bx_iKxd_pp2G7ZgyYLwFsPojpied_y17RLhGntMYQKH0yZvo0=' .. '\r\n'
  43. .. 'Content-Type: application/json\r\n',
  44. '{"ride_type" : "lyft", "origin" : {"lat" : 37.804427, "lng" : -122.429473 } }\r\n\r\n',
  45. function(code, data)
  46. debug_message('ride request status code: ' .. (code or 'nil'))
  47. debug_message('ride request resp data: ' .. (data or 'nil'))
  48. debug_message("Sending IFTTT notification")
  49. ip = wifi.sta.getip()
  50. http.post(
  51. 'https://maker.ifttt.com/trigger/lyftoff/with/key/' .. config.ifttt_event_key,
  52. 'Content-Type: application/json\r\n',
  53. '{"value1": "' .. ip .. '"}\r\n\r\n',
  54. function(code, data)
  55. debug_message('ifttt status code: ' .. (code or 'nil'))
  56. debug_message('ifttt resp data: ' .. (data or 'nil'))
  57. print("LYFT OFF!")
  58. end
  59. )
  60. end
  61. )
  62. -- end
  63. -- )
  64. end
  65. function start_server()
  66. debug_message('server start')
  67. debug_message(srv)
  68. if srv then
  69. srv = nil
  70. end
  71. srv = net.createServer(net.TCP, 30)
  72. srv:listen(80, connect)
  73. debug_message(srv)
  74. end
  75. function stop_server()
  76. debug_message('server stop')
  77. debug_message(srv)
  78. if srv then
  79. srv:close()
  80. srv = nil
  81. end
  82. debug_message(srv)
  83. end
  84. function connect(sock)
  85. sock:on('receive', function(sock, payload)
  86. if string.match(payload, 'fadeinplease') then
  87. pwm_fadein()
  88. end
  89. if string.match(payload, 'fadeoutplease') then
  90. pwm_fadeout()
  91. end
  92. end)
  93. sock:on('sent', function(sck)
  94. sck:close()
  95. end)
  96. end
  97. function on_start()
  98. debug_message('on_start')
  99. debug_message('on_start: reading config')
  100. file.open('config.json')
  101. config = cjson.decode(file.read())
  102. file.close()
  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. debug_message('on_start: enable pwm')
  108. pwm.setup(pwm_pin, pwm_freq, 0)
  109. pwm.start(pwm_pin)
  110. pwm_fadein()
  111. end
  112. function pwm_fadein()
  113. local brightness = pwm.getduty(pwm_pin)
  114. if brightness >= pwm_max_bright then
  115. tmr.unregister(pwm_timer)
  116. else
  117. pwm.setduty(pwm_pin, brightness + 1)
  118. tmr.alarm(pwm_timer, 2, tmr.ALARM_SINGLE, pwm_fadein)
  119. end
  120. end
  121. function pwm_fadeout()
  122. local brightness = pwm.getduty(pwm_pin)
  123. if brightness <= 0 then
  124. tmr.unregister(pwm_timer)
  125. else
  126. pwm.setduty(pwm_pin, brightness - 3)
  127. tmr.alarm(pwm_timer, 2, tmr.ALARM_SINGLE, pwm_fadeout)
  128. end
  129. end
  130. on_start()
  131. gpio.mode(button_pin, gpio.INT)
  132. gpio.trig(button_pin, 'down', debounce(on_change))