Introduction: Text-Controlled Raspberry Pi
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:
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:
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. 126.96.36.199: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
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:
- Initialize all of the libraries needed.
- Log in to Twilio with API key.
- There are two functions defined to turn the LED off and on.
- 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.
- 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.
We have a be nice policy.
Please be positive and constructive.