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.8k 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.
  • fredokF fredok

    .... u must to look the monitor for see "Envoi des données .... clamp=0" and after (a few minutes after) "Envoi des données .... clamp=1"
    Look after ur domotic soft for see if your gw become something.
    0_1490792642229_clamp1.jpg
    0_1490792655751_clamp2.jpg

    bye.

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

    @fredok

    nice ! thank you very much for fast solution :) I try it at home , it compiles well :+1:

    1 Reply Last reply
    0
    • warmaniacW warmaniac

      @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 Offline
      gohanG Offline
      gohan
      Mod
      wrote on last edited by
      #49

      @warmaniac you got errors because you need to move the
      "#include <MySensors.h>" after all the "#DEFINE....." otherwise the library does not know how to be configured

      1 Reply Last reply
      0
      • fredokF fredok

        .... u must to look the monitor for see "Envoi des données .... clamp=0" and after (a few minutes after) "Envoi des données .... clamp=1"
        Look after ur domotic soft for see if your gw become something.
        0_1490792642229_clamp1.jpg
        0_1490792655751_clamp2.jpg

        bye.

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

        @fredok

        I modyfied your sketch (deleting unwanted code like dallas and light sensor) it works but i think I done something wrong, because it spams my gateway , now it looks like this

        //#include <Arduino.h>
        #define MY_DEBUG
        #define MY_RADIO_NRF24
        // EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3  ***/
        #include <SPI.h>
        #include <MySensors.h>
        #include <OneWire.h>
        #include "EmonLib.h"             // Include Emon Library
        EnergyMonitor emon1;             // Create an instance
        
        #define CHILD_ID_CLAMP1 0
        #define PIN_ANALOG_I1 A1 //pince amperemetrique1
        
        bool receivedConfig = false;
        bool metric = true; //truc v2.0
        
        //int compat = 0;
        unsigned long lastSend_temp = millis();
        unsigned long lastSend_power = millis();
        unsigned long lastSend_c;
        unsigned long SEND_FREQUENCY = 120000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
        unsigned long SEND_FREQUENCY_C = SEND_FREQUENCY / 30;
        int index = 0;
        double Irms1 = 0;
        boolean pcReceived1 = false;
        
        float nrj1 = 0, old_nrj1;
        
        MyMessage IrmsMsg1(CHILD_ID_CLAMP1, V_WATT);
        MyMessage kWhMsg1(CHILD_ID_CLAMP1, V_KWH);
        MyMessage pcMsg1(CHILD_ID_CLAMP1, V_VAR1);
        
        void presentation()
        {
        
          sendSketchInfo("Multisensors_Entree", "2.0");  // Send the sketch version information to the gateway and Controller
          present(CHILD_ID_CLAMP1, S_POWER);   // Register this device as power sensor
          request(CHILD_ID_CLAMP1, V_VAR1);
        }
        
        
        void receive(const MyMessage &message)
        {
          if (message.type == V_VAR1)
          {
            nrj1 = old_nrj1 = message.getFloat();
            Serial.print("Received last nrj count from gw:");
            Serial.println(nrj1);
            pcReceived1 = true;
          }
        
        }
        
        void setup()
        {
          emon1.current(PIN_ANALOG_I1, 30.0);       // Current: input pin, calibration.
          Serial.println("SETUP completed");
        }
        
        void envoi_donnees(double intrms, int id_clamp)
        {
          Serial.print("Envoi des donnees .... clamp="); Serial.println(id_clamp);
          double energie = (intrms * 232.0 * SEND_FREQUENCY / 1000) / 3.6E6;
          switch (id_clamp)
          {
            case 1:
              {
                send(IrmsMsg1.set(intrms * 232.0, 1));
                nrj1 += energie;
                send(kWhMsg1.set(nrj1, 5));
                send(pcMsg1.set(nrj1, 5));
                old_nrj1 = nrj1;
              }
              break;
          }
        }
        
        void loop() {
          unsigned long now = millis();
          bool sendTime_c = now - lastSend_c > SEND_FREQUENCY_C;
          // Calcul de Irms1 et Irms2
          if (sendTime_c) //calcul Irms moy clamp1/clamp2
          {
            if (index == 0)
            {
              Irms1 = emon1.calcIrms(1480);
            }
            else  
            {
              Irms1 = (index * Irms1 + emon1.calcIrms(1480)) / (index + 1);
            }
            lastSend_c = now;
            index++;
          }
        
        bool sendTime_power = now - lastSend_power > SEND_FREQUENCY;
        
         if (sendTime_power)
        {
        
          // Envoi ou request Puissance1
          if (pcReceived1) envoi_donnees(Irms1, 1);
          else     {
            request(CHILD_ID_CLAMP1, V_VAR1);
            Serial.println("Request VAR1");
          }
        
          }
        }
        

        and I get this results now

        1_1490873287086_zaznam.png 0_1490873287086_meter.png

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

          hello, i see the sketch send everyloop ???
          try this

          #define MY_DEBUG
          #define MY_RADIO_NRF24
          // 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_CLAMP1 0
          #define PIN_ANALOG_I1 A2 //pince amperemetrique1
          
          unsigned long lastSend_power = millis();
          unsigned long lastSend_c = millis();
          unsigned long SEND_FREQUENCY = 120000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
          unsigned long SEND_FREQUENCY_C = SEND_FREQUENCY / 30;
          int index = 0;
          double Irms1 = 0;
          boolean pcReceived1 = false;
          float nrj1 = 0, old_nrj1;
          
          MyMessage IrmsMsg1(CHILD_ID_CLAMP1, V_WATT);
          MyMessage kWhMsg1(CHILD_ID_CLAMP1, V_KWH);
          MyMessage pcMsg1(CHILD_ID_CLAMP1, V_VAR1);
          
          void before() {
          
          }
          
          
          void presentation()
          {
            sendSketchInfo("Multisensors_Entree", "2.0");  // Send the sketch version information to the gateway and Controller
            present(CHILD_ID_CLAMP1, S_POWER);   // Register this device as power sensor
            request(CHILD_ID_CLAMP1, V_VAR1);
          }
          
          
          void receive(const MyMessage &message)
          {
            if (message.type == V_VAR1)
            {
              nrj1 = old_nrj1 = message.getFloat();
              Serial.print("Received last nrj count from gw:");
              Serial.println(nrj1);
              pcReceived1 = true;
            }
          }
          
          void setup()
          {
            emon1.current(PIN_ANALOG_I1, 30.0);       // Current: input pin, calibration.
            Serial.println("SETUP completed");
          }
          
          void envoi_donnees(double intrms, int id_clamp)
          {
            Serial.print("Envoi des donnees .... clamp="); Serial.println(id_clamp);
            double energie = (intrms * 232.0 * SEND_FREQUENCY / 1000) / 3.6E6;
            switch (id_clamp) //can be modified ...
            {
              case 1:
                {
                  send(IrmsMsg1.set(intrms * 232.0, 1));
                  nrj1 += energie;
                  send(kWhMsg1.set(nrj1, 5));
                  send(pcMsg1.set(nrj1, 5));
                  old_nrj1 = nrj1;
                }
                break;
            }
          }
          
          void loop() {
            unsigned long now = millis();
            bool sendTime_c = now - lastSend_c > SEND_FREQUENCY_C;
            // Calcul de Irms1 et Irms2
            if (sendTime_c) //calcul Irms moy clamp1/clamp2
            {
              if (index == 0)
              {
                Irms1 = emon1.calcIrms(1480);
              }
              else  
              {
                Irms1 = (index * Irms1 + emon1.calcIrms(1480)) / (index + 1);
              }
              lastSend_c = now;
              index++;
            }
          
          bool sendTime_power = now - lastSend_power > SEND_FREQUENCY;
          
           if (sendTime_power)
          {
          
            // Envoi ou request Puissance1
            if (pcReceived1) envoi_donnees(Irms1, 1);
            else     {
              request(CHILD_ID_CLAMP1, V_VAR1);
              Serial.println("Request VAR1");
            }
            //on reinitialise les compteurs
            lastSend_power = now;
            index = 0;
            }
          }
          

          if problem copy the screen of monitor.thx

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

            ahoj, if not working i will try with a new arduino for test the code.
            dobrý večer

            warmaniacW 1 Reply Last reply
            0
            • fredokF fredok

              ahoj, if not working i will try with a new arduino for test the code.
              dobrý večer

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

              @fredok

              sketch works better now , not spamming

              0_1490898851738_upload-e9fbdb1a-1c4f-4356-826f-4e9112a25036

              0_1490898895065_upload-f9c9fbff-f6e5-48ee-96de-8bf28054b129

              but I dont understand current values

              0_1490898965008_upload-d6846108-30b7-4a7d-ad02-c0b0897b20fd

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

                its seem good kWh every 2min
                1864W is the power value

                send(IrmsMsg1.set(intrms * 232.0, 1));
                

                the sketch send power value P (in W) and energy (in kWh) calculated over 2min.

                double energie = (intrms * 232.0 * SEND_FREQUENCY / 1000) / 3.6E6;
                
                // and after
                nrj1 += energie;
                send(kWhMsg1.set(nrj1, 5));
                

                in domoticz i have one "dispositif" included instant power Watt and day cumulate energy kWh
                bye

                warmaniacW 1 Reply Last reply
                0
                • fredokF fredok

                  its seem good kWh every 2min
                  1864W is the power value

                  send(IrmsMsg1.set(intrms * 232.0, 1));
                  

                  the sketch send power value P (in W) and energy (in kWh) calculated over 2min.

                  double energie = (intrms * 232.0 * SEND_FREQUENCY / 1000) / 3.6E6;
                  
                  // and after
                  nrj1 += energie;
                  send(kWhMsg1.set(nrj1, 5));
                  

                  in domoticz i have one "dispositif" included instant power Watt and day cumulate energy kWh
                  bye

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

                  @fredok

                  Thank you very much! Only one question left, with multimeter I measured my voltage 241.0 V could I update it in sketch or ?

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

                    a clamp measure only intensity I (in A) and Power =UrmsxIrms so if in u home Urms=241V u can change with this value but the power measurment not consider cosphy
                    by example the power measurment of my fridge =257W but true value is (with cosphy) 257x0,56=....W
                    so if u change 232V with 241V why not but at the end its will continu to be a approximate of the true power.
                    bye

                    warmaniacW 1 Reply Last reply
                    0
                    • fredokF fredok

                      a clamp measure only intensity I (in A) and Power =UrmsxIrms so if in u home Urms=241V u can change with this value but the power measurment not consider cosphy
                      by example the power measurment of my fridge =257W but true value is (with cosphy) 257x0,56=....W
                      so if u change 232V with 241V why not but at the end its will continu to be a approximate of the true power.
                      bye

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

                      @fredok

                      Okay :) understand now, and what about calibration for wiring I followig this site

                      https://learn.openenergymonitor.org/electricity-monitoring/ct-sensors/how-to-build-an-arduino-energy-monitor-measuring-current-only?redirected=true

                      And they calibrate

                       emon1.current(1, 111.1); 
                      

                      And yours calibration

                        emon1.current(PIN_ANALOG_I1, 30.0);
                      

                      Is some difference after this calibrations in measuring ?

                      1 Reply Last reply
                      0
                      • gohanG Offline
                        gohanG Offline
                        gohan
                        Mod
                        wrote on last edited by
                        #58

                        They also have the same sensor with also voltage measurement for a more accurate reading

                        warmaniacW 1 Reply Last reply
                        0
                        • gohanG gohan

                          They also have the same sensor with also voltage measurement for a more accurate reading

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

                          @gohan

                          Because with test sketch from that site I get actual consumption 0,5 kWh but from mysensors sketch I get much higher values look

                          0_1490962213606_upload-ed834413-8133-438b-8e2a-95c017b3f33c

                          1,7 kWh per one hour is too much , I had only TV and fridge in power on mode, I had total consumption per last year about 2140 kWh , which is about 6 kWh per day.

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

                            hello,

                            1. the constant 30 is the divide between ratio/burden resistor including inside the clamp
                              for me ration=1800 and R=62ohm (1800/62 approx 30)
                              http://www.planete-domotique.com/notices/S/C/SCT013-030V.pdf
                            2. i can see 1677 Wh=1,677 kWh ... but 1900W for only TV+fridge its many
                              mine clamp2 log in domoticz
                              0_1490978374266_grphclamp2.jpg
                            3. Maybe it can help u??? i have a pulse powercounter count every flash (1 Wh of energy) of mine electric box and the power (Watt) send to gw is wrong in domoticz but the energy counting (kWh) is great.
                              0_1490978648613_grphclamp2.jpg
                              Where come the wrong of fake power (GW ? sketch ? domoticz , i dont know)
                              bye
                            warmaniacW 1 Reply Last reply
                            0
                            • fredokF fredok

                              hello,

                              1. the constant 30 is the divide between ratio/burden resistor including inside the clamp
                                for me ration=1800 and R=62ohm (1800/62 approx 30)
                                http://www.planete-domotique.com/notices/S/C/SCT013-030V.pdf
                              2. i can see 1677 Wh=1,677 kWh ... but 1900W for only TV+fridge its many
                                mine clamp2 log in domoticz
                                0_1490978374266_grphclamp2.jpg
                              3. Maybe it can help u??? i have a pulse powercounter count every flash (1 Wh of energy) of mine electric box and the power (Watt) send to gw is wrong in domoticz but the energy counting (kWh) is great.
                                0_1490978648613_grphclamp2.jpg
                                Where come the wrong of fake power (GW ? sketch ? domoticz , i dont know)
                                bye
                              warmaniacW Offline
                              warmaniacW Offline
                              warmaniac
                              wrote on last edited by
                              #61

                              @fredok

                              I had SCT-013-000 , not SCT-013-030 maybe this is the problem of measuring .

                              1 Reply Last reply
                              0
                              • warmaniacW Offline
                                warmaniacW Offline
                                warmaniac
                                wrote on last edited by
                                #62

                                updated my sketch with new values for SCT-013-00 result is now

                                #define MY_DEBUG
                                #define MY_RADIO_NRF24
                                // 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_CLAMP1 0
                                #define PIN_ANALOG_I1 A1 //pince amperemetrique1
                                
                                unsigned long lastSend_power = millis();
                                unsigned long lastSend_c = millis();
                                unsigned long SEND_FREQUENCY = 120000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
                                unsigned long SEND_FREQUENCY_C = SEND_FREQUENCY / 30;
                                int index = 0;
                                double Irms1 = 0;
                                boolean pcReceived1 = false;
                                float nrj1 = 0, old_nrj1;
                                
                                MyMessage IrmsMsg1(CHILD_ID_CLAMP1, V_WATT);
                                MyMessage kWhMsg1(CHILD_ID_CLAMP1, V_KWH);
                                MyMessage pcMsg1(CHILD_ID_CLAMP1, V_VAR1);
                                
                                void before() {
                                
                                }
                                
                                
                                void presentation()
                                {
                                  sendSketchInfo("Multisensors_Entree", "2.0");  // Send the sketch version information to the gateway and Controller
                                  present(CHILD_ID_CLAMP1, S_POWER);   // Register this device as power sensor
                                  request(CHILD_ID_CLAMP1, V_VAR1);
                                }
                                
                                
                                void receive(const MyMessage &message)
                                {
                                  if (message.type == V_VAR1)
                                  {
                                    nrj1 = old_nrj1 = message.getFloat();
                                    Serial.print("Received last nrj count from gw:");
                                    Serial.println(nrj1);
                                    pcReceived1 = true;
                                  }
                                }
                                
                                void setup()
                                {
                                  emon1.current(PIN_ANALOG_I1, 111.1);       // Current: input pin, calibration.
                                  Serial.println("SETUP completed");
                                }
                                
                                void envoi_donnees(double intrms, int id_clamp)
                                {
                                  Serial.print("Envoi des donnees .... clamp="); Serial.println(id_clamp);
                                  double energie = (intrms * 240.0 * SEND_FREQUENCY / 1000) / 3.6E6;
                                  switch (id_clamp) //can be modified ...
                                  {
                                    case 1:
                                      {
                                        send(IrmsMsg1.set(intrms * 240.0, 1));
                                        nrj1 += energie;
                                        send(kWhMsg1.set(nrj1, 5));
                                        send(pcMsg1.set(nrj1, 5));
                                        old_nrj1 = nrj1;
                                      }
                                      break;
                                  }
                                }
                                
                                void loop() {
                                  unsigned long now = millis();
                                  bool sendTime_c = now - lastSend_c > SEND_FREQUENCY_C;
                                  // Calcul de Irms1 et Irms2
                                  if (sendTime_c) //calcul Irms moy clamp1/clamp2
                                  {
                                    if (index == 0)
                                    {
                                      Irms1 = emon1.calcIrms(1480);
                                    }
                                    else  
                                    {
                                      Irms1 = (index * Irms1 + emon1.calcIrms(1480)) / (index + 1);
                                    }
                                    lastSend_c = now;
                                    index++;
                                  }
                                
                                bool sendTime_power = now - lastSend_power > SEND_FREQUENCY;
                                
                                 if (sendTime_power)
                                {
                                  // Envoi ou request Puissance1
                                  if (pcReceived1) envoi_donnees(Irms1, 1);
                                  else     {
                                    request(CHILD_ID_CLAMP1, V_VAR1);
                                    Serial.println("Request VAR1");
                                  }
                                  //on reinitialise les compteurs
                                  lastSend_power = now;
                                  index = 0;
                                  }
                                }
                                

                                0_1490992284051_upload-583cafbf-bd94-4bad-8fd3-8c510f5cfbfc

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

                                  great !!!!!

                                  1 Reply Last reply
                                  0
                                  • B Offline
                                    B Offline
                                    brahim
                                    wrote on last edited by
                                    #64

                                    Hi , i update your sketch for Multiple sensor

                                    #define MY_RADIO_NRF24
                                    #define MY_DEBUG
                                    
                                    #include <SPI.h>
                                    #include <MySensors.h>  
                                    #include <EmonLib.h> 
                                    
                                    #define SERIAL_BAUD 115200
                                    
                                    #define PIN_ANALOG_I1 A0   // The input you attached SCT1.
                                    #define PIN_ANALOG_I2 A1   // The input you attached SCT2.
                                    #define PIN_ANALOG_I3 A2   // The input you attached SCT3.
                                    
                                    #define CHILD_ID_WATT 0    // child-id of sensor
                                    #define CHILD_ID_KWH  1    // child-id of sensor
                                    #define CHILD_ID_VAR1 2    // child-id of sensor
                                    
                                    #define MAX_CT 3           // Number of CT sensor
                                    
                                    EnergyMonitor ct1, ct2, ct3;
                                    
                                    MyMessage msgWatt(CHILD_ID_WATT,V_WATT);
                                    MyMessage msgKwh(CHILD_ID_KWH,V_KWH);
                                    MyMessage msgVar(CHILD_ID_VAR1,V_VAR1);
                                    
                                    unsigned long lastSend_power = millis();
                                    unsigned long lastSend_c = millis();
                                    unsigned long SEND_FREQUENCY   = 60000;               // 1 mn Minimum time between send (in milliseconds).
                                    unsigned long FREQUENCY_SAMPLE = SEND_FREQUENCY / 12 ; // ( 5 s)
                                    
                                    #define MAX_CT 3
                                    
                                    double  Irms[MAX_CT];
                                    float   nrj[MAX_CT];
                                    boolean pcReceived[MAX_CT];
                                    
                                    int    index = 0;
                                    
                                    void setup()
                                    {
                                    #ifdef MY_DEBUG
                                      Serial.begin(SERIAL_BAUD);
                                      Serial.println("Begin SETUP");
                                    #endif
                                      //ct1.current(PIN_ANALOG_I1, 111.1);     // Current: input pin, calibration.                                        
                                      ct1.current(PIN_ANALOG_I1, 60);          // current constant = (100 ÷ 0.050) ÷ 33 Homs = 60
                                      ct2.current(PIN_ANALOG_I2, 60);            
                                      ct3.current(PIN_ANALOG_I3, 60);            
                                    
                                      Irms[0] = ct1.calcIrms(1480);            // initial boot to charge up capacitor (no reading is taken) - testing
                                      Irms[1] = ct2.calcIrms(1480);
                                      Irms[2] = ct3.calcIrms(1480);
                                    
                                      for (int i=0; i<MAX_CT; i++) {
                                           Irms[i] = nrj[i] = 0;
                                           pcReceived[i]=false;
                                      }
                                    #ifdef MY_DEBUG
                                      Serial.println("SETUP completed");
                                    #endif
                                    
                                    }
                                    
                                    void presentation() {
                                      
                                      sendSketchInfo("Energy Meter", "2.1");
                                    
                                      present(0, S_POWER);
                                      request(0, V_VAR1);
                                      
                                      present(1, S_POWER);
                                      request(1, V_VAR1);
                                    
                                      present(2, S_POWER);
                                      request(2, V_VAR1);  
                                    }
                                    
                                    
                                    void receive(const MyMessage &message) {
                                      int SensorID ;
                                    
                                      SensorID = message.sensor ;
                                      
                                      if (message.type == V_VAR1) {
                                        
                                          nrj[SensorID] =  message.getFloat();
                                          pcReceived[SensorID] = true;
                                    
                                    #ifdef MY_DEBUG
                                          Serial.print("Received last nrj[");
                                          Serial.print(SensorID);
                                          Serial.print("] count from GW:");
                                          Serial.println(nrj[SensorID]);
                                    #endif
                                      }
                                    }
                                    
                                    void send_data(double intrms, int SensorID) {
                                    
                                    #ifdef MY_DEBUG
                                      Serial.print("Send data for SensorID="); 
                                      Serial.println(SensorID);
                                    #endif
                                    
                                      double energie = (intrms * 240.0 * SEND_FREQUENCY / 1000) / 3.6E6;
                                      
                                      nrj[SensorID] += energie;
                                      send(msgWatt.setSensor(SensorID).set(intrms * 240.0, 1));
                                      send(msgKwh.setSensor(SensorID).set(nrj[SensorID], 5));
                                      send(msgVar.setSensor(SensorID).set(nrj[SensorID], 5));
                                    }
                                    
                                    void loop() {
                                      
                                      unsigned long now = millis();
                                      double _Irms[MAX_CT];
                                      bool sendTime_c = now - lastSend_c > FREQUENCY_SAMPLE;
                                      
                                      if (sendTime_c)
                                      {
                                        _Irms[0] = ct1.calcIrms(1480) ; if (_Irms[0] < 0.3) _Irms[0] = 0;
                                        _Irms[1] = ct2.calcIrms(1480) ; if (_Irms[1] < 0.3) _Irms[1] = 0;
                                        _Irms[2] = ct3.calcIrms(1480) ; if (_Irms[2] < 0.3) _Irms[2] = 0;
                                    
                                        Irms[0] = (index * Irms[0] + _Irms[0]) / (index + 1);
                                        Irms[1] = (index * Irms[1] + _Irms[1]) / (index + 1);
                                        Irms[2] = (index * Irms[2] + _Irms[2]) / (index + 1);
                                    
                                        lastSend_c = now;
                                        index++;
                                    
                                    #ifdef MY_DEBUG
                                        for (int i=0; i<MAX_CT; i++) {
                                            Serial.print("CT");
                                            Serial.print(i);  Serial.print(" : ");
                                            Serial.print(Irms[i]*240.0);  // Apparent power
                                            Serial.print(" ");
                                            Serial.println(Irms[i]);      // Irms
                                        }
                                    #endif
                                      }
                                    
                                      bool sendTime_power = now - lastSend_power > SEND_FREQUENCY;
                                    
                                      if (sendTime_power)
                                      {
                                        for (int i=0; i<MAX_CT; i++) {
                                        // Envoi ou request Puissance1
                                           if (pcReceived[i])
                                            { 
                                             send_data(Irms[i], i);
                                            } else {
                                             request(i, V_VAR1);
                                             Serial.println("Request VAR1");
                                            }
                                        }
                                        //on reinitialise les compteurs
                                        lastSend_power = now;
                                        index = 0;
                                      }
                                    }
                                    
                                    1 Reply Last reply
                                    1
                                    • fredokF Offline
                                      fredokF Offline
                                      fredok
                                      wrote on last edited by
                                      #65

                                      fine thx....

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


                                      24

                                      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