-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwemos-dht22.ino
120 lines (94 loc) · 2.9 KB
/
wemos-dht22.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
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN 4
DHT dht(DHTPIN, DHTTYPE);
const int UPDATE_INTERVAL_MINUTES = 60;
const int WIFI_TIMEOUT = 60;
const char* host = "api.thingspeak.com";
const char* THINGSPEAK_API_KEY = "";
unsigned int raw = 0;
float volt = 0.0;
void setup() {
Serial.begin(115200);
delay(10);
WiFiManager wifiManager;
//reset settings - for testing
//wifiManager.resetSettings();
//sets timeout until configuration portal gets turned off
//useful to make it all retry or go to sleep
//in seconds
wifiManager.setTimeout(WIFI_TIMEOUT);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//and goes into a blocking loop awaiting configuration
if (!wifiManager.autoConnect("Wemos-DHT22")) {
Serial.println("failed to connect and hit timeout");
setDeepSleep();
}
//if you get here you have connected to the WiFi
Serial.println("connected to access point");
pinMode(A0, INPUT);
raw = analogRead(A0);
volt = raw / 1023.0;
volt = volt * 4.2;
dht.begin();
}
void setDeepSleep() {
Serial.println("putting ESP in deep sleep...");
ESP.deepSleep(UPDATE_INTERVAL_MINUTES * 60 * 1000 * 1000 - millis(), WAKE_RF_DEFAULT);
delay(3000);
}
void loop() {
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Prüfen ob eine gültige Zahl zurückgegeben wird. Wenn NaN (not a number) zurückgegeben wird, dann Fehler ausgeben.
if (isnan(temperature) || isnan(humidity))
{
Serial.println("DHT22 konnte nicht ausgelesen werden");
}
else
{
Serial.print("Luftfeuchte: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperatur: ");
Serial.print(temperature);
Serial.print(" C");
Serial.print(" ");
Serial.print("Spannung: ");
Serial.print(volt);
Serial.println(" Volt");
// We now create a URI for the request
String url = "/update?api_key=";
url += THINGSPEAK_API_KEY;
url += "&field1=";
url += String(temperature);
url += "&field2=";
url += String(humidity);
url += "&field3=";
url += String(volt);
Serial.println("Sending values to thingspeak...");
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
while (!client.available()) {
delay(100);
}
}
setDeepSleep();
}