The Personal Blog of Todd Sharp

Grails on Raspberry Pi Part 2 - Why Grails?

Posted By: Todd Sharp on 3/15/2017 8:05 GMT
Tagged: Grails, Groovy, Groovy On Raspberry Pi, Java, Raspberry Pi

So why Grails on a Raspberry Pi?  It's a valid question and one that I hope to answer with this post.  If you're not familiar with Grails, perhaps I should first answer - what is Grails? says:

Grails is a powerful web framework, for the Java platform aimed at multiplying developers’ productivity thanks to a Convention-over-Configuration, sensible defaults and opinionated APIs. It integrates smoothly with the JVM, allowing you to be immediately productive whilst providing powerful features, including integrated ORM, Domain-Specific Languages, runtime and compile-time meta-programming and Asynchronous programming.

There are a few buzzwords thrown in that description, but put simply - Grails is a fast, intuitive web framework that doesn't get hung up on verbose and redundant configuration files.  If you need a simple site you can do that quickly and easily with Grails.  Conversely, if you need a framework to empower you to build a robust, data-driven site with support for asynchronous programming and REST APIs - Grails has your back there too.  It's as simple and as complex as you want it to be.  

Why Grails on the Pi?

I'd been meaning to get into playing around with the Raspberry Pi for years.  When I finally took the plunge last year I had planned on taking the opportunity to learn some Python since many of the examples and demos I had seen online seemed to use it.  But the more I looked at Python, the more I realized that I already knew a pretty powerful scripting language that could do whatever Python does - Groovy.  Thing is, I've got nothing against Python - it's very readable and I have a few scripts that I have thrown together in the past year that I use often.  I just view it as a different type of hammer - and the one in my tool belt handles all the nails that I use just fine, thank you very much.  

I did have one issue with some of the Python solutions that I ended up finding on the web.  Well, a few issues - but, let's start with this one.  A lot of the examples I found looked like this one that checks the state of a magnetic door sensor attached to the Pi:

Can you spot what bothers me about that script?  Hint:  take a look at line 22.  Yeah, I'm not much of a fan of infinite loops either.  Is this the best practice for Python scripts on the Pi?  Not sure.  But, even for a proof of concept, it leaves a bit to be desired.

So I started to look into Java based solutions for interacting with the Pi's GPIO pins.  After all, that's pretty much the big draw with the Pi for tinkering.  The ability to hook up various sensors and switches and whatnot - basically it's the old '150 in 1' kit from Radio Shack that I used to play as a kid, but more socially acceptable to play with as a 40 year old man.

Yeah, I'm pretty much a dinosaur.

My search for Java based solutions quickly led me to Pi4J.  This got me excited pretty quickly, but then I took a look at a few of the examples like this one (via):

Much bet....uhhh...wait a sec....what is that on line 38?!  

Enter Grails

The problem with standalone scripts is that they're designed to be run once (and put to bed wet?).  So if you want them to continually listen for (and respond to) events you have to keep them from terminating and the only way to do that is with an infinite loop.  Grails solves that problem because it is a web application that runs on an application server so we can do things like create a singleton service to interact with the GPIO pins (via a dependency on Pi4J) and count on the fact that the service will be in memory, ready to respond to our requests at any time.  Since Grails requires nothing more than a single entry in a Gradle script to add a dependency we can easily add Pi4J and create a service that our application can use.

The other added benefit is that Grails lets us create a web accessible front end to control and display data from the various sensors that our GPIO service interacts with.  Plus the Grails plugin ecosystem is pretty huge, GORM is built in for easy Hibernate based object persistence and almost any Java project that you can think of can be easily integrated.  

You'll see in some upcoming posts how easy it is to get started running Grails on the Pi.  You may also be surprised how well it runs even with the limited system resources available on the Raspberry Pi.

Image by 12019 from Pixabay

Related Posts

Querying Autonomous Database from an Oracle Function (The Quick, Easy & Completely Secure Way)

Querying Autonomous Database from an Oracle Function (The Quick, Easy & Completely Secure Way)

I've written many blog posts about connecting to an Autonomous DB instance in the past. Best practices evolve as tools, services, and frameworks become...

Sending Email With OCI Email Delivery From Micronaut

Sending Email With OCI Email Delivery From Micronaut

Email delivery is a critical function of most web applications in the world today. I've managed an email server in the past - and trust me - it's not fun...

Brain to the Cloud - Part III - Examining the Relationship Between Brain Activity and Video Game Performance

Brain to the Cloud - Part III - Examining the Relationship Between Brain Activity and Video Game Performance

In my last post, we looked at the technical aspects of my Brain to the Cloud project including much of the code that was used to collect and analyze the...

Note: Comments are currently closed on this blog. Disqus is simply too bloated to justify its use with the low volume of comments on this blog. Please visit my contact page if you have something to say!