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.
  • 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
                  • fredokF Offline
                    fredokF Offline
                    fredok
                    wrote on last edited by
                    #46

                    Hello, at the end i write a v2.0 of the sketch

                    //#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 <DallasTemperature.h>
                    #include <OneWire.h>
                    #include "EmonLib.h"             // Include Emon Library
                    EnergyMonitor emon1;             // Create an instance
                    EnergyMonitor emon2;
                    
                    #define CHILD_ID_CLAMP1 0
                    #define CHILD_ID_CLAMP2 1
                    #define CHILD_ID_TEMP 2
                    #define CHILD_ID_LDR 3
                    
                    #define PIN_ANALOG_I1 A2 //pince amperemetrique1
                    #define PIN_ANALOG_I2 A3
                    #define PIN_LDR A0 // LDR
                    #define PIN_DALLAS 3
                    
                    //Definition de dallas
                    #define MAX_ATTACHED_DS18B20 1
                    #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
                    OneWire oneWire(PIN_DALLAS);
                    DallasTemperature sensors(&oneWire);
                    float lastTemperature[MAX_ATTACHED_DS18B20];
                    int numSensors = 1;
                    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_TEMP = 300000; //delais d'emission entre 2 mesures de temps
                    unsigned long SEND_FREQUENCY_C = SEND_FREQUENCY / 30;
                    int index = 0;
                    double Irms1 = 0;
                    double Irms2 = 0;
                    boolean pcReceived1 = false;
                    boolean pcReceived2 = false;
                    boolean first_time_temp = HIGH;
                    
                    float nrj1 = 0, old_nrj1;
                    float nrj2 = 0, old_nrj2;
                    int ldr = 0, old_ldr;
                    
                    MyMessage IrmsMsg1(CHILD_ID_CLAMP1, V_WATT);
                    MyMessage kWhMsg1(CHILD_ID_CLAMP1, V_KWH);
                    MyMessage pcMsg1(CHILD_ID_CLAMP1, V_VAR1);
                    MyMessage IrmsMsg2(CHILD_ID_CLAMP2, V_WATT);
                    MyMessage kWhMsg2(CHILD_ID_CLAMP2, V_KWH);
                    MyMessage pcMsg2(CHILD_ID_CLAMP2, V_VAR2);
                    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                    MyMessage msgLight(CHILD_ID_LDR, V_LIGHT_LEVEL);
                    
                    void before() {
                      sensors.begin();
                    }
                    
                    
                    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);
                      present(CHILD_ID_CLAMP2, S_POWER);   // Register this device as power sensor
                      request(CHILD_ID_CLAMP2, V_VAR2);
                      numSensors = sensors.getDeviceCount(); //compte le nb de ds18
                      present(CHILD_ID_TEMP, S_TEMP);
                      present(CHILD_ID_LDR, S_LIGHT_LEVEL);
                    }
                    
                    
                    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;
                      }
                    
                      if (message.type == V_VAR2)
                      {
                        nrj2 = old_nrj2 = message.getFloat();
                        Serial.print("Received last nrj count from gw:");
                        Serial.println(nrj2);
                        pcReceived2 = true;
                      }
                    
                    }
                    
                    void setup()
                    {
                      sensors.setWaitForConversion(false);
                      emon1.current(PIN_ANALOG_I1, 30.0);       // Current: input pin, calibration.
                      emon2.current(PIN_ANALOG_I2, 30.0);
                      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;
                        case 2:
                          {
                            send(IrmsMsg2.set(intrms * 232.0, 1));
                            nrj2 += energie;
                            send(kWhMsg2.set(nrj2, 5));
                            send(pcMsg2.set(nrj2, 5));
                            old_nrj2 = nrj2;
                          }
                          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);
                          Irms2 = emon2.calcIrms(1480);
                        }
                        else  
                        {
                          Irms1 = (index * Irms1 + emon1.calcIrms(1480)) / (index + 1);
                          Irms2 = (index * Irms2 + emon2.calcIrms(1480)) / (index + 1);
                        }
                        lastSend_c = now;
                        index++;
                      }
                    
                      bool sendTime_temp = now - lastSend_temp > SEND_FREQUENCY_TEMP;
                      if (sendTime_temp || first_time_temp)
                      {
                        Serial.println("........ quoiqu'il arrive, on envoi la temp+ldr");
                        first_time_temp = LOW;
                        //Send Temp
                        // Envoi Température d'après https://github.com/mysensors/MySensorsArduinoExamples/blob/master/examples/DallasTemperatureSensor/DallasTemperatureSensor.ino
                        sensors.requestTemperatures();
                        sleep(750);
                        for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++)
                          {
                         // float temperature = static_cast<float>(static_cast<int>((getConfig().isMetric ? sensors.getTempCByIndex(i) : sensors.getTempFByIndex(i)) * 10.)) / 10.;
                          float temperature = static_cast<float>(static_cast<int>((getControllerConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
                          if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00)
                          {
                    
                            // Send in the new temperature
                            send(msgTemp.setSensor(i).set(temperature, 1));
                            Serial.println(".....................Envoi Temp");
                            // Save new temperatures for next compare
                            lastTemperature[i] = temperature;
                          }
                        }
                     
                      //Send light LDR
                      //Envoi Light LDR
                      ldr = (1023 - analogRead(PIN_LDR)) / 10.23;
                      if (old_ldr != ldr)
                      {
                        send(msgLight.set(ldr));
                        old_ldr = ldr;
                      }
                       lastSend_temp=now;
                      }
                    
                    
                    
                     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");
                      }
                    
                    
                      // Envoi ou request Puissance2
                      if (pcReceived2) envoi_donnees(Irms2, 2);
                      else     {
                        request(CHILD_ID_CLAMP2, V_VAR2);
                        Serial.println("Request VAR2");
                      }
                      //on reinitialise les compteurs
                      lastSend_power = now;
                      index = 0;
                      }
                    }
                    
                    
                    
                    
                    
                    

                    attention, i use in the same node a ds18b20 for temp and LRD for light measurment
                    so u need to delete part of sketch.
                    i just compiling/Verifying this with mysensors v2.1 (but not loaded in arduino pro) and no error
                    for emonlib => https://github.com/openenergymonitor/EmonLib

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

                      .... 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 2 Replies Last reply
                      1
                      • 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
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          25

                                          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