-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiSensor.ino
316 lines (273 loc) · 8.5 KB
/
MultiSensor.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
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
/* MultiSensor_2_5.ino
--------------------------------------------------------------------------------
*/
#include <ESP8266WiFi.h>
//#include <WiFiClientSecure.h>
#include <esp8266-google-home-notifier.h> //ghn
#include "ThingSpeak.h"
#include "settings.h"
//#define MULT 1000000L
#define PIN_Vcc 4 //gpio4 or D2 on nodeMCU v3 to get Vcc
#define PIN_Mode 5 //gpio5 or D1 on nodeMCU v3 to change the Mode
WiFiClient klient; //ThingSpeak client
WiFiClient glient; //Google client
//WiFiClientSecure client; //PushBullet client
GoogleHomeNotifier ghn;
int avgReads = 5; //@
String myStatus = "";
String warningStatus = "";
String msg = "";
String notif = "";
String notif1 = "Moisture level too low for ";
String notif3 = " pots!";
String notif5 = "Battery level bellow minimum"; //minimum = 2.9V [?]
//---------------------in settings.h-------------
String google = "www.google.com";
const char displayName[] = "Hey Google"; //The name of your Google Home (mini) as given in its settings
//-----------------------------------------------
int count = 0;
int pw_sens[] = {14,12,13,15}; //pins to power the sensors
int m_sens[] = {0, 0, 0, 0 }; //the measured values from sensors
int m_per[] = {0, 0, 0, 0 }; //the percent values
bool warn[] = {0, 0, 0, 0 }; //the fields with warning msg
int active = 1000; //1sec power on to allow stabilization
int idle = 3000; //3sec to allow discharge of output cap
int H_max = 582; //get this values from calibration
int H_min = 339; //get this values from calibration
int hp_minW = 40; //min humidity Warning level 50%
int second = 0;
int minute = 0;
int hour = 0;
float vcc = 0.0;
float Vcc_min = 2.9;
float V_corr = 1.5; //corection due to the measurement circuit
int d = 500;
String getTime();
void readMoist();
void setup()
{
Serial.begin(115200); // Initialize serial
Serial.println("\nFor calibration loop put a jumper from D1 (gpio5) to GND");
pinMode(PIN_Mode,INPUT_PULLUP);
while(digitalRead(PIN_Mode) == LOW) //if PIN_Mode is LOW goes to Calibration
{
//calibration code -> see Calibration.ino
//not yet now
Serial.println("Calibration loop");
delay(5000);
}
WiFi.disconnect(); //
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
delay(10);
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}//while
Serial.print(" Connected to IP: "); //@
Serial.println(WiFi.localIP()); //@
ThingSpeak.begin(klient); // Initialize ThingSpeak
//--------------------ghn
Serial.println("\nConnecting to Google Home Mini...");
if (ghn.device(displayName, "en") != true)
{
Serial.println(ghn.getLastError());
return;
}
Serial.print("found Google Home("); //@
Serial.print(ghn.getIPAddress()); //@
Serial.print(":"); //@
Serial.print(ghn.getPort()); //@
Serial.println(")"); //@
//--------------------ghn
Serial.println("\n\rMultiSensor_2_5.ino - Setup done.");
//Vcc read:
pinMode(PIN_Vcc, OUTPUT);
digitalWrite(PIN_Vcc,HIGH); //enables the reading of Vcc
delay(d); //or less
int Vcc = analogRead(A0); //Vcc to be sent to TS
delay(d);
digitalWrite(PIN_Vcc,LOW); //disables the reading of Vcc
vcc = V_corr * Vcc / 100 ; //Vcc is integer, vcc is float
Serial.print("Vcc = ");Serial.print(vcc);Serial.println(" V");
}
void loop()
{
//---ask Google for time--------------------------------------------------------
String t = getTime();
Serial.println(t); //@
//TODO check for error messages!
//Read the four sensors
Serial.println("readMoist()"); //@
readMoist(); //read the sensors and fill the values in m_per[]
//Write m_per[] to ThingSpeak
Serial.println("TS.setFields -->"); //@
ThingSpeak.setField(field1, m_per[0]);
ThingSpeak.setField(field2, m_per[1]);
ThingSpeak.setField(field3, m_per[2]);
ThingSpeak.setField(field4, m_per[3]);
ThingSpeak.setField(field5, vcc);
//check for warning level
int m = 0;
int flag = 0;
for (count = 0; count < 4; count++)
{
m = m_per[count];
if (m <= hp_minW)
{
warn[count] = 1;//True
flag++;
}//if
}//for
//Feed the ThingSpeak channel
msg = " -> ";
for(int k = 0; k < 4; k++)
{
msg += "M" + String(k + 1) + "=" + String(m_per[k]) + ", ";
}
msg += "Vcc = " + String(vcc); //Vcc is read from A0, vcc is calculated as float
myStatus = String(t) + msg;
// set the status
Serial.println("TS.setStatus -->");//@
ThingSpeak.setStatus(myStatus);
int x = ThingSpeak.writeFields(ChannelNumber, myWriteAPIKey);
if(x == 200)
{
Serial.println("Channel update OK.");
}//if
else
{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}//else
if (flag > 0)
{
//message to Tweeter, from ThingSpeak
int tt = ThingSpeak.setTwitterTweet(TWITTER_ACC, myStatus);
if(tt == 200)
{
Serial.println("Tweet sent OK");
}
else
{
Serial.println("Twitter error code " + String (tt));
}
//-------------------------------------------------------Twitter
//message to Google Home Notifier
String notif2 = String(flag); //how many pots are at low level
notif = notif1 + notif2 + notif3;
if ((hour >= 8) && (hour < 22))
{
if (ghn.notify(notif.c_str()) != true)
{
Serial.println(ghn.getLastError());
//return;
}
}
//------------------------------------------Google Home Notifier
flag = 0;
}
//message to Google Home Notifier if Vcc < Vcc_min
if (vcc < Vcc_min)
{
notif = notif5;
if ((hour >= 8) && (hour < 22))
{
if (ghn.notify(notif.c_str()) != true)
{
Serial.println(ghn.getLastError());
//return;
}
}
}
Serial.println("Hour: " + String(hour));
WiFi.disconnect();
Serial.println("WiFi disconnected.");
long sleep = SLEEP_TIME_SECONDS * 1000000L - 8; //some correction
Serial.print("Go to sleep for ");
Serial.print(SLEEP_TIME_SECONDS);
Serial.println(" Seconds" );//
ESP.deepSleep(sleep);
}
//----------Functions-----------------------------------------------------------
String getTime()
{
//in the main prog is defined the client to access google as glient
Serial.println(F("connecting to google"));//@
//Serial.println(google); //@
glient.setTimeout(5000); //at the beginning
const int httpPort = 80; //at the beginning
if (!glient.connect(google, httpPort))
{
Serial.println(F("connection failed"));
return "google_fail";
}
// This will send the request to the server
glient.println("HEAD / HTTP/1.1");
glient.println("Host: www.google.com"); // "Host: www.google.com"
glient.println("Accept: */*");
glient.println("User-Agent: Mozilla/4.0 (compatible; esp8266 Arduino;)");
glient.println("Connection: close");
glient.println();
delay(500);
// Read all the characters of the reply from server and print them to Serial
String reply = String("");
while(glient.available())
{
char c = glient.read();
reply = reply + String(c);
}
//Serial.print(reply); //@
//String d = reply.substring(reply.indexOf("Date: ")+11,reply.indexOf("Date: ")+23); //string for date
String t = reply.substring(reply.indexOf("Date: ")+23,reply.indexOf("Date: ")+35); //string for time
hour = t.substring(0, 2).toInt();
minute = t.substring(3, 5).toInt();
second = t.substring(6, 8).toInt();
hour = hour + 20;//for summer is 20, for winter is 19
if (hour >= 24)
{
hour = hour % 24;
}
if(!glient.connected())
{
//Serial.println("disconnecting");
glient.stop();
}
Serial.println("connection closed"); //@
return t;
}
void readMoist() //new funtion
{
int an = 0;
for(count = 0; count < 4; count++)
{//put all control pins at GND -> sensors are inactive
pinMode(pw_sens[count],OUTPUT);
digitalWrite(pw_sens[count], LOW);
}//for
for(count = 0; count < 4; count++)
{
int m = 0;
digitalWrite(pw_sens[count], HIGH); //the active state
delay(active);
//analog read for sensor 'count'
an = analogRead(A0);
m = an;
//to prevent outliers above 100% and below 0%
if (an < H_min)
{
m = H_min;
}
if (an > H_max)
{
m = H_max;
}
//put the result in m_sens[count]
//Serial.println("\t" + String(count) + "\t" + String(an) + "\t" + String(m)); //@
m_sens[count] = m;
//Serial.println(m);
m_per[count] = map(m, H_min, H_max, 100, 0);
Serial.println("\t" + String(count) + "\t" + String(an) + "\t" + String(m_per[count])); //@
digitalWrite(pw_sens[count], LOW);
delay(idle);
}//for
}//readMoist()