Energy meter, 75 pulses (rotations) = 1kW Topic is solved

Moderator: leecollings

Post Reply
User avatar
proza
Posts: 24
Joined: Friday 12 February 2016 15:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11415
Location: The Netherlands
Contact:

Energy meter, 75 pulses (rotations) = 1kW

Post by proza »

Is there someone here (no reaction on mysensors so far) who can explain this script a little, I’ve adapt it for my energy meter with 375 rotations per kW. Now I’ve changed #define PULSE_FACTOR to 375, and I think this give me not the correct values in Domoticz. I'm using an TCRT5000 sensor to track the black spot on the wheel of the energy meter, 375 pulses (rotations) = 1kW).

Code: Select all

#define PULSE_FACTOR 375       // Number of blinks per KW(H) of your meter
Is this the only thing to change or do I have to do more and what to set in Domoticz, Setup, Meter/Counters, Energy Dividers (now it’s 1000). The counters are much to high now :?
2016-04-17 13_06_38-Energy.png
2016-04-17 13_06_38-Energy.png (90.5 KiB) Viewed 2730 times

Code: Select all

/**
 * The MySensors Arduino library handles the wireless radio link and protocol
 * between your home built sensors/actuators and HA controller of choice.
 * The sensors forms a self healing radio network with optional repeaters. Each
 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
 * network topology allowing messages to be routed to nodes.
 *
 * Created by Henrik Ekblad <[email protected]>
 * Copyright (C) 2013-2015 Sensnology AB
 * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
 *
 * Documentation: http://www.mysensors.org
 * Support Forum: http://forum.mysensors.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 *******************************
 *
 * REVISION HISTORY
 * Version 1.0 - Henrik EKblad
 * 
 * DESCRIPTION
 * This sketch provides an example how to implement a distance sensor using HC-SR04 
 * Use this sensor to measure KWH and Watt of your house meeter
 * You need to set the correct pulsefactor of your meeter (blinks per KWH).
 * The sensor starts by fetching current KWH value from gateway.
 * Reports both KWH and Watt back to gateway.
 *
 * Unfortunately millis() won't increment when the Arduino is in 
 * sleepmode. So we cannot make this sensor sleep if we also want 
 * to calculate/report watt-number.
 * http://www.mysensors.org/build/pulse_power
 */

#include <SPI.h>
#include <MySensor.h>  

#define DIGITAL_INPUT_SENSOR 3  // The digital input you attached your light sensor.  (Only 2 and 3 generates interrupt!)
#define PULSE_FACTOR 1000       // Nummber of blinks per KWH of your meter
#define SLEEP_MODE false        // Watt-value can only be reported when sleep mode is false.
#define MAX_WATT 10000          // Max watt value to report. This filetrs outliers.
#define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
#define CHILD_ID 1              // Id of the sensor child
unsigned long SEND_FREQUENCY = 20000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
MySensor gw;
double ppwh = ((double)PULSE_FACTOR)/1000; // Pulses per watt hour
boolean pcReceived = false;
volatile unsigned long pulseCount = 0;   
volatile unsigned long lastBlink = 0;
volatile unsigned long watt = 0;
unsigned long oldPulseCount = 0;   
unsigned long oldWatt = 0;
double oldKwh;
unsigned long lastSend;
MyMessage wattMsg(CHILD_ID,V_WATT);
MyMessage kwhMsg(CHILD_ID,V_KWH);
MyMessage pcMsg(CHILD_ID,V_VAR1);


void setup()  
{  
  gw.begin(incomingMessage);

  // Send the sketch version information to the gateway and Controller
  gw.sendSketchInfo("Energy Meter", "1.0");

  // Register this device as power sensor
  gw.present(CHILD_ID, S_POWER);

  // Fetch last known pulse count value from gw
  gw.request(CHILD_ID, V_VAR1);
  
  attachInterrupt(INTERRUPT, onPulse, RISING);
  lastSend=millis();
}


void loop()     
{ 
  gw.process();
  unsigned long now = millis();
  // Only send values at a maximum frequency or woken up from sleep
  bool sendTime = now - lastSend > SEND_FREQUENCY;
  if (pcReceived && (SLEEP_MODE || sendTime)) {
    // New watt value has been calculated  
    if (!SLEEP_MODE && watt != oldWatt) {
      // Check that we dont get unresonable large watt value. 
      // could hapen when long wraps or false interrupt triggered
      if (watt<((unsigned long)MAX_WATT)) {
        gw.send(wattMsg.set(watt));  // Send watt value to gw 
      }  
      Serial.print("Watt:");
      Serial.println(watt);
      oldWatt = watt;
    }
  
    // Pulse cout has changed
    if (pulseCount != oldPulseCount) {
      gw.send(pcMsg.set(pulseCount));  // Send pulse count value to gw 
      double kwh = ((double)pulseCount/((double)PULSE_FACTOR));     
      oldPulseCount = pulseCount;
      if (kwh != oldKwh) {
        gw.send(kwhMsg.set(kwh, 4));  // Send kwh value to gw 
        oldKwh = kwh;
      }
    }    
    lastSend = now;
  } else if (sendTime && !pcReceived) {
    // No count received. Try requesting it again
    gw.request(CHILD_ID, V_VAR1);
    lastSend=now;
  }
  
  if (SLEEP_MODE) {
    gw.sleep(SEND_FREQUENCY);
  }
}

