send_text_message.lua 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. function build_post_request(host, uri, data_table)
  29. local data = ""
  30. for param,value in pairs(data_table) do
  31. data = data .. param.."="..value.."&"
  32. end
  33. request = "POST "..uri.." HTTP/1.1\r\n"..
  34. "Host: "..host.."\r\n"..
  35. "Connection: close\r\n"..
  36. "Content-Type: application/x-www-form-urlencoded\r\n"..
  37. "Content-Length: "..string.len(data).."\r\n"..
  38. "\r\n"..
  39. data
  40. print(request)
  41. return request
  42. end
  43. local function display(sck,response)
  44. print(response)
  45. end
  46. -- When using send_sms: the "from" number HAS to be your twilio number.
  47. -- If you have a free twilio account the "to" number HAS to be your twilio verified number.
  48. local function send_sms(from,to,body)
  49. local data = {
  50. sid = TWILIO_ACCOUNT_SID,
  51. token = TWILIO_TOKEN,
  52. Body = string.gsub(body," ","+"),
  53. From = from,
  54. To = to
  55. }
  56. socket = net.createConnection(net.TCP,0)
  57. socket:on("receive",display)
  58. socket:connect(80,HOST)
  59. socket:on("connection",function(sck)
  60. local post_request = build_post_request(HOST,URI,data)
  61. sck:send(post_request)
  62. end)
  63. end
  64. function check_wifi()
  65. local ip = wifi.sta.getip()
  66. if(ip==nil) then
  67. print("Connecting...")
  68. else
  69. tmr.stop(0)
  70. print("Connected to AP!")
  71. print(ip)
  72. -- send a text message with the text "Hello from your esp8266"
  73. send_sms("15558889944","15559998845","Hello from your ESP8266")
  74. end
  75. end
  76. tmr.alarm(0,7000,1,check_wifi)