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