Categories
android-things

Android Things – Electricity Monitoring App

For those who may not know, I live in beautiful sunny South Africa (see my tweets for lots of awesome pictures). A few years ago we battled with a lack of electricity. While “load-shedding” is not so common these days, we have numerous unplanned outages. We even have a few of apps that give out schedules when the electricity is scheduled to go off in your area (see here and here).

While these apps serve a purpose, they are not so good when we have unplanned outages.

Which often leaves me asking myself the following questions:

  • Do I have electricity at home right now?
  • If not, how long has the electricity been out for?
  • Is it okay to eat the contents of my freezer?

Introducing “Electricity Monitor”…

I decided to use a Raspberry Pi 3 running Android Things and Firebase Realtime Database to monitor the electricity in my house.

Mainly because Firebase has a VERY powerful tool for monitoring if a client is connected to your Realtime database or not. By leveraging the onDisconnect()  method on the Firebase Realtime database, the server can automatically change some data (or log a time) when a client disconnects from it.

Setup Requirements

In order to get the app running yourself, you need to:

  1. Checkout the repository here.
  2. Create a new Firebase Project here.
  3. Download the google-service.json file from the Firebase Console to both the app folder and the companion-app folder.
  4. Set the Realtime database rules to be read and write for everyone (Firebase Console -> Database -> Rules). (WARNING: This means there is NO security on our database – you should not have these rules in production)
    {
        "rules": {
            ".read": true,
            ".write": true
        }
    }
  5. Deploy the “app” module to the Raspberry Pi or equivalent Android Things device (you need to make sure you have setup your Pi with the Android Things OS).
  6. Deploy the “companion-app” module to your phone.
  7. If you have electricity, you will see a house with lights on and the accumulated time you have had power for. If you don’t have electricity, the Raspberry Pi will lose its power source and trigger the onDisconnect()  callback on the Firebase server. This will then show up in our “companion-app” , it will display how long the electricity has been off for.

There you have it, a way to monitor your power at home using Android Things and Firebase!

Yes I know…

  • There are easier ways to monitor your power at home.
  • This is basically monitoring my Pi’s connection to the internet and not power, which in most cases will be accurate enough for me as I am hardly without internet. (I guess this could be rebranded as – “Do I have Internet at home?”)
  • Yes there is no security on the database right now. Luckily it is not controlling my power and its just a sample (Pull requests are welcome 🙂).
  • You might not understand the need for this app, which is okay, it is useful for me and hopefully fellow South Africans 😃

References

If you enjoy my blog posts, please consider buying me a cupcake!

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.