loading

I was inspired to make this instructable by groversarvesh00's and sumitgrover97's recent entries in the Raspberry Pi Contest 2016. Many Instructables and tutorials I have seen on the internet for text-controlled Raspberry Pi's have involved GSM modems. The problem with GSM modems is that they are specialized pieces of equipment that can add significant expense and power usage to a project.

If you are seeking SMS-controlled functionality in your Raspberry Pi without a GSM modem, you can use NodeJS and a service like Plivo or Twilio to provide text-enabled functionality without the expense and power needed for a GSM modem. The downside to this is that you will need to have your Raspberry Pi connected to the internet with a port forwarded to it.

Step 1: So How Does It Work?

Twilio and Plivo have libraries for NodeJS that can be used to provide text-controlled functionality to your Raspberry Pi. Basically, you will need to buy a phone number from Plivo or Twilio and configure it to forward texts to the IP address of the router where your Raspberry Pi will be connected. You will need to forward a port to your Raspberry Pi and set up a route to receive requests.

For the purposes of this Instructable, I will be using Twilio as the SMS provider, because I am more familiar with their service.

Prior to beginning this instructable, you will need to do the following:

1. Install NodeJS on your Raspberry Pi.

2. Set up a Twilio account.

For those who are more familiar with Python, you could also use Django (a Python web framework) for this project; however, I am not a Python developer, so I will be unable to help you with this project. Here are some resources for you if you feel more comfortable using Python for this type of project:

http://www.hackedexistence.com/project/raspi/djang...

https://django-twilio.readthedocs.org/en/latest/

Step 2: Configure Your Raspberry Pi

SSH into your Raspberry Pi. Change directory to the Desktop. Run these commands:

  • mkdir SMSPI - this will create a directory called SMSPI
  • cd SMSPI - this will change directory into SMSPI
  • npm init - this will initialize the NodeJS project in your directory
  • npm install express - this will install Express, a NodeJS library in your project
  • npm install twilio - this will install the Twilio library into your project
  • npm install rpi-gpio - this command will install the Raspberry Pi GPIO for NodeJS library into your project
  • npm install body-parser - this command will install a library for parsing the JSON responses sent from the Twilio service
  • nano index.js -this command will use a text editor called Nano to create a file called "index.js". It will allow you to paste code into the file.

Some of these commands will take a few minutes to run, especially when the RPi has to compile the RPI-GPIO library. After you have completed these steps, go to the Express tutorial website and copy their "Hello World" code into your project.

After you have copy-pasted the code, press Ctrl-X then Y to exit the nano window. If you type "ls", you should see "index.js" among the files listed in the directory. If you see it, type "node index.js" to run the webserver. Open up the IP of your Raspberry Pi in a web browser with port 3000 on the end (ex. 192.9.1.9:3000). You should see "Hello World" echoed out on the screen. Well done if you have gotten this far!

Step 3: Twilio Configuration

Buy a phone number in Twilio and get your API key and account SID. After you have done this, find your phone number in the Twilio dashboard. Click on your phone number in the menu. Scroll down to the "messaging" section of the screen and put your ip address into the box like so: ""http://yourIPaddress:3000". If you do not know your global IP address, you can get it from this website: http://www.ipchicken.com/

After this is done, your Twilio configuration should be finished.

Step 4: Port Forward

The next step is to portforward port 3000 to the local IP address of your Raspberry Pi. Every router will be set up a little bit differently. There are many tutorials on how to do this. Google your router and find out how to portforward. I attached a screenshot of what my router interface looks like.

Step 5: The Circuit

As a proof of concept, we will write an application that allows us to turn an LED on and off through SMS. The application will also send a text reply to let you know what it is doing. Build the same circuit like the one in step 4 of my Javascripting Your Raspberry Pi Instructable. I have also attached the image here for your convenience. You will need an LED and a resistor between 270 ohms and 330 ohms. A breadboard will be helpful as well.

Step 6: Time to Code!

Attached to this step is the code necessary to make your Raspberry Pi work with Twilio. Paste the attached code into your index.js file on your Raspberry Pi. Replace any ALL CAPS variables in the code with your information.

Basically, the code works in the following steps:

  1. Initialize all of the libraries needed.
  2. Log in to Twilio with API key.
  3. There are two functions defined to turn the LED off and on.
  4. There are two routes set up. One is a GET route, and the other is a POST route. The GET route is the "Hello World" route for testing purposes. The POST route is the one used by Twilio.
  5. Inside the POST route, there is an if statement that checks the body of the text message for the words 'on' and 'off'. Inside those "if" blocks, there is logic to turn the LED on and send a text notification back to the sender.

Overall, the code is pretty basic. In less than 80 lines of code, you can get some pretty cool functionality.

Step 7: Test

Start your webserver with "sudo node index.js". You MUST use sudo to start the program or else it will not be able to turn the GPIO pins on and off. Sudo gives the program root access (think of it like an admin account or super user privileges). Text your Twilio phone number with the word "on", and the LED should turn on. Text it with the word "off" and the LED should turn off.

The program will log things into the console for debugging purposes. If you experience an error, check the console.

Step 8: What Next?

To get the best use of the SMS functionality, I recommend that you go to Twilio's developer documentation and read up on how their API can be used.

I recommend that you add logic in your code to accept text messages from only your cell phone. You could do this with an "if" statement.

If you run into any errors while doing this Instructable, let me know in the comments and I will try to help you out.

<p>So, could you text with this? What does it do? I have a raspberry and this looks cool. I just don't know what the purpose of this is.</p>
<p>You could send texts with Twilio's API. I will try to take a video. Here is how the circuit I built works:</p><p>1. You text the word &quot;on&quot; to your Twilio phone number.</p><p>2. The Raspberry Pi processes the text and turns the LED on, and it sends the user a notification about the state of the LED.</p><p>3. You text the word &quot;off&quot; to your Twilio phone number.</p><p>4. The Raspberry Pi processes the text and turns the LED off. It sends you a text the the LED is off.</p><p>Your options are limitless. You could make a home security system that sends you text updates with a trigger is flipped. You could also combine the RPI with an Arduino and use the SMS functionality on the PI to make the Arduino do something based on text input.</p><p>That said, you could use this to send text messages as well. Twilio has an extensive service. If you have more questions, let me know.</p>
Yaaaay windows phone user!

About This Instructable

5,377views

62favorites

License:

More by wazupwiop:Text-Controlled Raspberry Pi Javascripting Your RaspberryPi Start Your Own Business 
Add instructable to: