Arduino : Monitor Solar Panels Production (Part 1)



Background

With this article, I'm actually starting a new section on my blog : some electronic builds. I have solar panels on my roof since 2009 and this is a real benefit. I mean, not only the environment is happy, my wallet as well. Price of electricity these days are crazy and this is a relief to count on this extra "free" production. The problem is : my inverter is old and provides no means to see how my production looks like during the day. I can only check the production since they have been in production (I have a so called green meter). Plan is to create a cheap mean to measure and log the production.

How to meter Solar Production ?

As I said in the introduction, my inverter is very old. This is a SMA Sunny Boy SB 3800 without any communications with the outside world. I know there is an optional serial module but you also need a specific software to communicate with it.


So, connectivity must be established by another mean.

I came across a Youtube video (in French - automatic translation almost works 🤣) which explains how to measure a current and a tension using a current clamp. Indeed, magnetic fields are produced around any conductor, and a well calibrated magnet can generate a current that could be converted into a current and voltage making use of a programmable chip. I'm obviously not an expert, you can discover more here (it's all about neuron excitement 🤣).

The principle is simple
  1. Measure the current and voltage;
  2. Compute the measured data;
  3. Log the data in the cloud on an IoT platform. 

It will generate graphs and we can manipulate the data later to create reports in any application we want using API calls. 

Ready ? Let's go !

Building the Hardware 

We have the logic, let's put it into music now ! 

This is the schematic for this project : 


As you can see, I'm using Fritzing, amazing Open Source software. Supports Windows, Linux and macOS.

I'm using the working principle from the Youtube video I mentioned above and I added 2 LEDs, changed the value of the capacitor and took the 3.3V instead of 5V. Basically, it reads the current from the phase on the output of my inverter and convert the AC input to a DC input - since the Arduino can only read DC current. There is a resistor called Burden, this one is chosen based on the current clamp capacity you have selected and the guessed output current you need to measure.

Bill of Material
First, I made every connections on a breadboard, then I soldered all components on a prototyping board to have something compact and avoid any component disconnection.

Here is my first prototype, not the best, but works like a charm ;)



Excuse the dusts, this is the electric cabinet, I'm not vacuum cleaning it every week there ;)

Few days later, I decided to do another prototype reducing size using WeMos Mini, adding 2 LEDs and an extra condensator to reduce the background noise. Here is what's came across : 

This is the new circuit I built. Note top left, few PINs to host the WeMos Mini

This is the WeMos D1 Mini. Be prepared, it is really mini !


This is the full assembly with the WeMos Mini in place

These are the 2 LEDs I have added to the assembly to have an idea of the platform status

At this stage, this is not clean, but at least it works ! We will do it better later. For now, this is testing and learning.

Some coding is required !

When everything is tied together, we have to create the software logic who can read and report the different values we measured.

The code is on my GitHub and I tried to comment it as much as possible.

There are a few things you have to configure in the code before being able to use it : 
  • The SSID credentials to connect to your network;
  • The API Key on ThingSpeak (the IoT platform hosting the data);
  • The adjustment variable in the formula emon1.current(0, 62); 
  • The ghost current read by the current clamp without any wire inside it
The first 2 parameters are self explanatory :

// Substitute by your own Wifi SSID and password
const char* ssid = "<your_ssid_here>";
const char* password = "<your_password_here>";

The Ghost current (in my case 0.3 A)

// Current detected when no wire is enclosed by the clamp
float ghostCurrent = 0.3;

and the API key (you can find it later) : 

// Substitute by API Key write of your own thingspeak channel
String apiKey = "<your_write_api_key>";

We will cover this API story later in this article.

Next, this is less straightforward. Prepare your calculator ;)

We are using emonlib.h. This is a library maintained by Open Energy Monitor group, you can find more details here. It offers useful functions for who wants to meter and monitor energy.

So, depending on the current clamp you have and depending on the Burden resistor, you need to adjust some variables.

When you the code ready with all the settings in place, you need to push the code into the microcontroller (the WeMos Mini). Of course, you can use the Arduino IDE to push the code. We need to add some special libraries to have it compiled.

First, we need to make sure we have the relevant libraries for our board. This could be configured int 2 steps, first in the Preferences / Additional Boards Manager URLs, enter the following URL : http://arduino.esp8266.com/stable/package_esp8266com_index.json 


Next, under Tools -> Board -> Board Manager, search for esp8266 and choose the relevant package to install.


At this stage, you can choose the WeMos D1 board into the boards available within the Arduino UI. The other name of this board is LOLIN.


Last but not least, we need to add the Emonlib libraries to be able to compile our sketch. It should already be there, if not, simply search for Emonlib and install it : 


Do not hesitate to open the Serial Monitor to see some important debug information. It will help you to configure the calibration variable.


Here is a sample output :

