· 6 years ago · Jan 15, 2020, 04:06 PM
1//Variabelen:
2double tempDht; //huidige temperatuur van de DHT11 Sensor
3int humiDht; //huidige vochtigheid van de DHT11 Sensor
4double tempBmp; //huidige temperatuur van de BMP180 Sensor
5double presBmp; //huidige druk van de BMP180 Sensor
6double presBmpSea; //druk relatief met zee niveau via de BMP180 Sensor
7const int lightLdrPin = A0; //Lichtsterkte (LDR) Sensor Pin
8int lightLdr; //lichtsterkte waarde
9const int waterSenPin = A1; //Water Sensor Signaal Pin
10int waterSen; //Water Sensor waarde
11const int joystickVrxPin = A2; //Joystick VrX Pin
12int joystickX; //Joystick X waarde
13const int joystickVryPin = A3; //Joystick VrY Pin
14int joystickY; //Joystick Y waarde
15const int joystickSigPin = 9; //Joystick Signaal Pin
16int joystickS; //Joystick Signaal/Switch waarde (ingedrukt of niet)
17int scherm = 0; //Scherm laten veranderen met joystick
18int groeneLedPin = 10; //Groene LED Digitale pin
19int geleLedPin = 11; //Gele LED Digitale pin
20int rodeLedPin = 12; //Rode LED Digitale pin
21
22//LCD - Digitale pinnen 2 tot 7:
23#include <LiquidCrystal.h>
24LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
25
26//DHT11 Sensor - Digitale pin 8:
27#include "DHT.h"
28#define DHTPIN 8
29#define DHTTYPE DHT11
30DHT dht(DHTPIN, DHTTYPE);
31
32//BMP180 Druk Sensor - SDA analoge pin A4, SCL analoge pin A5:
33#include <SFE_BMP180.h>
34#include <Wire.h>
35SFE_BMP180 bmp;
36char bmpStatus;
37#define ALTITUDE 6.0 //Hoogte van Sint-Katelijne-Waver in meter
38
39//WiFi:
40#include <SPI.h>
41#include <WiFiNINA.h>
42#include <WiFi.h>
43char ssid[] = ""; //WiFi naam (SSID)
44char pass[] = ""; //WiFi paswoord
45int status = WL_IDLE_STATUS; //WiFi ontvanger status
46const int GMT = 1; //Tijdzone, Brussel = +1
47
48//Real Time Clock:
49#include <RTCZero.h>
50RTCZero rtc;
51
52//WiFi Buiten Weer - Via OpenWeatherMap:
53#include <ArduinoJson.h>
54String apiKey = "61cb04e1c56086dd1670b0508cf2beb3"; //API Key OpenWeatherMap.org
55String location = "2786642"; //Sint-Katelijne-Waver, België
56char server[] = "api.openweathermap.org"; //Website of server die gebruikt wordt
57WiFiClient client;
58
59//WiFi Webserver:
60WiFiServer server2(80);
61
62
63
64//De SETUP functie:
65void setup() {
66 Serial.begin(9600);
67 lcd.begin(16, 2);
68 dht.begin();
69 bmp.begin();
70 lcd.clear();
71 lcd.setCursor(0, 0);
72 lcd.print("Opstarten...");
73
74 //Joystick knop:
75 pinMode(joystickSigPin, INPUT);
76 digitalWrite(joystickSigPin, HIGH);
77
78 //Status LED'S:
79 pinMode(groeneLedPin, OUTPUT);
80 pinMode(geleLedPin, OUTPUT);
81 pinMode(rodeLedPin, OUTPUT);
82
83 //Groene LED laten flikkeren:
84 for (int i = 0; i < 10; i++) {
85 digitalWrite(groeneLedPin, HIGH);
86 delay(200);
87 digitalWrite(groeneLedPin, LOW);
88 delay(200);
89 }
90
91 //WiFi:
92 //check if the WiFi module works:
93 if (WiFi.status() == WL_NO_SHIELD) {
94 Serial.println("WiFi shield not present");
95 digitalWrite(rodeLedPin, HIGH);
96 delay(5000);
97 digitalWrite(rodeLedPin, LOW);
98 // don't continue:
99 while (true);
100 }
101 String fv = WiFi.firmwareVersion();
102 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
103 Serial.println("Please upgrade the firmware");
104 digitalWrite(rodeLedPin, HIGH);
105 delay(5000);
106 digitalWrite(rodeLedPin, LOW);
107 }
108
109 //attempt to connect to WiFi network:
110 while (status != WL_CONNECTED) {
111 Serial.print("Attempting to connect to network: ");
112 Serial.println(ssid);
113 lcd.clear();
114 lcd.setCursor(0, 0);
115 lcd.print("Verbinding maken");
116 lcd.setCursor(0, 1);
117 lcd.print("met WiFi...");
118 status = WiFi.begin(ssid, pass);
119 delay(10000);
120 }
121
122 //Start webserver:
123 server2.begin();
124
125 //WiFi is connected:
126 printWiFiStatus();
127 digitalWrite(groeneLedPin, HIGH);
128 lcd.clear();
129 lcd.setCursor(0, 0);
130 lcd.print("Verbonden!");
131 delay(2000);
132
133 //Real Time Clock:
134 rtc.begin();
135 unsigned long epoch;
136 int numberOfTries = 0, maxTries = 6;
137 do {
138 epoch = WiFi.getTime();
139 numberOfTries++;
140 }
141 while ((epoch == 0) && (numberOfTries < maxTries));
142 if (numberOfTries == maxTries) {
143 Serial.print("RTC: NTP unreachable!!");
144 lcd.clear();
145 lcd.setCursor(0, 0);
146 lcd.print("RTC: Server RIP");
147 while (1);
148 } else {
149 Serial.print("Epoch received: ");
150 Serial.println(epoch);
151 rtc.setEpoch(epoch);
152 }
153}
154
155
156
157//De LOOP functie:
158void loop() {
159 //Om de de halve seconde alles uitvoeren:
160 delay(500);
161 Serial.println("\n***** ARDUINO WEERSTATION *****");
162
163 //Webserver:
164 // listen for incoming clients
165 WiFiClient client2 = server2.available();
166 if (client2) {
167 Serial.println("new client!");
168 // an http request ends with a blank line
169 boolean currentLineIsBlank = true;
170 while (client2.connected()) {
171 if (client2.available()) {
172 char c = client2.read();
173 Serial.write(c);
174 if (c == '\n' && currentLineIsBlank) {
175 // send a standard http response header
176 client2.println("HTTP/1.1 200 OK");
177 client2.println("Content-Type: text/html");
178 client2.println("Connection: close"); // the connection will be closed after completion of the response
179 client2.println("Refresh: 5"); // refresh the page automatically every 5 sec
180 client2.println();
181 client2.println("<!DOCTYPE HTML>");
182 client2.println("<html>");
183 //Tekst om op de website te printen:
184 client2.print("Arduino Slim IoT Weerstation:");
185 client2.print("<br />");
186 client2.print("<br />");
187 double tempDht2;
188 tempDht2 = dht.readTemperature();
189 client2.print("Binnen temperatuur: ");
190 client2.print(tempDht2);
191 client2.print(" C");
192 client2.print("<br />");
193 int humiDht2;
194 humiDht2 = dht.readHumidity();
195 client2.print("Binnen vochtigheid: ");
196 client2.print(humiDht2);
197 client2.print(" %");
198 client2.print("<br />");
199 client2.print("Lichtsterkte: ");
200 client2.print(analogRead(lightLdrPin));
201 client2.print(" (0 tot 1023: Laag getal = donker, Hoog getal = licht)");
202 client2.print("<br />");
203 client2.print("Regensensor: ");
204 client2.print(analogRead(waterSenPin));
205 client2.print(" (0 tot 1023: Laag getal = droog, Hoog getal = regen)");
206 //Tot hier
207 client2.println("</html>");
208 break;
209 }
210 if (c == '\n') {
211 // you're starting a new line
212 currentLineIsBlank = true;
213 }
214 else if (c != '\r') {
215 // you've gotten a character on the current line
216 currentLineIsBlank = false;
217 }
218 }
219 }
220 // give the web browser time to receive the data
221 delay(1);
222 // close the connection:
223 client2.stop();
224 Serial.println("client2 disonnected");
225 }
226
227 //Joystick waardes inlezen:
228 joystickX = analogRead(joystickVrxPin);
229 joystickY = analogRead(joystickVryPin);
230 joystickS = digitalRead(joystickSigPin);
231
232 //Joystick coördinaten op Serial Monitor:
233 Serial.println("\n- Joystick: -");
234 Serial.print("X: ");
235 Serial.println(joystickX);
236 Serial.print("Y: ");
237 Serial.println(joystickY);
238 Serial.print("Signal: ");
239 Serial.print(joystickS);
240 Serial.println(" (1 = Niet ingedrukt, 0 = Ingedrukt)");
241
242 //Verschillende schermen op LCD:
243 if (joystickX > 1000 || joystickX < 50 || joystickY < 50 || joystickY > 1000) { //Als joystick naar rechts, links, boven of onder gaat:
244 if (joystickX > 1000 || joystickY > 1000) { //Joystick naar rechts of naar beneden:
245 scherm++; //Volgend scherm.
246 digitalWrite(geleLedPin, HIGH);
247 delay(500);
248 digitalWrite(geleLedPin, LOW);
249 }
250 if (joystickX < 50 || joystickY < 50) { //Joystick naar links of naar boven:
251 scherm--; //Vorig scherm.
252 digitalWrite(geleLedPin, HIGH);
253 delay(500);
254 digitalWrite(geleLedPin, LOW);
255 }
256 }
257
258 if (scherm > 7 || scherm < 0) { //Als "scherm" aantal groter is dan hoeveelheid schermen er zijn of kleiner dan 0, zet terug op 0.
259 scherm = 0;
260 }
261
262 switch (scherm) {
263 case 0:
264 klok();
265 break;
266 case 1:
267 binnenTemp();
268 break;
269 case 2:
270 binnenHumi();
271 break;
272 case 3:
273 binnenDruk();
274 break;
275 case 4:
276 binnenLicht();
277 break;
278 case 5:
279 regenSensor();
280 break;
281 case 6:
282 buitenTemp();
283 break;
284 case 7:
285 buitenHumi();
286 break;
287 default:
288 lcd.clear();
289 lcd.setCursor(0, 0);
290 lcd.print("- WEERSTATION: -");
291 break;
292 }
293 Serial.print("\n***** Scherm: ");
294 Serial.print(scherm);
295 Serial.println(" *****");
296}
297
298
299
300//FUNCTIES:
301void klok() {
302 //Print op Serial Monitor:
303 Serial.println("\n- WiFi RTC: -");
304 printDate(); //Standaard RTC functie, staat verder onderaan
305 printTime(); //Standaard RTC functie, staat verder onderaan
306 //Print op LCD:
307 lcd.clear();
308 lcd.setCursor(0, 0);
309 lcd.print(rtc.getDay());
310 lcd.print("/");
311 lcd.print(rtc.getMonth());
312 lcd.print("/");
313 lcd.print(2000 + rtc.getYear());
314 lcd.setCursor(0, 1);
315 lcd.print(rtc.getHours() + GMT);
316 lcd.print(":");
317 lcd.print(rtc.getMinutes());
318 lcd.print(":");
319 lcd.print(rtc.getSeconds());
320}
321
322void binnenTemp() {
323 //BMP180 Sensor:
324 bmpStatus = bmp.startTemperature();
325 if (bmpStatus != 0) {
326 delay(bmpStatus);
327 bmpStatus = bmp.getTemperature(tempBmp); //Pak de temperatuur en steek het in de variabele "tempBmp"
328 //Print op Serial Monitor:
329 Serial.println("\n- BMP180 Sensor: -");
330 Serial.print("Binnen Temperatuur: ");
331 Serial.print(tempBmp);
332 Serial.println(" °C");
333 //Print op LCD:
334 lcd.clear();
335 lcd.setCursor(0, 0);
336 lcd.print("Binnen temp:");
337 lcd.setCursor(0, 1);
338 lcd.print(tempBmp);
339 lcd.print(" "); //spatie
340 lcd.print((char)223); //"°" tekentje
341 lcd.print("C");
342 }
343}
344
345void binnenHumi() {
346 //DHT11 Sensor:
347 tempDht = dht.readTemperature();
348 humiDht = dht.readHumidity();
349 //Print op Serial monitor:
350 Serial.println("\n- DHT11 Sensor: -");
351 Serial.print("Binnen Temperatuur: ");
352 Serial.print(tempDht);
353 Serial.println(" °C");
354 Serial.print("Binnen Vochtigheid: ");
355 Serial.print(humiDht);
356 Serial.println(" %");
357 //Print op LCD:
358 lcd.clear();
359 lcd.setCursor(0, 0);
360 lcd.print("Vochtigheid:");
361 lcd.setCursor(0, 1);
362 lcd.print(humiDht);
363 lcd.print(" %");
364}
365
366void binnenDruk() {
367 //BMP180 Sensor:
368 bmpStatus = bmp.startTemperature();
369 if (bmpStatus != 0) {
370 delay(bmpStatus);
371 bmpStatus = bmp.getTemperature(tempBmp); //Pak de temperatuur en steek het in de variabele "tempBmp"
372 if (bmpStatus != 0) {
373 bmpStatus = bmp.startPressure(3);
374 if (bmpStatus != 0) {
375 delay(bmpStatus);
376 bmpStatus = bmp.getPressure(presBmp, tempBmp); //Pak de druk (adhv "tempBmp") en steek het in de variabele "presBmp"
377 if (bmpStatus != 0) {
378 presBmpSea = bmp.sealevel(presBmp, ALTITUDE); //Pak de relatieve zee-niveau druk en steek het in de variabele "presBmpSea"
379 //Print op Serial Monitor:
380 Serial.print("Absolute druk: ");
381 Serial.print(presBmp);
382 Serial.println(" mb / hPa");
383 Serial.print("Hoogte: ");
384 Serial.print(ALTITUDE, 0);
385 Serial.println(" meter");
386 Serial.print("Relatieve (zee-niveau) druk: ");
387 Serial.print(presBmpSea, 2);
388 Serial.print(" mb, ");
389 Serial.print(presBmpSea * 0.0295333727, 2);
390 Serial.println(" inHg");
391 //Print op LCD:
392 lcd.clear();
393 lcd.setCursor(0, 0);
394 lcd.print("Absolute druk:");
395 lcd.setCursor(0, 1);
396 lcd.print(presBmp);
397 lcd.print(" mb / hPa");
398 }
399 }
400 }
401 }
402}
403
404void binnenLicht() {
405 //Lichtsterkte LDR Sensor - analoge pin A0:
406 lightLdr = analogRead(lightLdrPin);
407 //Print op Serial Monitor:
408 Serial.println("\n- LDR Sensor: -");
409 Serial.print(lightLdr);
410 if (lightLdr < 800) {
411 Serial.println(" = Donker");
412 } else {
413 Serial.println(" = Licht");
414 }
415 //Print op LCD:
416 lcd.clear();
417 lcd.setCursor(0, 0);
418 lcd.print("Lichtsterkte:");
419 lcd.setCursor(0, 1);
420 lcd.print(lightLdr);
421 if (lightLdr < 800) {
422 lcd.print(" = Donker");
423 } else {
424 lcd.print(" = Licht");
425 }
426}
427
428void regenSensor() {
429 //Water Sensor - analoge pin A1:
430 waterSen = analogRead(waterSenPin);
431 //Print op Serial Monitor:
432 Serial.println("\n- Water Sensor: -");
433 Serial.print("Signal: ");
434 Serial.print(waterSen);
435 if (waterSen < 100) {
436 Serial.println(" = Droog");
437 } else if (waterSen > 100 && waterSen < 500) {
438 Serial.println(" = Motregen");
439 } else {
440 Serial.println(" = Regen");
441 }
442 //Print op LCD:
443 lcd.clear();
444 lcd.setCursor(0, 0);
445 lcd.print("Regen sensor:");
446 lcd.setCursor(0, 1);
447 lcd.print(waterSen);
448 if (waterSen < 100) {
449 lcd.print(" = Droog");
450 } else if (waterSen > 100 && waterSen < 500) {
451 lcd.print(" = Mot regen");
452 } else {
453 lcd.print(" = Regen");
454 }
455}
456
457void buitenTemp() {
458 Serial.println("\n- Buiten Temperatuur via WiFi: -");
459 Serial.println("Starting connection to server...");
460 // if you get a connection, report back via serial:
461 if (client.connect(server, 80)) {
462 Serial.println("Connected to server");
463 // Make a HTTP request:
464 client.print("GET /data/2.5/forecast?");
465 client.print("id=" + location);
466 client.print("&appid=" + apiKey);
467 client.print("&cnt=1"); //bepaalde hoeveelheid lists ophalen, hier 1, 8 per dag maximum.
468 client.println("&units=metric");
469 client.println("Host: api.openweathermap.org");
470 client.println("Connection: close");
471 client.println();
472 } else {
473 Serial.println("Unable to connect");
474 digitalWrite(rodeLedPin, HIGH);
475 delay(5000);
476 digitalWrite(rodeLedPin, LOW);
477 lcd.clear();
478 lcd.setCursor(0, 0);
479 lcd.print("Geen connectie");
480 lcd.setCursor(0, 1);
481 lcd.print("met server");
482 }
483 delay(1000);
484 String line = "";
485 while (client.connected()) {
486 line = client.readStringUntil('\n');
487 StaticJsonDocument<10000> doc;
488 DeserializationError error = deserializeJson(doc, line);
489 if (error) {
490 Serial.print("deserializeJson() failed with code ");
491 Serial.println(error.c_str());
492 digitalWrite(rodeLedPin, HIGH);
493 delay(5000);
494 digitalWrite(rodeLedPin, LOW);
495 return;
496 }
497 //Data van de JSON Tree - HUIDIGE TEMPERATUUR BUITEN:
498 String tempWifi = doc["list"][0]["main"]["temp"];
499 //Print op Serial Monitor:
500 Serial.print("Temperatuur buiten: ");
501 Serial.print(tempWifi);
502 Serial.println(" °C");
503 //Print op LCD:
504 lcd.clear();
505 lcd.setCursor(0, 0);
506 lcd.print("Buiten temp:");
507 lcd.setCursor(0, 1);
508 lcd.print(tempWifi);
509 lcd.print(" "); //spatie
510 lcd.print((char)223); //"°" tekentje
511 lcd.print("C");
512 }
513}
514
515void buitenHumi() {
516 Serial.println("\n- Buiten Vochtigheid via WiFi: -");
517 Serial.println("Starting connection to server...");
518 // if you get a connection, report back via serial:
519 if (client.connect(server, 80)) {
520 Serial.println("Connected to server");
521 // Make a HTTP request:
522 client.print("GET /data/2.5/forecast?");
523 client.print("id=" + location);
524 client.print("&appid=" + apiKey);
525 client.print("&cnt=1"); //bepaalde hoeveelheid lists ophalen, 8 per dag.
526 client.println("&units=metric");
527 client.println("Host: api.openweathermap.org");
528 client.println("Connection: close");
529 client.println();
530 } else {
531 Serial.println("Unable to connect");
532 digitalWrite(rodeLedPin, HIGH);
533 delay(5000);
534 digitalWrite(rodeLedPin, LOW);
535 lcd.clear();
536 lcd.setCursor(0, 0);
537 lcd.print("Geen connectie");
538 lcd.setCursor(0, 1);
539 lcd.print("met server");
540 }
541 delay(1000);
542 String line = "";
543 while (client.connected()) {
544 line = client.readStringUntil('\n');
545 StaticJsonDocument<10000> doc;
546 DeserializationError error = deserializeJson(doc, line);
547 if (error) {
548 Serial.print("deserializeJson() failed with code ");
549 Serial.println(error.c_str());
550 digitalWrite(rodeLedPin, HIGH);
551 delay(5000);
552 digitalWrite(rodeLedPin, LOW);
553 return;
554 }
555 //Data van de JSON Tree - HUIDIGE VOCHTIGHEID BUITEN:
556 String humiWifi = doc["list"][0]["main"]["humidity"];
557 //Print op Serial Monitor:
558 Serial.print("Vochtigheid buiten: ");
559 Serial.print(humiWifi);
560 Serial.println(" %");
561 //Print op LCD:
562 lcd.clear();
563 lcd.setCursor(0, 0);
564 lcd.print("Buiten vochtigh:");
565 lcd.setCursor(0, 1);
566 lcd.print(humiWifi);
567 lcd.print(" %");
568 }
569}
570
571void printDate() {
572 Serial.print(rtc.getDay());
573 Serial.print("/");
574 Serial.print(rtc.getMonth());
575 Serial.print("/");
576 Serial.print(2000 + rtc.getYear());
577 Serial.println();
578}
579
580void printTime() {
581 print2digits(rtc.getHours() + GMT);
582 Serial.print(":");
583 print2digits(rtc.getMinutes());
584 Serial.print(":");
585 print2digits(rtc.getSeconds());
586 Serial.println();
587}
588
589void print2digits(int number) {
590 if (number < 10) {
591 Serial.print("0");
592 }
593 Serial.print(number);
594}
595
596void printWiFiStatus() {
597 // print the SSID of the network you're attached to:
598 Serial.print("SSID: ");
599 Serial.println(WiFi.SSID());
600 // print your WiFi shield's IP address:
601 IPAddress ip = WiFi.localIP();
602 Serial.print("IP Address: ");
603 Serial.println(ip);
604 // print the received signal strength:
605 long rssi = WiFi.RSSI();
606 Serial.print("signal strength (RSSI):");
607 Serial.print(rssi);
608 Serial.println(" dBm");
609}