Curent sensor/power with 2xcts013-30 + light + temperature
-
Hello everybody today i upgrade my 1 cts013 power sensor with a second curent sensor.
this is my new sketch (since now, it seem to work ...)#include <Arduino.h> // EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3 ***/ #include <SPI.h> #include <MySensor.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_TEMP 1 #define CHILD_ID_LDR 2 #define CHILD_ID_CLAMP2 3 #define PIN_ANALOG_I1 A2 //pince amperemetrique1 #define PIN_ANALOG_I2 A3 #define PIN_LDR A0 // LDR #define PIN_DALLAS 3 MySensor gw; //Definition de dallas #define MAX_ATTACHED_DS18B20 1 OneWire oneWire(PIN_DALLAS); DallasTemperature sensors(&oneWire); float lastTemperature[MAX_ATTACHED_DS18B20]; int numSensors=1; int compat =0; unsigned long lastSend=millis(); unsigned long lastSend_c; unsigned long SEND_FREQUENCY = 90000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway. unsigned long SEND_FREQUENCY_C = SEND_FREQUENCY / 30; int index_temp =3; int index_temp_max =2; int index = 0; double Irms1=0; double Irms2=0; //double power1; //double power2; boolean pcReceived1 = false; boolean pcReceived2 = false; boolean onyva=true; 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 incomingMessage(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.begin(); numSensors = sensors.getDeviceCount(); //compte le nb de ds18 gw.begin(incomingMessage); gw.sendSketchInfo("Energy Meter", "1.0"); // Send the sketch version information to the gateway and Controller gw.present(CHILD_ID_CLAMP1, S_POWER); // Register this device as power sensor gw.request(CHILD_ID_CLAMP1, V_VAR1); gw.present(CHILD_ID_CLAMP2, S_POWER); // Register this device as power sensor gw.request(CHILD_ID_CLAMP2, V_VAR2); gw.present(CHILD_ID_TEMP, S_TEMP); gw.present(CHILD_ID_LDR, S_LIGHT_LEVEL); 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: { gw.send(IrmsMsg1.set(intrms*232.0,1)); nrj1 += energie; gw.send(kWhMsg1.set(nrj1,5)); gw.send(pcMsg1.set(nrj1,5)); old_nrj1=nrj1; } break; case 2: { gw.send(IrmsMsg2.set(intrms*232.0,1)); nrj2 += energie; gw.send(kWhMsg2.set(nrj2,5)); gw.send(pcMsg2.set(nrj2,5)); old_nrj2=nrj2; } break; } } void loop() { if (onyva) gw.process(); onyva = false; 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 = now - lastSend > SEND_FREQUENCY; if (sendTime) { // quoiqu'il arrive, on envoi la temp+ldr index_temp++; if (index_temp>index_temp_max) { index_temp=0; sensors.requestTemperatures(); for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) { float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.; if (lastTemperature[i] != temperature && temperature != -127.00) { gw.send(msgTemp.setSensor(i).set(temperature,1)); lastTemperature[i]=temperature; } } ldr = (1023-analogRead(PIN_LDR))/10.23; if (old_ldr != ldr) { gw.send(msgLight.set(ldr)); old_ldr = ldr; } } if (pcReceived1) { // on envoi les données de puissance+nrj+vvar1 envoi_donnees(Irms1,1); } else { //on demande les données au gw gw.request(CHILD_ID_CLAMP1, V_VAR1); Serial.println("Request VAR1"); } if (pcReceived2) { envoi_donnees(Irms2,2); } else { gw.request(CHILD_ID_CLAMP2, V_VAR2); Serial.println("Request VAR2"); } //on reinitialise les compteurs lastSend=now; index=0; onyva=true; } }:point_right: For each cts013 circuit, i use the classic circuit (http://systemadmin.es/wp-content/uploads/emolib_schema.jpg)
:point_right: be carefull, with only curent sensor, the sketch can't calculate true power P (active power P=UxIxcos(phi)) but only P=UxI) (look like OWL cm180).
:point_right: Sorry for my english ....
have a nice day...:smiley:
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login