/* * DHT-Sensor: */ #include "DHT.h" #define DHTPIN 2 // Digital pin connected to the DHT sensor // Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 -- // Pin 15 can work but DHT must be disconnected during program upload. // Uncomment whatever type you're using! //#define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 #define DHTTYPE DHT21 // DHT 21 (AM2301) // Connect pin 1 (on the left) of the sensor to +5V // NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 // to 3.3V instead of 5V! // Connect pin 2 of the sensor to whatever your DHTPIN is // Connect pin 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins) // Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins) // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor // Initialize DHT sensor. // Note that older versions of this library took an optional third parameter to // tweak the timings for faster processors. This parameter is no longer needed // as the current DHT reading algorithm adjusts itself to work on faster procs. DHT dht(DHTPIN, DHTTYPE); /* * TimeServer */ #include char tmp[50]; char time_value[20]; time_t old_time; int fail_counter = 0; int total_counter = 0; /* * WiFi */ #include #include #include #define WIFI_SSID "Network" #define WIFI_PASSWORD "Password" // MQTT Broker Server Settings: #define MQTT_HOST IPAddress(192, 168, 1, 95) #define MQTT_PORT 1888 #define MQTT_PUB_Humidity "ESP8266/Humidity" #define MQTT_PUB_Temp "ESP8266/Temperature" #define MQTT_PUB_LastUpdate "ESP8266/LastUpdate" #define MQTT_PUB_Error "ESP8266/Errors" #define MQTT_PUB_Messages "ESP8266/Messages" AsyncMqttClient mqttClient; Ticker mqttReconnectTimer; WiFiEventHandler wifiConnectHandler; WiFiEventHandler wifiDisconnectHandler; Ticker wifiReconnectTimer; void connectToWifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); int initcounter = 0; while (WiFi.status() != WL_CONNECTED) { initcounter = initcounter + 1; delay(200); Serial.print("."); if (initcounter == 100) { Serial.print("Verbindungsabbruch"); delay(200); } } } void onWifiConnect(const WiFiEventStationModeGotIP& event) { Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); connectToMqtt(); } void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { Serial.println("Disconnected from Wi-Fi."); mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi wifiReconnectTimer.once(2, connectToWifi); } void connectToMqtt() { Serial.println("Connecting to MQTT..."); mqttClient.connect(); } void onMqttConnect(bool sessionPresent) { Serial.println("Connected to MQTT."); Serial.print("Session present: "); Serial.println(sessionPresent); } void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { Serial.println("Disconnected from MQTT."); if (WiFi.isConnected()) { mqttReconnectTimer.once(2, connectToMqtt); } } void onMqttPublish(uint16_t packetId) { Serial.print("Publish acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); } void onMqttSubscribe(uint16_t packetId, uint8_t qos) { Serial.println("Subscribe acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); Serial.print(" qos: "); Serial.println(qos); } void onMqttUnsubscribe(uint16_t packetId) { Serial.println("Unsubscribe acknowledged."); Serial.print(" packetId: "); Serial.println(packetId); } void Wifi_MQTT_Init(){ wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect); wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect); mqttClient.onConnect(onMqttConnect); mqttClient.onDisconnect(onMqttDisconnect); mqttClient.onPublish(onMqttPublish); mqttClient.setServer(MQTT_HOST, MQTT_PORT); mqttClient.onSubscribe(onMqttSubscribe); mqttClient.onUnsubscribe(onMqttUnsubscribe); mqttClient.onMessage(onMqttMessage); // If your broker requires authentication (username and password), set them below //mqttClient.setCredentials("REPlACE_WITH_YOUR_USER", "REPLACE_WITH_YOUR_PASSWORD"); connectToWifi(); } void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { Serial.println("Publish received."); Serial.print(" topic: "); Serial.println(topic); Serial.print(" qos: "); Serial.println(properties.qos); Serial.print(" dup: "); Serial.println(properties.dup); Serial.print(" retain: "); Serial.println(properties.retain); Serial.print(" len: "); Serial.println(len); Serial.print(" index: "); Serial.println(index); Serial.print(" total: "); Serial.println(total); String messageTemp; for (int i = 0; i < total; i++) { Serial.print((char)payload[i]); messageTemp += (char)payload[i]; } Serial.println(); long Position = messageTemp.toInt(); Serial.print("Nachricht: "); Serial.println(Position); } void setup() { Serial.begin(9600); Serial.println(""); Serial.print("Modul wird hochgefahren."); Wifi_MQTT_Init(); delay(100); dht.begin(); delay(600); Serial.println(""); time_t now = time(nullptr); now = now + 3600 + 3600; String time = String(ctime(&now)); time.trim(); time.substring(0,19).toCharArray(time_value, 20); Serial.print("Uhrzeit: "); Serial.println(time); old_time = now; Serial.println(""); } void DHT_Run() { // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); time_t now = time(nullptr); now = now + 3600 + 3600; String time = String(ctime(&now)); time.trim(); time.substring(0,19).toCharArray(time_value, 20); Serial.print("Uhrzeit: "); Serial.println(time); // Check if 1 hour is passed. if ((old_time + 3600) < now) { Serial.println("Time Reset."); old_time = now; fail_counter = 0; total_counter = 0; } // Check if any reads failed and exit early (to try again). total_counter = total_counter + 1; if (isnan(h) || isnan(t)) { fail_counter = fail_counter + 1; Serial.print(F("Failed to read from DHT sensor! (")); Serial.print(String(total_counter)); Serial.print("/"); Serial.print(String(fail_counter)); Serial.println(")"); // Publish an MQTT error message on topic MQTT_PUB_Error uint16_t packetIdPub4 = mqttClient.publish(MQTT_PUB_Error, 1, true, String((fail_counter)).c_str()); delay(100); uint16_t packetIdPub5 = mqttClient.publish(MQTT_PUB_Messages, 1, true, String((total_counter)).c_str()); delay(100); return; } Serial.print(F("Humidity: ")); Serial.print(h); Serial.print(F("% Temperature: ")); Serial.print(t); Serial.println(F("°C ")); // Publish an MQTT message on topic MQTT_PUB_Humidity uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_Humidity, 1, true, String(h).c_str()); delay(100); // Publish an MQTT message on topic MQTT_PUB_Temp uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_Temp, 1, true, String(t).c_str()); delay(100); // Publish an MQTT message on topic LastUpdate uint16_t packetIdPub3 = mqttClient.publish(MQTT_PUB_LastUpdate, 1, true, time.c_str()); delay(100); // Publish an MQTT error message on topic MQTT_PUB_Error //uint16_t packetIdPub4 = mqttClient.publish(MQTT_PUB_Error, 1, true, String((fail_counter)).c_str()); delay(100); uint16_t packetIdPub5 = mqttClient.publish(MQTT_PUB_Messages, 1, true, String((total_counter)).c_str()); delay(100); } void loop() { delay(10000); DHT_Run(); }