Sunday, April 29, 2018

Creating an External Power Monitoring app for Android

I needed to make a power monitoring system to let people know when the AC power to a critical pump had failed.

I realized an old cell phone would be ideal for this task, so long as it ran Android 4.4 (Kit Kat) or later.  I found several phones between US$20 and US$30 that would do, and these were either fully-functional used phones, or in one case, a new phone!  That's less expensive than a Raspberry Pi, and far more capable, as the phone already includes the cellular modem, the battery system, the display and the touchscreen, not to mention a whole bunch of other sensors and capabilities.

A cheap cell phone is an awesome platform!

The phone would always be plugged into its charger, and the charger would be plugged in to the AC power system to be monitored.  When the phone charger loses power, a text would be sent, and another would be sent when power was restored. The only UI needed was for the user to enter the phone number(s) to which the texts would be sent.

Being a Python rapid-development fanboi, I installed SL4A (via the QPython package in Google Play) and soon had a short console script running that did the basics of what was needed.


Simple, right?

Unfortunately, providing this simple script with a GUI and turning it into an installable Android application package (apk) was frustratingly difficult, so I decided to look elsewhere. But at least I now knew it was truly a trivial app, and I expected no significant barriers on the development side.

I installed Android Studio, the standard Android integrated development environment (IDE), and was overwhelmed by the infrastructure needed to create even the simplest app.  I'm primarily an embedded/real-time algorithm developer, so I'm used to programming very close to the hardware.  There was nothing "simple" here!  The Android platform is extremely capable, and is also quite complex.

One very pleasant surprise using Android Studio was my introduction to Kotlin, a language that pretty much eliminates the boilerplate verbosity and bloat of Java without losing any significant features, while delivering an elegant high-productivity language. I want more of this!

For fun, I also installed Visual Studio for Android, mainly to see if Xamarin's Mono would let me use Microsoft's excellent C# language to quickly develop my app.  Again, I was unable to get even the simplest demo to load, much less build.  And the full installation was nearly 60GB!

I had thought the best way forward would be to get a minimal demo app loaded and built, then modify it to meet my needs.  I was beginning to think I had a real problem trying to do even a "simple" thing with these heavy-weight programming environments.

The other recommended Android development alternative was Eclipse for Android.  But I've had an ambivalent off-and-on relationship with Eclipse for over a decade.  When it works, it's sensational.  But when it doesn't work it can be frustrating to remedy.  So, no, I didn't bother to install it.

I really wanted a straightforward tool that would take code as simple as the Python above, then with a single click would generate a fully functional apk. I had no need to see behind the curtain, so I wanted a wizard to handle all that stuff for me.

A quick search brought me to App Inventor, the ex-Google now MIT project that used the Scratch programming environment that also has a simple drag'n'drop GUI editor.  I had been wanting to play with Scratch anyway, since I plan to help with the local Scratch Day in May.  This seemed like an excellent opportunity to meet multiple goals with a single project!

While everything initially went as planned, I soon found there were features of my app that App Inventor would not support.  I then learned that App Inventor has not been receiving very much love, and there was little hope the shortcomings would be addressed any time soon.

I was delighted to find that the App Inventor code base (it's Open Source) had been cloned and improved by several groups, all of which could import App Inventor's "aia" project file format.  A quick search brought me to Thunkable, where my app both built and ran as expected.

Here's the mockup of the app from the Thunkable GUI editor:


And here's the code that drives the above GUI, with some features added since the Python prototype.  Yes, the code is an image: The Scratch language is itself graphical.


App Inventor and it's clones all share multiple ways to test your app:  Via USB Debugging, via a WiFi interface app, and via a generated apk that can be installed by scanning a QR Code.

What could be easier!

The last piece of the puzzle was to get the Power Monitoring app to always launch when Android started.  The Startup Manager app in Google Play was the right tool for the job, as well as also being able to prevent a slew of default phone apps from starting.