Graphing My Solar Panels (Open Source)

by @edent | # # | 1 comment | Read ~1,652 times.

I've only had my solar panels for a few weeks, but I'm already trying to hook them up to the Internet of Things.

I'm using the Fronius DataManager card. The API is fairly well documented - but you should be aware that it offers no authentication! The API is Read-Only - but I would still recommend against opening up your firewall to allow unfettered access.

Aim

I want to draw a (static) graph of my day's energy generation - suitable for sharing on social media.
Graph of Solar Power

I've released the PHP code required to do this on GitHub.

Why

Fronius's Solar.web service is nice - but has a few shortcomings.

  • 5 minute data resolution.
  • No data export.
  • Dynamic charts which aren't easily shareable.

How

I wanted to poll the DataManager every minute and write its current power output to a .csv file. Then generate a graph and post it to Twitter.

First thing to note is that it's pointless making the API call before sunrise and after sunset. Luckily, PHP provides us with an easy way to check if the sun is up.

$sunriseTimestamp = date_sunrise(time(), SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, ini_get("date.sunrise_zenith"), $offset);
$sunsetTimestamp = date_sunset(time(), SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, ini_get("date.sunset_zenith"), $offset);

Also worth noting, if there isn't enough sunlight to switch the inverter on, an error status will be returned by the API call.

After that, it's a simple matter of decoding the JSON and writing to a .csv file.

I've placed a call to the PHP file into my crontab. It will run every minute of every day - but won't do much if the code detects the sun isn't up.

Graphing

One thing you should know about me is that I hate using external code libraries. It's always a confusing ordeal to install and configure them - and that's assuming the code hasn't vanished from the net.

Ideally, I would have liked to have used a web API to generate the graphs. Sadly, Google has killed its static charts service and there are no other comparable services.

So, I've resorted to JpGraph. It's simple to install (just copy the files into your directory) and requires no configuration. That said, the graphs don't look brilliant, and it's a pain to get everything looking even half decent.

Posting To Twitter

I want to post the graph to Twitter every day. Because Twitter uses OAuth, I've used Jmathai's Twitter Async library.

This makes posting to Twitter as easy as these few lines of code:

include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';

$twitterObj = new EpiTwitter('YourConsumerKey', 'YourConsumerSecret', 'Token', 'TokenSecret');

$graphImage = "2013-12-29.png";
$status = 'Total Solar Power Generated';
$uploadResp = $twitterObj->post('/statuses/update_with_media.json',
								array('@media[]'  => "@{$graphImage};type=png;filename={$graphImage}",
								'status'   => $status));

You will need to register a new application with Twitter, ensure it has read and write permissions, and then generate your OAuth keys.

I've made a minor change to EpiOAuth - as per this post on StackOverflow - in order to ensure the status text isn't URL Encoded.

if ($isMultipart) {
   $params['request']['status'] = urldecode($params['request']['status']);
}

Putting It All Together

  1. Once per minute, cron runs a script to read the current solar panels.
  2. If the time is between sunrise and sunset, the script writes that data to a CSV (YYYY-MM-DD.csv)
  3. If the time is after sunset, check to see if a graph has been drawn. If not...
  4. If a graph hasn't yet been drawn, draw one (YYYY-MM-DD.png) and write it to disk.
  5. Post the graph to Twitter.

What's Next?

At the end of the year, I'll create an animation of how the solar panels have performed over 365 days.
I'll also add some different phrases to the Twitter status, depending on how much has been generated.

If you'd like to play with the code, or suggest improvements, all the code is on GitHub.

One thought on “Graphing My Solar Panels (Open Source)

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.