-
Notifications
You must be signed in to change notification settings - Fork 0
/
OLD_ESP8266_Weather_Station.ino
402 lines (291 loc) · 9.89 KB
/
OLD_ESP8266_Weather_Station.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-mysql-database-php/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#endif
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_TSL2561_U.h>
#include <Adafruit_MPL3115A2.h>
#include "Ai_AP3216_AmbientLightAndProximity.h"
int BoardLed = 16;
int AntenaLed = 2;
bool stat;
bool ambStat;
long alsValue = 0;
// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "XXXX";
// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = "http://192.168.1.88/Cathy/post-Cathy-data.php";
// Keep this API Key value to be compatible with the PHP code provided in the project page.
// If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
String apiKeyValue = "tPmAT5Ab3j7F9";
String sensorName = "MULTI";
String sensorLocation = "Living Room";
String BoardName = "Cathy";
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
Ai_AP3216_AmbientLightAndProximity aps = Ai_AP3216_AmbientLightAndProximity();
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
Adafruit_MPL3115A2 baro;
void setup() {
pinMode(AntenaLed, OUTPUT);
pinMode(BoardLed, OUTPUT);
delay(100);
digitalWrite(AntenaLed,HIGH);
delay(100);
digitalWrite(BoardLed,LOW);
delay(100);
Serial.begin(115200);
////////////////////////////////////////////////////////////////// Wifi /////////////////////////////////////
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
flash_B();
delay(100);
digitalWrite(AntenaLed,HIGH);
delay(100);
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
flash_A();
delay(100);
digitalWrite(AntenaLed,HIGH);
delay(100);
////////////////////////////////////////////////////////////////// AP 3216 /////////////////////////////////////
aps.begin();
aps.startAmbientLightAndProximitySensor ();
ambLight ();
delay (100);
flash_B();
delay(100);
digitalWrite(AntenaLed,HIGH);
delay(100);
////////////////////////////////////////////////////////////////// TSL 2561 /////////////////////////////////////
if(!tsl.begin())
{
/* There was a problem detecting the TSL2561 ... check your connections */
Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
flash_B();
delay(100);
while(1);
}
Lux ();
delay (100);
flash_B();
delay(100);
digitalWrite(AntenaLed,HIGH);
delay(100);
////////////////////////////////////////////////////////////////// MPL 3115A2 /////////////////////////////////////
if (!baro.begin()) {
Serial.println("Could not find sensor. Check wiring.");
flash_B();
delay(100);
while(1);
}
// use to set sea level pressure for current location
// this is needed for accurate altitude measurement
// STD SLP = 1013.26 hPa
baro.setSeaPressure(1013.26);
delay (100);
alt();
delay (100);
flash_B();
delay(100);
digitalWrite(AntenaLed,HIGH);
delay(100);
////////////////////////////////////////////////////////////////// BME 280 /////////////////////////////////////
// (you can also pass in a Wire library object like &Wire2)
stat = bme.begin(0x76);
if (!stat) {
Serial.println("Could not find a valid BME280 sensor, check wiring or change I2C address!");
flash_B();
delay(100);
while (1);
}
flash_B();
delay(100);
digitalWrite(BoardLed,LOW);
delay(100);
flash_A();
delay(100);
}
////////////////////////////////////////////////////////////////// void loop() \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
void loop() {
// Update Sensors again:
ambLight();
delay (100);
Lux();
sensors_event_t event;
tsl.getEvent(&event);
float lux = event.light;
String luxString = String (lux);
delay (100);
alt();
delay(100);
//Check connection status
if(WiFi.status()== WL_CONNECTED && bme.begin(0x76) == 1){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare your HTTP POST request data
String httpRequestData =
"api_key=" + apiKeyValue
+ "&board=" + BoardName
+ "&sensor=" + sensorName
+ "&location=" + sensorLocation
+ "&value4=" + luxString
+ "&value5=" + String(aps.getAmbientLight())
+ "&value1=" + String(bme.readTemperature())
+ "&value2=" + String(bme.readHumidity())
+ "&value3=" + String(bme.readPressure()/100.0F)
+ "&value6=" + String(baro.getPressure())
+ "&value7=" + String(baro.getAltitude())
+ "&value8=" + String(baro.getTemperature())
+ "";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
// You can comment the httpRequestData variable above
// then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
//String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=24.75&value2=49.54&value3=1005.14";
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
// If you need an HTTP request with a content type: text/plain
//http.addHeader("Content-Type", "text/plain");
//int httpResponseCode = http.POST("Hello, World!");
// If you need an HTTP request with a content type: application/json, use the following:
//http.addHeader("Content-Type", "application/json");
//int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
flash_A();
delay(100);
digitalWrite(BoardLed,LOW);
delay(100);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
flash_B();
delay(100);
digitalWrite(AntenaLed,HIGH);
delay(100);
digitalWrite(BoardLed,LOW);
delay(100);
}
// Free resources
http.end();
flash_A();
delay(100);
digitalWrite(BoardLed,LOW);
delay(100);
}
else {
Serial.println("WiFi Disconnected OR Sensor ERORR");
digitalWrite(AntenaLed,HIGH);
delay(100);
flash_B();
delay(100);
digitalWrite(BoardLed,LOW);
delay(100);
}
//Send an HTTP POST request every 60 seconds
delay(60000);
}
////////////////////////////////////////////////////////////////// FUNCTIONS: /////////////////////////////////////
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
void alt() {
float pressure = baro.getPressure();
float altitude = baro.getAltitude();
float temperature = baro.getTemperature();
Serial.println("-----------------");
Serial.print("pressure = "); Serial.print(pressure); Serial.println(" hPa");
Serial.print("altitude = "); Serial.print(altitude); Serial.println(" m");
Serial.print("temperature = "); Serial.print(temperature); Serial.println(" C");
delay(200);
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
void Lux()
{
/* Get a new sensor event */
sensors_event_t event;
tsl.getEvent(&event);
/* Display the results (light is measured in lux) */
if (event.light)
{
Serial.print("lux: ");
Serial.println(event.light);
}
else
{
/* If event.light = 0 lux the sensor is probably saturated
and no reliable data could be generated! */
Serial.println("Sensor overload");
}
delay(200);
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
void ambLight() {
alsValue = aps.getAmbientLight();
long psValue = aps.getProximity();
Serial.print("Ambient Light: ");
Serial.println(alsValue);
Serial.print("Proximity: ");
Serial.println(psValue);
delay(200);
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
void flash_A()
{
digitalWrite(AntenaLed,HIGH);
delay(100);
digitalWrite(AntenaLed,LOW);
delay(100);
digitalWrite(AntenaLed,HIGH);
delay(100);
digitalWrite(AntenaLed,LOW);
delay(100);
digitalWrite(AntenaLed,HIGH);
delay(100);
digitalWrite(AntenaLed,LOW);
delay(100);
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
void flash_B()
{
digitalWrite(BoardLed,LOW);
delay(100);
digitalWrite(BoardLed,HIGH);
delay(100);
digitalWrite(BoardLed,LOW);
delay(100);
digitalWrite(BoardLed,HIGH);
delay(100);
digitalWrite(BoardLed,LOW);
delay(100);
digitalWrite(BoardLed,HIGH);
delay(100);
}
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\