void incomingMessage(const MyMessage &message) {
  if (message.type==V_VAR1) {  
    pulseCount = oldPulseCount = message.getLong();
    Serial.print("Received last pulse count from gw:");
    Serial.println(pulseCount);
    pcReceived = true;
  }
}

void onPulse()     
{ 
  if (!SLEEP_MODE) {
    unsigned long newBlink = micros();  
    unsigned long interval = newBlink-lastBlink;
    if (interval<10000L) { // Sometimes we get interrupt on RISING
      return;
    }
    watt = (3600000000.0 /interval) / ppwh;
    lastBlink = newBlink;
  } 
  pulseCount++;
}


Last edited by proza on Thursday 21 April 2016 16:07, edited 2 times in total.
If you see me collapse, pause my Garmin fenix 3
NietGiftig
Posts: 121
Joined: Sunday 11 October 2015 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6224
Location: Holland
Contact:

Re: Energy meter, 75 pulses (rotations) = 1kW

Post by NietGiftig »

The possibility is that with slower turning of the disc the count is 1 with black on and 1 with black off
Depending on the speed of the disc
RPI-2 + SSD / ESPEasy Sensors & Switches / Sonoff / RFLink / Action Switches / TP-Link switch / Node-Red / Reacticz
User avatar
proza
Posts: 24
Joined: Friday 12 February 2016 15:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11415
Location: The Netherlands
Contact:

Energy meter, 75 pulses (rotations) = 1kW

Post by proza »

NietGiftig wrote:The possibility is that with slower turning of the disc the count is 1 with black on and 1 with black off
Depending on the speed of the disc
That's not what i have seen, control led on TCRT5000 is always on and goes off when the black part is showing up. As far I can see. Image
If you see me collapse, pause my Garmin fenix 3
NietGiftig
Posts: 121
Joined: Sunday 11 October 2015 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6224
Location: Holland
Contact:

Re: Energy meter, 75 pulses (rotations) = 1kW

Post by NietGiftig »

proza wrote: That's not what i have seen
It is not so important what you see but what the software see and "thinks". :twisted:
The software must count the pulses not you.

Maybe the software thinks that on is a pulse, and off also a pulse.
RPI-2 + SSD / ESPEasy Sensors & Switches / Sonoff / RFLink / Action Switches / TP-Link switch / Node-Red / Reacticz
User avatar
proza
Posts: 24
Joined: Friday 12 February 2016 15:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11415
Location: The Netherlands
Contact:

Energy meter, 75 pulses (rotations) = 1kW

Post by proza »

NietGiftig wrote:
proza wrote: That's not what i have seen
Maybe the software thinks that on is a pulse, and off also a pulse.
Then i'll set the energy divider on 2000 to see off this give the correct results.
If you see me collapse, pause my Garmin fenix 3
NietGiftig
Posts: 121
Joined: Sunday 11 October 2015 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6224
Location: Holland
Contact:

Re: Energy meter, 75 pulses (rotations) = 1kW

Post by NietGiftig »

proza wrote: Then i'll set the energy divider on 2000 to see off this give the correct results.
That's a good start. :)

But remember that if the disc turns quickly the software maybe count one pulse because on/of is in the time limits of a pulse defined for the software.
In that case is the total also not correct.
RPI-2 + SSD / ESPEasy Sensors & Switches / Sonoff / RFLink / Action Switches / TP-Link switch / Node-Red / Reacticz
User avatar
proza
Posts: 24
Joined: Friday 12 February 2016 15:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11415
Location: The Netherlands
Contact:

Re: Energy meter, 375 pulses (rotations) = 1kW

Post by proza »

Sorry subject is not correct, this should be: Energy meter, 375 pulses (rotations) = 1kW
If you see me collapse, pause my Garmin fenix 3
User avatar
proza
Posts: 24
Joined: Friday 12 February 2016 15:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11415
Location: The Netherlands
Contact:

Re: Energy meter, 375 pulses (rotations) = 1kW

Post by proza »

NietGiftig wrote: That's a good start. :)
I'v used the code below for mine water- and energymeter to see what happens, and in both situations the serial monitor shows me a incremantal of 2 by one rotation.

So now i'v used for the energy meter the next PULSE_FACTOR

Code: Select all

#define PULSE_FACTOR 750// Number of blinks per KW(H) of your meter

Code: Select all

const byte ledPin = 13;
const byte interruptPin = 2;
int pulseCount = 0;
volatile unsigned long lastBlinkWater = 0;
unsigned long lastPulseWater =0;

void setup() {
  
   // initialize serial:
  Serial.begin(115200);

  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), pulse, FALLING);
}

void loop() {

 
}

void pulse() {
  
  unsigned long newBlinkWater = micros();   
    unsigned long intervalWater = newBlinkWater-lastBlinkWater;
    
    if (intervalWater!=0)
    {
      lastPulseWater = millis();
      if (intervalWater<500000L) {
        // Sometimes we get interrupt on RISING,  500000 = 0.5sek debounce ( max 120 l/min)
        return;   
      }
      pulseCount++;
  
  Serial.println(pulseCount);
    }
    lastBlinkWater = newBlinkWater;
 
}
If you see me collapse, pause my Garmin fenix 3
User avatar
gizmocuz
Posts: 2352
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Energy meter, 75 pulses (rotations) = 1kW

Post by gizmocuz »

Okey, we can flag this topic as solved ?
Quality outlives Quantity!
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests