Featured

Setting up a Raspberry Pi for Swifty IoT

I recently gave a talk on implementing an Internet of Things bridge in Swift using a Raspberry Pi. If you’re interested in following in my footsteps, here’s a quick abridged guide on how to set up your Pi. Many smart people have done the heavy lifting here, I’ve just put the building blocks together. Where appropriate, steps link out to these external tutorials with more information.

Prerequisites:

  • Raspberry Pi 3
  • 8GB+ SD Card
  • HDMI Display
  • USB Keyboard and Mouse
  • A way to connect your Pi to the internet (WiFi or Ethernet)

1. Install Ubuntu

The best distribution of Linux for running Swift on Pi is Ubuntu Mate. Version 16.04 is recommended for compatibility with Swift. You can find instructions for installing it on your Pi here.

2. Install Swift

Swift is open-source and supports Linux as a platform for running the compiler. Getting the whole thing standing up on a Pi is a bit complicated, but luckily Umberto Raimondi has provided an amazing service by hosting precompiled Swift binaries for the Raspberry Pi 3. Instructions for installing those are available here.

3. Start a Vapor project

I found that the easiest way to get a Vapor project started is to just clone a simple sample project and build off it. Here’s a good starting point, git clone this to a directory on your Pi.

4. Add Dependencies

My project used additional libraries, SwiftyGPIO and vapor-apns. To install these, add the following dependencies to the Package.swift file:

        .Package(url: "https://github.com/uraimo/SwiftyGPIO.git", majorVersion: 0),
        .Package(url: "https://github.com/matthijs2704/vapor-apns.git", majorVersion: 2)

Then, run a swift package install

5. (BONUS!) Install Dataplicity

If you want to access your Pi from outside your network, Dataplicity hosts a remote access service designed for Raspberry Pi, and it’s currently free for one device! I can’t recommend them enough, they even provide an externally resolvable hostname for your Pi to use, so you can connect to it from the internet. Here are instructions for setting up Dataplicity

The End!

That should be it! You should now have everything you need to do cool, Swifty IoT things with your Raspberry Pi!

If you have any questions or concerns, feel free to reach out on Twitter: @huebnerob

Advertisements