Solar panel power monitoring in Domoticz using a kWh pulse meter.

  Domoticz

Because my solar inverter has no easy way of me accessing the current or total generated energy I chose an approach that will work with 100% of the inverters available.

First of all I use an MID kWh meter, specifically this one. Other, similar models will also work but make sure it has a pulse output. The pulse output of this type of meter allows only a very small current to flow over the port. I’m  Furthermore we will be using an Arduino (cheapest nano will suffice and you need a free usb port on the system running Domoticz).

When installing the kWh meter in your fuse box pay attention to the ongoing and outgoing side. Since the power comes from the inverter and goes to the grid. After installing the kWh meter watch the led blink as your panels generate power. If it is working it’s time to proceed with the next steps. Flashing the code below to the arduino.

Arduino Code:

// This sketch is based on http://domoticz.com/forum/viewtopic.php?f=38&t=7300
// It will update counters in Domoticz.
// Via the serial port of the Pi you can set the initial counter values.
// do this while the Arduino is connected to the Pi but Domoticz is not running.
// Set the serial port to 9600 Baud.
// If not set, the counter will not start updating.
// Send the following command (with Newline ending):
// M[number of internal S0 counter]=[value in whole liters/Wh].
// So if your gasmeter is M1 and has value 123,456 m3 send:
// M1=123456
// So if your energymeter is M2 and has value 3456,23 kWh send:
// M2=3456230
// Start with M2, then M1. As soon as M1 !=0, the counter starts sending data.

//Number of pulses, used to measure energy.
volatile unsigned int pulseCount1  = 0;   
volatile long pulseCount1_sinceStart  = 0; 
volatile unsigned int pulseCount2  = 0;   
volatile long pulseCount2_sinceStart  = 0; 

volatile unsigned int reportInterval = 10000;  //Interval between messages being sent out (in milliseconds)
int PulseCounterID = 12345;
int PulseCounterVersion = 999;

String readString = String(100); //string for fetching data from address

int ind1 = 0;
int ind2 = 0;
String valueSet;
String valueSetID;

float lastTime = 0;

// The interrupt routine
void onPulse1()
{
//pulseCounter
    pulseCount1++;
    pulseCount1_sinceStart++;
//Blink built-in LED on S0-pulse
}

// The interrupt routine
//void onPulse2()
//{  
    //pulseCounter
    //pulseCount2++;
    //pulseCount2_sinceStart++;
//}


void setup()
{
    // KWH interrupt attached to IRQ 0  = pin2 (D2)
    attachInterrupt(0, onPulse1, FALLING);
    //KWH interrupt attached to IRQ 1  = pin3    
    //attachInterrupt(1, onPulse2, FALLING);
    Serial.begin(9600);
    Serial.print("/");
    Serial.print(PulseCounterID);
    Serial.print(":S0 Pulse Counter V");
    Serial.println(PulseCounterVersion);
}

void loop()
{
  // decode the start values
  // it looks for M1=123456
  while (Serial.available() > 0) {

char c = Serial.read();

//read char by char
// it expects a maximum of 20 char
if (readString.length() < 20) { //store characters to string readString +=c; Serial.println(c); } //if endline has been received if (c == '\n') { Serial.println("endline"); if(readString.indexOf("M") >=0){
 ind1 = readString.indexOf('M');
 valueSetID = readString.substring(ind1+1, ind1+2);
 ind2 = readString.indexOf('=');
 valueSet = readString.substring(ind2+1);
 if (valueSetID.toInt() == 1){
 pulseCount1_sinceStart = valueSet.toFloat();
 } else if (valueSetID.toInt() == 2){
 pulseCount2_sinceStart = valueSet.toFloat(); 
 } else {return;}

 } // end readString M
 readString = "";
 } // end endline 
     
  } // end serial available



  // send the data every reportInterval
  if(millis() - lastTime > reportInterval) {
    lastTime = millis(); 
  // Only send the counter data if the initial pulse count is not zero  
  if (pulseCount1_sinceStart != 0){
        Serial.print("ID:");
        Serial.print(PulseCounterID);
        Serial.print(":I:10:M1:");
        Serial.print(pulseCount1);
        Serial.print(":");
        Serial.print(pulseCount1_sinceStart);
        Serial.print(":");
        Serial.print("M2");
        Serial.print(":");
        Serial.print(pulseCount2);
        Serial.print(":");
        Serial.println(pulseCount2_sinceStart);

        pulseCount1 = 0;
        pulseCount2 = 0;

  } // end if not zero
  } // end interval loop

} // end void loop()

This code has not been written by me. All credit to goes to Thinkpad and bbqkees. source

Connecting kWh meter

Connect the kWh meter as shown in the scheme, take into account that the power comes from the solar inverter. This means that the live cable coming from the inverter goes into connection 1 and then from 2 onto the grid.

Connect the +5V from the arduino to the + ( connection 7) pulse connection of the kWh meter.

Connect the pulse connection (connection 6 on pulse meter) output to pin D2 of the arduino.

Connect a 10kΩ between pin D2 and the ground pin of the arduino. The connection is done to prevent any false readings due to floating voltage on pin D2. The high resistance prevents any current flowing between the 5v+ and gnd as this would cause short circuit. The arduino will receive it’s power via USB.

See below for the scheme.

Arduino din kWh meter connection scheme.

After connecting the whole bunch it is time to add the S0 Meter to Domoticz.

In order to find the right usb port to which the arduino will be connected I recommend to SSH into your domoticz device and immediately after plugging in the usb device issue the dmesg command.
You will receive an output that will inform you under which usb port the arduino is connected. It should look something similar to this.

root@Domoticz:~$ dmesg
[ 7.255698] usb 3-1.3: ch341-uart converter now attached to ttyUSB1

This informs you that the arduino is found under ttyUSB1.

In Domoticz go to Setup > Hardware and add a new device, something like this.

You can give any name, make sure to select the right usb port for your device, in my case ttyUSB1.
After you add the hardware, the new device should appear under Setup > Devices.
From there the device can be added to used devices and will appear under the Utility tab.

LEAVE A COMMENT