Repository navigation
Expand file tree
/
Copy pathProject-Code-Report.c
More file actions
294 lines (243 loc) · 7.58 KB
/
Copy pathProject-Code-Report.c
File metadata and controls
294 lines (243 loc) · 7.58 KB
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
/*
@Author : Mayur Khatri
Description: Smart Energy Meter
Last Edited: 01-May-2016
Implementatation: GSM Module shield implementation with arduino tested with LCD.
*/
#include <SoftwareSerial.h>
#include <LiquidCrystal.h> // Importing LCD Library file
/*
* RS: Pin 12
* EN: Pin 11
* D4: Pin 5
* D5: Pin 4
* D6: Pin 3
* D7: Pin 2
*/
LiquidCrystal lcd (12, 11, 5, 4, 3, 2); //lcd display pin
char mobileNumber; // Will hold the incoming character from the GSM shield
SoftwareSerial SIM900(9, 10);
const int voltageSensor = A0; // voltage pin
const int currentSensor = A1; // current pin
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
double sumWH = 0.00000;
float WH = 0;//energy consumption in watt hour
double sumRupees = 0.00000;//Total energy consumption in rupees
float rupees = 0;//energy consumption in rupees
double Voltage = 0;//AC supply peak voltage
double vrms = 0;//AC supply rms voltage
double current = 0;//load peak current
double irms = 0;//load rms current
double power = 0;//
void setup()
{
Serial.begin(19200);
SIM900.begin(19200);
delay(500);
SIM900.print("AT+CLIP=1\r"); // turn on caller ID notification , to get missed call number
delay(100);
lcd.begin(16,2); // Display Columms, Rows and Size
lcd.clear();
}
// Used to send Total Energy Consumption Billing to Customer
void sendBilling()
{
SIM900.println("AT+CLIP=1\r");
SIM900.println("AT+CMGF=1"); // Setting the GSM Module in Text mode
delay(1000);
SIM900.println("AT+CMGS=\"XXXXXXXXXX\"\r"); // Sending Energy Consumption to Customer's Mobile Number
delay(1000);
SIM900.print("Dear Customer, Your Energy Consumption is :");
SIM900.print(sumWH);
SIM900.print(" and Total Billing is Rs. ");
SIM900.print(sumRupees);
delay(100);
SIM900.println((char)26); // ASCII code of CTRL+Z
delay(100);
}
void energyCalculations()
{
// getting voltage from Input PIN
Voltage = getVPP(0);
vrms = (Voltage / 2.0) * 0.707 * 575; //find total voltage
Serial.print("VOLTAGE : ");
Serial.print(vrms);
Serial.println("Volt");
// getting current from Input PIN
current = getVPP(1);
irms = (current / 2.0) * 0.707 * 1000 / mVperAmp;
// Serial.print("CURRENT :");
// Serial.print(irms);
// Serial.println("Amps");
power=(vrms * irms * 0.3099);
//Serial.print("POWER :");
// Serial.print(power);
//Serial.println("watt");
WH = (power / 3600);
//Serial.print("ENERGY CONSUMED :");
//Serial.print(WH);
//Serial.println("Watt-Hour");
sumWH = sumWH + WH;
//Serial.print("TOTAL ENERGY CONSUMED :");
//Serial.print(sumWH);
//Serial.println("Watt-Hour");
rupees = getReading();
// Serial.print("ENERGY CONSUMED IN RUPEES :");
//Serial.print(rupees);
// Serial.println("Rs.");
sumRupees = sumRupees + rupees ;
//Serial.print("TOTAL ENERGY CONSUMED IN RUPEES :");
//Serial.print(sumRupees);
//Serial.println("Rs.");
//Serial.println(""); // print the next sets of parameter after a blank line
lcd.setCursor(1,0); // set the cursor at 1st col and 1st row
lcd.print(vrms);
lcd.print("v ");
lcd.print(irms);
lcd.print("A");
lcd.setCursor(1,1); // set the cursor at 1st col and 2nd row
lcd.print(power);
lcd.print("w");
//delay(1000);
lcd.clear(); // clear the screen
lcd.setCursor(1,0); // set the cursor at 1st col and 1st row
lcd.print(WH);
lcd.print("WH ");
lcd.setCursor(1,1); // set the cursor at 1st col and 2nd row
lcd.print(rupees);
lcd.print("Rs.");
//delay(1000);
lcd.clear(); // clear the screen
lcd.setCursor(1,0); // set the cursor at 1st col and 1st row
lcd.print(sumWH);
lcd.print("Total WH ");
lcd.setCursor(1,1); // set the cursor at 1st col and 2nd row
lcd.print(sumRupees);
lcd.print("Total Rs.");
//delay(1000);
lcd.clear();
}
void loop()
{
energyCalculations();
gsm();
//if(SIM900.available() > 0)
// {
// Serial.println("Inside SIM900 avaialble");
// Serial.write(SIM900.read());
// delay(200);
// }
}
void gsm()
{
//Serial.println("Inside GSM");
if(SIM900.available() > 0)
{
//Serial.println("SIM900 is available");
delay(10);
mobileNumber = SIM900.read();
Serial.print(mobileNumber);
if(mobileNumber == 'X')
{
delay(10);
mobileNumber = SIM900.read();
if(mobileNumber == 'X')
{
delay(10);
mobileNumber = SIM900.read();
if(mobileNumber == 'X')
{
delay(10);
mobileNumber = SIM900.read();
if(mobileNumber == 'X')
{
delay(10);
mobileNumber = SIM900.read();
if(mobileNumber == 'X')
{
delay(10);
mobileNumber = SIM900.read();
if(mobileNumber == 'X')
{
delay(10);
mobileNumber = SIM900.read();
if(mobileNumber == 'X')
{
delay(10);
mobileNumber = SIM900.read();
if(mobileNumber == 'X')
{
delay(10);
mobileNumber = SIM900.read();
if(mobileNumber == 'X')
{
delay(10);
mobileNumber = SIM900.read();
if(mobileNumber == 'X')
{
// delay(10);
Serial.println("Number Matched !");
Serial.println("Sending Billing to Customer");
//delay(90000);
sendBilling();
}
}
}
}
}
}
}
}
}
}
}
}
float getVPP(int pinValue)
{
// pinValue = 0 means it is Voltage Input , pinValue = 1 means it is Current Input
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
{
if(pinValue == 0)
{
// reading Voltage Input PIN
readValue = analogRead(voltageSensor);
}
else if(pinValue == 1)
{
// reading Current Input PIN
readValue = analogRead(currentSensor);
}
// 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.0) / 1024.0;
return result;
}
float getReading()
{
float solution;
if(sumWH <= 50)
solution = (WH * 3.15);
if(( sumWH > 50 ) && ( sumWH <= 100 ))
solution = ( WH * 3.60 );
if(( sumWH > 100 ) && (sumWH <= 250))
solution = (WH * 4.25);
if(sumWH > 250)
solution = (WH * 5.20);
return solution;
}