paint-brush
Setting up a Swift development environment on Raspberry Piby@piotr.gorzelany
10,733 reads
10,733 reads

Setting up a Swift development environment on Raspberry Pi

by Piotr GorzelanyFebruary 5th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In my <a href="https://medium.com/@piotr.gorzelany/the-state-of-swift-on-raspberry-pi-f23445418ee2#.4drnxi3pp" target="_blank">previous post</a>, I gave an overview of the current state of Swift on the Raspberry Pi platform. In this post, I will try highlight how to set up a pleasant development environment to write Swift code on the Pi.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Setting up a Swift development environment on Raspberry Pi
Piotr Gorzelany HackerNoon profile picture

Image taken from raspberrypi.org

In my previous post, I gave an overview of the current state of Swift on the Raspberry Pi platform. In this post, I will try highlight how to set up a pleasant development environment to write Swift code on the Pi.

One of the biggest pains right now when writing Swift code on any Linux platform is the lack of a real IDE. Sure, you may hate Xcode but go start typing your program in Nano on the command line and you will see how real misery feels.

You get a lot of these since you do not have compiler warnings at write time

The first step for a good development experience is a good IDE and the best Swift IDE for Linux right now is…. Xcode.

What!? But Xcode does not work on Linux. How do I install it on the Pi? Well, you don’t, and frankly, you would not want to write code on the Pi. It is a slow computer and running it with a monitor and keyboard is unnecessary. So where do I write my code? On your Mac of course, where else dummy.

The first thing you need to do is setup a headless Pi. There is a really nice tutorial on how to do that on the Raspberry forums. The tutorial is written for Raspian and I recommend using Ubuntu for the Pi since the SPM works there. I first ran my Pi with a keyboard and monitor and did the initial setup and connected it to my WiFi network. Once you have the Pi connected to your router you can start controlling it by SSH from your mac. First, you need to find the IP address of your Pi on the network. I do that using nmap which scans devices in your network that listed on port 22 (ssh port).

To find your Pi, you need to replace the 192.168.6.0 part with your local address space. Once you have it, connect your mac through ssh.

ssh {your-pi-username}@{you-pi-ip-address}

And there you go, the sky is the limit now. Try to build and run your first Swift script remotely on the Pi.


// Create the scriptecho "print(\"hello from swift\")" >> hello.swift


// Run itswift hello.swift

This is all fine and dandy but what are you still doing on the command line? Go fire up Xcode and begin some serious development!

We will create a program that will switch on and off an LED connected to the Pi if you press the return key on your Mac. To switch nicely between the two environments we will use the Swift Package Manager, which is a system-agnostic dependency manager for Swift. It runs fine both on OS X and on Linux and it will be a vital part that will enable us to develop code on the Mac and run it on the Pi.






// create the project foldermkdir led-blink// go to the foldercd led-blink// initialize the swift project using SPMswift package init --type executable

You should now have the directory structure and main.swift file generated by SPM. Edit the Package.swift to include the GPIO swift library.

import PackageDescription






let package = Package(name: "led-blink",dependencies: [.Package(url: "https://github.com/uraimo/SwiftyGPIO.git", majorVersion: 0)])

Now fetch the library using the command:

swift package update

You can now use SPM to generate an Xcode project for you! Do this using the command:

swift package generate-xcodeproj

Open the generated Xcode project and build it (cmd+B), so the compiler knows about the SwiftyGPIO library. Voila, you can now use Xcode to develop your program, having access to all those sweet tools like code completion, syntax highlighting, compiler errors, documentation and more.

Open the main.swift file and add the following code:


import SwiftyGPIOimport Foundation


// Get a dictionary of all the gpio pinslet gpios = SwiftyGPIO.GPIOs(for: .RaspberryPi2)




// Get the pin that you connected the LED to. Remember to set the right pin number, for me it was 27guard let ledGpio = gpios[GPIOName.P27] else {fatalError("Could not initialize the gpio")}



// Set the pin direction to .OUT and turn it offledGpio.direction = .OUTledGpio.value = 0

// Read user input from keyboard and switch the LED on/off each time the user presses the return key





print("Press return to switch the LED on/off. To quit type exit")while let userInput = readLine(strippingNewline: true), userInput != "exit" {print("Switching LED")ledGpio.value = ledGpio.value == 0 ? 1 : 0}

Ok, the program is completed. The only thing left to do is to build and run it on the Pi. To do this, you will need to copy the project you created on your mac to the Pi. There are a couple of solutions how you might do it. For one, you could create a GIT repository and host it on Github. Then you would push the changes from your mac, and pull them on the Pi. Another option is to use SCP to copy the folder.

scp -r {path-to-project-folder-on-mac} {your-pi-username}@{you-pi-ip-address}:{path-to-folder-on-pi}

Once you copied the project, you can ssh to the Pi and go to the project folder. The folder has all the files there but you still have to build it using the compiler on the Raspberry.

swift build

Should create an executable which you can then run and marvel at your work! Here is the masterpiece I’ve created in action.

Raspberry Pi controlled by a MacBook Pro

Conclusion:

Developing for the Pi is not that painful once you can use your familiar environment on the Mac. Using this technique you can develop your programs faster using Xcode.

The workflow looks roughly like this: Write code on Mac -> Copy to Pi -> Build and run on Pi

This process can easily be completely automated but I just didn’t have time to do it.

Using this setup I plan to use Xcode to develop a server application, run it on the Pi, and control LEDs with my iPhone. Fingers crossed.