Twilio to IMP LEDs

I’m putting together Twilio > imp application to take SMS directed at my Twilio # and turn it into a controller for my imp. I have three LEDs (red, yellow, green) and wrote a simple blink() program to toggle them on and off, so I know the hardware’s okay.

I configured my Messaging Request URL to point to my agent URL, and have the following code as my agent to forward along the message body to the imp:

function mywebserver(request,res) { t<-http.urldecode(request.body); if(t.From=="+1<myNumber>") { device.send("sendbody",t.Body); res.send(200,"okay"); } }

On the device, I have the following code to toggle the LEDs individually, or just set them to blink:

`imp.configure(“GetSMS”, [], []);

agent.on(“sendbody”,function(value){ // value = the body of the SMS message
if(value==“Red”){
toggleRed();
}

if(value=="Green"){
  toggleGreen();
} 

if(value=="Yellow"){
  toggleYellow();
}
if(value=="Blink"){
    blinkAll();
}

});

// Define the LED vars
rLED <- hardware.pin1;
yLED <- hardware.pin2;
gLED <- hardware.pin5;

// Configure as Digital Outputs
rLED.configure(DIGITAL_OUT);
yLED.configure(DIGITAL_OUT);
gLED.configure(DIGITAL_OUT);

rState <- 0;
yState <- 0;
gState <- 0;

// Set the state (0 = off)
rLED.write(rState);
yLED.write(yState);
gLED.write(gState);

// Blink them in unison
function blinkAll() {
// flip the current LED States
if(rState == 0) {
rState = 1;
} else {
rState = 0;
}

if(yState == 0) {
yState = 1;
} else {
yState = 0;
}

if(gState == 0) {
gState = 1;
} else {
gState = 0;
}

// Apply them
rLED.write(rState);
yLED.write(yState);
gLED.write(gState);

//imp.wakeup(3.0, blinkAll);

}

function toggleRed() {
if(rState == 0) {
rState = 1;
} else {
rState = 0;
}

rLED.write(rState);
}

function toggleYellow() {
if(yState == 0) {
yState = 1;
} else {
yState = 0;
}

yLED.write(yState);
}

function toggleGreen() {
if(gState == 0) {
gState = 1;
} else {
gState = 0;
}

gLED.write(gState);
}
`

From the IDE and can see the device online / engine running indicators are green.
The device log is not responsive, though, beyond

2014-02-17 13:33:38 UTC-5: [Status] Device booting; 4.2% program storage used
2014-02-17 13:33:38 UTC-5: [Device] imp.configure command is deprecated

Thoughts?

This line remove:
imp.configure(“GetSMS”, [], []);

That is the deprecated error.

imp.configure() won’t cause any problems, it’s simply ignored (but will log a message).

What do you mean by “the device log is unresponsive” - it doesn’t look like you’re logging anything in your device code…

I would expect to see some indication that there was activity, though some add’l logging would be useful, admittedly.

Have this working now, will likely have it listen to another Twilio project I have to make it more interesting than my controlling LEDs by sending texts directly. (I’d also like to switch to a single RGB LED at some point.)

Anyhow, here’s the code:
`//Agent:
function mywebserver(request,res) {
t<-http.urldecode(request.body);
server.log(“Message received from " + t.From + “: " + t.Body);
if(t.From==”+1”)
device.send(“sendbody”,t.Body);
res.send(200,“okay”);
}

http.onrequest(mywebserver);`

`/* Device code */
// create global variables for each LED
REDled <- hardware.pin9;
GREENled <- hardware.pin7;
BLUEled <- hardware.pin5;
// configure as digital outputs
REDled.configure(DIGITAL_OUT);
GREENled.configure(DIGITAL_OUT);
// I’m calling it BLUE, but for the moment it’s actually a YELLOW LED (bulk order)
BLUEled.configure(DIGITAL_OUT);

// function to turn LED on or off … ledState=Twilio Message.Body
agent.on(“sendbody”, function(ledState) {
server.log("ledState = " + ledState);
//if(ledState = 1) {
// server.log("ledState = " + ledState);
//}
if(ledState == “Red” || ledState == “RED”) {
server.log("Set Red LED: " + ledState);
REDled.write(1);
}
if(ledState == “Green” || ledState == “GREEN”) {
server.log("Set Green LED: " + ledState);
GREENled.write(1);
}
if(ledState == “Yellow” || ledState == “YELLOW”) {
server.log("Set Yellow LED: " + ledState);
BLUEled.write(1);
}
if(ledState == “Off” || ledState == “off” || ledState == “OFF”) {
server.log("Set All LEDs: " + ledState);
REDled.write(0);
GREENled.write(0);
BLUEled.write(0);
}
});`

/* On the Twilio Side */ // Under the phone number I'm receiving the texts at, // as the Messaging / Request URL, I enter https://agent.electricimp.com/<device-specific URL>

/* System log */ 2014-03-23 12:49:06 UTC-4: [Agent] Message received from +1<number>: Red 2014-03-23 12:49:06 UTC-4: [Device] ledState = Red 2014-03-23 12:49:06 UTC-4: [Device] Set Red LED: Red 2014-03-23 12:49:13 UTC-4: [Agent] Message received from +1<number>: Green 2014-03-23 12:49:13 UTC-4: [Device] ledState = Green 2014-03-23 12:49:13 UTC-4: [Device] Set Green LED: Green 2014-03-23 12:49:22 UTC-4: [Agent] Message received from +1<number>: Off 2014-03-23 12:49:22 UTC-4: [Device] ledState = Off 2014-03-23 12:49:22 UTC-4: [Device] Set All LEDs: Off

Edit: Wrapped code in < code > </ code > tags.

My comments were truncated as I posted them, sorry. Where the agent code reads if(t.From=="+1"), “+1” represents my phone #. My initial idea was to color code my kids’ phone numbers so that if they text me at work, say, I would get a visual heads up from the imp indicating who it was.

Now I’m looking at connecting it to another Twilio project that would send me color-coded indications that there were inbound messages that I should take a look at, such as RED for opt-outs, GREEN for questions, and YELLOW for inbound messages from unfamiliar numbers.