My Take on the Connected Pi (Part 1)

Inspiration

I have got about three Raspberry Pis knocking about my house, all in various states of incomplete projects. I love to play with them, and get things working, but I never seem to finish the projects as they always seem to involve a bit of extra tech/resources that I don’t have.

But recently I came across a blog entry by Frederick Vandenbosh where he used a Pi Zero to create a small, internet connected, display with two buttons and a range of functions. I decided that I would give it a try, and commit to building it, and extending it. I found his blog entry lacked a few steps for a basic user that can halt a project like this, so I have tried to provide a few bits of information that Frederick hasn’t mentioned.

So firstly, thanks to Frederick for the inspiration, you can check out his blog here for lots of cool Pi Projects.

Getting all the parts

For the case, Frederick’s project involved a 3D printed case, and while I would love a 3D printer, I don’t have one yet, and decided to improvise. I chose to take some Perspex and cut it into an acceptable shape, finish it off with a mix of hot glue and Sugru.

I am working using a Raspberry Pi 3 as a development machine, but will be porting the final product over to a Raspberry Pi Zero before adding it into the case.

I have had to do some soldering for the display and Pi Zero itself, nothing major and easily doable for even a beginner with a few youtube tutorials.

I have some parts from an Arduino starter kit (like a breadboard and buttons) that made life a bit easier.

Sugru
Raspberry Pi 3
Raspberry Pi Zero
Soldering Kit
Arduino Display
Arduino Kit

Setting up the Raspberry Pi

Check out this blog post for setting up your Raspberry Pi.

Soldering the display

I used the kit I mentioned above to solder the adafruit display to the pins that come with it, this means that I can use standard Male/Female wiring or a breadboard to control the device without soldering more and more bits together. I didn’t take any pictures while soldering, as that would have required more hands than evolution blessed me with. There is a good tutorial here on how to solder pins to a PCB. The guy is using an arduino, but it’s a similar size and pin setup.

If everything goes ok, you should end up with something like this. (Remember to put the short bits on the display side, or you will need to connect wires above the display and that will make it pretty hard to fit in a case.

IMG_9166
(Clearly I’m not an expert, but this is good enough for me!) 

We will be wiring up the display and communicating with it using a protocol called “I2C” this is a setting that you need to enable on the pi itself. The best method is to use

sudo raspi-config

the process is described here in great detail. Take note of the two apt-get commands that you need to run to install the libraries required to communicate over I2C.

It is also important to note, that if you are using the Adafruit SSD1306 display like I am, there are two jumpers on the back of the display that you need to solder over to connect and enable I2C on the device (I was pulling my hair out before realising that I had to do this) it should look like this when complete.

IMG_9185
(Note the SJ1 and SJ2 jumpers that are connected)

That is your display ready to be used!

Connecting the display to the Pi

The wiring pattern is the I2C Pattern as mentioned before. This can be a little slower to respond than something like SPI but it uses less pins, so is suited for our fairly compact project.

The wiring is exactly as Frederick mentions in his blog and he provides a great diagram for hooking it up.

Screen-Shot-2015-12-11-at-09.41.55
(The original image is from Frederick’s blog and can be found here)

If you have a breadboard like I have, you can use it to connect the display to the pi without any further soldering. Remember, breadboards connect horizontally so if you plug your display in Rows 1-8 in slot A you can use slot B (or any other) on those same rows to connect to the appropriate pins (As I have done below)

IMG_9169

Preparing the software

Now you need to install some libraries on the Pi so that you can interact with the display, Frederick doesn’t mention that in his blog, but if you follow the steps here you should be up and running. You need to update the apt libraries, install some python and linux tools, install PIP (A python specific package manager), install a Python GPIO library and finally a python image library to allow you to construct images to output to the screen .In short:

sudo apt-get update
sudo apt-get install build-essential python-dev python-pip
sudo pip install RPi.GPIO
sudo apt-get install python-imaging python-smbus

Once that is complete, you need to get the Adafruit libraries for interacting with the display. They are all available on the Adafruit GitHub page. That is done as follows

sudo apt-get install git
git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git
cd Adafruit_Python_SSD1306
sudo python setup.py install

This downloads the Adafruit python libraries for talking to the display, and installs/builds them so that they are easy to use.

Modifying the python files to work with the Pi

Now this is a step that took me a while to figure out, and I didn’t find it documented anywhere. Remote onto your Pi with SSH or Microsoft Remote Desktop (using XRDP) or simply connect it to a monitor, you need to run a command as follows (once the Pi is wired up correctly)

sudo i2cdetect -y l

You should get a grid output, telling you how your display is connected that looks something like this

Screen Shot 2016-07-31 at 17.51.37

In the image above, you can see that 3d is shown. That means I am connected on address 0x3D this is important in a minute.

On the pi, navigate to the Adafruit git folder you downloaded. Mine was located in /home/pi/Adafruit_Python_SSD1306. In here, navigate to the “Adafruit_SSD1306” folder and you can see a file called “SSD1306.py”

One of the first lines in there you will see is

SSD1306_I2C_ADDRESS = 0x3C

and you need to change that to

SSD1306_I2C_ADDRESS = 0x3D

(Note the 3D that we spotted earlier)

That alone isn’t quite enough, as remember we ran an “Install” command a while ago? Well that was compiling this code, and it’s the compiled code that we are running when we reference the library. So we need to re-run the command

sudo python setup.py install

in the root of the Adafruit git folder. i.e. From inside the folder we downloaded with the git command earlier.

Fonts

Now, Frederick uses a font called “Minecraftia.ttf” in his blog post, and in the comments he mentions where it can be downloaded from. If you download the .zip file from here (on your pi using it’s web browser) http://www.dafont.com/minecraftia.font and extract it using package manager (right click on the pi, and extract here).

I tried a few approaches to installing this font and found that the best way was to copy it to a folder beside the code I am running and reference the file directly.

Time to code

Now, Frederick’s prototype has some great code for a little device like this, so I just took his python script exactly and followed his instructions on how to load it at run time (so it runs as soon as the Pi boots up) .. I am not going to try and improve it right now, my only change was to reference the font file in the folder below the script one, so every line like this:

font = ImageFont.truetype('Minecraftia.ttf', 35)

now looks like this

font = ImageFont.truetype('fonts/Minecraftia.ttf', 35)

Your best bet is to just take his script and save it onto your pi, and make that change if you are having issues with the fonts. Here is a link to the  raw script.
Placement of this script is important, it needs to be able to see the Adafruit libraries and the font we mentioned. I copied the folders and files so my directory structure looks like this:

/home/pi/project/
    -- Adafruit_Python_SSD1306 (the git folder)
    --info_display.py (Frederick's program)
    --launcher.sh (Frederick's launcher script)
    --fonts (folder)
         --Minecraftia-Regular.ttf

You can also see Frederick’s blog for writing a launcher bash script and using a cron job to run it when the Pi boots (you don’t have to know what those things are, Frederick explains exactly how to use them to get the script to run on booting up the Pi)

Done!

And that’s it! I copied Frederick’s diagram and added a few buttons to my breadboard to switch between modes, and it worked perfectly. You can also use the IDLE IDE on the Pi itself to run the code by opening the info_display.py file and selecting “Run Module” from the menu, or just reboot the Pi and watch it work. If you run into any errors, run the module through IDLE and it should report any problems.

Hope it all works out well, and thanks very much to Frederick for the blog, this is basically all his work with a few pointers from me!

I’ll post updates soon on constructing a case and migrating the project to a Pi Zero, then onto modifying it to run off batteries and adding new features!

Setting up a Raspberry Pi

Getting Started

First things, first. Setting up the Raspberry Pi from scratch. There are a load of tutorials and resources on the official site for doing this. You could always download NOOBS (their ‘starter’ version of the operating system, which is a simple copy and paste onto a memory card, then following some on screen instructions when the Pi boots) But my process is as follows:

Download Raspbian

The easiest place to do this is on raspberry.org from their Raspbian downloads page

Install Raspbian to the Micro SD card for the Raspberry Pi

The first stage is formatting the SD Card for usage. I am using a Mac so I use SDFormatter and I believe they have a windows version of the application too. Pretty simple to use. Plug in your SD card, and make a note of the name of your card from the App, notably the disk number before the main card name. In the screenshot below mine is “disk6” (either directly or via an adaptor) then click “Format” (See Below) 

Screen Shot 2016-07-31 at 15.09.33

After this you to install your raspbian image onto the card. On a Mac I use the built in command line tool “dd” to do this.

If you aren’t comfortable with command line tools like this, I would recommend using NOOBS.

There is a great tutorial here for installing Raspbian onto an SD card. I used the command

sudo dd bs=1m if=/Volumes/Media/Software/2016-05-27-raspbian-jessie.img of=/dev/rdisk6

As my img file was on an external drive called “Media” and as you can see the last portion says “rdisk6” as I mentioned above, that is the drive number from when I formatted my card.

If “dd” is working correctly, nothing will happen… I know that sounds counter intuitive, but there is no console feedback from “dd” and it can take a fair bit of time to write all the files to the disk. I usually leave for about 10/15 minutes and then come back. If there are no errors, chances are it is still working away. Don’t be tempted to hit return again, just leave it for a while, and when it eventually completes you will get some feedback and the cursor will return to wherever it was before.

That’s it! Plug the SD Card into the little slot on your Pi, power it on, and after the boot sequence it should log you in! The default user is “pi” and the password is “raspberry” if you are going to leave the Pi exposed and online outside a firewall, you might want to change this.

Once my Pi is up and running I launch “terminal” (the command line for Linux based systems) and run

sudo apt-get update

which updates my Pi’s records of what packages are available online, then

sudo apt-get upgrade

to actually upgrade any packages installed on my Pi (there are a bunch by default, not to mention the actual Linux kernel itself) to the latest version.

The “sudo” command at the start of these is the Linux version of “Run as Administrator”; it runs the command with elevated privileges as the updates are going to modify system files, and you don’t want non-admin users to be doing that.

You can access other Pi options by running

sudo raspi-config

From the terminal, which will save you editing config files manually.

At this stage, you are good to go with a fully working Pi. Enjoy!

 

 

 

 

Blog at WordPress.com.

Up ↑