Kotlin on a Raspberry Pi. There are a bunch of articles and…
Tags: Kotlin, Raspberry Pi, Software Development, Java, Programming

There are a bunch of articles and videos on how to use Kotlin on a Raspberry Pi, but they all are focused on using Kotlin/Native to compile code directly to the Pi. What if you want to use Kotlin on the Raspberry Pi, while not worrying about cross-compiling? Kotlin on the good old JVM is your answer!

TLDR: All of my code has been uploaded to this github repository.

Why JVM?

The JVM is a great piece of engineering, it’s fast, battle tested, and it runs pretty much everywhere. You also get the benefit of a huge ecosystem of tools and libraries.

That all sounds pretty great, now how do we do it?

Setting up the Raspberry Pi Itself

If you’ve already got a Raspberry Pi setup, you can skip to Adding Java, otherwise, let’s start at the very beginning.

Downloading the OS

The first step is download the OS from the Raspberry Pi website. There are a bunch of options, but the easiest way is to use the Raspberry Pi Imager tool that is linked at the top of that page. If you’d rather, you can download OS image directly, and flash it onto your SD card with your tool of choice.

Setting up Wifi on the Raspberry Pi

Once you have the OS flashed onto your SD card, the next step will be to get your Raspberry Pi connected to your local network. This is really easy if you have a monitor with a keyboard and mouse connected it (you can just set it up using it’s GUI like any other computer), but what if you don’t have either of those, or what if you flashed the “lite” version of the OS that doesn’t include a GUI by default? Luckily for us, there’s a way to get the Raspberry Pi connected to wifi without any of those things.

Plug your Raspberry Pi’s SD card into your computer Create a wpa_supplicant.conf file in the Raspberry Pi’s boot folder, it’s contents should be the same as below:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

update_config=1

country=<Insert 2 letter country code here>



network={

ssid="<Name of your wireless LAN>"

psk="<Password for your wireless LAN>"

}

Remember to set the name of your network, its password, and your 2 letter country code.

The next time your Raspberry Pi boots, it’ll connect to your wifi! Now since you have the Pi’s SD plugged into your computer already, let’s enable SSH too.

Enabling SSH

Enabling SSH will allow us to control the Raspberry Pi remotely. It allows us to easily deploy files and debug our code, without needing direct access to the Raspberry Pi itself. It’s super important to enable.

There are a few ways of enabling SSH, but the easiest is by creating a blank file in the root of the Raspberry Pi’s SD card named SSH (note the file doesn’t have an extension). If you are using Linux, or a Mac, the easiest way to do this is by typing the following command into your terminal: touch /PATH_TO_RASPBERRY_PIS_SD_CARD/SSH . The touch command will create a blank file for you. If you’re using Windows, you can open the SD card in Explorer, right click and choose New, then choose text file, enter the name SSH, and finally delete the .txt file extension.

Connecting to the Raspberry Pi for the first time

At this point, your Raspberry Pi should be setup and ready for its first boot. You can take your SD card out of your computer and plug it into your Raspberry Pi. Plug in your power cable and give it a few minutes to start up, some of the lights onboard should start to light up and blink. After waiting a few minutes, it’s now time to test out and see if everything you did so far worked. We are going to try and remotely connect to the Raspberry Pi for the first time.

To connect to the Raspberry Pi, we need to first figure out its name on our local network. The easiest test is by typing the following command into your terminal: ping raspberrypi.local . If it’s successful, it’ll result in something like this filling your terminal:

$ ping raspberrypi.local

PING raspberrypi.local (192.168.1.131): 56 data bytes

64 bytes from 192.168.1.131: icmp_seq=0 ttl=255 time=2.618 ms

If the results look like this instead:

$ ping raspberrypi.local

ping: cannot resolve raspberrypi.local: Unknown host

You’ll need to do a little more work to figure out what the Raspberry Pi is called. There are a few more suggestions here to help. Most likely you’ll need to call the Pi by its IP address, rather than raspberrypi.local.

The ping command will keep repeating until you stop it. Pressing CTRL + c on your keyboard will put an end to it. Now that we know the Raspberry Pi’s name, we can connect to it.

