-
Notifications
You must be signed in to change notification settings - Fork 0
/
Energy_Meter_Basic_Setup
391 lines (260 loc) · 14.5 KB
/
Energy_Meter_Basic_Setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#define SKETCH_NAME "Energy_Meter_Basic_Startup"
#define SERIAL_SPEED 230400
/* This is a Get Started Arduino sketch for an ESP32
mounted on a CircuitSetup 6 Channel Energy Meter Main Board
https://circuitsetup.us/product/expandable-6-channel-esp32-energy-meter/?v=7516fd43adaa
This sketch reads the 6 channels monitored by the Energy Meter,
calculates Watt-Hours for each channel, and
Serial.prints out the values for each channel in a formatted table.
It does nothing more, nothing less.
You have my permission to use this code free of charge.
https://github.com/thorathome/Energy-Meter/blob/main/Energy_Meter_Basic_Setup
July 2024
*/
/* ATM90E32 Energy Monitor Demo Application
The MIT License (MIT)
Copyright (c) 2016 whatnick and Ryzee
Modified for simplicity as a basic get-started sketch by thorathome
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* This sketch is based on multiple documents and code sources throughout CircuitSetup's realm
https://github.com/CircuitSetup
https://github.com/CircuitSetup/Expandable-6-Channel-ESP32-Energy-Meter
I found these two sketches to be valuable
https://github.com/CircuitSetup/Expandable-6-Channel-ESP32-Energy-Meter/blob/master/Software/EmonESP/src/energy_meter.cpp
https://github.com/CircuitSetup/Expandable-6-Channel-ESP32-Energy-Meter/blob/master/Software/EmonESP/src/energy_meter.h
Info on the customized ATM90E32 lib
Just create a library with these four files in it as the "custon" ATM90E32 library needed for this device
https://github.com/CircuitSetup/ATM90E32
Configuration info
https://esphome.io/components/sensor/atm90e32.html
Read info here on US 220 VAC power monitoring
https://esphome.io/components/sensor/atm90e32.html#voltage
Read here for better understanding of VApower, RealPower and Power Factor
ALL of which are calculated by this marvelous board.
Short article
https://www.apc.com/us/en/solutions/industry-insights/watts-vs-va-whats-difference-anyway.jsp
*/
#include <SPI.h>
#include <ATM90E32.h> // Make sure you have the ATM90E32.h library from CircuitSetup's library, above
// The Expandable 6-Channel Energy Monitor has 6, count 'em 6 channels
#define NUM_CHANNELS 6
/***** CALIBRATION SETTINGS *****/
/* Line Frequency
* 4485 for 60 Hz (North America)
* 389 for 50 hz (rest of the world)
*/
unsigned short lineFrequency = 4321; // As per energy_meter.h
/* Current Gain for the CT
* 0 for 1x (CTs up to 60mA/720mV) // This sketch sets pgaGain to Zero, adjusts currentGain per each sensor/clamp
* 21 for 2x (CTs up to 30mA/360mV) // I am using 20A and 80A clamps in this sketch
* 42 for 4x (CTs up to 15mA/180mV)
*/
unsigned short pgaGain = 0; // As per energy_meter.h
/* Voltage Gain for the Board
* Here are common voltage calibrations for the Expandable 6 Channel Energy Meter:
* For meter <= v1.2:
* 42080 - 9v AC Transformer - Jameco 112336
* 32428 - 12v AC Transformer - Jameco 167151
* For meter > v1.3:
* 7305 - 9v AC Transformer - Jameco 157041
*/
// Set for the entire board
unsigned short voltageGain = 7406; // 9v AC Transformer - Jameco 157041
// calibrated by 1% to match Kill-a-Watt voltage
/* Current Gain for each CT clamp
* Here are common current calibrations for the Expandable 6 Channel Energy Meter when gain_pga is set to 1X:
* (from https://esphome.io/components/sensor/atm90e32.html)
* 20A/25mA SCT-006: 11131
* 30A/1V SCT-013-030: 8650
* 50A/1V SCT-013-050: 15420
* 80A/26.6mA SCT-010: 41996 (note this will saturate at 2^16/10^3 amps)
* 100A/50ma SCT-013-000: 27961
* 120A/40mA: SCT-016: 41880
*/
// 80A 80A 80A 100A 100A 100A = my setup
unsigned short currentGain[NUM_CHANNELS] = { 41996, 41996, 41996, 27961, 27961, 27961 };
// These values can be tweaked for each amp clamp sensor
#if defined ESP32
#warning "Yes, we found the ESP32"
/*
Chip Select Pins on the ESP32 for the two ATM90E32 chips
ESP32 Pins
CLK - 18
MISO - 19
MOSI - 23
CS1 - 5 (CT1-CT3 & Voltage 1)
CS2 - 4 (CT4-CT6 & Voltage 2)
*/
const int cs1pin = 5; // CT 1-3 These are fixed for the main board.
const int cs2pin = 4; // CT 4-6 These are fixed for the main board.
#else
#warning "No idea what this board is"
#endif
// Initialize the two ATM90E32 ICs on the 6-Channel Board
ATM90E32 sensorIC1{};
ATM90E32 sensorIC2{};
// Global variables to be read from ATM90E32 chips
float temp1, temp2, freq1, freq2,
voltageCT[NUM_CHANNELS], currentCT[NUM_CHANNELS],
activePowerCT[NUM_CHANNELS], vaPowerCT[NUM_CHANNELS], powerFactorCT[NUM_CHANNELS];
// We're going to calculate and sum up wattSeconds every time we take a reading.
float wattSecondsCalculated[NUM_CHANNELS] = { 0.0 }; // We will sum up watt-seconds using Active (Real) Power over time
time_t mostRecentCheckInTime[NUM_CHANNELS]; // Tracks time interval between readings
time_t previousCheckInTime[NUM_CHANNELS] = { 0 }; // Tracks time interval between readings, also signals initial reading if = 0
// -------------------------------------------------------------------
// SETUP
// -------------------------------------------------------------------
void setup()
{
/* Initialize the serial port to host */
Serial.begin ( SERIAL_SPEED );
while ( ! Serial )
{ /* wait for serial port to connect. Needed for native USB */ }
delay ( 1000 );
Serial.println ( "\n\n**********************************************" );
Serial.println ( "Firing up " + String ( SKETCH_NAME ) );
// Initialise the two ATM90E32 ICs & Pass Chip Elect (CS) pin and calibrations to its library
Serial.println ( "\nInitialize the two ATM90E32 ICs" );
sensorIC1.begin ( cs1pin, lineFrequency, pgaGain, voltageGain, currentGain[0], currentGain[1], currentGain[2] ); // Sensors 1-3 on IC1
sensorIC2.begin ( cs2pin, lineFrequency, pgaGain, voltageGain, currentGain[3], currentGain[4], currentGain[5] ); // Sensors 4-6 on IC2
Serial.println ( " ATM90E32 initialization completed." );
delay ( 500 ); // Let the ATM90E32 settle down
Serial.println ( "Setup complete." );
}
// Repeatedly fetch values from the ATM90E32
long loopCount = 0; // Simple counter to see progress in the Serial output window
void loop()
{
loopCount++;
Serial.println ( "\n\nReading " + String ( loopCount ) + " at " + String ( float ( millis() / 1000.0 ), 1 ) + " secs." );
// Get System Status readings. No doc found on what these mean.
unsigned short sys0_1 = sensorIC1.GetSysStatus0(); //EMMState0
unsigned short sys1_1 = sensorIC1.GetSysStatus1(); //EMMState1
unsigned short en0_1 = sensorIC1.GetMeterStatus0(); //EMMIntState0
unsigned short en1_1 = sensorIC1.GetMeterStatus1(); //EMMInsState1
unsigned short sys0_2 = sensorIC2.GetSysStatus0();
unsigned short sys1_2 = sensorIC2.GetSysStatus1();
unsigned short en0_2 = sensorIC2.GetMeterStatus0();
unsigned short en1_2 = sensorIC2.GetMeterStatus1();
Serial.println ( "Sys Status IC1: S0:0x" + String ( sys0_1, HEX ) + " S1:0x" + String ( sys1_1, HEX ) );
Serial.println ( "Meter Status IC1: E0:0x" + String ( en0_1, HEX ) + " E1:0x" + String ( en1_1, HEX ) );
Serial.println ( "Sys Status IC2: S0:0x" + String ( sys0_2, HEX ) + " S1:0x" + String ( sys1_2, HEX ) );
Serial.println ( "Meter Status IC2: E0:0x" + String ( en0_2, HEX ) + " E1:0x" + String ( en1_2, HEX ) );
// Line frequency each chip sees (Mine both use the same power supply)
freq1 = sensorIC1.GetFrequency();
freq2 = sensorIC2.GetFrequency();
// Chip temperature
temp1 = sensorIC1.GetTemperature();
temp2 = sensorIC2.GetTemperature();
// Print out chip temperature and line frequency for each chip
Serial.println ( "\n IC1 IC2" );
Serial.print ( "Temp: " );
printPad ( temp1 );
Serial.print ( String ( temp1, 1 ) + " C " );
printPad ( temp2 );
Serial.println ( String ( temp2, 1 ) + " C" );
Serial.print ( "Freq: " );
printPad ( freq1 );
Serial.print ( String ( freq1, 1 ) + " Hz " );
printPad ( freq2 );
Serial.println ( String ( freq2, 1 ) + " Hz" );
// Look for errors from the two ICs
if ( sys0_1 == 65535 || sys0_1 == 0 || sys0_2 == 65535 || sys0_2 == 0 )
{
/* Print error message if we can't talk to the master board */
Serial.println ( "Error: Not receiving data from the energy meter - check your connections" );
Serial.println ( "\n\n\n\n\n" );
}
else // Board appears to be working properly.
{ // Let's get some readings
/* Get readings from each IC */
// Line voltage
voltageCT[0] = sensorIC1.GetLineVoltageA();
voltageCT[1] = sensorIC1.GetLineVoltageB();
voltageCT[2] = sensorIC1.GetLineVoltageC();
voltageCT[3] = sensorIC2.GetLineVoltageA();
voltageCT[4] = sensorIC2.GetLineVoltageB();
voltageCT[5] = sensorIC2.GetLineVoltageC();
// Current
currentCT[0] = sensorIC1.GetLineCurrentA();
currentCT[1] = sensorIC1.GetLineCurrentB();
currentCT[2] = sensorIC1.GetLineCurrentC();
currentCT[3] = sensorIC2.GetLineCurrentA();
currentCT[4] = sensorIC2.GetLineCurrentB();
currentCT[5] = sensorIC2.GetLineCurrentC();
// Active Energy in Watt Hours (Real Power) Note time when reading was taken
activePowerCT[0] = sensorIC1.GetActivePowerA(); mostRecentCheckInTime[0] = millis();
activePowerCT[1] = sensorIC1.GetActivePowerB(); mostRecentCheckInTime[1] = millis();
activePowerCT[2] = sensorIC1.GetActivePowerC(); mostRecentCheckInTime[2] = millis();
activePowerCT[3] = sensorIC2.GetActivePowerA(); mostRecentCheckInTime[3] = millis();
activePowerCT[4] = sensorIC2.GetActivePowerB(); mostRecentCheckInTime[4] = millis();
activePowerCT[5] = sensorIC2.GetActivePowerC(); mostRecentCheckInTime[5] = millis();
// Wattage at volts x amps ( Apparent Power )
vaPowerCT[0] = sensorIC1.GetApparentPowerA();
vaPowerCT[1] = sensorIC1.GetApparentPowerB();
vaPowerCT[2] = sensorIC1.GetApparentPowerC();
vaPowerCT[3] = sensorIC2.GetApparentPowerA();
vaPowerCT[4] = sensorIC2.GetApparentPowerB();
vaPowerCT[5] = sensorIC2.GetApparentPowerC();
// Power Factor ( Real Power / Apparent Power )
powerFactorCT[0] = sensorIC1.GetPowerFactorA();
powerFactorCT[1] = sensorIC1.GetPowerFactorB();
powerFactorCT[2] = sensorIC1.GetPowerFactorC();
powerFactorCT[3] = sensorIC2.GetPowerFactorA();
powerFactorCT[4] = sensorIC2.GetPowerFactorB();
powerFactorCT[5] = sensorIC2.GetPowerFactorC();
// Calculate and sum up power consumed in wattSeconds for each channel
for ( int channel = 0; channel < NUM_CHANNELS; channel++ )
{
if ( previousCheckInTime[channel] != 0 )
{
// Calculate time since last reading in seconds
float timeInterval; // Seconds since last time we checked in
timeInterval = float ( mostRecentCheckInTime[channel] - previousCheckInTime[channel] ) / 1000.00; // Convert to elapsed Seconds
// Calculate Watt Seconds watts seconds
wattSecondsCalculated[channel] = wattSecondsCalculated[channel] + ( abs ( activePowerCT[channel] ) * timeInterval );
}
// Set up for the next reading
previousCheckInTime[channel] = mostRecentCheckInTime[channel];
} // summing up wattSeconds
// Print out a little energy table for the 6 channels
printoutTable();
}
delay ( 5000 ); // Measure every 5 seconds for now
} // end loop
// Print out a formatted little table using Serial.print
void printoutTable()
{
Serial.println ( "\n Active VA Power Total" );
Serial.println ( " Volts Amps Power Power Factor Power (calculated)" );
for ( int channel = 0; channel < NUM_CHANNELS; channel++ )
{
Serial.print ( " channel [" + String ( channel + 1 ) + "] " );
printPad ( voltageCT[channel] );
Serial.print ( String ( voltageCT[channel], 1 ) + " VAC "); // AC Voltage measured by the sensor
printPad ( currentCT[channel] );
Serial.print ( String ( currentCT[channel], 1 ) + " A " ); // AC Current measured by the sensor
printPad ( activePowerCT[channel] );
Serial.print ( String ( activePowerCT[channel], 1 ) + " W " ); // Active (Real) Power measured/calculated by the board
printPad ( vaPowerCT[channel] );
Serial.print ( String ( vaPowerCT[channel], 1 ) + " W " ); // Apparent (Volts x Amps) Power measured/calculated by the board
printPad ( powerFactorCT[channel] );
Serial.print ( String ( powerFactorCT[channel], 1 ) + " PF " ); // Power Factor (Active / Apprent) Power calculated by the board
float wattHours = wattSecondsCalculated[channel] / ( 60.0 * 60.0 );
printPad ( wattHours );
Serial.print ( String ( wattHours, 1 ) + " WH " ); // Watt-Seconds calculated here in the sketch over time
Serial.println();
Serial.println();
}
} // end printoutTable
// To keep Serial.print table formatted, adjust spacing
void printPad ( float value )
{
if ( abs ( value ) < 1000.0 ) Serial.print ( " " ); // space instead of leading zeros
if ( abs ( value ) < 100.0 ) Serial.print ( " " );
if ( abs ( value ) < 10.0 ) Serial.print ( " " );
if ( value >= 0.0 ) Serial.print ( " " ); // Negative values throw in a - sign
} // end printPad