Step 1: Create a Twilio Account
Head over to Twilio.com and create a free or paid account. In your account dashboard find your Account SID and Token and save them as will need it in the code.
Step 2: Make a POST Request to IoT HTTPS Relay
IoT HTTPS Relay is a Google App Engine app I created for relaying information between HTTP and HTTPS. Since Twilio does not allow access to their APIs via HTTP, and since the ESP module does not support HTTPS, IoT HTTPS Relay receives the HTTP request from the ESP8266 and changes it to an HTTPS.
The POST request to send a text message is the following:
POST /twilio/Messages.json HTTP/1.1 Host: iot-https-relay.appspot.com Connection: close Content-Type: application/x-www-form-urlencoded Content-Length: xxx token=xxxxxx&From=15558689998&To=19998675584&Body=myURLEncodedMessage&sid=xxxxxx
Where the “token” and “sid” parameters are your Twilio’s account Account SID and Token values from step 1. The “From” and “To” parameters are your Twilio’s given phone number, and the destination phone number respectively. Please remember that if you a free Twilio account the “To” phone number can only be your Twilio verified phone.
The “Body” parameter is the body of the text messages.
Step 3: NodeMcu Text Message Code
If your ESP8266 has the NodeMcu firmware installed you can use the script below to automate the SMS process.
-- 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,2000,1,check_wifi)