Kotlin on a Raspberry Pi

Why JVM?

Setting up the Raspberry Pi Itself

Downloading the OS

Setting up Wifi on the Raspberry Pi

  1. Plug your Raspberry Pi’s SD card into your computer
  2. 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>"
}

Enabling SSH

Connecting to the Raspberry Pi for the first time

$ 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
$ ping raspberrypi.local
ping: cannot resolve raspberrypi.local: Unknown host

Changing the root Password

Adding Java

$ 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)
$ sudo apt update
$ sudo apt install default-jdk

Getting Your Environment Ready

Installing IntelliJ

Installing the Embedded Linux JVM Debugger Plugin

The Programming Part

Project Setup

  • Choose a Name for your Raspberry Pi project, and a Location to save it (any name you want and any where you want)
  • Choose the Console Application under Project Template
  • Choose Gradle Kotlin for the Build System
  • 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.
  • Click Next
Default options here, feel free to change the Test Framework, if you want.

Initial Test Run

Program output of the initial code

Testing on the Raspberry Pi

Controlling the Raspberry Pi

  1. Add Pi4J and Pi4K to your project’s dependencies
  2. Use the new dependencies in your code
  3. Run your code

Adding Pi4J and Pi4K as Dependencies

Using the New Dependencies in your Code

Bonus: Simulating a Raspberry Pi

Deploying to the Pi

Creating a Fat Jar (Uber Jar)

Setting up ShadowJar

Actually Creating your Fat Jar

Deploying your Fat Jar

Running your Fat Jar

Done

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pete32

Pete32

Peter is an engineer at Amazon. Any opinions written are strictly my own and do not represent Amazon in anyway.