Setup your Raspberry Pi 2/3 with Raspbian headless (without cables)
As mentioned in a previous article, I need to control some electronic circuit with the GPIOs pins of the Raspberry Pi. I could easily achieve that with Python or C. But I am a cool kid. I want to use Swift.
Jokes aside, I work full-time with Swift, so I got pretty good at it. And I think it will be a great language for robotics.
The plan is that I am going to install Raspbian (Debian customised for Raspberry Pi), configure it and then install the Swift 3.0 binaries.
Background excursus
The Swift team is supporting Ubuntu as their main Linux distro. Ubuntu is great. But to setup a basic Ubuntu installation, you need a monitor and keyboard. With Raspbian, you just turn the RPi on and after 2 minutes it has configured itself. It will have a default user ready. And it comes with installed WebMD, which exposes the machine to the network. So you can immediately ssh into the it. Pretty handy.
Step 1: Preparation
Get your Raspberry Pi 2 or 3, an SD memory card with 8Gb or more memory (class 10 or more), a micro USB cable and an ethernet cable and the Wifi USB dongle.
Step 2: Flashing Raspbian on the SD card
Start by downloading Raspbian Jessie Light (no GUI. GUIs are for posh folks. or kids. or non-techies. or people who want to actually do some productive work. not for you).
No you have to flash the image you just downloaded to the SD card. On the web you find a lot of multi step command line guides. But that’s nuts, since you have a great apps, called Etcher.io, that make the process incredible simple, faster and safer.
Download Etcher app (for all platform), then select the Raspbian image, the SD card (which you have to insert in your computer) and flash it. Few minutes and it will be done and verified.
Put the SD card into the RPi, you’re ready to rock!
Step 3: Ssh-ing into your RPi
After you’ve inserted the freshly backed SD card into your RPi connect the ethernet cable between the RPi and your router. Turn the RPi on by connecting the micro USB cable to the electricity or to your computer.
Wait 2–3 minutes, then open your terminal and type:
ssh pi@raspberrypi.local (password raspberry)
You should be in! That’s badass like the Administrator in the movie Hackers.
You are now connected to the RPi via the network. The only problem is that you still need to keep the RPi connected to the network through the ethernet cable. But let’s address that.
Step 4: Going wireless
Attach the USB dongle to your Raspberry Pi. Then, while connected to the RPi with ssh, open the wireless configuration file:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
and add at the end of the file the informations about your wifi network:
network={ ssid=”your-network-ssid-name” psk=”your-network-password” }
Save and exit the editor. Remove the ethernet cable and then reboot with:
sudo reboot
Now in about 30 seconds your RPi should be up an running. If you try again to ssh into the RPi should just be able to connect as before. But on wifi!!
Step 5: Final configurations
I suggest you to secure your login with a rsa key authentication and enabling it only the port 22. Here a guide.
A classic system update is also suggested:
sudo apt-get update sudo apt-get upgrade
The-wise-guy-last-step: Backup your RPi
It’s very easy to burn an SD card by writing to it too many times. Or irreversibly fuckup your configuration by doing what sudoer should not do. Cloning your SD card, as an image that you can flash on a new card when you need it, is the perfect backup strategy. Here you find how.
You are now ready to install Swift on your hot-n-sexy Raspberry Pi.
To get my latest Swift misadventures subscribe to the publication. Cheers.
Installing Swift 3.0 preview on a Raspberry Pi 2/3 with Raspbian
We’re halfway trough our journey of controlling electric circuits and hardware with Swift and a Raspberry Pi. If you missed anything, here lie the answers to your questions.
Ssh into your RPi:
ssh pi@raspberrypi.local (password raspberry)
Open the apt sources file:
sudo nano /etc/apt/sources.list
Uncomment the last line:
deb-src http://archive.raspbian.org/raspbian/ jessie main contrib non-free rpi
Update your system and install some required packages:
sudo apt-get update sudo apt-get upgrade sudo apt-get install clang git
Installing Swift 3.0 build at root level. We’re going to use House Dillon’s tarballs:
cd ~ sudo wget http://www.housedillon.com/other/swift-armv7-3.0.tar.gz sudo tar -xzpf swift-armv7–3.0.tar.gz -C / rm swift-armv7–3.0.tar.gz
You have installed Swift!! Now start screaming like Steve Ballmer in a moment of uncontrolled excitement.
Let’s write our first Swift script to see that it’s actually working:
mkdir swifttest cd swifttest nano main.swift
Add this simple hello world (which ironically doesn’t say “hello world”):
import Glibc import Foundation
let currentDate = NSDate() print(“My first Swift binary! Executed at \(now)”)
Compile and run:
swiftc main.swift ./main
Well done! You’re ready for the world. Now go and write beautiful swift code.Or turn some LED on with Swift!
Using Swift to control the Raspberry Pi GPIO pins and turn an LED on
Here we are with a Raspberry Pi with Raspbian and Swift 3.0 installed. Now we can actually do something with it. Like flashing an unsexy LED. But hey, we have to start from somewhere…
Step 1: Understanding the GPIO pins numbers
The core concept is that not all the pins in available on the board are GPIOs (General Purpose Input/Output). Some are power supply, others are ground connection and others are other-stuff.
The number inside the circle is the pin number. Some of these pins match aGPIO pin. For example the pin 7 matches the GPIO4 pin.
We can control only the GPIO pins, not the other ones. The other ones are used to power up your circuit or to do other-stuff.
Step 2: Connecting the circuit
We want to create a simple circuit where an LED is turn on by the GPIO4 (pin 7). So we create a simple circuit like this:
Step 3: Making sure we can actually set a value on a GPIO pin
We are installing a package that allow us to set values on a pin from command line. Just to make sure we’re actually able to turn the GPIO value high and low.
sudo apt-get install wiringpi
Now let’s set pin 7 as output and turn it off, then on and then off again:
gpio mode 7 out gpio write 7 0 gpio write 7 1 gpio write 7 0
It should work. If it doesn’t, well, it’s very likely you’ve made some wiring mistake in your circuit.
Step 4: Using SwiftyGPIO library to control the GPIO pins
Uraimo has created a very nice Swift wrapper around the GPIO pins, so we can control them in our Swift app. There are few things that could be done better (e.g. more protocol oriented representation of the different GPIOs types, better representation of the GPIOs states and more immutability of a GPIO representation), but it’s still a good starting point.
Since the Swift Package Manager is still not working properly on Raspbian to date (July the 8th, 2016), we are going to clone the git repo and use the file directly within our code.
The important bit is to have all the sources in one folder, to be able to compile them all in one simple command.
mkdir ~/ledtest mkdir ~/ledtest/Sources cd ~/ledtest git clone https://github.com/uraimo/SwiftyGPIO.git cp SwiftyGPIO/Sources/* Sources/
Let’s create our main:
nano Sources/main.swift
with this code:
import Glibc
// Get the Pin where the LED will be attached to let gpios = SwiftyGPIO.GPIOs(for: .RaspberryPi2) guard let ledGPIO = gpios[.P4] else { fatalError(“It has not been possible to initialised the LED GPIO pin”) }
// Set the GPIO to output ledGPIO.direction = .OUT
// Turn on and off the led few times ledGPIO.value = 1 sleep(1) ledGPIO.value = 0 sleep(1) ledGPIO.value = 1 sleep(1) ledGPIO.value = 0
Now compile and run as super user (it needs sudoer permissions to access the GPIO pins):
swiftc Sources/*.swift sudo ./main
The LED should blink two times and then stop. Success!!
Step 5: Cleanse our soul with some cleaner code
I feel dirty in writing hacky scripts. I start regretting all my life decisions when I understand I wrote something that’s not even close to be acceptable good. So the following code is the v2 of the app.
But first I really suggest you to configure your Atom or SublimeText editor (on your main computer) to edit code remotely on your RPi, so you don’t have to use nano or vim. (Info for Atom and for SublimeText).
That should be it. Compile and run. The LED should keep blinking until you stop the execution.
This is the basic you need to know to start doing amazing things in electronics with a very nice programming language and all the things that comes with it.