-
-
Save llpamies/f718fe9b8cb3fd7283dd8875b37277e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <avr/wdt.h> | |
#include "emonLibCM.h" | |
#define NUM_CTS 4 | |
// EmonTx Settings | |
constexpr float ct_cal[] = {90.9, 90.9, 90.9, 16.67}; | |
constexpr float ct_lead[] = {4.2, 4.2, 4.2, 6}; | |
constexpr float vCal = 154.76; | |
// Voltage to use for calculating assumed apparent power if a.c input is absent. | |
constexpr float assumedVrms = 125.0; | |
// Period of readings in seconds. Every that many seconds a new reading is | |
// produced. | |
constexpr float datalog_period_secs = 9.96; | |
// Status LED pin. | |
constexpr byte LEDpin = 13; | |
typedef struct { | |
unsigned long Msg; | |
int Vrms; | |
int power[NUM_CTS]; | |
double energy[NUM_CTS]; | |
} PayloadTx; | |
typedef struct { | |
bool ct_present[NUM_CTS]; | |
} EmonTxStatus; | |
EmonTxStatus status; | |
PayloadTx emontx; | |
void blink() { | |
digitalWrite(LEDpin, HIGH); | |
delay(200); | |
digitalWrite(LEDpin, LOW); | |
} | |
void send_data(const PayloadTx& emontx, const EmonTxStatus& status) { | |
Serial.print(F("MSG:")); | |
Serial.print(emontx.Msg); | |
Serial.print(F(",Vrms:")); | |
Serial.print(emontx.Vrms * 0.01); | |
for (byte i = 0; i< NUM_CTS; i++){ | |
if (status.ct_present[i]) { | |
Serial.print(F(",P")); | |
Serial.print(i); | |
Serial.print(F(":")); | |
Serial.print(emontx.power[i]); | |
Serial.print(F(",E")); | |
Serial.print(i); | |
Serial.print(F(":")); | |
Serial.print(emontx.energy[i]); | |
} | |
} | |
Serial.println(""); | |
delay(20); | |
} | |
void setup() { | |
wdt_enable(WDTO_8S); | |
// Initialize LED; | |
pinMode(LEDpin, OUTPUT); | |
// Serial | |
Serial.begin(115200); | |
delay(5000); | |
// AC channel configuration. | |
EmonLibCM_SetADC_VChannel(0, vCal); | |
// ADC Reference voltage, (3.3 V for emonTx, 5.0 V for Arduino) | |
EmonLibCM_ADCCal(5.0); | |
EmonLibCM_setAssumedVrms(assumedVrms); | |
for (byte i = 0; i< NUM_CTS; i++){ | |
// Check connected CT sensors | |
if (analogRead(i+1) > 0 || i == 0) { | |
status.ct_present[i] = true; | |
Serial.print(F("CT detected in ADC ")); | |
Serial.print(i+1); | |
Serial.print(F(", calibration: ")); | |
Serial.println(ct_cal[i]);; | |
EmonLibCM_SetADC_IChannel(i+1, ct_cal[i], ct_lead[i]); | |
} | |
} | |
// Mains frequency 60Hz | |
EmonLibCM_cycles_per_second(60); | |
EmonLibCM_datalog_period(datalog_period_secs); | |
EmonLibCM_min_startup_cycles(10); | |
EmonLibCM_Init(); | |
emontx.Msg = 0; | |
} | |
void loop() { | |
blink(); | |
if (EmonLibCM_Ready()) { | |
Serial.println("ready"); | |
if (emontx.Msg == 0) { | |
Serial.println(EmonLibCM_acPresent() ? F("With AC ") : F("Without AC ")); | |
delay(5); | |
} | |
for (byte i = 0; i< NUM_CTS; i++){ | |
emontx.power[i] = EmonLibCM_getRealPower(i); | |
emontx.energy[i] = EmonLibCM_getWattHour(i); | |
} | |
emontx.Msg++; | |
emontx.Vrms = EmonLibCM_getVrms() * 100; | |
send_data(emontx, status); | |
} | |
wdt_reset(); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment