Where’s the server? It’s under the table next to the couch!
The Raspberry Pi can be used for lots of cool projects, but because it’s cheap, small, and consumes far less power than a regular laptop or desktop, it’s perfect for applications where a computer needs to be running constantly, such as a server for running Twitter bots!
I have seven bots running at the moment, tweeting things like art assignments, “would you rather?” questions, and links from a 1995 “internet directory” book. Most of them post once an hour at varying times during the hour, meaning I need to run them from a computer that is always on, 24/7. I had previously used a Mac Mini, but it felt wasteful to have such a powerful computer that consumed so much energy, just to post 140 characters to Twitter.
|RASPBERRY PI (MODEL B)||MAC MINI (2012)|
The Mac Mini also creates a lot of heat, even when not really doing anything. It’s average heat dissipation is 126 BTUs per hour, or the equivalent of 1/3 of a human!
Update: turning off video output (via RCA/HDMI) can save power consumption even more, especially for battery operation. Turn it off using the following command: /opt/vc/bin/tvservice -off, though it may not work on your device.
That’s enough convincing: let’s run some bots!
STEP 0: BASIC SETUP AND THINGS WE NEED
This tutorial assumes you have your Pi set up with an operating system, etc. If you need help with that, there are lots of great tutorials to help you out.
For this tutorial, you will need:
- A working bot – if you need help writing one, you can see my tutorial!
- Your Raspberry Pi, plugged into power and ethernet.
- Your Raspberry Pi’s main username and password. The default username is usually pi (we’ll use this to connect to your Pi via SSH in the Terminal).
- FTP client such as Filezilla or Transmit (we’ll use this to send files to your Pi remotely).
- Some experience using the command-line. This tutorial shows Mac screenshots, but should work the same for Linux users; Windows users may need some tweaking to get this to work. If you have any problems or suggestions, please leave them in the comments.
STEP 1: HEADLESS MODE
We need the Pi to run without a screen, keyboard or mouse, or any other peripherals, and to accept connections via SSH. This is called running “headless” and is how most servers are configured.
Note that this step, and all others below, can be done remotely via SSH once the Pi is set up!
- Start up your Pi as usual with a monitor and keyboard connected (this is the last time we’ll need to do this!)
- Once the startup completes, don’t start a graphical session. If it is set to launch automatically, you can complete this step in the Terminal (called LXTerminal on many Pi distributions).
- Type sudo raspi-config and enter your password
- Scroll down to ssh using the arrow keys, then hit Enter
- Select Enable and hit Enter
- Exit raspi-config by selecting Finish
Note that some RPi distributions, including the standard Raspian, log in the first time to the graphical OS. You can change this in the raspi-config menu, accessed via the command line.
STEP 2: ENABLE AUTO-LOGIN
We want our Pi to boot and run without asking for a username and password. If the Pi was to crash or have an interruption in power, we want it to restart automatically and start posting again without us having to intervene.
- In the Terminal, type sudo nano /etc/inittab
- This will open the inittab file in Nano, a command-line text editor
- Look for this line (or something very similar) and put a
# in front of it to comment it out – it should look like this:
# 1:2345:respawn:/sbin/getty 115200 tty1
- Add a new line below:
1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/devtty1 2>&1
Note that you should replace -f pi with your username if necessary
- Hit control-X , then Y to save
This step is via this useful guide, if you’d like to read more in depth.
STEP 3: CONNECT VIA SSH
Secure Shell (SSH) lets us connect to the Pi via another computer on the same network. With SSH, you can change configurations on your Pi, run maintenance, and most anything else via the command-line on your computer – much easier than finding a monitor cable and spare keyboard!
You can connect one of two ways:
Log in using your username and the name of your Pi:
- From your main computer, type the following into the Terminal:
Change pi to whatever your main username is, if necessary
- Enter your password when prompted, then you’re in!
- Disconnect by typing exit
If the above method doesn’t work, you’ll need to find the IP address of your Pi.
- From your main computer, type the following command:
- The result will be an IP address in the form of four numbers separated by commas, such as: 192.xxx.x.x
- This may change as new computers join or leave the network, so you will need to run ping each time you want to connect to your Pi. Alternatively, you can set a fixed IP address, but for me it hasn’t been worth the amount of “under the hood” work.
- Connect to your Pi by typing ssh xxx.xxx.x.x -l pi , where xxx.xxx.x.x is the IP address you found in the previous steps and pi is your username
- Enter your password when prompted, then you’re in!
- Disconnect by typing exit
You can now enter Terminal commands, just like if you had a keyboard and monitor plugged right in! If you don’t use the command-line very much, here’s some useful commands to learn:
- pwd to print the current directory path
- ls to list the files/directories in the current location
- cd <folder> to move into a folder in the current directory – for example, to go to the Desktop from the home folder, type cd Desktop
- cd ../ to go up one level in the directory structure
Adafruit also has a great tutorial about SSH and the Raspberry Pi.
STEP 4: INSTALL MODULES AND LIBRARIES
To run your bots, you’ll need to install any modules, packages, or libraries that they need.
First, make sure everything is up to date by running these two commands:
sudo apt-get update and sudo apt-get dist-upgrade
These will update your OS and installed packages – it may take a while, so now’s a good time to take a little break :)
All my bots run using Python, and if yours do too I suggest installing pip :
- Type sudo apt-get install python-setuptools
- Then install pip using sudo easy_install pip
Using pip and easy_install , you can install most packages. Other tools should be installed as needed. I particularly use python-twitter , tweepy , nltk , and pattern , all of which can be installed using pip .
STEP 5: MOVE YOUR BOTS
SSH is great, but it can’t be used to move files from one computer to another. You could drop your files onto a thumb drive, plug it into the Pi, and use some command-line magic to move the files, but that’s complicated! Instead, we can connect to the Pi via Secure File Transfer Protocol (SFTP), similar to how you upload files to a web server.
- Open your FTP client of choice – above is a screenshot using Transmit, which is not free like Filezilla, but has a very good, easy to use interface. Filezilla and other FTP programs are very similar.
- Choose to connect via SFTP, then enter the following information:
Server : <username>@raspberrypi , or the IP address of your Pi (same as SSH above)
Username/password : the username and password we’ve been using above
- You’re connected! Make a new folder on the Desktop for your bots (or wherever you’d like) and drag-and-drop your files from your hard-drive.
STEP 6: SET UP AUTOMATIC POSTING
Last step! We now want to make our bot run automatically at a set interval. To do that, we have to edit the crontab , a system file for automating.
- Using SSH, type crontab -e to edit the file in Nano (the same editor used above to enable auto-login)
- Scroll down to the bottom and type a cron configuration (more info below). For example:
0 * * * * python ~/Desktop/Bots/MySuperCoolBot/MySuperCoolBot.py
- Hit control-X to exit, then Y followed by Enter to save the crontab
Cron commands can take a little getting used to. The above commands runs a python script called MySuperCoolBot.py located in the Bots folder on the Desktop once per hour on the hour. The five places (specified by numbers or an asterix) set minute (0–59), hour (0–23), day of month (1–31), month (1–12), and day of week (0/Sunday–6/Saturday).
A few more examples (from this great tutorial):
- To run a bot every six hours on the hour:
0 */6 * * * python ~/Desktop/Bots/MySuperCoolBot/MySuperCoolBot.py
- To run a bot every day at 6:30pm:
30 18 * * * python ~/Desktop/Bots/MySuperCoolBot/MySuperCoolBot.py
- To run a bot at 8:00pm on weekdays only:
0 20 * * 1-5 python ~/Desktop/Bots/MySuperCoolBot/MySuperCoolBot.py
STEP 7: SIT BACK AND WATCH YOUR BOTS!
That’s it – your bots are alive and posting. Ran into problems or got a bot running using these instructions? Post them in the comments below!