-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCT_3Phase_Volt_Current_Serial_EEPROM_2Byte.ino
242 lines (170 loc) · 4.58 KB
/
CT_3Phase_Volt_Current_Serial_EEPROM_2Byte.ino
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
#include "EmonLib.h"
#include "Arduino.h"
#include <EEPROM.h>
#include <avr/pgmspace.h>
EnergyMonitor emon1, emon2, emon3;
double Irmsold1 = 0;
double Irms1 = 0;
double Vrms1 = 0;
double Pwr1 = 0;
float Wh1 = 0;
uint32_t kWh1 = 0;
double Irmsold2 = 0;
double Irms2 = 0;
double Vrms2 = 0;
double Pwr2 = 0;
float Wh2 = 0;
uint32_t kWh2 = 0;
double Irmsold3 = 0;
double Irms3 = 0;
double Vrms3 = 0;
double Pwr3 = 0;
float Wh3 = 0;
uint32_t kWh3 = 0;
int adresse1=0; // startadress for kWh1 adress 0 to 1
int adresse2=2; // startadress for kWh2 adress 2 to 3
int adresse3=4; // startadress for kWh3 adress 4 to 5
const long writeinterval = 3600000; // 60 minute timer for write kWh to eeprom
const long measureinterval = 60000; // 1 minute timer for meassure volt & current
unsigned long previousMillis = 0; // counter for write
unsigned long previousTime = 0; // counter for meassure
int erasePin = 4; //the erase button pin
void setup()
{
Serial.begin(9600);
Serial.println("Starting 3 Phase Energy Monitor V2 : ");
pinMode(erasePin, INPUT_PULLUP);
readWh();
Serial.print("Energy : ");
Serial.print(kWh1); // kWh
Serial.print(" kWh1 : ");
Serial.print(kWh2); // kWh
Serial.print(" kWh2 : ");
Serial.print(kWh3); // kWh
Serial.println(" kWh3 ");
emon1.current(A2, 64.5); // Current: input pin, calibration.
emon2.current(A1, 64.3); // Current: input pin, calibration.
emon3.current(A0, 64.4); // Current: input pin, calibration.
emon1.voltage(A3, 210, 1.7); // Voltage: input pin, calibration, phase_shift
emon2.voltage(A4, 210, 1.7); // Voltage: input pin, calibration, phase_shift
emon3.voltage(A5, 210, 1.7); // Voltage: input pin, calibration, phase_shift
}
void loop()
{
if(!digitalRead(erasePin)) //check if erase button is pressed
{
eraseFlash();
delay(500);
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= writeinterval)
{
previousMillis = currentMillis;
writeWh();
}
if (currentMillis - previousTime >= measureinterval)
{
previousTime = currentMillis;
measure();
}
}
void writeWh()
{
Serial.println("writing values to eeprom : ");
eepromWriteInt(adresse1,kWh1);
eepromWriteInt(adresse2,kWh2);
eepromWriteInt(adresse3,kWh3);
}
void readWh()
{
Serial.println("reading values from eeprom : ");
kWh1 = (eepromReadInt(adresse1));
Wh1 = kWh1*1000;
delay(10);
kWh2 = (eepromReadInt(adresse2));
Wh2 = kWh2*1000;
delay(10);
kWh3 = (eepromReadInt(adresse3));
Wh3 = kWh3*1000;
}
void eraseFlash()
{
for (int i = 0; i <= 5; i++) {
EEPROM.write(i, 0);
Serial.println(i);
delay(100);
}
Serial.println("EEPROM erased");
}
void measure()
{
emon1.calcVI(1480,2000);
Vrms1 = emon1.Vrms;
Irms1 = emon1.Irms-0.11; // Calculate Irms only
if (Irms1<0.01){
Irms1 = 0.00;
}
Pwr1 = Vrms1*Irms1;
Wh1 = Wh1+(Pwr1/60);
kWh1 = Wh1/1000;
emon2.calcVI(1480,2000);
Vrms2 = emon2.Vrms;
Irms2 = emon2.Irms-0.11; // Calculate Irms only
if (Irms2<0.01){
Irms2 = 0.00;
}
Pwr2 = Vrms2*Irms2;
Wh2 = Wh2+(Pwr2/60);
kWh2 = Wh2/1000;
emon3.calcVI(1480,2000);
Vrms3 = emon3.Vrms;
Irms3 = emon3.Irms-0.11; // Calculate Irms only
if (Irms3<0.01){
Irms3 = 0.00;
}
Pwr3 = Vrms3*Irms3;
Wh3 = Wh3+(Pwr3/60);
kWh3 = Wh3/1000;
Serial.print("Phase_1 : ");
Serial.print(Pwr1); // Pwr
Serial.print(" W : ");
Serial.print(Irms1); // Irms
Serial.print(" A : ");
Serial.print(Vrms1); // Vrms
Serial.println(" V ");
Serial.print("Phase_2 : ");
Serial.print(Pwr2); // Pwr
Serial.print(" W : ");
Serial.print(Irms2); // Irms
Serial.print(" A : ");
Serial.print(Vrms2); // Vrms
Serial.println(" V ");
Serial.print("Phase_3 : ");
Serial.print(Pwr3); // Pwr
Serial.print(" W : ");
Serial.print(Irms3); // Irms
Serial.print(" A : ");
Serial.print(Vrms3); // Vrms
Serial.println(" V ");
Serial.print("Energy : ");
Serial.print(kWh1); // kWh
Serial.print(" kWh1 : ");
Serial.print(kWh2); // kWh
Serial.print(" kWh2 : ");
Serial.print(kWh3); // kWh
Serial.println(" kWh3 ");
}
void eepromWriteInt(int adr, uint16_t wert) {
byte low, high;
low=wert&0xFF;
high=(wert>>8)&0xFF;
EEPROM.write(adr, low); // dauert 3,3ms
EEPROM.write(adr+1, high);
return;
}
int eepromReadInt(int adr) {
byte low, high;
low=EEPROM.read(adr);
high=EEPROM.read(adr+1);
return low + ((high << 8)&0xFF00);
}