Hello i created this code, ussing shapes of all sample codes:
/*
EmonTx CT123 all in one example
An example sketch for the emontx module for
CT only electricity monitoring.
Part of the openenergymonitor.org project
Licence: GNU GPL V3
Authors: Glyn Hudson, Trystan Lea
Builds upon JeeLabs RF12 library and Arduino
emonTx documentation: http://openenergymonitor.org/emon/modules/emontx/
emonTx firmware code explination: http://openenergymonitor.org/emon/modules/emontx/firmware
emonTx calibration instructions: http://openenergymonitor.org/emon/modules/emontx/firmware/calibration
THIS SKETCH REQUIRES:
Libraries in the standard arduino libraries folder:
- JeeLib https://github.com/jcw/jeelib
- EmonLib https://github.com/openenergymonitor/EmonLib.git
Other files in project directory (should appear in the arduino tabs above)
- emontx_lib.ino
*/
define FILTERSETTLETIME 5000 // Time (ms) to allow the filters to settle before sending data
const int CT1 = 1; // Set to 0 to disable CT channel 1
const int CT2 = 1; // Set to 0 to disable CT channel 2
const int CT3 = 1; // Set to 0 to disable CT channel 3
const int RealPower = 1; // Set to 0 to disable Real Power calculation, AC imput 9v may be suplied
const int Pulse = 1; // Set to 0 to disable Pulse Sensor
const int TP = 1; // Set to 0 to disable Temp sensors
const int AC = 227.5; // Set AC RMS voltage for RMS power calculate
const int CalCoef = 238.5; // Set calibration coeficient voltage for CT123
const int CTSamplesIrms = 1480; // Set number of samples (wafeform) for calculate RMS
const int CTSamples = 50; // Set number of samples (wafeform) for calculate Real RMS
const int SamTimeOut = 2000; // Set Time Out in ms for calculate samples RMS
const int sensorResolution = 10; // DS18B20 resolution 9,10,11 or 12bit corresponding to (0.5, 0.25, 0.125, 0.0625 degrees C LSB), lower resolution means lower power
define freq RF12_868MHZ // Frequency of RF12B module can be RF12_433MHZ, RF12_868MHZ or RF12_915MHZ. You should use the one matching the module you have.
const int nodeID = 10; // emonTx RFM12B node ID
const int networkGroup = 100; // emonTx RFM12B wireless network group - needs to be same as emonBase and emonGLCD
const int UNO = 1; // Set to 0 if your not using the UNO bootloader (i.e using Duemilanove) - All Atmega's shipped from OpenEnergyMonitor come with Arduino Uno bootloader
include <avr/wdt.h>
ISR(WDT_vect) { Sleepy::watchdogEvent(); } // Attached JeeLib sleep function to Atmega328 watchdog -enables MCU to be put into sleep mode inbetween readings to reduce power consumption
include <OneWire.h>
include <DallasTemperature.h>
include "EmonLib.h"
EnergyMonitor ct1,ct2,ct3; // Create instances for each CT channel
define ONE_WIRE_BUS 4 // Data wire is plugged into port 2 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
DeviceAddress sensor; // arrays to hold device address
typedef struct { int ct1rp,ct1ap,ct1pf,ct1vr,ct1ir, ct2rp,ct2ap,ct2pf,ct2vr,ct2ir, ct3rp,ct3ap,ct3pf,ct3vr,ct3ir, battery, pulse, power, temp1, temp2; } PayloadTX; // create structure - a neat way of packaging data for RF comms
PayloadTX emontx;
const int LEDpin = 9; // On-board emonTx LED
boolean settled = false;
// Pulse counting settings
long pulseCount = 0; // Number of pulses, used to measure energy.
unsigned long pulseTime,lastTime; // Used to measure power.
double power, elapsedWh; // power and energy
int ppwh = 4; // pulses per Wh
unsigned int TPcount = 0;
void setup()
{
Serial.begin(9600);
Serial.println("emonTX CT123 + Pulse + Temp 12");
Serial.println("OpenEnergyMonitor.org");
Serial.print("Node: ");
Serial.print(nodeID);
Serial.print(" Freq: ");
if (freq == RF12_433MHZ) Serial.print("433Mhz");
if (freq == RF12_868MHZ) Serial.print("868Mhz");
if (freq == RF12_915MHZ) Serial.print("915Mhz");
Serial.print(" Network: ");
Serial.println(networkGroup);
if (CT1) ct1.currentTX(1, 111.1); // Setup emonTX CT channel (ADC input, calibration)
if (CT2) ct2.currentTX(2, 111.1); // Calibration factor = CT ratio / burden resistance
if (CT3) ct3.currentTX(3, 111.1); // Calibration factor = (100A / 0.05A) / 33 Ohms
if (CT1) ct1.voltageTX(CalCoef, 1.7); // Calibration, phase_shift
if (CT2) ct2.voltageTX(CalCoef, 1.7); // Calibration, phase_shift
if (CT2) ct3.voltageTX(CalCoef, 1.7); // Calibration, phase_shift
if (Pulse) attachInterrupt(1, onPulse, FALLING); // KWH interrupt attached to IRQ 1 = pin3 - hardwired to emonTx pulse jackplug. For connections see: http://openenergymonitor.org/emon/node/208
if (TP) {
pinMode(7,OUTPUT); // DS18B20 power control pin - see jumper setup instructions above
digitalWrite(7,HIGH); // turn on DS18B20
delay(10);
sensors.begin();
if (!sensors.getAddress(sensor, 0)) {
Serial.println("Unable to find temperarture sensors");
} else {
TPcount = sensors.getDeviceCount();
Serial.print("Total Temperature Sensors: ");Serial.println(TPcount);
sensors.setResolution(sensor, sensorResolution);
Serial.print("Temperature Sensor Resolution: ");Serial.println(sensors.getResolution(sensor), DEC);
}
}
rf12_initialize(nodeID, freq, networkGroup); // initialize RFM12B
rf12_sleep(RF12_SLEEP);
pinMode(LEDpin, OUTPUT); // Setup indicator LED
digitalWrite(LEDpin, HIGH);
if (UNO) wdt_enable(WDTO_8S); // Enable anti crash (restart) watchdog if UNO bootloader is selected. Watchdog does not work with duemilanove bootloader // Restarts emonTx if sketch hangs for more than 8s
}
void loop()
{
if (CT1) {
if (RealPower) {
ct1.calcVI(CTSamples,SamTimeOut); // Calculate (No.of wavelengths, time-out) realPower,apparentPower,powerFactor,Vrms,Irms. No.of wavelengths, time-out
emontx.ct1rp = ct1.realPower;
emontx.ct1ap = ct1.apparentPower;
emontx.ct1pf = ct1.powerFactor;
emontx.ct1vr = ct1.Vrms;
emontx.ct1ir = ct1.Irms;
} else {
emontx.ct1rp = 0;
emontx.ct1ap = ct1.calcIrms(CTSamplesIrms) * AC; // Calculate apparentPower ct.calcIrms(number of wavelengths sample)*AC RMS voltage
emontx.ct1pf = 0;
emontx.ct1vr = 0;
emontx.ct1ir = 0;
}
Serial.print("CT1 RealPower: ");Serial.print(emontx.ct1rp);
Serial.print(" ApparentPower: ");Serial.print(emontx.ct1ap);
Serial.print(" PowerFactor: ");Serial.print(emontx.ct1pf);
Serial.print(" Vrms: ");Serial.print(emontx.ct1vr);
Serial.print(" Irms: ");Serial.println(emontx.ct1ir);
}
if (CT2) {
if (RealPower) {
ct2.calcVI(CTSamples,SamTimeOut); // Calculate (No.of wavelengths, time-out) realPower,apparentPower,powerFactor,Vrms,Irms. No.of wavelengths, time-out
emontx.ct2rp = ct2.realPower;
emontx.ct2ap = ct2.apparentPower;
emontx.ct2pf = ct2.powerFactor;
emontx.ct2vr = ct2.Vrms;
emontx.ct2ir = ct2.Irms;
} else {
emontx.ct2rp = 0;
emontx.ct2ap = ct2.calcIrms(CTSamplesIrms) * AC; // Calculate apparentPower ct.calcIrms(number of wavelengths sample)*AC RMS voltage
emontx.ct2pf = 0;
emontx.ct2vr = 0;
emontx.ct2ir = 0;
}
Serial.print("CT2 RealPower: ");Serial.print(emontx.ct2rp);
Serial.print(" ApparentPower: ");Serial.print(emontx.ct2ap);
Serial.print(" PowerFactor: ");Serial.print(emontx.ct2pf);
Serial.print(" Vrms: ");Serial.print(emontx.ct2vr);
Serial.print(" Irms: ");Serial.println(emontx.ct2ir);
}
if (CT3) {
if (RealPower) {
ct3.calcVI(CTSamples,SamTimeOut); // Calculate (No.of wavelengths, time-out) realPower,apparentPower,powerFactor,Vrms,Irms. No.of wavelengths, time-out
emontx.ct3rp = ct3.realPower;
emontx.ct3ap = ct3.apparentPower;
emontx.ct3pf = ct3.powerFactor;
emontx.ct3vr = ct3.Vrms;
emontx.ct3ir = ct3.Irms;
} else {
emontx.ct3rp = 0;
emontx.ct3ap = ct3.calcIrms(CTSamplesIrms) * AC; // Calculate apparentPower ct.calcIrms(number of wavelengths sample)*AC RMS voltage
emontx.ct3pf = 0;
emontx.ct3vr = 0;
emontx.ct3ir = 0;
}
Serial.print("CT3 RealPower: ");Serial.print(emontx.ct3rp);
Serial.print(" ApparentPower: ");Serial.print(emontx.ct3ap);
Serial.print(" PowerFactor: ");Serial.print(emontx.ct3pf);
Serial.print(" Vrms: ");Serial.print(emontx.ct3vr);
Serial.print(" Irms: ");Serial.println(emontx.ct3ir);
}
if ((TP) && (TPcount>0)) {
sensors.requestTemperatures(); // Send the command to get temperatures
float temp1=(sensors.getTempCByIndex(0));
emontx.temp1=(temp1_100);
Serial.print("TP1 Temperature: ");Serial.println(emontx.temp1);
}
if ((TP) && (TPcount>1)) {
sensors.requestTemperatures(); // Send the command to get temperatures
float temp2=(sensors.getTempCByIndex(1));
emontx.temp2=(temp2_100);
Serial.print("TP2 Temperature: ");Serial.println(emontx.temp2);
}
emontx.battery = ct1.readVcc(); // Read emonTx battey (supply voltage)
Serial.print("Battery: "); Serial.println(emontx.battery);
if (Pulse) {
emontx.pulse = pulseCount;
Serial.print("Pulses: "); Serial.println(emontx.pulse);
Serial.print("Power: "); Serial.println(emontx.power);
}
delay(100);
// because millis() returns to zero after 50 days !
if (!settled && millis() > FILTERSETTLETIME) settled = true;
if (settled) // Send data only after filters have settled
{
send_rf_data(); // SEND RF DATA - see emontx_lib
digitalWrite(LEDpin, HIGH); delay(2); digitalWrite(LEDpin, LOW); // Flash LED
pulseCount=0;
emontx.power=0;
emontx_sleep(5); // Sleep or delay in seconds - see emontx_lib
}
}
// The interrupt routine - runs each time a falling edge of a pulse is detected
void onPulse()
{
lastTime = pulseTime; // Used to measure time between pulses.
pulseTime = micros();
pulseCount++; // PulseCounter
emontx.power = int((3600000000.0 / (pulseTime - lastTime))/ppwh); // Calculate power
}