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.
By way of comparison, here is the energy use of a 2012 Mac Mini and a Raspberry Pi Model B:
RASPBERRY PI (MODEL B) | MAC MINI (2012) | |
---|---|---|
IDLE | 2.19W | 11W |
MAX | 2.64W | 85W |
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:
EASIEST
Log in using your username and the name of your Pi:
- From your main computer, type the following into the Terminal:
ssh pi@rasbperrypi
Change pi to whatever your main username is, if necessary - Enter your password when prompted, then you’re in!
- Disconnect by typing exit
HARDER
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:
ping raspberrypi - 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!
Since your ‘bots are running under the user python from Cron, you do not need to have your pi set to auto-login… My Pi computers never auto login, and all run headless and always work exactly as expected…
I don’t know. I remember reading somewhere that it was required for either headless or SSH, but that might not be true? I wonder if there are any security reasons not to have it auto-login.
Having auto-login means that anyone could use it when physically attached, when running headless and hidden away somewhere it doesn’t really matter but speaking with my security hat on, I prefer not to leave anything without a password as it is unnecessary.
The other thing I noticed is that I often copy files to my pi from my file server (another Pi running Samba 3.6.6) without using ftp from an SSH remote terminal. Obviously I have the Samba server running anyway but if you have that then an FTP server is not required. I’m only mentioning this because you mention that SSH “can’t” be used to move files when, strictly speaking, it can…
If you wanted to use a remote desktop then ‘apt-get install xrdp’ and you can log in using an xrdp client to our beloved Pi desktop from your other favourite desktop, although you’ll still need access to remote network shares to copy files between computers…
I still cannot stop being surprised at how wonderfully effective a $40 credit card sized computer is…
28:00 isn’t eight o’clock. ;)
But seriously, where are your Python scripts from? I knew most of everything else!
Oops, typo! For scripts you’ll have to write everything yourself. I have a tutorial here on writing bots in Python, which includes some libraries for posting.
Alas this section links to a dead page now…
Can you turn on the video again via ssh if needed?
Update: this post suggests that 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.
Thanks for the broken link report – you should be able to turn it back on with the command
/opt/vc/bin/tvservice -on
.Sorry my bad English,
The thing is, to build a raspi-bot,
which runs fast (a bit faster than human walk),
entertains,
knows his own position,
talks
understands spoken Commands(/ Conversation)
and which is built modular,
that means:
the mechanical parts and the processing part
should be exchangeable easily (chassis, botarms, RP, PowerPack, SwitchRelays …)
I am sticking to the Problem of the weight, whenever i build a tiny bot. So i’m searching for a solution to switch the e-motors only by opto-coupler (without switch-relays).
I (have to) struggle for easy and ‘lowbudget’ solutions.
Till now, I could develop systems which can talk (voice), can check out there own position (without GPS), having interconnected communication, can drive, can recognize moving Persons, and act in some funny way.
I try to get also the listening thing (speech2text),
but therefore I need Google like smartphone(at the Moment).
I do it all Linux-Shell and C-based, and I’m more passionated for autonom systems, not so good programmer,
I have to learn a lot, and I can share some results.
Thanks for this site
nice post it worts reading it was very usefull for me website bookmarked realy enjoyed your post
Any idea how to run a bot with 400+ servers? It just kinda freezes and never fully boots.
@Nat – not sure what you mean. Each RPi would be a server. What happens when you try to boot?
How can I run a java bot on this Raspberry pi?
@Robin – is your bot made in Processing? If so, you can try runing your sketch using the command line, then paste that command into
cron
. If it’s just Java, you’ll probably need to compile it and run it with thejava
command, similar to a Processing sketch.Hallo and thank you for your work here. I tryed your guide but I got the following error :
socket.gaierror: [Errno -3] Temporary failure in name resolution
and
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host discordapp.com:443 ssl:default [Temporary failure in name resolution]
Do you have any Idea why that happens? When I start my bot manually from my Praspberry Pi 3, it logs in.
But I dosen’t login automaticly.
@Neosilver: hmm not sure what that error is from. Is this the auto-login to the RPi itself or to your Twitter account? If the RPi, you might want to post to the forums for that.