/* "Maximal" sketch to demonstrate emonLibCM This demonstrates the use of every API function. This sketch provides an example of every Application Interface function. Many in fact set the default value for the emonTx V3.4, and are therefore not needed in most cases. If you do need to change a value, the Application Interface section of the User Documentation gives full details. */ #include #include "emonLibCM.h" #include #include int boardID = 8; // TO DEFINE BEFORE FLASHING int CMD; /*6 : "TOP" one close to ESP board //Address = 6 //left jack = current sensor //8 : "DOWN" one //Address = 8 //left jack = pulse sensor */ //ISR(WDT_vect) { Sleepy::watchdogEvent(); } bool recalibrate = false; // Do not demonstrate the recalibration functions int waittime = 10; /* emonhub.conf nodeid is 10 - switch is ignored) See: https://github.com/openenergymonitor/emonhub/blob/emon-pi/configuration.md [[10]] nodename = emontx1 [[[rx]]] names = power1, power2, power3, power4, vrms, temp1, temp2, temp3, temp4, temp5, temp6, pulse datacode = h scales = 1,1,1,1,0.01,0.1,0.1,0.1,0.1,0.1,0.1,1 units =W,W,W,W,V,C,C,C,C,C,C,p */ typedef struct {int val1, val2, val3, val4, val5, Vrms; } PayloadTX; volatile PayloadTX emontx; // create an instance volatile PayloadTX emontx_cum; // create an instance bool SENT = 1; int address; bool bPulse; void setup() { Serial.begin(9600); Serial.println("Set baud=115200"); Serial.end(); Serial.begin(115200); if (boardID == 6){ address = 6; bPulse = false; } else { address = 8; bPulse = true; } Serial.println("\nEmonTx v3.4 EmonLibCM Continuous Monitoring Maximal Demo"); EmonLibCM_SetADC_VChannel(PC3, 268.97); // ADC Input channel, voltage calibration - for Ideal UK Adapter = 268.97 EmonLibCM_SetADC_IChannel(PC0, 90.91, 4.2); // ADC Input channel, current calibration, phase calibration EmonLibCM_SetADC_IChannel(PC1, 90.91, 4.2); // The current channels will be read in this order EmonLibCM_SetADC_IChannel(PC2, 90.91, 4.2); // 90.91 for 100 A : 50 mA c.t. with 22R burden - v.t. leads c.t by ~4.2 degrees EmonLibCM_SetADC_IChannel(PIN_A6, 90.91, 4.2); // 16.67 for 100 A : 50 mA c.t. with 120R burden - v.t. leads c.t by ~1 degree if (bPulse==false){ EmonLibCM_SetADC_IChannel(PIN_A7, 90.91, 4.2); // 16.67 for 100 A : 50 mA c.t. with 120R burden - v.t. leads c.t by ~1 degree } else { //void EmonLibCM_setPulsePin(int _pin [, int _interrupt]) // for the moment I do not use pulse couting } EmonLibCM_setADC(10, 104); // ADC Bits (10 for emonTx & Arduino except Due=12 bits, ADC Duration 104 us for 16 MHz operation) EmonLibCM_ADCCal(3.3); // ADC Reference voltage, (3.3 V for emonTx, 5.0 V for Arduino) EmonLibCM_cycles_per_second(50); // mains frequency 50Hz, 60Hz EmonLibCM_datalog_period(10); // period of readings in seconds - normal value for emoncms.org EmonLibCM_min_startup_cycles(10); // number of cycles to let ADC run before starting first actual measurement //Initialize I2C //rf12_initialize(nodeID, RF_freq, networkGroup); // initialize radio module EmonLibCM_Init(); // Start continuous monitoring. // Wire.onRequest(requestEvent); // register event Wire.begin(address); // join i2c bus with address #8 Wire.onRequest(requestEvent); Wire.onReceive(receiveEvent); } void loop() { if (EmonLibCM_Ready()) { //Serial.println(EmonLibCM_acPresent()?"AC present ":"AC missing "); waittime = millis(); while (millis()-waittime < 5){ } //Real time power emontx.val1 = EmonLibCM_getRealPower(0); // Copy the desired variables ready for transmission emontx.val2 = EmonLibCM_getRealPower(1); emontx.val3 = EmonLibCM_getRealPower(2); emontx.val4 = EmonLibCM_getRealPower(3); if (bPulse == false){ emontx.val5 = EmonLibCM_getRealPower(4); } else { emontx.val5 = 0; //nothing here for pulse counting } emontx.Vrms = EmonLibCM_getVrms(); //Cumulated energy consumption emontx_cum.val1 = EmonLibCM_getWattHour(0); emontx_cum.val2 = EmonLibCM_getWattHour(1); emontx_cum.val3 = EmonLibCM_getWattHour(2); emontx_cum.val4 = EmonLibCM_getWattHour(3); if (bPulse == false){ emontx_cum.val5 = EmonLibCM_getWattHour(4); } else { emontx_cum.val5 = 0;//EmonLibCM_getPulseCount( ) // For the moment pulse couting is not used } SENT = 0; Serial.print("Power1:"); Serial.print(emontx.val1); Serial.print(","); Serial.print("Power2:"); Serial.print(emontx.val2); Serial.print(","); Serial.print("Power3:"); Serial.print(emontx.val3); Serial.print(","); Serial.print("Power4:"); Serial.print(emontx.val4); Serial.print(","); Serial.print("Power5:"); Serial.print(emontx.val5); Serial.print(","); Serial.print("Vrms:"); Serial.println(emontx.Vrms); Serial.print("Power_cum_1:"); Serial.print(emontx_cum.val1); Serial.print(","); Serial.print("Power_cum_2:"); Serial.print(emontx_cum.val2); Serial.print(","); Serial.print("Power_cum_3:"); Serial.print(emontx_cum.val3); Serial.print(","); Serial.print("Power_cum_4:"); Serial.print(emontx_cum.val4); Serial.print(","); Serial.print("Power_cum_5:"); Serial.println(emontx_cum.val5); } } void receiveEvent(int HowMany) { if(Wire.available() > 0) { CMD = Wire.read(); } } void sendval(volatile PayloadTX *PassedStruct){ if (SENT == 0){ byte PayLoad[10]; uint16_t POW1 = PassedStruct->val1; PayLoad[0] = (POW1 >> 8) & 0xFF; PayLoad[1] = POW1 & 0xFF; uint16_t POW2 = PassedStruct->val2; PayLoad[2] = (POW2 >> 8) & 0xFF; PayLoad[3] = POW2 & 0xFF; uint16_t POW3 = PassedStruct->val3; PayLoad[4] = (POW3 >> 8) & 0xFF; PayLoad[5] = POW3 & 0xFF; uint16_t POW4 = PassedStruct->val4; PayLoad[6] = (POW4 >> 8) & 0xFF; PayLoad[7] = POW4 & 0xFF; uint16_t POW5 = PassedStruct->val5; PayLoad[8] = (POW5 >> 8) & 0xFF; PayLoad[9] = POW5 & 0xFF; Wire.write((byte *) &PayLoad, sizeof(PayLoad)); SENT == 1; } else { //Nothing sent Serial.print("NO SENT"); } } void requestEvent() { switch(CMD) { case 0x01: sendval(&emontx); break; //sending real power case 0x02: sendval(&emontx_cum); break; //sending accumulated power in kWh //case 0x03: sendval(); break; default: break; // do nothing } }