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