Solar Panel Monitoring

Android application

The Android app contains 5 main parts.

  • The main application showing continuously the measured values from the Arduino board
  • The database and functions to access the database
  • The app widget which show the last measurements on the device home screen
  • The background service that syncs the Android once every day with the Arduino board
  • The reboot service which starts update timers for the app widget and the synchronization service automatically after the Android device was switched on or rebootet.

I will not go into every small detail of the source code, instead I will show the important code snippets of each modul.

The main application

The SplashActivity

When the application is started the first activity that is called is the SplashActivity. SplashActivity general program flow:

Searching for the Arduino board

Arduino board found

Optional the IP address can be entered manually.

The search for the Arduino board is done in an AsyncTask =>

Check out SplashActivity.java for details.

The main activity

First activity here is to synchronisation the local database of the Android device with the database on the Arduino board. Again this is done in an AsyncTask =>

The synchronization can take some time, that’s why it needs to run in the background. After the synchronisation is finished the UI will show today’s measurements and a timer is started to update the information every minute.

Continuous update view

Only solar production visible

Only consumption visible

As you can see I used a screen from emoncms.org as a template for the main UI.

The screen shows in the top row the current power consumption (blue), the production of the solar panel (light orange) and in the middle if I export or import power to or from the power network. Below the maximum values are shown.

The chart view shows the overview from midnight to the current time. The blue line shows the power consumption. This line can show negative values if the solar panel produces more energy than the house consumes. The light orange line shows the power production of the solar panel.

The charts can be hidden with the checkboxes in the lower part of the screen. On the next screenshots only one of the charts is visible:

Only solar production visible

Only solar production visible

Only consumption visible

Only consumption visible

This chart is created with the open source graph library MPAndroidChart. It is a nice library and easy to use. You should check it out if you need some charts in your Android app. It offers a wide range of different chart types.

The chart can be zoomed horizontally and vertically. If zoomed it is as well possible to scroll through the chart. A tap on any part of the chart shows a small window with the values at this point.

The lower middle part of the screen is used for status messages. It shows after every update the values read from the Arduino board.

The buttons above the chart (from right to left):

Close => close the application

Stop (Start) => stop or start the continuous update of the chart

Refresh => sync the data of the device with the Arduino board

Status => show the calibration values used on the Arduino board in the status message window

<< and >> => show historical data

With the << and >> buttons the mode of the UI changes. With every press on one of these buttons historical data from the previous or next day is shown instead of the continuous refreshed data.

Historical data

Pressing the start button goes back to the continuous mode.

Two functions that are still missing is to backup the database to a file/cloud/PC and to restore data.

The app widget

App widgets are small windows that can be placed on the Android home screen. They can be used to show information or data of the application on the home screen without having the application running. I use this here to have the three main values (consumed power, exported or imported power and produced power) always visible when I use my phone or tablet.

The app widget uses its own timer

to update the data every 1 minute. The refreshed data is pulled from the Arduino board

with help of the okhttp library which has easy to use functions to communicate over a network.

The database functions

I use an extension of the SQLiteOpenHelper to get easy access to a local database on the Android.

The database is automatically generated when the application is opening it the first time

All necessary functions to access the database are implemented in the

The synchronization service

The Arduino adds every minute a data set into the database. If the Android application is starting, the amount of data that needs to be synced can be quite large and take a long time to synchronize. To shorten this I created a background service that is called once a day to synchronize the databases of the Arduino and the Android device. For the synchronization the script query.php on the Arduino is called.

Again the okhttp library is used to access the Arduino board.

The reboot service

It is necessary to start the timers for the app widget updates and the daily synchronization again after the Android device is switched on or was rebootet. This is done in the AutoStart class, which is called by the Android OS every time the device starts or was rebootet.

The AutoStart class checks if there are any app widgets active and starts the update timer if necessary. The timer for the daily synchronization is started every time.

That’s it for the Android application. I didn’t go into details here, because the source code is quite big. But if you are interested you can check out the whole source code from my Github repository.

Finally on the last page you can find the parts list, the programming IDE’s I used and required software libraries.

Loading...
Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published. Required fields are marked *

Free Link Directory