Hi chaps,
Thanks Robert for the reply,
So far I have emonlib sending values every 5 seconds over serial and a solar panel voltage monitor on A3 using the sketch below âŠ
// These are from left to right: real power, apparent power, rms voltage, rms current and power factor.
#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
#define NUM_SAMPLES 10
int sum = 0; // sum of samples taken for DC solar volt meter
unsigned char sample_count = 0; // current sample number
float voltage = 0.0; // calculated voltage
void setup() {
Serial.begin(9600);
emon1.voltage(5, 173.85, 1.7); // Voltage: input pin, calibration, phase_shift
emon1.current(4, 111.1); // Current: input pin, calibration.
}
void loop() {
emon1.calcVI(20,2000); // Calculate all. No.of crossings, time-out
//Serial.println ("Real Power,Apparent Power,RMS Voltage,RMS Current,Power Factor");
//emon1.serialprint(); // Print out all variables
Serial.print(" D4567 ");Serial.print(emon1.realPower);
Serial.print(" D4567 ");Serial.print(emon1.apparentPower);
Serial.print(" D2168 ");Serial.print(emon1.Vrms);
Serial.print(" D2166 ");Serial.print(emon1.Irms);
Serial.print(" D4567 ");Serial.print(emon1.powerFactor);
while (sample_count < NUM_SAMPLES) {
sum += analogRead(A3);
sample_count++;
delay(10);
}
voltage = ((float)sum / (float)NUM_SAMPLES * 5.02) / 1024.0;
Serial.print(" D2170 ");Serial.print(voltage * 11.211);
Serial.println(" ");
Serial.println(" ");
sample_count = 0;
sum = 0;
// delay(5000);
}
I am trying to get voltage,current,power,etc values sent to different registers (eg D1544,D1548,D1554) to a PLC using a serial modbus protocol.
The following example works but I have had no success getting real time values from emonlib sent to the PLC using this library.
/*
Modbus-Arduino Example - TempSensor (Modbus Serial)
Copyright by André Sarmento Barbosa
http://github.com/andresarmento/modbus-arduino
*/
#include "Modbus.h"
#include "ModbusSerial.h"
//Modbus Registers Offsets (0-9999)
const int SENSOR_IREG = 100;
//Used Pins
const int sensorPin = A0;
// ModbusSerial object
ModbusSerial mb;
long ts;
void setup() {
// Config Modbus Serial (port, speed, byte format)
mb.config(&Serial, 38400, SERIAL_8N1);
// Set the Slave ID (1-247)
mb.setSlaveId(10);
// Add SENSOR_IREG register - Use addIreg() for analog Inputs
mb.addIreg(SENSOR_IREG);
ts = millis();
}
void loop() {
//Call once inside loop() - all magic here
mb.task();
//Read each two seconds
if (millis() > ts + 2000) {
ts = millis();
//Setting raw value (0-1024)
mb.Ireg(SENSOR_IREG, analogRead(sensorPin));
}
}
Sketch will not compile without const int SENSOR_IREG = 100; and mb.Ireg(SENSOR_IREG, analogRead(sensorPin)); commented out.
Any pointers on how to get emonlib values sent instead of arduino pin states,or an easier to configure modbus library,welcome.
Thanks
Dave