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;
}
}