· 6 years ago · Jan 28, 2020, 12:00 PM
1#include <DHT.h>
2#include <DHT_U.h>
3
4#include <LiquidCrystal_I2C.h>
5#include "ThingSpeak.h"
6#include <Ethernet.h>
7#include "secrets.h"
8// Sensor-Settings Lufttemp
9
10#define DHTPIN 2 //Der Sensor wird an PIN 2 angeschlossen
11
12#define DHTTYPE DHT22 // Es handelt sich um den DHT22 Sensor
13
14DHT dht(DHTPIN, DHTTYPE); //Der Sensor wird ab jetzt mit „dth“ angesprochen
15
16
17byte mac[] = SECRET_MAC;
18
19// Set the static IP address to use if the DHCP fails to assign
20
21IPAddress ip(192, 168, 0, 177);
22IPAddress myDns(192, 168, 0, 1);
23EthernetClient client;
24
25//Think-Speak Setup
26
27unsigned long myChannelNumber = SECRET_CH_ID;
28const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
29
30int number = 0;
31
32//Sensor-Settings
33
34int trigger=7;
35int echo=6;
36long dauer=0;
37long entfernung=0;
38
39//Display
40
41LiquidCrystal_I2C lcd(0x27, 16, 2);
42
43const long interval = 60000;
44
45unsigned long previousMillis = 0;
46
47//Taster Dsiplay
48
49int schalter=8;
50int schalterstatus=0;
51
52
53
54
55
56void setup() {
57
58pinMode (schalter,INPUT);
59
60
61//Sensor Setup
62
63Serial.begin (9600);
64dht.begin(); //DHT22 Sensor starte
65pinMode(trigger, OUTPUT);
66pinMode(echo, INPUT);
67
68
69lcd.init(); //Im Setup wird der LCD gestartet
70lcd.backlight();
71 Ethernet.init(10); // Most Arduino Ethernet hardware
72lcd.print("boot.....");
73 // start the Ethernet connection:
74
75 Serial.println("Initialize Ethernet with DHCP:");
76 if (Ethernet.begin(mac) == 0) {
77 Serial.println("Failed to configure Ethernet using DHCP");
78
79 // Check for Ethernet hardware present
80
81 if (Ethernet.hardwareStatus() == EthernetNoHardware) {
82 Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
83 while (true) {
84 delay(1); // do nothing, no point running without Ethernet hardware
85 }
86 }
87 if (Ethernet.linkStatus() == LinkOFF) {
88 Serial.println("Ethernet cable is not connected.");
89 }
90 // try to congifure using IP address instead of DHCP:
91 Ethernet.begin(mac, ip, myDns);
92 } else {
93 Serial.print(" DHCP assigned IP ");
94 lcd.clear();
95 lcd.print("System bereit");
96 Serial.println(Ethernet.localIP());
97 }
98 // give the Ethernet shield a second to initialize:
99 delay(1000);
100
101 ThingSpeak.begin(client); // Initialize ThingSpeak
102
103schalterstatus=digitalRead(schalter);
104 if(schalterstatus==HIGH)
105 {
106 lcd.noBacklight();
107 }
108 else
109 {
110 lcd.backlight();
111 }
112
113}//ENDE SETUP-TEIL
114
115void loop() {
116
117lcd.clear();
118lcd.print("Feuerwehr VG");
119lcd.setCursor(0, 1);
120lcd.print("Woerrstadt");
121delay(5000);
122lcd.clear(); //Display löschen
123
124lcd.print("SW-Version:");
125lcd.setCursor(0, 1);
126lcd.print("Beta 0.8.7"); //Software-Version
127delay(5000);
128 //SensorSchleife
129
130 digitalWrite(trigger, LOW);
131delay(5);
132digitalWrite(trigger, HIGH);
133delay(10);
134digitalWrite(trigger, LOW);
135dauer = pulseIn(echo, HIGH);
136entfernung = (dauer/2) * 0.03432;
137if (entfernung >= 500 || entfernung <= 0)
138{
139Serial.println("Kein Messwert");
140}
141else
142{
143Serial.print(entfernung);
144
145Serial.println(" cm ");
146}
147
148lcd.setCursor(0, 0);
149lcd.print("Wasserstand:");
150lcd.setCursor(0, 1);
151lcd.print(entfernung);
152lcd.println("cm ");
153
154delay(5000);//Fünf Sekunden bis zur Messung warten damit der Sensor etwas //messen kann weil er relativ langsam ist
155
156
157 float Luftfeuchtigkeit = dht.readHumidity(); //die Luftfeuchtigkeit auslesen und unter „Luftfeutchtigkeit“ speichern
158
159 float Temperatur = dht.readTemperature();//die Temperatur auslesen und unter „Temperatur“ speichern
160
161 lcd.clear();
162 Serial.print("Luftfeuchtigkeit: "); //Im seriellen Monitor den Text und
163 Serial.print(Luftfeuchtigkeit); //die Dazugehörigen Werte anzeigen
164 lcd.setCursor(0, 0);
165 lcd.print("Luftfeuchtigkeit: "); //Im seriellen Monitor den Text und
166 lcd.setCursor(0, 1);
167 lcd.print(Luftfeuchtigkeit); //die Dazugehörigen Werte anzeigen
168 lcd.println(" % ");
169 delay(5000);
170
171 lcd.clear();
172
173
174 Serial.print("Temperatur: ");
175 Serial.print(Temperatur);
176 Serial.println(" Grad Celsius");
177
178 lcd.setCursor(0, 0);
179 lcd.print("Temperatur: ");
180 lcd.setCursor(0, 1);
181 lcd.print(Temperatur);
182 lcd.println(" Grad Cel ");
183 delay(5000);
184
185
186unsigned long currentMillis = millis();
187
188
189 // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
190 // pieces of information in a channel. Here, we write to field 1.
191 if (currentMillis - previousMillis >= interval) {
192 previousMillis = currentMillis;
193int x = ThingSpeak.writeField(myChannelNumber, 1, entfernung, myWriteAPIKey);
194lcd.clear();
195lcd.print("Messwert");
196lcd.setCursor(0, 1);
197lcd.print("upload");
198delay(5000);
199
200}
201
202 lcd.clear();
203}
204
205
206 secrets.h
207// Use this file to store all of the private credentials
208// and connection details
209
210// Enter a MAC address for your controller below.
211// Newer Ethernet shields have a MAC address printed on a sticker on the shield
212#define SECRET_MAC {0x90, 0xA2, 0xDA, 0x10, 0x40, 0x4F}
213
214#define SECRET_CH_ID 00000000 // replace 0000000 with your channel number
215#define SECRET_WRITE_APIKEY "000000" // replace XYZ with your channel write API Key