-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenergy_monitor.ino
250 lines (169 loc) · 6.46 KB
/
energy_monitor.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
243
244
245
246
247
248
249
250
#include <ESP8266WiFi.h>
#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
#include <ArduinoJson.h>
#include <ArduinoJson.hpp>
#include <PubSubClient.h>
//----------------------------------------------------------------------------------------------
const char* mqtt_server = "192.168.137.227";
// ----------------------------------------------------------------------------------------------
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ADHAM";
char pass[] = "adham123";
String GAS_ID = "AKfycbxrwXo9Y1ob0XrHmuY4CoEnKXriHKltzP8tQAHALs6KyZQHsC9K4XUp1TL2crg0fvJJpg"; //--> spreadsheet script ID
//Your Domain name with URL path or IP address with path
const char* host = "script.google.com"; // only google.com not https://google.com
// ----------------------Google Sheets Timer Variable Specification Variables----------------------------------------
#define UPDATE_INTERVAL_HOUR (0)
#define UPDATE_INTERVAL_MIN (0)
#define UPDATE_INTERVAL_SEC (5)
#define UPDATE_INTERVAL_MS ( ((UPDATE_INTERVAL_HOUR*60*60) + (UPDATE_INTERVAL_MIN * 60) + UPDATE_INTERVAL_SEC ) * 1000 )
// ----------------------------------------------------------------------------------------------
float Volt;
float power;
float Bill;
double Irms;
// Initializes the espClient. You should change the espClient name if you have multiple ESPs running in the system
//Creates a client that can connect to to a specified internet IP address and port as defined in client.connect().
WiFiClient espClient;
//MQTT client
PubSubClient client(espClient);
// ----------------------------Function to update google sheets with values------------------------------
void update_google_sheet()
{
Serial.print("connecting to ");
Serial.println(host);
// Initializes the https client.
//Creates a client that can connect to to a specified internet IP address and port as defined in client.connect().
WiFiClientSecure client;
// 80 is for HTTP / 443 is for HTTPS!
const int httpPort = 443;
// this is disaples the certificate check needed stage in the https handshaking framework
client.setInsecure();
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
//-----Processing data and sending data--------
String url = "/macros/s/" + GAS_ID + "/exec?power=";
url += String(power);
url += "&bill=";
url += String(Bill);
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println();
Serial.println("closing connection");
}
//--------------------------------------------------------------------------------
void callback(char* topic, byte* payload, unsigned int length) {
}
//----------------------------to ensure connectivity to the broker------------------------------
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------
void setup()
{
Serial.begin(115200);
Volt = 220.0;
//
emon1.current(A0, 111.1); // Current: input pin, calibration.
//----------------------------------------Wait for connection
Serial.print("Connecting");
WiFi.begin(ssid, pass); //--> Connect to your WiFi router
while (WiFi.status() != WL_CONNECTED)
{
Serial.println("Alert Wifi Not Connected");
delay(2000);
}
////mqtt broker adress and port
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
// ----------------------------------------------------------------------------------------------
unsigned long time_ms;
unsigned long time_1000_ms_buf;
unsigned long time_sheet_update_buf;
unsigned long time_dif;
unsigned long time_dif1;
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
StaticJsonDocument<64> doc;
char output[55];
// Update data to google sheet in specific period
time_ms = millis();
time_dif = time_ms - time_sheet_update_buf;
if ( time_dif >= UPDATE_INTERVAL_MS ) // Specific period
{
time_sheet_update_buf = time_ms;
Irms = 0;
Irms += emon1.calcIrms(1480); // Calculate Irms current only
power = (Irms * 220)/1000;
/////billing condional system based on which segment your power consumption is
if (power < 50){
Bill = power * 0.48 ;
}
else if (power > 50 && power < 100){
Bill = power * 0.58;
}
else if ( power > 100 && power < 200) {
Bill = power * 0.77;
}
Serial.print("Current: ");
Serial.print(Irms);
Serial.println(" A");
Serial.print("Power: ");
Serial.print(power);
Serial.println(" Kwatt");
Serial.print("Billing Price: ");
Serial.print(Bill);
Serial.println(" EGP");
update_google_sheet();
}
time_ms = millis();
time_dif1 = time_ms - time_1000_ms_buf;
// Read and print serial data every 1 sec
if ( time_dif1 >= 1000 ) // 1sec
{
//specifying keys and value in the json document
doc["I"] = Irms;
doc["V"] = Volt;
doc["P"] = power;
Serial.println("Read");
/////function to serialize json document
serializeJson(doc, output);
Serial.println(output);
client.publish("/home/Power", output);
Serial.println("Sent");
}
}///--------The End of The Void Loop-------///