• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Electricity Monitor using Android Things and Firebase Realtime Database

Electricity Monitor

For those who may not know, I live in South Africa, a country where we sometimes battle with electricity.
Hey we even have a multitude of apps that give out "load-shedding" schedules. 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 power at home right now?
  • If not, how long has the power been out for?
  • Is it okay to eat the contents of my freezer?

Introducing "Electricity Monitor"...

phone image

phone image

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.

Setup Requirements

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

  1. Checkout this repository.

  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.

Push Notification setup using Firebase Cloud Functions

If you want to enable notifications for when your power goes off and on, you can follow the below guide. By leveraging Firebase Cloud Functions, we are able to easily send push notifications to any device with the companion-app installed to notify them when the power has been cut.

  1. Make sure you have NodeJS and NPM installed. You can follow the guide here to install it.

  2. Install Firebase CLI tools:

     npm install -g firebase-tools
    
  3. Initialize the required dependencies by navigating into the cloud-functions folder:

     cd cloud-functions
     npm install 
    
  4. Login with Firebase (this will prompt you to login via Browser):

     firebase login
    
  5. Run the following command to deploy the Firebase functions:

     firebase deploy --only functions
    
  6. You should then be able to see the functions appear in the Firebase Console as seen below:

firebase console

Now when your Android Things device disconnects from power/internet, you will receive a push notification to your devices running the companion app. As seen below:

push notification image

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 its not controlling my power. (pull requests are welcome :bowtie:)
  • This should probably be moved into a background service so it can run in the background.
  • You might not understand the need for this app, which is okay, it is useful for me and hopefully fellow South Africans 😄

References

More Repositories

1

MaterialIntroTutorial

[DEPRECATED] Library demonstrating a material intro tutorial much like the ones on Google Sheets
Java
811
star
2

compose-playtime

A repo with a bunch of Compose experiments
Kotlin
329
star
3

ConstraintLayoutDemo

Demo showing the possibilities of ConstraintLayout for the Mastering Android Layouts Workshop
Kotlin
323
star
4

android-arch-components-date-countdown

Java
203
star
5

android-retrofit-test-examples

Examples using different mechanisms of testing for Retrofit in Android
Java
173
star
6

GithubUsersSearchApp

Sample app showcasing how to go about writing tests for Android Apps
Java
99
star
7

adopt-me-compose

🐶 Adopt Me - Jetpack Compose Android Dev submission using Mobius (MVI) and Compose
Kotlin
78
star
8

AndroidDatabaseUpgrades

A demo application showing the correct way to do database upgrades in your Android application using SQLite and the onUpgrade method
Java
70
star
9

android-studio-group-templates-mvp

Example of template for Android Studio, to create a set of files for MVP functionality.
FreeMarker
60
star
10

AnimationFactory

Example of Android Animations for presentation titled "In a world of Pure Android Animation"
Kotlin
54
star
11

android-arch-components-lifecycle

A demo application showcasing the use of the new Android Architecture Components Lifecycle classes. In this example, a Lifecycle Aware Video Player is created using the Exoplayer Library
Java
48
star
12

android-things-motion-camera

Build a motion sensing camera using Android Things
Kotlin
34
star
13

android-demo-mlkit

Examples of using MLkit on Android
Kotlin
34
star
14

android-rainbow-opengl

Sample source code for Android Pride Rainbow Bounding Box written in Kotlin and OpenGL 🏳️‍🌈
Kotlin
34
star
15

android-things-distributed-piano

A distributed piano example using a Raspberry Pi 3, a Piezo and Google Nearby API
Java
33
star
16

public-speaking-cfps

A collection of my CFPs that I've submitted and used at conferences
27
star
17

countdown-timer-compose

#AndroidDevChallege Jetpack Compose Countdown timer
Kotlin
27
star
18

variable-fonts-android-o

Demo app showcasing some variable fonts on Android P
Kotlin
27
star
19

email-app-layouts-android

[WIP] Example layouts for Android using ConstraintLayout and MotionLayout.
Kotlin
15
star
20

remote-config-demo-ssa-launchpad-2016

Java
9
star
21

android-things-workshop-camera

Build a motion sensing camera in this workshop
Kotlin
6
star
22

kotlin-coroutines-android-example

A quick example showing the usage of Kotlin Coroutines on Android.
Kotlin
5
star
23

Leviosa

Set of Android Jetpack Compose Samples
Kotlin
3
star
24

riggaroo

3
star
25

AndroidThings-BlinkingLED

Example blinking LED repo to go with blog post on hardware basics
Java
2
star
26

MinionLiveWallpaper

Java
2
star
27

riggaroo.github.io

HTML
1
star
28

android-studio-file-templates

1
star