Taking 40 samples and compute average
* DEBUG : Sample #0 -> 0.27 A. (59.62 Watt)
* DEBUG : Sample #1 -> 0.27 A. (60.13 Watt)
* DEBUG : Sample #2 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #3 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #4 -> 0.40 A. (88.19 Watt)
* DEBUG : Sample #5 -> 0.24 A. (53.54 Watt)
* DEBUG : Sample #6 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #7 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #8 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #9 -> 0.41 A. (89.51 Watt)
* DEBUG : Sample #10 -> 0.24 A. (53.25 Watt)
* DEBUG : Sample #11 -> 0.28 A. (61.79 Watt)
* DEBUG : Sample #12 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #13 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #14 -> 0.40 A. (88.34 Watt)
* DEBUG : Sample #15 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #16 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #17 -> 0.40 A. (87.87 Watt)
* DEBUG : Sample #18 -> 0.24 A. (53.72 Watt)
* DEBUG : Sample #19 -> 0.28 A. (61.71 Watt)
* DEBUG : Sample #20 -> 0.27 A. (60.31 Watt)
* DEBUG : Sample #21 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #22 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #23 -> 0.40 A. (88.28 Watt)
* DEBUG : Sample #24 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #25 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #26 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #27 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #28 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #29 -> 0.41 A. (89.97 Watt)
* DEBUG : Sample #30 -> 0.24 A. (53.19 Watt)
* DEBUG : Sample #31 -> 0.28 A. (61.75 Watt)
* DEBUG : Sample #32 -> 0.27 A. (59.74 Watt)
* DEBUG : Sample #33 -> 0.28 A. (61.40 Watt)
* DEBUG : Sample #34 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #35 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #36 -> 0.00 A. (0.00 Watt)
* DEBUG : Sample #37 -> 0.41 A. (89.51 Watt)
* DEBUG : Sample #38 -> 0.24 A. (53.30 Watt)
* DEBUG : Sample #39 -> 0.00 A. (0.00 Watt)
Average : 0.04 A

*** Current: 0.00 A. Power: 0.00 W (sent to ThingSpeak).
Next data reading in 20 secs...

Any power below 30W is ignored and considered as wrong reading and outside the precision range of the current clamp I'm using. In the above screen, the average measure is 0.04 A. It means 0.04 * 220 V = 8.8 W -> we discard this measure according to our accuracy rule. The measure we just did is without plugin the current clamp into the mini jack.

When you have deployed the software in the WeMos, it is time to physically install the probe. You need to follow the cables coming out from the inverter heading to the main electric panel of your house. You should see 2 wires. One is neutral and the other is the phase. You have to select one of the cable and wrap the clamp arround it : 


It's not important to have either the neutral of the phase, as soon as only ONE cable is passing thru the clamp. Next, plug the mini-jack into the board and you can power the board with a micro USB cable. The board is "booting up" in a few seconds, there are some LEDs showing the state.

LED explanation
  • Blinking Blue : connecting to WiFi;
  • Solid Blue : connected to WiFi;
  • Slowly Blinking Yellow : initializing;
  • 8 times Fast Blinking Yellow : sending data to ThingSpeak
Calibration

At this stage, we have a working system with default values. Here comes the calibration

Why do we need to calibrate ? To adjust the environment to the emon sensor package and the current clamp.

Locate this line in the code (somewhere in the middle of the code) : 

 emon1.current(0, 62.282);  

62.282 is the calibration value we will modify to fit as much as possible our environment.

Let's create a little conversion table : value we can read directly from the inverter and value we can read in the serial monitor, returned by the current clamp.

Rectifier : 306 W
Measured by our system : 295 W

To get the right calibration variable, we have to do a quick cross-multiplication : 

306 / 295 = 1.03
Calibration variable is now 62.282 * 1.03 = 64.15

The new line will be : 

 emon1.current(0, 64.15);  

Compile and upload the code once more and redo the same test. You will be closer and closer to a real match between what the inverter is reporting and what you are measuring. I my case, I'm accurate as close as 10 W !

Ok, now we are able to measure, this about time to store data for long term !

ThingSpeak, the IoT platform

ThingSpeak is a platform maintained by MathWorks. Well known product - MatLab - by all engineers and mathematical nerds. This IoT platform is actually ideal to store data and create visualisation around it. 

The reason why we are using a 3rd party platform is simple to understand : there is no storage on the WeMos by default (we can add a SD card component for some specific projects) and we have to keep the data we are measuring and transfer them somewhere for storage and analysis.

You can create a free account and you are allowed to push up to 3 billion measures a year. It's enough for us it allows 6 query per minutes and we will do 3 ;)

When you have created your account, you need to create a "channel" with the following settings :


Field 1 will get the current in Amps and Field 2 is getting the power in Watt. Field 1 is directly read from the current clamp and Field 2 is actually computed from Field 1 multiplied by the voltage. In my region the voltage is 220 V. If your region is using 110 V, you have to change the variable in the code (the variable is named localVoltage). 

Push on Save and then click on the channel name to see the various settings. We are very much interested by the write API key (from the API tab).


Modify the code in the Arduino IDE to match the write API key, re-compile and push the sketch back on the WeMos board.

// Substitute by API Key write of your own thingspeak channel
String apiKey = "<Put_you_write_key_here>";

After a couple of measures, you should be able to see some figures on the ThingSpeak graphs ! You can also check the Serial Monitor to see what's going on.

Here is my data for an entire day : 


You can tweak the graphs using the pencil icon. Here is my tweaks for Field 2 Chart : 


The graphical capabilities of ThingSpeak are very powerful and not easy to deal with at first. Some learning is required and nothing comes out of the box. Indeed, the possibilities are endless with the MatLab module. But, when it comes to storing data, ThingSpeak is very easy and powerful.

We have now achieved something nice ! This is not finalized yet, we are only in the beginning of what I have in mind, but it works !

What's Next ?

Here is what I have in mind for the next steps : 
  • Design and print an enclosure;
  • Transfer the data to a home automation platform to have better graphic capabilities;
  • Create a custom PCB from a PCB factory like jlcpcb.com;
  • Add a power button or reset button;
  • Add a screen to display captured data in real time 
  • ...
Let's continue in a future post, and as always, I hope this helps !


Comments

What's hot ?

ShredOS : HDD degaussing with style

Mac OS X : Display images in-line with terminal