Simple code to receive SMS from Twilio

I know this is Basic,
but I thought you could use it to do something besides blink the LED!

`Agent:
//https://agent.electricimp.com/xxxxxxxx
function mywebserver(request,res) {
t<-http.urldecode(request.body);
if(t.From=="+18055551212")
device.send(“sendbody”,t.Body);
res.send(200,“okay”);
}
http.onrequest(mywebserver);

Device:
imp.configure(“GetSMS”, [], []);
agent.on(“sendbody”,function(value){
imp.onidle(function(){server.sleepfor(5);}); //blinks for 60 secs
});
`

Whoah, thanks for posting this. While I’ve used SMS services it never occurred to me how easy this is now - praise the agents! Now that I have deployed electric imps in my summer house I can just send SMS to switch heating etc, and that for much lower price than the readymade things you can buy.

It’s just as easy to go the opposite direction.
Let me know if you want help.

Anyone tried it? Twilio account is free.

Twilio account is free.
??? http://www.twilio.com/sms/pricing

Sssshhhh don’t tell anyone
https://www.twilio.com/try-twilio

sorry, I’ve tried but this is what I get:
SMS is unavailable for this phone number.

You clicked Get Started? The big red button.

yes.
I will try to get an U.S. number by using a proxy.

ok, I have put my agent url in the SMS Request URL of my US Twillio phonenumber and sent it an SMS.

…Nothing happend.

Did you validate the number you are calling/texting from?

yes, but I edited your code to log any incoming data

I found it. I have to send the SMS from my US mobile number to the US Twillo number, then it works

Even better. I was able to validate my national number as well. now it should work.
Thanks.

Hi all,
I saw you guy having fun to blink the LED with sms.
Any example for sending alarm sms when imp trigger?

This is all of the code that I use for my security system, including Twilio and talking to my iOS app Pitchfork. The Twilio code is in the agent.nut and is marked with comments.

Where?

I think this is it:
https://github.com/joel-wehr/electric_imp_security_system/blob/master/agent.nut

Thank guys,

It help, just need some time to figure out and match with the local SMS service provider.

This is what I use to send SMS function. Just to share with other who plan to use in Singapore.

// Agent code
const user_name = "name"
const password = "password"
const sending_number = “phone number” // your phone no goes here exp123456;654321;65739345
const message1 = “message” // massage max 900 char
const message_type = “1” //standard English
const sender_id = “send id”

function send_sms(){
local isms_url = "http://www.isms.com.my/isms_send.php?";
local body = http.urlencode({un=user_name, pwd=password, dstno=sending_number, msg=message1, type=message_type, sendid=sender_id});
local req = http.post(isms_url, {}, body);
local res = req.sendsync();
if(res.statuscode != 2000){
server.log("error sending message: " + res.body)
}
}

// call function
send_sms()

//Enjoy :slight_smile:

I’ve been working with Twilio for ~ a month, and electric imp for a bit less. I wired up a simple LED setup based on Agent for imp Web-Controlled LEDs by: Jim Lindblom [SparkFun Electronics, November 1, 2013].

Right now I’m only interested in receiving SMS on the imp, to create an indicator based on who sent the SMS. A couple of questions:

  1. In the agent code: if(t.From=="+18055551212")
    device.send(“sendbody”,t.Body);, is this # ^ my Twilio #, or the phone # that the text originated from?
  2. In the device code, is this the whole of it?
    imp.configure(“GetSMS”, [], []);
    agent.on(“sendbody”,function(value){
    imp.onidle(function(){server.sleepfor(5);}); //blinks for 60 secs
    });

Or is ‘function()’ a placeholder? I would think there would be more to it than that, i.e. I would pass a parameter on to my LED control() method and go from there.

At some point I would like to move on to both sending and receiving, but for the moment I’m only looking to process based on the inbound message content.

Thanks!

Duncan