Still in your terminal, type: ssh pi@raspberrypi.local or ssh pi@192.168.x.x (if you’re using its IP address). Your computer will now connect and “SSH into” your Raspberry Pi. It should prompt you to enter in a password, which by default is raspberry . If it asks you a yes/no question about “finger prints”, you can just type out “yes”, and you’ll see that it’s connected. You can now use your terminal to move around your Raspberry Pi, as if it was your personal computer.

Changing the root Password

While not needed, it’s good practice to change your Raspberry Pi’s root password away from the default. While not likely, this will make it more difficult to hack.

While logged into your Raspberry Pi’s pi account, type the command passwd . This will ask you to enter in the current password, then your new password, then to confirm your new password.

Adding Java

We have one final thing to set up on the Raspberry Pi itself: making sure Java is installed. If you have the standard “full” version of Raspberry OS, Java is probably already installed. To test, in the terminal where you’ve SSH’d into your Raspberry Pi, type java -version . If it outputs something like the following, you’re good to go and can skip to the next section:

$ java -version

java version "11.0.1" 2018-10-16 LTS

Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)

Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

Otherwise, you’ll need to install Java. Luckily for you, this is actually pretty easy. All you need to do is run the following two commands to install it:

$ sudo apt update

$ sudo apt install default-jdk

Once this is finished, you can check that Java was install properly by running the java -version command again, it should now output something similar to what I outlined above.

Awesome! That takes care of setting up the Raspberry Pi, now onto the good stuff! You can now log out of your SSH session by entering the command exit into your terminal, we are going to focus on your host computer for a bit.

Getting Your Environment Ready

You can write Kotlin using pretty much any text editor, but IntelliJ is an awesome IDE (and it’s free) and a great tool for Kotlin development. Both IntelliJ and Kotlin were created by the same company, so its Kotlin support is first rate.

Installing IntelliJ

You can download and install IntelliJ directly from their website. We will only need the free Community edition, and not the Ultimate one. Once it’s downloaded and installed, open it up and work through the “first time setup wizard”. You should eventually come IntelliJ’s start screen, where you can choose to start a new project/open an existing one.

Installing the Embedded Linux JVM Debugger Plugin

Now that we have IntelliJ all setup, we are going to install a plugin that will make running and testing our Kotlin code on our actually Raspberry Pi way easier. It’s called the Embedded Linux JVM Debugger plugin and it allows you to run, deploy and debug Kotlin code directly on your Raspberry Pi via IntelliJ’s UI. It’s a really time saver and makes the overall development experience substantially nicer.

From the IntelliJ welcome screen, click on the Configure in the bottom right corner. This should open a drop down menu, from that menu, click on Plugins. Select the Marketplace tab at the top of the window and start typing Embedded Linux JVM Debugger, and click the Install button. The plugin should start downloading and installing, once finished, click the Okay button to finish up and return the welcome screen.

Now that we have our environment set up, let’s get programming!

The Programming Part

Before we get into the code, we have to step through the project wizard, to set everything up.

Project Setup

Click on the + New Project button to get started. You’ll be greeted with a huge amount of options to choose from. For our use case, it’s pretty easy, we will choose Kotlin from the list on the left. Then set the options from the top:

Choose a Name for your Raspberry Pi project, and a Location to save it (any name you want and any where you want)

for your Raspberry Pi project, and a to save it (any name you want and any where you want) Choose the Console Application under Project Template

under Choose Gradle Kotlin for the Build System

for the Choose a Project JDK that matches what you installed on the Raspberry Pi earlier, if you don’t already have a JDK installed, click on the Download JDK option, and choose one from the list. OpenJDK is a great choice here, but you really can’t go wrong.

that matches what you installed on the Raspberry Pi earlier, if you don’t already have a JDK installed, click on the option, and choose one from the list. OpenJDK is a great choice here, but you really can’t go wrong. Click Next

On this next screen, you can leave everything default:

Default options here, feel free to change the Test Framework, if you want.

Click Finish and it’s time to get started!

Initial Test Run

This will open up a new window that looks like: