SMS Control Demo
In this example we'll put together everything we've learnt from the past sections on modems, SMS messages, GPIO and Cameras to create a simple application that can be used to switch GPIO lines high/low or take a photo and email it to you.
This project has some prerequisites:
- A working SIM (with some credit), modem, aerial combination
- At least 1 working Raspberry Pi Camera
- A mobile phone
Here are all the files we are going to use for this demo (located in /usr/local/bin)
Command/Program | Language | Function |
deleteallsmsmessages | Perl | Deletes all stored SMS messages |
sendsms | Perl | Sends SMS messages |
smslist | Perl | Program used by smscmd to extract incoming SMS messages |
smscmd | Bash | Checks modem for new SMS messages and processes valid actions |
takephoto | Bash | Takes a photo from a given camera and emails it to configured email address |
smscmddemo.sh | Bash | Demo wrapper for smscmd |
let's quickly test that all the devices are correctly connected :
OK that's showing we have an unlocked SIM, good signal and all registered on the modem front. the camera status program is showing we have 1 camera correctly attached and configured.
So far so good
Next we need to find out the phone number for the SIM, we'll need that to send the messages to, so send yourself a test message via the sendsms command as shown below :
Once you have that on your phone you should add the number to the contacts list so you can find it again easily
Next we'll need to configure the SMTP email settings to allow the system to send mail out, for this you'll need to know the server settings and account details for a known email account (or sign up for a yahoo/gmail/hotmail account)
Edit the file takephoto and alter the section at the top as highlighted below to reflect the correct settings for your account
Once this has been done run the command takephoto 0 or takephoto 1 depending on which camera port you're using.
If all goes well you'll see something like the below
And a new email message will appear shortly in your email account with a photo attachment (also keep an eye on the junk mail folder just incase)
Next we'll check and erase all the SMS messages in the SIM (good to do so we start with a blank slate)
readmessages will show if there's anything currently stored, whereas deleteallsmsmessages will bulk delete all the SMS messages currently stored (useful to get rid of any "Hey you've got xx credit remaining" messages)
if we now run smscmddemo.sh after a short pause it will run the smscmd program every 5 seconds
Each command set sent needs to be in block capitals in the format shown below, message not conforming to this format will be ignored
CMD [COMMAND] [NUMBER]
Here are some sample commands you can use
Command | Action |
CMD SETLOW 36 | Turn ON The Green Status LED (GPIO36) |
CMD SETHIGH 36 | Turn OFF The Green Status LED (GPIO36) |
CMD TAKEPHOTO 0 | Take A Photo Using Camera 0 And Email It |
CMD TAKEPHOTO 1 | Take A Photo Using Camera 1 And Email It |
Here's a screenshot showing it running successfully toggling the LEDs
As you can see once a message has been received and read back it's immediately deleted, so no received message is re-processed.
The bash script smscmd which does most of the work in controlling the SMS command set is fairly easy to extend via case statements and the wiringPi gpio utility
The script has points where debug echo statements can be re-enabled to help with with this
The smscmd script could be added to /etc/crontab to start the program at regular intervals from start up or called in a similar fashion to smscmddemo.sh via rc.local (provided it has & added to the end to launch the script into the background otherwise the bootprocess will freeze)