Two pulse power meters on one Arduino



  • Hi,
    I use MySensors 2.0 and trying to set up a pulse power meter. If I use the default sketch everything works fine but my mission is to have two pulse meters connected to one Arduino.I have duplicated all variables. The sketch presents two childs to the controller (Domoticz).
    What I don´t really understand is the use of the "receive" function.

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

    How should I get the pulsecount from child 2?

    Here is the entire sketch.

    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensors.h>  
    
    #define DIGITAL_INPUT_SENSOR 2  // The digital input you attached your light sensor.  (Only 2 and 3 generates interrupt!)
    #define DIGITAL_INPUT_SENSOR2 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 meeter
    #define PULSE_FACTOR2 2000       // Nummber of blinks per KWH of your meeter
    #define SLEEP_MODE false        // Watt-value can only be reported when sleep mode is false.
    #define MAX_WATT 25000          // Max watt value to report. This filetrs outliers.
    #define MAX_WATT2 25000
    #define CHILD_ID 1              // Id of the sensor child
    #define CHILD_ID2 2              // Id of the sensor child
    
    unsigned long SEND_FREQUENCY = 5000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
    double ppwh = ((double)PULSE_FACTOR)/1000; // Pulses per watt hour
    double ppwh2 = ((double)PULSE_FACTOR2)/1000; // Pulses per watt hour
    boolean pcReceived = false;
    boolean pcReceived2 = false;
    volatile unsigned long pulseCount = 0;   
    volatile unsigned long pulseCount2 = 0;   
    volatile unsigned long lastBlink = 0;
    volatile unsigned long lastBlink2 = 0;
    volatile unsigned long watt = 0;
    volatile unsigned long watt2 = 0;
    unsigned long oldPulseCount = 0;   
    unsigned long oldPulseCount2 = 0;   
    unsigned long oldWatt = 0;
    unsigned long oldWatt2 = 0;
    double oldKwh;
    double oldKwh2;
    unsigned long lastSend;
    unsigned long lastSend2;
    MyMessage wattMsg(CHILD_ID,V_WATT);
    MyMessage kwhMsg(CHILD_ID,V_KWH);
    MyMessage pcMsg(CHILD_ID,V_VAR1);
    
    MyMessage wattMsg2(CHILD_ID2,V_WATT);
    MyMessage kwhMsg2(CHILD_ID2,V_KWH);
    MyMessage pcMsg2(CHILD_ID2,V_VAR1);
    
    
    void setup()  
    {  
      // Fetch last known pulse count value from gw
      request(CHILD_ID, V_VAR1);
      request(CHILD_ID2, V_VAR1);
    
      // Use the internal pullup to be able to hook up this sketch directly to an energy meter with S0 output
      // If no pullup is used, the reported usage will be too high because of the floating pin
      pinMode(DIGITAL_INPUT_SENSOR,INPUT_PULLUP);
      pinMode(DIGITAL_INPUT_SENSOR2,INPUT_PULLUP);
    
      attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, RISING);
      attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR2), onPulse2, RISING);
      lastSend=millis();
    }
    
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Energy Meter", "1.1");
    
      // Register this device as power sensor
      present(CHILD_ID, S_POWER);
      present(CHILD_ID2, S_POWER);
    }
    
    void loop()     
    { 
      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)) {
            send(wattMsg.set(watt));  // Send watt value to gw 
          }  
          Serial.print("Watt huset:");
          Serial.println(watt);
          oldWatt = watt;
          
        }
    
    
    if (!SLEEP_MODE && watt2 != oldWatt2) {
          // Check that we dont get unresonable large watt value. 
          // could hapen when long wraps or false interrupt triggered
          if (watt2<((unsigned long)MAX_WATT2)) {
            send(wattMsg2.set(watt2));  // Send watt value to gw 
          }  
          Serial.print("Watt varmepump:");
          Serial.println(watt2);
          oldWatt2 = watt2;
        }
    
    
    
        
      
        // Pulse count has changed
        if (pulseCount != oldPulseCount) {
          send(pcMsg.set(pulseCount));  // Send pulse count value to gw 
          double kwh = ((double)pulseCount/((double)PULSE_FACTOR));     
          oldPulseCount = pulseCount;
          if (kwh != oldKwh) {
            send(kwhMsg.set(kwh, 4));  // Send kwh value to gw 
            oldKwh = kwh;
            Serial.print("kWh huset:");
            Serial.println(kwh);
          }
        }    
        // Pulse count2 has changed
        if (pulseCount2 != oldPulseCount2) {
          send(pcMsg2.set(pulseCount2));  // Send pulse count2 value to gw 
          double kwh2 = ((double)pulseCount2/((double)PULSE_FACTOR2));     
          oldPulseCount2 = pulseCount2;
          if (kwh2 != oldKwh2) {
            send(kwhMsg2.set(kwh2, 4));  // Send kwh value to gw 
            oldKwh2 = kwh2;
            Serial.print("kWh varmepump:");
            Serial.println(kwh2);
          }
        } 
          
        lastSend = now;
      } else if (sendTime && !pcReceived) {
        // No count received. Try requesting it again
        request(CHILD_ID, V_VAR1);
        request(CHILD_ID2, V_VAR1);
        lastSend=now;
      }
      
      if (SLEEP_MODE) {
        sleep(SEND_FREQUENCY);
      }
    } //End loop
    
    void receive(const MyMessage &message) {
      Serial.print("Receive.....");
      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++;
    }
    
    void onPulse2()     
    { 
      if (!SLEEP_MODE) {
        unsigned long newBlink2 = micros();  
        unsigned long interval2 = newBlink2-lastBlink2;
        if (interval2<10000L) { // Sometimes we get interrupt on RISING
          return;
        }
        watt2 = (3600000000.0 /interval2) / ppwh2;
        lastBlink2 = newBlink2;
      } 
      pulseCount2++;
    }
    


  • I think I solved it myself after some searching on this forum.

    void receive(const MyMessage &message) {
    Serial.print("Receive.....");
    if (message.type == V_VAR1) {
          if (message.sensor == 1) {
            pulseCount = oldPulseCount = message.getLong();
            Serial.println("Received data from child 1: ");
            Serial.println(pulseCount);
        } 
          else if (message.sensor == 2) {
            pulseCount2 = oldPulseCount2 = message.getLong();
            Serial.println("Received data from child 2: ");
            Serial.println(pulseCount2);
        }
      pcReceived = true;
      }
    }
    

  • Mod

    @Pewh yes, that's the way. Great work, and thanks for posting the solution!


Log in to reply
 

Suggested Topics

1
Online

11.2k
Users

11.1k
Topics

112.5k
Posts