-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDIAruduinocode.ino
219 lines (180 loc) · 4.48 KB
/
EDIAruduinocode.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
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int sensorIn = A0;
const int sensorIn2 = A1;
int mVperAmp = 185; // use 185 for 5A, 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double V=0,A=0;
double ARMS = 0;
double AmpsRMS = 0;
double Pow = 000.00;
float energy = 0.0; // cumulative energy in kWh
unsigned long lastMillis = 0;
float bill_amount = 0;
int sensorValue1 = 0;
int crosscount = 0;
int climb_flag = 0;
int val[100];
int max_v = 0;
double VmaxD = 0;
double VeffD = 0;
double Veff = 0;
void setup(){
lcd.init(); // initialize the lcd
lcd.backlight(); // Turn on the LCD screen backlight
pinMode(A0, INPUT);
pinMode(A1, INPUT);
Serial.begin(115200);
delay(10);
Serial.println(F("Init...."));
}
void loop(){
A = getAmp();
ARMS = (A/2.0) *0.707; // sq root
AmpsRMS = (ARMS * 1000)/mVperAmp;
V = getVeff();
float Wattage = (V*AmpsRMS)-18; //Observed 18-20 Watt when no load was connected, so substracting offset value to get real consumption.
Pow=AmpsRMS*V;
if(AmpsRMS < 0.34 ){
Serial.print(0.00);
//Serial.println(" Amps RMS ");
Serial.print("||");
Serial.print(0.00,4);
//Serial.println(" Voltage ");
Serial.print("||");
Pow=0;
Serial.print(Pow,4);
Serial.print(",");
//Serial.println(" Power ");
Serial.print(0.00,4);
unsigned long nowMillis = millis();
energy = energy + (Pow * (nowMillis - lastMillis) / 3600000.0);
lastMillis = nowMillis;
bill_amount = energy ;
Serial.print(",");
Serial.print(energy,4);
//Serial.println(" Energy ");
Serial.print(",");
Serial.print(bill_amount *0.008,5);
//Serial.println(" bill_amount ");
//Serial.println("--------------");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("Units=");
lcd.print(energy,4);
lcd.setCursor(0, 1);
lcd.print("Amount=");
lcd.print(bill_amount *0.008,5);
}
else{
Serial.print(AmpsRMS);
// Serial.println(" Amps RMS ");
Serial.print("||");
Serial.print(V);
Pow=AmpsRMS*V;
if(Pow>=100){
Serial.print("||");
Serial.print(Pow);
}
else{
Serial.print("||");
Serial.print(Pow,3);
}
if(Wattage>=100){
Serial.print(",");
Serial.print(Wattage);
}
else{
Serial.print(",");
Serial.print(Wattage,3);
}
unsigned long nowMillis = millis();
energy = energy + (Pow * (nowMillis - lastMillis) / 3600000.0);
lastMillis = nowMillis;
bill_amount = energy ;
Serial.print(",");
Serial.print(energy,4);
//Serial.println(" Energy ");
Serial.print(",");
Serial.print(bill_amount *0.008,5);
//Serial.println(" bill_amount ");
//Serial.println("--------------");
Serial.println();
lcd.setCursor(0, 0);
lcd.print("Units=");
lcd.print(energy,4);
lcd.setCursor(0, 1);
lcd.print("Amount=");
lcd.print(bill_amount *0.008,5);
}
}
double getVeff()
{
double sensorValue1 = 0;
int crosscount = 0;
int climb_flag = 0;
int val[100];
int max_v = 0;
double VmaxD = 0;
double VeffD = 0;
double Veff = 0;
for ( int i = 0; i < 100; i++ ) {
sensorValue1 = analogRead(sensorIn2);
if (analogRead(sensorIn2) > 511) {
val[i] = sensorValue1;
}
else {
val[i] = 0;
}
delay(1);
}
max_v = 0;
for ( int i = 0; i < 100; i++ )
{
if ( val[i] > max_v )
{
max_v = val[i];
}
val[i] = 0;
}
if (max_v != 0) {
VmaxD = max_v;
VeffD = VmaxD / sqrt(2);
Veff = (((VeffD - 420.76) / -90.24) * -210.2) + 210.2;
}
else {
Veff = 0;
}
VmaxD = 0;
delay(100);
return Veff;
}
float getAmp()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
//record the maximum sensor value/
maxValue = readValue;
}
if (readValue < minValue)
{
//record the maximum sensor value/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue - minValue) * 5)/1024.0;
return result;
// voltage
delay(5000);
}