· 6 years ago · Dec 27, 2019, 01:54 PM
1/*
2 * Weerstation Project
3 * Graduaat Internet Of Things
4 * Thomas More Campus De Nayer
5 * 2019 - 2020
6 * Dries Debouver, Jordi De Greef & Stef Van Elst
7 */
8
9/*
10 bool = true of false, ja of nee, 1 of 0
11 byte = 0 tot 255
12 char = -128 tot 127
13 word = 0 tot 65535
14 int = -32768 tot 32767
15 long = -2miljard tot 2 miljard
16 float = heel kleine tot heel grote kommagetallen
17 double = heel kleine tot heel grote kommagetallen
18*/
19
20//Variabelen:
21double tempDht; //huidige temperatuur van de DHT11 Sensor
22double humiDht; //huidige vochtigheid van de DHT11 Sensor
23double tempBmp; //huidige temperatuur van de BMP180 Sensor
24double presBmp; //huidige druk van de BMP180 Sensor
25double presBmpSea; //druk relatief met zee niveau via de BMP180 Sensor
26const int lightLdrPin = A0; //Lichtsterkte (LDR) Sensor Pin
27int lightLdr; //lichtsterkte waarde
28const int waterSenPin = A1; //Water Sensor Signal Pin
29int waterSen; //Water Sensor waarde
30const int joystickVrxPin = A2; //Joystick VrX Pin
31int joystickX; //Joystick X waarde
32const int joystickVryPin = A3; //Joystick VrY Pin
33int joystickY; //Joystick Y waarde
34const int joystickSigPin = 10; //Joystick Signaal Pin
35int joystickS; //Joystick Signaal waarde (ingedrukt of niet)
36int scherm = 0; //Scherm laten veranderen met joystick
37
38//LCD - Digitale pinnen 2 tot 7:
39#include <LiquidCrystal.h>
40LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
41
42//DHT11 Sensor - Digitale pin 8:
43#include "DHT.h"
44#define DHTPIN 8
45#define DHTTYPE DHT11
46DHT dht(DHTPIN, DHTTYPE);
47
48//BMP180 Druk Sensor - SDA pin A4, SCL pin A5:
49#include <SFE_BMP180.h>
50#include <Wire.h>
51SFE_BMP180 bmp;
52char bmpStatus;
53#define ALTITUDE 6.0 //Hoogte van Sint-Katelijne-Waver in meter
54
55//WiFi Real Time Clock (RTC):
56#include <SPI.h>
57#include <WiFiNINA.h>
58//#include <WiFiUdp.h>
59#include <RTCZero.h>
60RTCZero rtc;
61char ssid[] = "WiFi-Debouver-2.4"; //WiFi naam (SSID), Hier uw eigen WiFi naam van thuis ingeven!!!
62char pass[] = "UzpSk6g9D3E3"; //WiFi paswoord, Hier uw eigen WiFi wachtwoord van thuis ingeven!!!
63//int keyIndex = 0; //Netwerk key index number
64int status = WL_IDLE_STATUS; //WiFi ontvanger status
65const int GMT = 1; //Tijdzone, Brussel = +1
66
67//WiFi Buiten Weer - Open Weather Map:
68//#include <WiFi101.h>
69#include <ArduinoJson.h>
70String apiKey = "61cb04e1c56086dd1670b0508cf2beb3"; //API Key OpenWeatherMap.org
71String location = "2786642"; //Sint-Katelijne-Waver, België
72char server[] = "api.openweathermap.org"; //Website of server die gebruikt wordt
73WiFiClient client;
74
75
76
77//De SETUP functie:
78void setup() {
79 Serial.begin(9600);
80 lcd.begin(16, 2);
81 dht.begin();
82 bmp.begin();
83
84 pinMode(joystickSigPin, INPUT);
85 digitalWrite(joystickSigPin, HIGH);
86
87 //WiFi Real Time Clock:
88 // check if the WiFi module works
89 if (WiFi.status() == WL_NO_SHIELD) {
90 Serial.println("WiFi shield not present");
91 // don't continue:
92 while (true);
93 }
94 String fv = WiFi.firmwareVersion();
95 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
96 Serial.println("Please upgrade the firmware");
97 }
98 // attempt to connect to WiFi network:
99 while (status != WL_CONNECTED) {
100 Serial.print("Attempting to connect to network: ");
101 Serial.println(ssid);
102 status = WiFi.begin(ssid, pass);
103 delay(10000); //10 seconden wachten
104 }
105 // you're connected now:
106 printWiFiStatus();
107 rtc.begin();
108 unsigned long epoch;
109 int numberOfTries = 0, maxTries = 6;
110 do {
111 epoch = WiFi.getTime();
112 numberOfTries++;
113 }
114 while ((epoch == 0) && (numberOfTries < maxTries));
115 if (numberOfTries == maxTries) {
116 Serial.print("NTP unreachable!!");
117 while (1);
118 } else {
119 Serial.print("Epoch received: ");
120 Serial.println(epoch);
121 rtc.setEpoch(epoch);
122 Serial.println();
123 }
124}
125
126
127
128//De LOOP functie:
129void loop() {
130 delay(1000); //om de 1 seconde alles doen:
131 Serial.println("\n***** ARDUINO WEERSTATION *****");
132
133 //Joystick - analoge pinnen x=A2 en y=A3, digitale pin 10:
134 joystickX = analogRead(joystickVrxPin);
135 joystickY = analogRead(joystickVryPin);
136 joystickS = digitalRead(joystickSigPin);
137
138 //Joystick coördinaten op Serial Monitor om te testen (kan later weg):
139 Serial.println("\n- Joystick: -");
140 Serial.print("X: ");
141 Serial.println(joystickX);
142 Serial.print("Y: ");
143 Serial.println(joystickY);
144 Serial.print("Signal: ");
145 Serial.print(joystickS);
146 Serial.println(" (1 = Niet ingedrukt, 0 = Ingedrukt)");
147
148 //DHT11 Sensor:
149 tempDht = dht.readTemperature();
150 humiDht = dht.readHumidity();
151
152 //BMP180 Sensor:
153 bmpStatus = bmp.startTemperature();
154 if (bmpStatus != 0) {
155 delay(bmpStatus);
156 bmpStatus = bmp.getTemperature(tempBmp); //Pak de temperatuur en steek het in de variabele "tempBmp"
157 if (bmpStatus != 0) {
158 bmpStatus = bmp.startPressure(3);
159 if (bmpStatus != 0) {
160 delay(bmpStatus);
161 bmpStatus = bmp.getPressure(presBmp, tempBmp); //Pak de druk (adhv "tempBmp") en steek het in de variabele "presBmp"
162 if (bmpStatus != 0) {
163 presBmpSea = bmp.sealevel(presBmp, ALTITUDE); //Pak de relatieve zee-niveau druk en steek het in de variabele "presBmpSea"
164 }
165 }
166 }
167 }
168
169 //Lichtsterkte LDR Sensor - analoge pin A0:
170 lightLdr = analogRead(lightLdrPin);
171
172 //Water Sensor - analoge pin A1:
173 waterSen = analogRead(waterSenPin);
174
175 //Verschillende schermen op LCD:
176 if (joystickX > 1000 || joystickX < 50) { //Als joystick naar rechts of naar links gaat:
177 if (joystickX > 1000) { //Joystick naar rechts:
178 scherm++; //Volgend scherm.
179 }
180 if (joystickX < 50) { //Joystick naar links:
181 scherm--; //Vorig scherm.
182 }
183 if (scherm > 4) { //Als "scherm" aantal groter is dan hoeveel schermen er zijn, zet terug op 0
184 scherm = 0;
185 } else {
186 switch (scherm) {
187 case 0:
188 klok();
189 break;
190 case 1:
191 binnenTemp();
192 break;
193 case 2:
194 binnenHumi();
195 break;
196 case 3:
197 binnenDruk();
198 break;
199 case 4:
200 binnenLicht();
201 break;
202 case 5:
203 water();
204 break;
205 default:
206 lcd.clear();
207 lcd.setCursor(0, 0);
208 lcd.print("- WEERSTATION -");
209 break;
210 }
211 }
212 }
213
214 //Om te tonen op welk scherm we zitten op de LCD: (kan later weg)
215 Serial.print("Scherm: ");
216 Serial.println(scherm);
217}
218
219
220
221//Eigen functies:
222void klok() {
223 //Print op Serial Monitor:
224 Serial.println("\n- WiFi RTC: -");
225 printDate(); //Functie staat onderaan na de loop();
226 printTime(); //Functie staat onderaan na de loop();
227 //Print op LCD:
228 lcd.clear();
229 lcd.setCursor(0, 0);
230 lcd.print(rtc.getDay());
231 lcd.print("/");
232 lcd.print(rtc.getMonth());
233 lcd.print("/");
234 lcd.print(2000 + rtc.getYear());
235 lcd.setCursor(0, 1);
236 lcd.print(rtc.getHours() + GMT);
237 lcd.print(":");
238 lcd.print(rtc.getMinutes());
239 lcd.print(":");
240 lcd.print(rtc.getSeconds());
241}
242
243void binnenTemp() {
244 //Print op Serial Monitor:
245 Serial.println("\n- BMP180 Sensor: -");
246 Serial.print("Binnen Temperatuur: ");
247 Serial.print(tempBmp);
248 Serial.println(" °C");
249 //Print op LCD:
250 lcd.clear();
251 lcd.setCursor(0, 0);
252 lcd.print("Temperatuur:");
253 lcd.setCursor(0, 1);
254 lcd.print(tempBmp);
255 lcd.print(" "); //spatie
256 lcd.print((char)223); //"°" tekentje
257 lcd.print("C");
258}
259
260void binnenHumi() {
261 //Print op Serial monitor:
262 Serial.println("\n- DHT11 Sensor: -");
263 Serial.print("Binnen Vochtigheid: ");
264 Serial.print(humiDht);
265 Serial.println(" %");
266 //Print op LCD:
267 lcd.clear();
268 lcd.setCursor(0, 0);
269 lcd.print("Vochtigheid:");
270 lcd.setCursor(0, 1);
271 lcd.print(humiDht);
272 lcd.print(" %");
273}
274
275void binnenDruk() {
276 //Print op Serial Monitor:
277 Serial.print("Absolute druk: ");
278 Serial.print(presBmp);
279 Serial.println(" mb / hPa");
280 Serial.print("Hoogte: ");
281 Serial.print(ALTITUDE, 0);
282 Serial.println(" meter");
283 Serial.print("Relatieve (zee-niveau) druk: ");
284 Serial.print(presBmpSea, 2);
285 Serial.print(" mb, ");
286 Serial.print(presBmpSea * 0.0295333727, 2);
287 Serial.println(" inHg");
288 //Print op LCD:
289 lcd.clear();
290 lcd.setCursor(0, 0);
291 lcd.print("Absolute druk:");
292 lcd.setCursor(0, 1);
293 lcd.print(presBmp);
294 lcd.print(" mb / hPa");
295}
296
297void binnenLicht() {
298 //Print op Serial Monitor:
299 Serial.println("\n- LDR Sensor: -");
300 Serial.print(lightLdr);
301 if (lightLdr < 800) {
302 Serial.println(" = Donker");
303 } else {
304 Serial.println(" = Licht");
305 }
306 //Print op LCD:
307 lcd.clear();
308 lcd.setCursor(0, 0);
309 lcd.print("Licht:");
310 lcd.setCursor(0, 1);
311 lcd.print(lightLdr);
312 if (lightLdr < 800) {
313 lcd.print(" = Donker");
314 } else {
315 lcd.print(" = Licht");
316 }
317}
318
319void water() {
320 //Print op Serial Monitor:
321 Serial.println("\n- Water Sensor: -");
322 Serial.print("Signal: ");
323 Serial.print(waterSen);
324 if (waterSen < 100) {
325 Serial.println(" = Droog");
326 } else if (waterSen > 100 && waterSen < 300) {
327 Serial.println(" = Beetje nat");
328 } else {
329 Serial.println(" = Nat");
330 }
331 //Print op LCD:
332 lcd.clear();
333 lcd.setCursor(0, 0);
334 lcd.print("Water sensor:");
335 lcd.setCursor(0, 1);
336 lcd.print(waterSen);
337 if (waterSen < 100) {
338 lcd.print(" = Droog");
339 } else if (waterSen > 100 && waterSen < 300) {
340 lcd.print(" = Beetje nat");
341 } else {
342 lcd.print(" = Nat");
343 }
344}
345
346//Bestaande RTC Functies:
347void printDate() {
348 Serial.print(rtc.getDay());
349 Serial.print("/");
350 Serial.print(rtc.getMonth());
351 Serial.print("/");
352 Serial.print(2000 + rtc.getYear());
353 Serial.println(); //Enter
354}
355
356void printTime() {
357 print2digits(rtc.getHours() + GMT);
358 Serial.print(":");
359 print2digits(rtc.getMinutes());
360 Serial.print(":");
361 print2digits(rtc.getSeconds());
362 Serial.println(); //Enter
363}
364
365void print2digits(int number) {
366 if (number < 10) {
367 Serial.print("0");
368 }
369 Serial.print(number);
370}
371
372//Bestaande WiFi Functie:
373void printWiFiStatus() {
374 // print the SSID of the network you're attached to:
375 Serial.print("SSID: ");
376 Serial.println(WiFi.SSID());
377 // print your WiFi shield's IP address:
378 IPAddress ip = WiFi.localIP();
379 Serial.print("IP Address: ");
380 Serial.println(ip);
381 // print the received signal strength:
382 long rssi = WiFi.RSSI();
383 Serial.print("signal strength (RSSI):");
384 Serial.print(rssi);
385 Serial.println(" dBm");
386}