When you send your Raspberry Pi out in the world on stealthy missions, you might not be able to stay connected to it at all times. However, as long as the Pi has Internet access via a Wi-Fi network or USB modem, you'll be able to communicate with it from anywhere in the world.
In this example, we'll be using Twitter, a popular social networking service for sharing short messages. We're going to make the Pi send regular tweets about the mission and its whereabouts. If you do not already have a Twitter account, or you'd like a separate account for the Pi, you'll need to sign up at https://twitter.com first.
To enable tweet privacy, go to the Account settings (https://twitter.com/settings/account) and check the Protect my tweets checkbox, then click on the Save changes button.
pi@raspberrypi ~ $ sudo apt-get install ttytter
pi@raspberrypi ~ $ ttytter
ttytter
again without arguments to start the client in interactive mode, where anything you type that doesn't start with a slash will be tweeted to the world. Type /help
to see a list of the possible commands and /quit
to exit ttytter
.pi@raspberrypi ~ $ ttytter -status="Alive: $(date) from $(curl -s ipogre.com)" -ssl -autosplit -hold
-status
argument with the tweet enclosed in double quotes is the quickest way of sending a single message from the command line without entering interactive mode. In this message, we're using a feature of the shell called command substitution that allows the output of a command to be inserted back in place.-ssl
argument enables encryption when we're talking to Twitter.-autosplit
is used to automatically split messages that are longer than 140 characters into multiple tweets.-hold
instructs ttytter
to keep retrying to send the message in case there's a problem communicating with Twitter.ttytter
commands, therefore it makes sense to put them into a file called ~/.ttytterrc
that will be interpreted by ttytter
as a list of features to enable automatically on startup. Open it up for editing with the following command:pi@raspberrypi ~ $ nano ~/.ttytterrc
ssl=1 autosplit=1 hold=1
y
when prompted to save the modified buffer, then press the Enter key to confirm the filename to write to.As an alternative to regular tweets, we can also send direct messages to a specific person using the following command, but replace [user]
with the person's Twitter account name:
pi@raspberrypi ~ $ ttytter -runcommand="/dm [user] My hovercraft is full of eels"
The -runcommand
argument is used to launch from the command line any action that you could type while in interactive mode.
What if we need our Pi to report the contents of an important document or other lengthy output? How can we break the 140-character barrier? Simple, paste the document to a private pastebin and report the link on Twitter. Debian's Pastezone at http://paste.debian.net is a good candidate; it's easy to interact with and supports hidden pastes.
Download a utility Python script to interact with Debian's Pastezone written by Michael Gebetsroither with the following command:
pi@raspberrypi ~ $ sudo wget http://www.intestinate.com/debpaste.py -O /usr/bin/debpaste && sudo chmod +x /usr/bin/debpaste
We can now combine the debpaste
and ttytter
utilities in the following command line:
pi@raspberrypi ~ $ cat /boot/config.txt | debpaste -n ScrtSqrl -e 24 -p add | grep -o 'http://paste.debian.net/hidden/.*' | ttytter -status=-
We start with piping the text file that is to be pasted to the debpaste
utility. The -n
argument is optional and sets the name to be associated with the paste. The -e
argument sets the number of hours the paste will remain readable before it is deleted. The -p
flag is important and enables the hiding of your paste from public view. After the paste has been submitted, the debpaste
utility outputs a bit of information about your entry. Since we can't fit all of this information in a tweet, we use grep
to fish out only the URL that we're interested in from that output. We then pipe the URL to ttytter
and tell it to read the message to be posted from standard input by using the -
character.
If you have a GPS connected to the Pi, we can tag each tweet with a geographical location.
geotagging
for your Twitter account. Go to the Account settings and check the Add a location to my Tweets checkbox, then click on the Save changes button.gpsd
and feeding them to ttytter
. We'll need to create our own shell script for this purpose. Open up ~/passgps.sh
for editing with the following command:pi@raspberrypi ~ $ nano ~/passgps.sh
#!/bin/bash LAT="" LONG="" gpspipe -d -w -o /tmp/gpsdump while ([ -z $LAT ] || [ -z $LONG ]) ; do if [ -f /tmp/gpsdump ] ; then LAT=$(cat /tmp/gpsdump | awk 'BEGIN{RS=","; FS=":"} /lat/ {save=$2} END {print save}') LONG=$(cat /tmp/gpsdump | awk 'BEGIN{RS=","; FS=":"} /lon/ {save=$2} END {print save}') fi done killall gpspipe rm /tmp/gpsdump echo "-lat=$LAT -long=$LONG"
The scripts launches a gpspipe
session in the background, which will fill up /tmp/gpsdump
with data obtained from gpsd
. We then enter a while loop until we're able to filter out the latitude and longitude from /tmp/gpsdump
by using an awk
command and we put the coordinates into the LAT
and LONG
variables. Then we clean up a bit after our script and output the coordinates on a line suitable for ttytter
.
-location
added as an argument to enable geotagging for this particular tweet, then let our script pass in the GPS coordinates. Just remember that you need to have gpsd
running for our script to work.pi@raspberrypi ~ $ ttytter -status="$(vcgencmd measure_temp) today, feeling cozy" -location $(~/passgps.sh)
While we've done plenty of command scheduling with at
in this book, it will only run a command once. If we need a command to be run regularly at certain times, cron
is better for the job and is already installed. To add a new task to run, we need to add it to our scheduling table, or crontab
, with the following command:
pi@raspberrypi ~ $ crontab -e
Add your task to the bottom of the file on a blank line according to the following form:
Minute | Hour | Day of month | Month | Day of week | Command to execute
For example, to tweet a status update every hour:
0 * * * * ttytter -status="Alive: $(date)"
To tweet a status update every 10 minutes:
0/10 * * * * ttytter -status="Alive: $(date)"
You can also use one of the special predefined values among @hourly
, @daily
, @weekly
, @monthly
, @yearly
, or @reboot
to have a command run at startup.
Once you're happy with your line, save and exit nano
to have your new crontab
installed.
18.221.117.214