Last active
April 2, 2016 21:47
-
-
Save AllAboutEE/6fe3ac554caaa2e3b450 to your computer and use it in GitHub Desktop.
ESP8266 NodeMcu SMS (Text Message)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Your access point's SSID and password | |
| local SSID = "xxxxxx" | |
| local SSID_PASSWORD = "xxxxxx" | |
| -- configure ESP as a station | |
| wifi.setmode(wifi.STATION) | |
| wifi.sta.config(SSID,SSID_PASSWORD) | |
| wifi.sta.autoconnect(1) | |
| local TWILIO_ACCOUNT_SID = "xxxxxx" | |
| local TWILIO_TOKEN = "xxxxxx" | |
| local HOST = "iot-https-relay.appspot.com" | |
| local URI = "/twilio/Messages.json" | |
| function build_post_request(host, uri, data_table) | |
| local data = "" | |
| for param,value in pairs(data_table) do | |
| data = data .. param.."="..value.."&" | |
| end | |
| request = "POST "..uri.." HTTP/1.1\r\n".. | |
| "Host: "..host.."\r\n".. | |
| "Connection: close\r\n".. | |
| "Content-Type: application/x-www-form-urlencoded\r\n".. | |
| "Content-Length: "..string.len(data).."\r\n".. | |
| "\r\n".. | |
| data | |
| print(request) | |
| return request | |
| end | |
| local function display(sck,response) | |
| print(response) | |
| end | |
| -- When using send_sms: the "from" number HAS to be your twilio number. | |
| -- If you have a free twilio account the "to" number HAS to be your twilio verified number. | |
| -- The numbers MUST include the country code. | |
| -- DO NOT add the "+" sign. | |
| local function send_sms(from,to,body) | |
| local data = { | |
| sid = TWILIO_ACCOUNT_SID, | |
| token = TWILIO_TOKEN, | |
| Body = string.gsub(body," ","+"), | |
| From = from, | |
| To = to | |
| } | |
| socket = net.createConnection(net.TCP,0) | |
| socket:on("receive",display) | |
| socket:connect(80,HOST) | |
| socket:on("connection",function(sck) | |
| local post_request = build_post_request(HOST,URI,data) | |
| sck:send(post_request) | |
| end) | |
| end | |
| function check_wifi() | |
| local ip = wifi.sta.getip() | |
| if(ip==nil) then | |
| print("Connecting...") | |
| else | |
| tmr.stop(0) | |
| print("Connected to AP!") | |
| print(ip) | |
| send_sms("15551234567","12223456789","Hello from your ESP8266") | |
| end | |
| end | |
| tmr.alarm(0,7000,1,check_wifi) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Miguel ......... I've been looking for years to bring my antiquated alarm system into the 21st century! The Nodemcu has made that possible without the expense of Arduino or Raspberry Pi and leaving everything running 24/7. When the alarm fires and sends 12v, 20 seconds later I have a message on the cell.
Thanks for the lua code and relay,
[http://mlaine.net/alarm-sms.jpg]
Mike