Send Texts With Intel Edison (Party Alarm)

Introduction: Send Texts With Intel Edison (Party Alarm)

"I'M GONNA POP SOME TAGS, ONLY GOT TWENTY DOLLARS IN MY POCKET. I'M A HUNTING, LOOKING FOR A COME UP, THIS IS F......" Parties are a blast when they are YOUR party. They aren't so much fun when the sound is booming from across the street. While I'm SURE you love your loud neighbors and want to tell them in person to turn it down, it's cold outside and even your warmest flannel zebra jammies can't keep the chill out. Why don't we make a button the texts our neighbors when they are too loud? Perhaps a text from your handy Intel Edison?

This instructable is all about how to send SMS messages (ie texts) using node.js.

So drape yourself in (faux) leopard mink, a velour jumpsuit, and get ready to MAKE!

The first is the node packages from Twilio. Twilio is a company in San Francisco that has an API that allows folks to make calls and texts via THE CLOUD! (all hail the cloud). You will need to make a free account at www.Twilio.com in order to complete this instructable. Open up your terminal and connect to your Edison. Type the following and press enter:

>> npm install twilio

Cool. Now you can text. Now we need to get a package that allows us to run our app even when our terminal has been closed. Note that the "-g" flag is extremely important for our needs.

>> npm install forever -g

Awesome. Now we need a text editor. There is Vim/Vi on the Edison, but it doesn't always like to work. Here's how to install Nano (which is also much easier to use for beginners):

Step 2: Let's Write Some Code!

Before you write any code (ahw maaaannnnnnn), you will need to set up a Twilio account if you haven't already. Go to www.Twilio.com and set one up. For our purposes, we can use the free trial.

We are using node.js to write our code. It is a JavaScripting language that can be insanely wonderful to use. The first thing we want to do is open up our code file in Nano. You can save by pressing control+o and exit by pressing control+x.

>> nano partyAlarm.js -$ -i

Cool. Now in the words of every Game of Thrones fan, "Code is coming". Type all of this fine jazz into the text editor, save, and then exit. The instructables text editor LOVES to screw up the formatting. If you come across a <br>, delete it and start a newline there instead (ie, press enter or return).

// Set up our input pin on pin 5 of the Edison Arduino Boardvar mraa = require('mraa');
var buttonPin = new mraa.Gpio(5);
buttonPin.dir(mraa.DIR_IN);
//Set up the stuffs for Twilio. You will need to set up an
//account to get your SID and your token/authentication keyvar twilio = require('twilio');
var client = new twilio.RestClient('<<twilio sid>>', '<<twilio token>>');
//Read our button
var buttonPushed = buttonPin.read();
//If our button was pressed, send our message
//You will need to fill in your own twilio number to make this work.//Note, we are texting 1(555)123-4567 here. This is obviously a //wrong number. Put your friend's in there instead.
if(buttonPushed){
client.sms.messages.create({
to:'+15551234567',
from: '<<your twilio number>>', body: 'I can\'t hear my light saber go SWOOSH SWOOSH over your racket.'
}, function(error, message){
if(!error){ console.log("Message sent at " + message.dateCreated); } else { console.log("Message wasn't sent..."); } }); }
//Hold in the program until we release the button.while (buttonPushed){ setTimeout(emptyFunction, 200); //debounce buttonPushed = buttonPin.read();
}
//When we use forever.js, it will need to make sure that our program lasts at//least 1 millisecond each time it runs. So, we wait for 4 milliseconds before//ending our functionsetTimeout(emptyFunction, 4);

//Basically, we use this for our delay. We have to call some flavor of//function to use the setTimeout function.function emptyFunction(){
}

Step 3: Simple Circuits and Start Up.

Since this is an instructable about texting with your Edison, we'll keep the circuit simple - a button with a 10K pulldown resistor. If you aren't familiar with how to wire that, search "Arduino button circuit" on Google.

Now that you've written your code and wired your button, we need to start our code running! Forever.js is a neat little package that will run your code until the end of time, restarting it over and over again until Godzilla knocks over the power lines. Unlike my email tutorial, you may notice that this code doesn't automatically loop - in order for the SMS message to be sent, the node.js app needs to exit. Once it exits, forever.js will restart it and keep going. In your Edison's terminal, type the following and press enter:

>> forever start --minUptime 1 partyAlarm.js

Ah man, you added some fancy stuff outside of this instructable, screwed up and your friend is getting text messages every half second. You need to stop the madness immediately! How do you do this? In the terminal, type the following and press enter:

>> forever list

This will tell you what jobs are running and what the job numbers are. The job number will typically be "0" if you aren't running anything else with forever.js. So, we will stop job 0. Type the following and press enter:

>> forever stop 0

GREAT! You are done! You now have a weapon of mass annoyance to bug your neighbors! Sweet!

As I mentioned throughout this instructable, I am using Node.js here. You can write code in any language you want. Ones like Python, Node.Js and C++ are the best supported, but anything that can read and write files will work.

Also, Putty doesn't really "do" anything when it comes to programming. Think of it like a telephone that you are using to call a friend for help - the only thing Putty does is relay a message between you and your Edison in a format called SSH. What you see on the screen is a message from the Edison and what you type into Putty is messaged over to the Edison. Putty (or any other SSH client) is not a programming software, just a messenger.

Putty is a Terminal Emulator, it is used to talk to the Edison's Linux command line. So go research command line commands for Intel Edison, or Yocto Image. It wouldn't hurt to research Linux commands in general, but they vary by distributions, or versions of the same distribution (usually do to missing packages, memory limits or they only compiled the bare minimum they needed).

Good call on installing Nano - I can see how Vi would be super discouraging to a first-time user. I may poach some of this for a public transit tracker I'm working on. I'll definitely link over to you if I end up using some of your code!