send_text_message.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. --[[
  2. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  3. documentation files (the "Software"), to deal in the Software without restriction,
  4. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  5. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  6. furnished to do so, subject to the following conditions:
  7. The above copyright notice and this permission notice shall be
  8. included in all copies or substantial portions of the Software.
  9. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  10. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  11. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  12. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  13. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  14. IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15. ]]--
  16. -- Your access point's SSID and password
  17. local SSID = "xxxxxx"
  18. local SSID_PASSWORD = "xxxxxx"
  19. -- configure ESP as a station
  20. wifi.setmode(wifi.STATION)
  21. wifi.sta.config(SSID,SSID_PASSWORD)
  22. wifi.sta.autoconnect(1)
  23. local TWILIO_ACCOUNT_SID = "xxxxxx"
  24. local TWILIO_TOKEN = "xxxxxx"
  25. local HOST = "iot-https-relay.appspot.com" -- visit http://iot-https-relay.appspot.com/ to learn more about this service
  26. -- Please be sure to understand the security issues of using this relay app and use at your own risk.
  27. local URI = "/twilio/Messages.json"
  28. local wifiTimer = tmr.create()
  29. local function build_post_request(host, uri, data_table)
  30. local data = ""
  31. for param,value in pairs(data_table) do
  32. data = data .. param.."="..value.."&"
  33. end
  34. local request = "POST "..uri.." HTTP/1.1\r\n"..
  35. "Host: "..host.."\r\n"..
  36. "Connection: close\r\n"..
  37. "Content-Type: application/x-www-form-urlencoded\r\n"..
  38. "Content-Length: "..string.len(data).."\r\n"..
  39. "\r\n"..
  40. data
  41. print(request)
  42. return request
  43. end
  44. local function display(socket, response) -- luacheck: no unused
  45. print(response)
  46. end
  47. -- When using send_sms: the "from" number HAS to be your twilio number.
  48. -- If you have a free twilio account the "to" number HAS to be your twilio verified number.
  49. local function send_sms(from,to,body)
  50. local data = {
  51. sid = TWILIO_ACCOUNT_SID,
  52. token = TWILIO_TOKEN,
  53. Body = string.gsub(body," ","+"),
  54. From = from,
  55. To = to
  56. }
  57. local socket = net.createConnection(net.TCP,0)
  58. socket:on("receive",display)
  59. socket:connect(80,HOST)
  60. socket:on("connection",function(sck)
  61. local post_request = build_post_request(HOST,URI,data)
  62. sck:send(post_request)
  63. end)
  64. end
  65. local function check_wifi()
  66. local ip = wifi.sta.getip()
  67. if(ip==nil) then
  68. print("Connecting...")
  69. else
  70. wifiTimer.stop()
  71. print("Connected to AP!")
  72. print(ip)
  73. -- send a text message with the text "Hello from your esp8266"
  74. send_sms("15558889944","15559998845","Hello from your ESP8266")
  75. end
  76. end
  77. wifiTimer.alarm(7000, tmr.ALARM_AUTO, check_wifi)