Tutorial: Remote Development on Raspberry Pi
We all know that ISPs have a habit of overselling their connections, and this sometimes leads our connections to not be as good as we’d like them to be. Also, many of us have Raspberry Pi’s laying around waiting for cool projects. So let’s put that Pi to use to check on our internet connection!
One of the key metrics of an internet connection is its ping time to other hosts on the internet. So let’s write a program that regularly pings another host and records the results.
For those of you wanting to execute code remotely on other remote computers, like an AWS instance or a DigitalOcean droplet, the process is exactly the same.
This tutorial has been created on Windows, and the code was executed on the RPi, which is a Linux computer. On the other supported operating systems, some of the paths and workflows might be slightly different.
Before you start
For performing this tutorial, make sure that you meet the following prerequisites:
You have the Professional Edition of PyCharm.
The code will run on any Linux machine. You can also use a local VM as well.
You'll also need some additional software:
PostgreSQL, because it will be used to create a database.
Libpq-dev, needed for Psycopg2.
Python-dev, needed to compile Psycopg2.
To install all of these at once, run the following command:
The application we will build consists of two parts:
First, we actually do the measurements. To measure the results, we can just call the ping command-line tool that every Linux machine (including the Raspberry Pi) ships with. We will then store the results in a PostgreSQL database.
Second, we'll visualize the results using a Flask app, which uses Matplotlib to draw a graph of recent results.
Preparing your Raspberry Pi
As we will want to be able to view the web page with the results later, it’s important to give our Pi a fixed IP within our network. To do so, edit /etc/network/interfaces. See this tutorial for additional details.
After you’ve set the Pi to use a static IP, use
raspi-config on the command line. Go to Advanced Options, choose SSH, and choose Yes. When you’ve done this, you’re ready to get started with PyCharm.
Creating a project
Create a pure Python project: to do that, choose Create a Python project.. Name the project "raspberry.pi" See
Then add an SSH remote interpreter as described in Configure an interpreter using SSH. When configuring an SSH server, specify the IP-address of your Pi (Host), for example, 172.27.120.177. Also, specify credentials for logging into the device.
Next, in PyCharm, press Ctrl+Shift+A, begin typing ‘Start SSH session’:
Then pick your Raspberry Pi from the list of hosts, and you should be connected.
Next, install additional software.
Setting up permissions and creating a database in PostgreSQL
We now need to set up the permissions in PostgreSQL. The easiest way to do this is to go back to our SSH terminal, and run the following command to get an SQL prompt as the Postgres user:
Now let's create a user (a ‘role’ in Postgres terminology) with the same name as the user that we run the process with:
Important note! Make sure that the role in PostgreSQL has the same name as your Linux username. You might also want to substitute a better password. It is important to end your SQL statements with a semicolon (;) in psql, because it will assume you’re writing a multi-line statement until you terminate with a semicolon. We’re granting the pi user login rights, which just means that the user can log in. Roles without login rights are used to create groups.
Next, create a database:
Next, exit psql with
To get information on the quality of the internet connection, let’s ping a server using the system’s ping utility, and then read the result with a regex. So let’s take a look at the output of ping:
All the lines with individual round trip times begin with
64 bytes from. So let’s create a file ping.py ( ), and start coding.
We can first get the output of ping, and then iterate over the lines, picking the ones that start with a number followed by the words ‘bytes from’:
At this point if you run the code Ctrl+Shift+F10, you should see this code running remotely on the Raspberry Pi:
To check if the deployment settings are set up correctly, point to the Automatic Upload should be checked.on the main menu. The check command
Storing the pings
We wanted to store our pings in PostgreSQL, so let’s create a table for them. First, we need to create a PostgreSQL database:
When PostgreSQL is selected, the datasource page opens:
On this page, download the required drivers, enter the required credentials, and then click the Test Connection button to make sure you are connected.
As our database is only exposed to localhost, we will need to use an SSH tunnel:
Next, after you’ve connected, create the table by executing the setup_db.sql script. To do this, copy-paste the script from GitHub into the SQL console that opened up right after connecting, and then use the green play button to execute.
Now that we’ve got this working, let’s expand our script to record the pings into the database. To connect to the database from Python we’ll need to install psycopg2. To do it, point to (for Windows and Linux) or (for macOS users), then click the page Python Interpreter, and then use to install the package. If you’d like to see the full script, you can have a look on GitHub.
To make sure that we actually regularly record the pings, we need to schedule this script to be run. For this, we will use cron. As we’re using peer authentication to the database, we need to make sure that the script is run as the pi user. So let’s open an SSH session (making sure we’re logged in as pi), and then run
crontab -e to edit our user crontab. Then at the bottom of the file add the following line:
Make sure you have a newline at the end of the file!
*/5 means that the script will be run every 5 minutes. If you’d like a different frequency, you can learn more about crontabs. Now we also need to create the log file and make sure that the script can write to it:
At this point take your time, and when you come back later, some ping times should have been logged. To verify, let’s check with PyCharm’s database tools. Open the Database tool window (on the right of the screen) and double-click the table. You should see that it contains values:
Creating a Flask application
It’s no good to just record pings, if we can’t see some statistics about them. So let’s write a small Flask application (to install Flask, use
pip install in the Terminal), and use matplotlib to draw a graph of recent ping times.
In our Flask application, we’ll create two routes:
/, we’ll list the destinations that we’ve pinged in the last hour with basic stats (min, average, max time in the last hour).
/graphs/<destination>we’ll draw a graph of the pings in the last 3 hours.
/ route is simple: we just execute a query to get the data we’re interested in and pass that to the template. See the full code on GitHub.
To make sure that everything works right, let's put a breakpoint on the call to
Then start the debugging session and see the result in your browser.
/graphs/<destination> route is a lot more complex. First, we have to get the ping averages for the past three hours in reasonably sized bins (let’s say, 10 minutes). Second, we have to draw the graph.
Querying the data
So the data we’re looking for is:
For each 10 minute period in the last 3 hours
Get the minimum, average, and maximum ping time to a specified destination
The first part makes this a fairly complex query. Even though PostgreSQL has support for intervals, date ranges, and a way to generate a series of dates, there is no way to generate a series of ranges. One solution to this problem is a common table expression (CTE), this is a way to execute a subquery which you can later refer to as if it were a real table.
To get a series of timestamps for the last three hours at 10 minute intervals is easy:
The generate_series function takes three arguments:
step. The function works with numbers and with timestamps, so that makes it easy. If we wanted pings at exactly these times, we’d be done now. However, we need times between the two timestamps. So we can use another bit of SQL magic: window functions which allow us to do things with rows before or after the row that we’re currently on. So let’s add
end_time to our query:
LEAD takes the value of the next row in the results, as ordered in the way specified in the
OVER clause. You can use
LAG to get the previous row in a similar way. So now we can wrap this query with
WITH intervals as ( … query goes here … ) to make it a CTE. Then we can join our pings table and get the results we’re looking for:
Next, let's execute this query. To do that, right-click the editor background and choose Execute on the context menu:
(if you don't see Execute, choose Attach Console to let PyCharm know on which database you’d like to execute your query).
By the way, you can make the query work 30 times faster. To achieve this significant speedup, add this index to your query:
Graphing the data
After getting the data, matplotlib is used to generate a line graph with lines for the minimum, average, and maximum ping time per bin. Matplotlib makes it easy to plot time-based data using the plot_date function.
When the plot is ready, it’s ‘saved’ as a .png file to a StringIO object, which is then used to create an HTTP response. By setting the
content_type header to
image/png, everything is arranged.
So the final result is:
If you want to see the full code, check out analyze.py on GitHub.
Congrats for passing this tutorial! Let's repeat what has been done with the help of PyCharm: