Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Hardware
  3. SCT-013-030 current monitor sensor

SCT-013-030 current monitor sensor

Scheduled Pinned Locked Moved Hardware
65 Posts 16 Posters 50.7k Views 15 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 5546dug5 Offline
    5546dug5 Offline
    5546dug
    wrote on last edited by
    #26

    I have got this to work on my Cdn system 120/240 vac using A0 and A1 pins. The code needed to be changed to address our voltage and another clamp added., but I do have a few questions;

    1. Is there a way I can record or view amps consumed in the vera dashboard?

    2. I used the sketch above displayed by @chatainsim and in it there are two lines of code with values 29.0 and second 1480 both in green and in brackets. For the life of me I can not find an explanation of these or how the values were arrived at. Can anyone help with explanation?

        gw.present(CHILD_ID, S_POWER);
        emon1.current(A0, 29.0);       // Current: input pin, calibration.
        }
      
        void loop()
       {
       gw.process();
       unsigned long now = millis();
       double Irms = emon1.calcIrms(1480);
      
    3. Is there a method to show current in phase. I tried presenting CHILD_ID, S_MULTIMETER and MyMessage IrmsMsg(CHILD_ID,V_CURRENT); but got nowhere.

    4. There is/was much running up and down stairs from electrical panel to computer and back is OTA IDE what I think it might be-- firmware or sketches can be updated over the air from my computer>vera>gw>node while not needing to access the node? If so there is much learning for me to do.

    5. Using arduino 1.6.5, Lib 1.4.1, vera 3/veralite, ui5, Ethernet g/w , win 8.1, pro mini clone

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #27

      Hello, last week i buy SCT013-30 (ebay.fr) and follow this topic for have powermeter with mysensors. I need only energy (kWh) and power (W) same my OWL sensors (use with domoticz controler)
      I modifie the sketch from mysensors/pulse powermeter (http://www.mysensors.org/build/pulse_power)

      // EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3  ***/
      #include <SPI.h>
      #include <MySensor.h>  
      #include "EmonLib.h"             // Include Emon Library
      EnergyMonitor emon1;             // Create an instance
      #define CHILD_ID 0 
      #define PIN_ANALOG_I A2
      MySensor gw;
      unsigned long lastSend;
      unsigned long lastSend2;
      unsigned long SEND_FREQUENCY = 90000; // Minimum time between send (in milliseconds).
      unsigned long SEND_FREQUENCY2 = SEND_FREQUENCY / 25;
      int index = 0;
      double Irms=0;
      double power;
      boolean pcReceived = false;
      double nrj=0, old_nrj;
      MyMessage IrmsMsg(CHILD_ID,V_WATT);
      MyMessage kWhMsg(CHILD_ID,V_KWH);
      MyMessage pcMsg(CHILD_ID,V_VAR1);
      
      
      void incomingMessage(const MyMessage &message) 
      {
        if (message.type==V_VAR1) 
        {  
          nrj = old_nrj = message.getLong();
          Serial.print("Received last nrj count from gw:");
          Serial.println(nrj);
          pcReceived = true;
        }
      }
      
      void setup()
      {  
        gw.begin(incomingMessage);
        gw.sendSketchInfo("Energy Meter", "1.0");  // Send the sketch version information to the gateway and Controller
        gw.present(CHILD_ID, S_POWER);   // Register this device as power sensor
        gw.request(CHILD_ID, V_VAR1);
        emon1.current(PIN_ANALOG_I, 30.0);       // Current: input pin, calibration.
      }
      
      void loop()
      {
        gw.process(); 
        unsigned long now = millis();
        unsigned long now2 = millis();
        bool sendTime2 = now2 - lastSend2 > SEND_FREQUENCY2;
        if (sendTime2) //calcul Irms moy
        {
          if (index==0) Irms=emon1.calcIrms(1480);
          else {
          index++;
          Irms = (index*Irms+emon1.calcIrms(1480))/(index+1);
          }
          lastSend2 = now2;
        }
        bool sendTime = now - lastSend > SEND_FREQUENCY;
        if (sendTime && pcReceived) 
        { 
          power = Irms*232.0;
          gw.send(IrmsMsg.set(power,1));
          Serial.println(Irms*232.0);
          nrj += (power*SEND_FREQUENCY/1000)/3.6E6;
          gw.send(kWhMsg.set(nrj,3));
          lastSend = now;
          index = 0;
          old_nrj=nrj;
        }
       else if (sendTime && !pcReceived)
       {
        gw.request(CHILD_ID, V_VAR1);
        lastSend=now;
       }
      }
      

      it seems that it works for now (but not optimal).
      0_1456322448197_upload-3801900a-dd69-437d-ac99-6f2baa635713

      Note 1) my cosphy=1 because i use only ampere clamp, i cant measure the phase between curent i and tension v.
      2) my calibration constant (used in emonlib) is 30
      3) Sorry for my english :disappointed: - Je suis français.

      1 Reply Last reply
      0
      • O Offline
        O Offline
        oudini
        wrote on last edited by
        #28

        Hello,
        The cos phi applies only to the three-phase, single phase not

        Je suis français aussi...

        1 Reply Last reply
        0
        • fredokF Offline
          fredokF Offline
          fredok
          wrote on last edited by
          #29

          Hello, if i remember physics course in university Power P=u.i and energy E=integrale(Pdt)=integrale(u.i.dt)=UeffxIeffxcos(phi)
          but for particular citizen same me, energy provider non consider cos(phi)

          So, i correct my last sketch for request last nrj value to the gw.

          // EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3  ***/
          #include <SPI.h>
          #include <MySensor.h>  
          #include "EmonLib.h"             // Include Emon Library
          EnergyMonitor emon1;             // Create an instance
          #define CHILD_ID 0 
          #define PIN_ANALOG_I A2
          MySensor gw;
          unsigned long lastSend;
          unsigned long lastSend2;
          unsigned long SEND_FREQUENCY = 20000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
          unsigned long SEND_FREQUENCY2 = SEND_FREQUENCY / 25;
          int index = 0;
          double Irms=0;
          double power;
          boolean pcReceived = false;
          boolean onyva=true;
          float nrj=0, old_nrj;
          MyMessage IrmsMsg(CHILD_ID,V_WATT);
          MyMessage kWhMsg(CHILD_ID,V_KWH);
          MyMessage pcMsg(CHILD_ID,V_VAR1);
          
          
          void incomingMessage(const MyMessage &message) 
          {
            if (message.type==V_VAR1) 
            {  
              nrj = old_nrj = message.getFloat();
              Serial.print("Received last nrj count from gw:");
              Serial.println(nrj);
              pcReceived = true;
            }
          }
          
          void setup()
          {  
            gw.begin(incomingMessage);
            gw.sendSketchInfo("Energy Meter", "1.0");  // Send the sketch version information to the gateway and Controller
            gw.present(CHILD_ID, S_POWER);   // Register this device as power sensor
            gw.request(CHILD_ID, V_VAR1);
            emon1.current(PIN_ANALOG_I, 30.0);       // Current: input pin, calibration.
          }
          
          void loop()
          {
            if (onyva) gw.process();
            onyva = false; 
            unsigned long now = millis();
            //unsigned long now2 = millis();
            bool sendTime2 = now - lastSend2 > SEND_FREQUENCY2;
            if (sendTime2) //calcul Irms moy
            {
              if (index==0) Irms=emon1.calcIrms(1480);
              else {
              index++;
              Irms = (index*Irms+emon1.calcIrms(1480))/(index+1);
              }
              lastSend2 = now;
            }
            bool sendTime = now - lastSend > SEND_FREQUENCY;
            if (sendTime && pcReceived) 
            { 
              power = Irms*232.0;
              gw.send(IrmsMsg.set(power,1));
              Serial.println(Irms*232.0);
              nrj += (power*SEND_FREQUENCY/1000)/3.6E6;
              gw.send(kWhMsg.set(nrj,5));
              gw.send(pcMsg.set(nrj,5));
              lastSend = now;
              index = 0;
              old_nrj=nrj;
              onyva=true;
            }
           else if (sendTime && !pcReceived)
           {
            gw.request(CHILD_ID, V_VAR1);
            lastSend=now;
            index=0;
            onyva=true;
           }
          }
          
          

          thx...

          chatainsimC warmaniacW 2 Replies Last reply
          0
          • M Offline
            M Offline
            maciejka
            wrote on last edited by
            #30

            Hi, I'm beginner of arduino programmer. Can You explain a little this sketch? What for You use "SEND_FREQUENCY2 = SEND_FREQUENCY / 25;" ? Why You using two if statements sendTime2 and sendTime ?

            Thanks
            Maciej

            1 Reply Last reply
            0
            • fredokF Offline
              fredokF Offline
              fredok
              wrote on last edited by
              #31

              Sorry, I'm late debause i come back holiday today
              This sketch send Power and energy (kWh) every Sendfrequency=90s but the sketch not measure power every 90s but everyre 90/25=3,6s and send to the GW the mean power (Pmean)
              this is calculate here :
              if (index==0) Irms=emon1.calcIrms(1480);
              else {
              index++;
              Irms = (index*Irms+emon1.calcIrms(1480))/(index+1);
              }
              lastSend2 = now;

              chatainsimC 1 Reply Last reply
              0
              • fredokF fredok

                Hello, if i remember physics course in university Power P=u.i and energy E=integrale(Pdt)=integrale(u.i.dt)=UeffxIeffxcos(phi)
                but for particular citizen same me, energy provider non consider cos(phi)

                So, i correct my last sketch for request last nrj value to the gw.

                // EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3  ***/
                #include <SPI.h>
                #include <MySensor.h>  
                #include "EmonLib.h"             // Include Emon Library
                EnergyMonitor emon1;             // Create an instance
                #define CHILD_ID 0 
                #define PIN_ANALOG_I A2
                MySensor gw;
                unsigned long lastSend;
                unsigned long lastSend2;
                unsigned long SEND_FREQUENCY = 20000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
                unsigned long SEND_FREQUENCY2 = SEND_FREQUENCY / 25;
                int index = 0;
                double Irms=0;
                double power;
                boolean pcReceived = false;
                boolean onyva=true;
                float nrj=0, old_nrj;
                MyMessage IrmsMsg(CHILD_ID,V_WATT);
                MyMessage kWhMsg(CHILD_ID,V_KWH);
                MyMessage pcMsg(CHILD_ID,V_VAR1);
                
                
                void incomingMessage(const MyMessage &message) 
                {
                  if (message.type==V_VAR1) 
                  {  
                    nrj = old_nrj = message.getFloat();
                    Serial.print("Received last nrj count from gw:");
                    Serial.println(nrj);
                    pcReceived = true;
                  }
                }
                
                void setup()
                {  
                  gw.begin(incomingMessage);
                  gw.sendSketchInfo("Energy Meter", "1.0");  // Send the sketch version information to the gateway and Controller
                  gw.present(CHILD_ID, S_POWER);   // Register this device as power sensor
                  gw.request(CHILD_ID, V_VAR1);
                  emon1.current(PIN_ANALOG_I, 30.0);       // Current: input pin, calibration.
                }
                
                void loop()
                {
                  if (onyva) gw.process();
                  onyva = false; 
                  unsigned long now = millis();
                  //unsigned long now2 = millis();
                  bool sendTime2 = now - lastSend2 > SEND_FREQUENCY2;
                  if (sendTime2) //calcul Irms moy
                  {
                    if (index==0) Irms=emon1.calcIrms(1480);
                    else {
                    index++;
                    Irms = (index*Irms+emon1.calcIrms(1480))/(index+1);
                    }
                    lastSend2 = now;
                  }
                  bool sendTime = now - lastSend > SEND_FREQUENCY;
                  if (sendTime && pcReceived) 
                  { 
                    power = Irms*232.0;
                    gw.send(IrmsMsg.set(power,1));
                    Serial.println(Irms*232.0);
                    nrj += (power*SEND_FREQUENCY/1000)/3.6E6;
                    gw.send(kWhMsg.set(nrj,5));
                    gw.send(pcMsg.set(nrj,5));
                    lastSend = now;
                    index = 0;
                    old_nrj=nrj;
                    onyva=true;
                  }
                 else if (sendTime && !pcReceived)
                 {
                  gw.request(CHILD_ID, V_VAR1);
                  lastSend=now;
                  index=0;
                  onyva=true;
                 }
                }
                
                

                thx...

                chatainsimC Offline
                chatainsimC Offline
                chatainsim
                wrote on last edited by
                #32

                Thanks @fredok I will give a try !

                And I'm french too ;)

                1 Reply Last reply
                0
                • fredokF fredok

                  Sorry, I'm late debause i come back holiday today
                  This sketch send Power and energy (kWh) every Sendfrequency=90s but the sketch not measure power every 90s but everyre 90/25=3,6s and send to the GW the mean power (Pmean)
                  this is calculate here :
                  if (index==0) Irms=emon1.calcIrms(1480);
                  else {
                  index++;
                  Irms = (index*Irms+emon1.calcIrms(1480))/(index+1);
                  }
                  lastSend2 = now;

                  chatainsimC Offline
                  chatainsimC Offline
                  chatainsim
                  wrote on last edited by
                  #33

                  Hi @fredok
                  Your sketch working fine.
                  Except in domoticz only the kWh is updated. Watt is not.
                  Do you have a clue about that ?
                  0_1460530318251_upload-2328793c-760b-4d2f-9a6e-5b379895cf58

                  1 Reply Last reply
                  0
                  • fredokF Offline
                    fredokF Offline
                    fredok
                    wrote on last edited by
                    #34

                    Hello chatainsim, in my domoticz i have only one device counting (kWh+instant Power)
                    0_1460535247416_upload-ddcdf9df-e7f6-441a-bdc0-2d426f76aa27
                    0_1460535269881_upload-a2371cbe-49f0-497d-af85-7e2433941d15

                    The instant power updated every 90s is writing in Watt value(83.3) , the kWh of the day (1.174) and total energy (61.582 kWh).... the device log show the power = f(t).
                    Look if Watt value in (kWhMeter) device change, look the log graph ... if is ok, u not need Wattmeter device.
                    Thx Fredok

                    chatainsimC 1 Reply Last reply
                    0
                    • fredokF fredok

                      Hello chatainsim, in my domoticz i have only one device counting (kWh+instant Power)
                      0_1460535247416_upload-ddcdf9df-e7f6-441a-bdc0-2d426f76aa27
                      0_1460535269881_upload-a2371cbe-49f0-497d-af85-7e2433941d15

                      The instant power updated every 90s is writing in Watt value(83.3) , the kWh of the day (1.174) and total energy (61.582 kWh).... the device log show the power = f(t).
                      Look if Watt value in (kWhMeter) device change, look the log graph ... if is ok, u not need Wattmeter device.
                      Thx Fredok

                      chatainsimC Offline
                      chatainsimC Offline
                      chatainsim
                      wrote on last edited by
                      #35

                      @fredok Yes, the kWhMeter is working fine:
                      0_1460558668986_upload-6238dfee-4382-4716-913e-6fb03b0f2ddf

                      1 Reply Last reply
                      0
                      • chatainsimC Offline
                        chatainsimC Offline
                        chatainsim
                        wrote on last edited by chatainsim
                        #36

                        Ok, so it just stop working after almost 24 hours ...
                        I have some issue with mysensors, I don't know why, sometimes sensors stop working but I don't know why ...

                        send: 10-10-2-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
                        send: 10-10-2-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.4
                        send: 10-10-2-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:2
                        sensor started, id=10, parent=2, distance=2
                        send: 10-10-2-0 s=255,c=3,t=11,pt=0,l=12,sg=0,st=ok:Energy Meter
                        send: 10-10-2-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=fail:1.0
                        send: 10-10-2-0 s=0,c=0,t=13,pt=0,l=0,sg=0,st=ok:
                        send: 10-10-2-0 s=0,c=2,t=24,pt=0,l=0,sg=0,st=fail:
                        send: 10-10-2-0 s=0,c=2,t=24,pt=0,l=0,sg=0,st=ok:
                        send: 10-10-2-0 s=0,c=2,t=24,pt=0,l=0,sg=0,st=ok:
                        
                        1 Reply Last reply
                        0
                        • fredokF Offline
                          fredokF Offline
                          fredok
                          wrote on last edited by
                          #37

                          hummm. i use this sketch with arduino mini near the GW (approx 5m max / 1 Wall) and i not see disconnection. So i cant help u.

                          1 Reply Last reply
                          0
                          • chatainsimC Offline
                            chatainsimC Offline
                            chatainsim
                            wrote on last edited by
                            #38

                            This is a recurring problem I have with MySensors.
                            All my sensors stop working, I have to clear the eprom then reupload the sketch.
                            Now It's working fine, until it fail again ;)

                            1 Reply Last reply
                            0
                            • qqlapralineQ Offline
                              qqlapralineQ Offline
                              qqlapraline
                              wrote on last edited by
                              #39

                              Hello All,

                              I have tried this code and it works fine.
                              I was questionning myself though. If the index request fails at some point, what would be the behaviour of the code ? From my understanding of it, it will try to get the index again and again until it succeeds. May be a another way would be not to reset the index to 0 and send the additionnal index the next time.
                              What do you think ?

                              QQ.

                              1 Reply Last reply
                              0
                              • fredokF Offline
                                fredokF Offline
                                fredok
                                wrote on last edited by
                                #40

                                Hello, sorry for my late reply.
                                About the sketch,

                                1. At the beginning, the sketch request the last kWh to the GW (I use Domoticz, so GW ask Domoticz the last kWh value) and send to the node.
                                2. If node receiv, the variable pcreceiv=true and it working fine else node wait for the request until he receiv. A serial.print will inform if receiv.

                                Fredok

                                1 Reply Last reply
                                0
                                • qqlapralineQ Offline
                                  qqlapralineQ Offline
                                  qqlapraline
                                  wrote on last edited by
                                  #41

                                  Yep, got that lately :)
                                  Thanks.

                                  QQ.

                                  1 Reply Last reply
                                  0
                                  • fredokF fredok

                                    Hello, if i remember physics course in university Power P=u.i and energy E=integrale(Pdt)=integrale(u.i.dt)=UeffxIeffxcos(phi)
                                    but for particular citizen same me, energy provider non consider cos(phi)

                                    So, i correct my last sketch for request last nrj value to the gw.

                                    // EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3  ***/
                                    #include <SPI.h>
                                    #include <MySensor.h>  
                                    #include "EmonLib.h"             // Include Emon Library
                                    EnergyMonitor emon1;             // Create an instance
                                    #define CHILD_ID 0 
                                    #define PIN_ANALOG_I A2
                                    MySensor gw;
                                    unsigned long lastSend;
                                    unsigned long lastSend2;
                                    unsigned long SEND_FREQUENCY = 20000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
                                    unsigned long SEND_FREQUENCY2 = SEND_FREQUENCY / 25;
                                    int index = 0;
                                    double Irms=0;
                                    double power;
                                    boolean pcReceived = false;
                                    boolean onyva=true;
                                    float nrj=0, old_nrj;
                                    MyMessage IrmsMsg(CHILD_ID,V_WATT);
                                    MyMessage kWhMsg(CHILD_ID,V_KWH);
                                    MyMessage pcMsg(CHILD_ID,V_VAR1);
                                    
                                    
                                    void incomingMessage(const MyMessage &message) 
                                    {
                                      if (message.type==V_VAR1) 
                                      {  
                                        nrj = old_nrj = message.getFloat();
                                        Serial.print("Received last nrj count from gw:");
                                        Serial.println(nrj);
                                        pcReceived = true;
                                      }
                                    }
                                    
                                    void setup()
                                    {  
                                      gw.begin(incomingMessage);
                                      gw.sendSketchInfo("Energy Meter", "1.0");  // Send the sketch version information to the gateway and Controller
                                      gw.present(CHILD_ID, S_POWER);   // Register this device as power sensor
                                      gw.request(CHILD_ID, V_VAR1);
                                      emon1.current(PIN_ANALOG_I, 30.0);       // Current: input pin, calibration.
                                    }
                                    
                                    void loop()
                                    {
                                      if (onyva) gw.process();
                                      onyva = false; 
                                      unsigned long now = millis();
                                      //unsigned long now2 = millis();
                                      bool sendTime2 = now - lastSend2 > SEND_FREQUENCY2;
                                      if (sendTime2) //calcul Irms moy
                                      {
                                        if (index==0) Irms=emon1.calcIrms(1480);
                                        else {
                                        index++;
                                        Irms = (index*Irms+emon1.calcIrms(1480))/(index+1);
                                        }
                                        lastSend2 = now;
                                      }
                                      bool sendTime = now - lastSend > SEND_FREQUENCY;
                                      if (sendTime && pcReceived) 
                                      { 
                                        power = Irms*232.0;
                                        gw.send(IrmsMsg.set(power,1));
                                        Serial.println(Irms*232.0);
                                        nrj += (power*SEND_FREQUENCY/1000)/3.6E6;
                                        gw.send(kWhMsg.set(nrj,5));
                                        gw.send(pcMsg.set(nrj,5));
                                        lastSend = now;
                                        index = 0;
                                        old_nrj=nrj;
                                        onyva=true;
                                      }
                                     else if (sendTime && !pcReceived)
                                     {
                                      gw.request(CHILD_ID, V_VAR1);
                                      lastSend=now;
                                      index=0;
                                      onyva=true;
                                     }
                                    }
                                    
                                    

                                    thx...

                                    warmaniacW Offline
                                    warmaniacW Offline
                                    warmaniac
                                    wrote on last edited by
                                    #42

                                    @fredok

                                    hi , do you have please updated sketch for newest version of domoticz ? This old cause problems when I compiling it.

                                    Thanks :+1:

                                    gohanG 1 Reply Last reply
                                    0
                                    • warmaniacW warmaniac

                                      @fredok

                                      hi , do you have please updated sketch for newest version of domoticz ? This old cause problems when I compiling it.

                                      Thanks :+1:

                                      gohanG Offline
                                      gohanG Offline
                                      gohan
                                      Mod
                                      wrote on last edited by
                                      #43

                                      @warmaniac look here
                                      https://forum.mysensors.org/topic/4276/converting-a-sketch-from-1-5-x-to-2-0-x

                                      warmaniacW 1 Reply Last reply
                                      0
                                      • fredokF Offline
                                        fredokF Offline
                                        fredok
                                        wrote on last edited by
                                        #44

                                        @warmaniac
                                        Sorry i not update this sketch ...

                                        1 Reply Last reply
                                        0
                                        • gohanG gohan

                                          @warmaniac look here
                                          https://forum.mysensors.org/topic/4276/converting-a-sketch-from-1-5-x-to-2-0-x

                                          warmaniacW Offline
                                          warmaniacW Offline
                                          warmaniac
                                          wrote on last edited by warmaniac
                                          #45

                                          @gohan

                                          tried to modify but I always get error

                                          /MySensors.h:328:2: error: #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless.

                                          #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless.

                                          // EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3  ***/
                                          #include <SPI.h>
                                          #include <MySensors.h>  
                                          #include "EmonLib.h"             // Include Emon Library
                                          EnergyMonitor emon1;             // Create an instance
                                          #define CHILD_ID 0 
                                          #define MY_RADIO_NRF24
                                          #define PIN_ANALOG_I A1
                                          
                                          //MySensor gw;
                                          unsigned long lastSend;
                                          unsigned long lastSend2;
                                          unsigned long SEND_FREQUENCY = 20000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
                                          unsigned long SEND_FREQUENCY2 = SEND_FREQUENCY / 25;
                                          int index = 0;
                                          double Irms=0;
                                          double power;
                                          boolean pcReceived = false;
                                          boolean onyva=true;
                                          float nrj=0, old_nrj;
                                          MyMessage IrmsMsg(CHILD_ID,V_WATT);
                                          MyMessage kWhMsg(CHILD_ID,V_KWH);
                                          MyMessage pcMsg(CHILD_ID,V_VAR1);
                                          
                                          
                                          //void incomingMessage(const MyMessage &message) 
                                          
                                          void receive(const MyMessage &message) 
                                          {
                                            if (message.type==V_VAR1) 
                                            {  
                                              nrj = old_nrj = message.getFloat();
                                              Serial.print("Received last nrj count from gw:");
                                              Serial.println(nrj);
                                              pcReceived = true;
                                            }
                                          }
                                          
                                          void presentation () { 
                                            
                                            sendSketchInfo("Energy Meter", "1.0");  // Send the sketch version information to the gateway and Controller
                                            present(CHILD_ID, S_POWER);   // Register this device as power sensor
                                            request(CHILD_ID, V_VAR1);
                                           
                                            }
                                          
                                          void setup()
                                          {  
                                            begin(incomingMessage);
                                            emon1.current(PIN_ANALOG_I, 30.0);       // Current: input pin, calibration.
                                          }
                                          
                                          void loop()
                                          {
                                            if (onyva) process();
                                            onyva = false; 
                                            unsigned long now = millis();
                                            //unsigned long now2 = millis();
                                            bool sendTime2 = now - lastSend2 > SEND_FREQUENCY2;
                                            if (sendTime2) //calcul Irms moy
                                            {
                                              if (index==0) Irms=emon1.calcIrms(1480);
                                              else {
                                              index++;
                                              Irms = (index*Irms+emon1.calcIrms(1480))/(index+1);
                                              }
                                              lastSend2 = now;
                                            }
                                            bool sendTime = now - lastSend > SEND_FREQUENCY;
                                            if (sendTime && pcReceived) 
                                            { 
                                              power = Irms*232.0;
                                              send(IrmsMsg.set(power,1));
                                              Serial.println(Irms*232.0);
                                              nrj += (power*SEND_FREQUENCY/1000)/3.6E6;
                                              send(kWhMsg.set(nrj,5));
                                              send(pcMsg.set(nrj,5));
                                              lastSend = now;
                                              index = 0;
                                              old_nrj=nrj;
                                              onyva=true;
                                            }
                                           else if (sendTime && !pcReceived)
                                           {
                                            request(CHILD_ID, V_VAR1);
                                            lastSend=now;
                                            index=0;
                                            onyva=true;
                                           }
                                          }
                                          

                                          It is possible to compile it with old mysensors library ? It will work with gateway which has a newest version ?

                                          gohanG 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          22

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular