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: Send a POST Request to IoT HTTPS Relay
IoT HTTPS Relay is a Google App Engine app I created to use HTTPS APIs since the ESP8266 does not support SSL.
To make a call using IoT HTTPS Relay send the following POST request from your ESP8266:
POST /twilio/Calls.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
The token and sid parameters are your Twilio’s Token and Account SID. The From number can only be your Twilio’s given number. The To number can be any number if you are paying for Twilio’s service, or it can be your verified number if you are using the free account. The Body value will be played when the To phone number answers the phone.
Step 3: Use NodeMcu to Make The Call
If your ESP has the NodeMcu firmware installed you can use the following script to make the calls:
-- Your access point's SSID and password local SSID = "xxxxxx" local SSID_PASSWORD = "xxxxxxx" -- configure ESP as a station wifi.setmode(wifi.STATION) wifi.sta.config(SSID,SSID_PASSWORD) wifi.sta.autoconnect(1) local TWILIO_ACCOUNT_SID = "xxxxxxxxxxxx" local TWILIO_TOKEN = "xxxxxxxxxxxx" local HOST = "iot-https-relay.appspot.com" local URI = "/twilio/Calls.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. local function make_call(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) make_call("18889866685","155534566809","Your house in on fire!") end end tmr.alarm(0,2000,1,check_wifi)