Our Cookies Policy
To make full use of www.electromaker.io, enjoy the personalised features and ensure the websites works to its full potential, your computer, tablet or mobile phone will need to accept cookies.
Our cookies don’t store sensitive information such as your name, address or payment details: they simply hold information about how you use our site so we can improve your experience and resolve any errors.
If you’d prefer to restrict, block or delete cookies from www.electromaker.io, or any other website, you can use your browser to do this. Each browser is different, so check the ‘Help’ menu of your particular browser (or your mobile phone’s handset manual) to learn how to change your cookie preferences.
Here’s a list of the main cookies we use, and what we use them for:
Electromaker – Login session
Google Analytics – Analytics
Twitter – Twitter Feed
Each web browser handles cookie management differently, please follow instructions for your chosen browser:
First, ensure that your raspberry pi is on, has SSH enabled, and has a static IP address set up.
Next, open up your PyCharm Professional IDE. Select "New Project" and choose "Pure Python"
I only had the option to connect to a remote host when I selected the "Existing Interpreter" radio button seen below. Then click on the cog and click "Add Remote"
Adding a remote connection
Next, you'll need to specify the protocol, location, user, and password for your connection. In my example, I chose SSH. Enter the Static IP address you set up earlier in the tutorial.
Entering the Pi SSH connection credentials
After confirming your credentials, click "OK" and continue back to the previous screen. You will want to specify the location of your script folder (specifying a folder that does not exist will mean that PyCharm creates that folder upon connection). Make sure that you include "/home/pi/yourFolderHere".
Entering your script location
Now, click "Create" and you will have a PyCharm application that will run using the interpreter of the raspberry pi
Setting up PostgreSQL
Now that you've set up your Pi, you'll want to set up the postgreSQL.
To easily do this from the IDE, simply press command + shift + a (ctrl + shift + a on the PC). Then enter Start SSH Session and choose the option that is presented in the list. You will then need to choose the host to connect to. Simply choose the option that you initially created when creating the remote host at the beginning of the tutorial.
After Postgress is istalled, you'll want to open the console for it by entering
sudo -u postgres psql
You need to add a role to be able to execute queries from
CREATE ROLE "pi" WITH LOGIN PASSWORD '[password]';
Make sure that the role in PostgreSQL has the same name as your linux username
Now you'll create a database with the same owner
CREATE DATABASE pi WITH OWNER pi;
Next, create a python script in your PyCharm IDE with the following
host = 'jetbrains.com'
ping_output = subprocess.check_output(["ping", host, "-c 5"])
for line in ping_output.split('n'):
if re.match("d+ bytes from", line):
Press Ctrl + Shift + F10 to run your code and you'll see the output below.
Python code and output
Connecting to the Database
Using the Databases link on the far right side of the application, connect to the postgress sql database on the raspberry pi.
Since you are connecting to the raspberry pi, you are entering everything as localhost. Also, be sure to use the pi user that we created above.
PostgreSQL Database Connection - General
Next, you'll need to specify the SSH/SSL tunnel settings. Enter this information from the perspective of your host computer. The proxy host would be the static IP address you set up at the beginning of the tutorial.
PostgreSQL Database Connection - SSH/SQL
Open an SQL console to enter the commands to create a table that will store ping information. Enter the following information:
create table pings
recorded_at timestamp with time zone default now(),
create index pings_recorded_at on pings(recorded_at);
Now, you'll need to modify your ping script to be a little more robust. Navigate to the following github link to find the code
Apparently RaspberryPi has this cool program scheduler, so we are going to utilize it to run the ping program. SSH back into the Raspberry Pi and run the command crontab -e to open up cron file. If this is the first time you're opening the file, you will be asked to create a new one. Also, you'll be asked which editor you prefer to use. Add this information to the end of the file.