Running a Clojure Pedestal application on Raspberry Pi model B revision 2

Jul 15, 2019

Compiling a fat JAR (uberjar) from the Pedestal application

Given that we already have a working application with an endpoint (/temperature) we now can deploy it and run it. Thinking on where this app should be run, I noticed that I have sitting around a Raspberry Pi Model B Rev 2. For those who might ask what processing power this little device has, this is fitted with 512MB RAM and an ARM CPU running at 700MHz. Not a very powerful device, but good enough for our little Clojure app.

If you have a Raspberry Pi sitting around and you want to find out what version it is, just type in the command line:

pi@raspberrypi:~ $ cat /proc/device-tree/model

And you should see something similar on your screen:

Raspberry Pi Model B Rev 2pi@raspberrypi:~ $

On the development machine, just go to the root folder of your Pedestal application and run:

➜  pedestal-api git:(master) lein uberjar
Compiling pedestal-api.server
INFO  org.eclipse.jetty.util.log  - Logging initialized @20621ms to org.eclipse.jetty.util.log.Slf4jLog
Created /Users/x/Projects/clojure/pedestal-api/target/pedestal-api-0.0.1-SNAPSHOT.jar
Created /Users/x/Projects/clojure/pedestal-api/target/pedestal-api-0.0.1-SNAPSHOT-standalone.jar

And the output should be a JAR file in target/pedestal-api-0.0.1-SNAPSHOT.jar. This file contains everything that is needed for the app to run on a system capable of running Java 8 applications.

Install Java 8 on Raspberry Pi Model B Rev 2

In this case, the CPU architecture restricts our options in which JRE (Java Runtime Environment) to install. I struggled a little bit until I managed to find a version that works on Raspbian Buster Lite (my OS on Raspberry Pi).

Let’s start clean and remove any traces of the OpenJDK:

sudo apt-get purge openjdk-8-jre-headles

Now let’s install Java 8:

sudo apt-get install openjdk-8-jre-headless
sudo apt-get install openjdk-8-jre

In the end the output should show Java 8 installed

java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-8u212-b01-1+rpi1-b01)
OpenJDK Client VM (build 25.212-b01, mixed mode)

SSH into the Raspberry Pi

Until we will setup continuous deployments onto our little server (Raspberry Pi) we will do it by hand. We just ssh into the box and copy the uber JAR.

Enable SSH onto Raspberry Pi

My distro of Raspbian comes with SSH disabled by default. This is a headless distro and all you need to do to enable SSH at boot time is to place a file named ssh onto the boot partition of the SD card. No file content is required and no extension. Raspbian will pickup the file at boot time and enable SSH.

More information can be found into official documentation.

Copy the uber JAR via SSH

To copy the uber JAR we will be using scp (scp – secure copy (remote file copy program)).

scp target/pedestal-api-0.0.1-SNAPSHOT-standalone.jar pi@192.168.1.151:/home/pi/clojure-jars

Arguments passed to the scp command:

  • File to copy: target/pedestal-api-0.0.1-SNAPSHOT-standalone.jar
  • Destination: username@host:location

Start the Pedestal app on the Raspberry Pi

To start the Clojure app we need to SSH into the server and run the JAR as any Java archive.

➜  pedestal-api git:(master) ✗ ssh pi@192.168.1.151
pi@raspberrypi:~ $ java -jar clojure-jars/pedestal-api-0.0.1-SNAPSHOT-standalone.jar

It might seem that the command is stuck, but be patient. We need to reminder that this is a less powerful device and it needs some time to start. After the server starts, you should see something like this into your terminal:

pi@raspberrypi:~ $ java -jar clojure-jars/pedestal-api-0.0.1-SNAPSHOT-standalone.jar
INFO  org.eclipse.jetty.util.log  - Logging initialized @93062ms to org.eclipse.jetty.util.log.Slf4jLog

Creating your server...
INFO  org.eclipse.jetty.server.Server  - jetty-9.4.z-SNAPSHOT; built: 2019-04-29T20:42:08.989Z; git: e1bc35120a6617ee3df052294e433f3a25ce7097; jvm 1.8.0_212-8u212-b01-1+rpi1-b01
INFO  o.e.j.server.handler.ContextHandler  - Started o.e.j.s.ServletContextHandler@40e9cd{/,null,AVAILABLE}
INFO  o.e.jetty.server.AbstractConnector  - Started ServerConnector@ce284d{HTTP/1.1,[http/1.1, h2c]}{localhost:8080}
INFO  org.eclipse.jetty.server.Server  - Started @97551ms

This means that your server is started and is ready to accept connections.

Test the app via curl

At this stage, the application is not accessible outside of the Raspberry Pi box, so a way to test it is to use curl (curl - transfer a URL).

pi@raspberrypi:~ $ curl localhost:8080/temperature
{"celsius":0,"fahrenheit":32}

What we achieved so far

  • We packaged a Pedestal web service into an uber JAR
  • We installed Java8 on Raspberry Pi Model B Rev 2
  • We enabled SSH onto the Raspberry Pi and we connected to it
  • We copied the uber JAR onto the server via scp
  • We started the web service and tested via curl

Tags: programmingclojureraspberrypipedestal

Archives

  1. February 2024
  2. Maximizing Software Development Productivity: The Power of Flow and Minimizing Interruptions
  3. December 2023
  4. Clean Code in Java: Writing Code that Speaks
  5. Clean Code in Java: A concise guide
  6. Understanding Value Objects in Java: A Brief Guide
  7. August 2023
  8. Consuming RabbitMQ Messages with Clojure: A Step-by-Step Tutorial with Tests
  9. January 2023
  10. Running a Spring Boot service with kubernetes
  11. December 2022
  12. Hosting a PWA with Jekyll and Github pages
  13. November 2022
  14. Global Day of Code Retreat
  15. Facilitating a mini Code Retreat
  16. October 2022
  17. The Curse of Optional
  18. September 2022
  19. Testing Spring Boot Microservices - Presentation
  20. March 2022
  21. TDD Workshop
  22. February 2022
  23. Value Objects in Java
  24. Efficient Java
  25. January 2022
  26. Spring Boot testing - Focus on your changes
  27. Product users - Personas
  28. December 2021
  29. Write code fit for testing
  30. November 2020
  31. Running a Spring Boot app with kubernetes
  32. September 2019
  33. Setup GPG on Mac and sign git repositories
  34. July 2019
  35. Running a Clojure Pedestal application on Raspberry Pi model B revision 2
  36. Clojure from zero to hero (part 3) - First endpoint
  37. Clojure from zero to hero (part 2) - A bit of syntax
  38. June 2019
  39. Clojure from zero to hero (1) - explaining project.clj
  40. Clojure from zero to hero (0) - creating a Pedestal app
  41. November 2017
  42. Introduction to Docker
  43. April 2015
  44. Git micro commits
  45. July 2014
  46. Google Glass Development - setup tools, environment and turn on debugging on Glass
  47. June 2013
  48. How To: Get the rendered HTML of a webpage with Python
  49. Set union of two lists in Python