How to protect your device with PiJuice and balenaCloud

Last week we published a blog post reviewing some UPS (uninterruptible power supply) HATS for the balenaFin and Raspberry Pi to keep your device up and running during power outages.

In this post, we will show you how you can easily add the PiJuice to your current project with the magic of multi-containers so that you can keep tabs on the power status of your devices.

PiJuice Board

As mentioned last week, the PiJuice is a HAT for the balenaFin or Raspberry Pi developed by the folks at PiSupply that was founded on Kickstarter back in 2015 and raised over 1200% of the target amount.

The HAT comes packed with a Motorola BP7X 1820mAh battery, which will keep your device running for a few hours depending on the project. If your system needs more juice, you can swap the battery to another one with higher capacity, PiSupply offers batteries with a capacity of up to 12Ah for power-hungry projects or long run-time requirements.

The board comes packed with many features, one of them being a real-time clock (RTC) chip that communicates with the Pi and ensures the device never loses track of time. Another useful feature is the presence of an STM32-F0 microcontroller that can communicate with the Raspberry Pi and offers smart power management, allowing you to trigger a soft-shutdown or even turn your device back ON when power is restored.

PiJuice Board 2

The PiJuice will use five GPIO pins from the device (power and I2C) and will allow you to communicate with the battery management system via I2C, to make your project even more robust. If you would like to know more about the PiJuice, you can check the official documentation here, with hardware and software examples plus much more.

After spending some time with the board, the next logical step was to adapt the original code to work with balenaCloud on a multi-container project, so that it would be easy to improve any existing project with this UPS board.

balenaCloud PiJuice

The code for the multi-container project can be found at https://github.com/balena-io-playground/balena-pijuice.

As soon as the container starts running, it communicates with the battery and creates a list of tags, which are updated every minute with the current battery data, such as state of charge, temperature, voltage, and more. The full list of tags along with description can be found in the table below:

TAG list:

TAG Description
START_TIME The time when the container started [yyyy-mm-dd hh:mm:ss].
charge Current battery charge state in [%].
ibat Battery current [A].
iio System current [A].
power_input PRESENT if the system is being powered from the PiJuice input. NOT_PRESENT otherwise.
power_input_board PRESENT if the system is being powered from the Raspberry Pi input. NOT_PRESENT otherwise.
temperature Battery temperature [C].
vbat Current battery voltage [V].
vio Current system voltage [V].

Receiving SMS message on power loss

Another useful feature added to the project is the option to receive a text message on your mobile phone in case of power loss. For that, you need to create a TWILIO account and setup a text message environment. Once that is completed, on balenaCloud, add the following Device Environment Variables:

ENV VAR VALUE
TWILIO_ALARM True
TWILIO_FROM_NUMBER +1...
TWILIO_NUMBER +1...
TWILIO_SID ...
TWILIO_TOKEN ...

So that your environment variables will look similar to the image below:

Now whenever your device loses power, you will receive an hourly notification on your mobile reminding you that the device is running off the PiJuice battery only. Of course, you can change the code to fit your needs.

Here is my balenaFin running exclusively from the PiJuice Hat 😎😍.

balenaFin

You can buy the PiJuice board from the PiSupply website at https://uk.pi-supply.com/products/pijuice-standard for £47.99, or around US$63.

Conclusion

In this post, we review the PiJuice HAT and showed how easy it is to add it to an existing project with the use of our multi-container capabilities. What will you build with it?

Thanks for reading! If this post has raised any questions or you'd like share your thoughts, please join us in our forums, on Twitter @balena_io, on Instagram @balena_io or on Facebook.

Until next time!

comments powered by Disqus
Terms of Service | Privacy Statement | Master agreement | Copyright 2019 Balena | All Rights Reserved