Gartenhäuschen mit Micro Controller und Sensoren überwachen - Teil 1

In our garden cottage we store gasoline mowers and cans, the gas grill and some cans with colours and nitro thinners. When flammable gases or liquids run out or even a fire breaks out, I want to be alerted. Since the shed is still in the area of our WLAN, I have therefore chosen a D1 Mini (ESP8266) with DHT22 temperature sensor and MQ-2 gas sensor. The data is transmitted using the ESP as web server

Hardware needed

Number Component
1 D1 Mini NodeMcu with ESP8266-12F WLAN module
optional Battery Shield for lithium batteries for D1 Mini
optional Lithium Battery 3,7V
1 DHT22 AM2302 Temperature and humidity sensor
1 MQ-2 Gas Sensor Smoke Sensor Air Quality Module
Mini Breadboard, Jumper cable

For the sketch, I was able to use pieces from other programs. The simplest is to ask for the gas sensor MQ-2, which is supplied with a voltage of 5V for internal heating. For the  data , you have the choice between a digital and an analogue output. Both options require a voltage divider (e.g. 1 kOhm and 2,2 kOhm), since the ESP8266 inputs only support 3.3V. Although the small microcontroller has only one analog input, I use this one. Because here you can set the limit value with a certain value in the sketch. Alternatively, you can set the digital output limit with a built-in potentiometer. Although the condition HIGH is also shown on a green LED, the calibration is difficult due to lack of comparisons. To query the analog pin we only need:

  sensorValue = analog(0);

The DHT22 (AM2302) is a combined temperature and humidity meter from the one-wire sensor family (1-wire). The voltage supply is flexible between 3 and 6 V, the data is sent to a digital pin. A program library is conveniently used to query the measurement values, as so often developed by the Adafruit team around Limor Fried. So we only need to instantiate the object dht:

  #include "DHT.h"
 # define DHTPIN 12              //D6-GPIO12
 # define DHTTYPE DHT22    // DHT22(AM2302)

Since we use the measured values in different functions, we define them as global variables before void setup() :

 float t = 0.0;
 float h = 0.0;
 int sensorValue = 0;

Programming the web server on the ESP8266 is not a secret science. There are examples in the Arduino IDE when installing the following program libraries.

 #include <ESP8266WiFi.h>
 #include <ESP8266WebServer.h>
 #include <WifiUDP.h>
However, the sketch can become very extensive if the output in the browser of the PC or smartphone is also to look good and be updated automatically. A colleague for the new eBook Smart Home Starter Set
developed a code block that I had little to adapt. The HTML code can be found in the complete sketch: (Download)

     This sketch reads sensor data and provides these
     as HTML-Text on a TCP server in your local network.
     By Mihajlo Manojlovic and Bernd Albrecht for AZ-Delivery*/
 #include <ESP8266WiFi.h>
 #include <ESP8266WebServer.h>
 #include <WifiUDP.h>
 #include "DHT.h"
 #define DHTPIN 12 //D6-GPIO12
 #define DHTTYPE DHT22   // DHT22(AM2302)
 const char* ssid = "---your SSID---";
 const char* password = "---your-PASSWORD---";
 float t = 0.0;
 float h = 0.0;
 int sensorValue = 0;
 ESP8266WebServer server(80);
 void setup () {
   // Connect to wifi
   Serial.print("Connecting ...");
   while (WiFi.status() != WL_CONNECTED)
   Serial.print("Connecting to WiFi... ");
   //start server
   Serial.println("HTTP server started");
 void readData() {
   h = dht.readHumidity();
   t = dht.readTemperature();
   Serial.print(F("Temperature: "));
   Serial.print(F(" °C Humidity: "));
   Serial.println(F(" %"));
   sensorValue = analogRead(0);
   Serial.print("Sensor Value: ");
   if(sensorValue > 100) {
     Serial.println("Gas detected!");   }
 void loop() {
   if (WiFi.status() == WL_CONNECTED//Check WiFi connection status
   else // attempt to connect to wifi again if disconnected
 void handle_OnConnect() {
 void handle_NotFound() {
   server.send(404"text/plain""Not found");
 String SendHTML() {
   String ptr = "<!DOCTYPE html> <html>\n";
   ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
   ptr += "<title>ESP8266 Web Server</title>\n";
   ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
   ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
   ptr += "p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
   ptr += "</style>\n";
   //ajax refresh start
   ptr += "<script>\n";
   ptr += "setInterval(loadDoc,200);\n";
   ptr += "function loadDoc() {\n";
   ptr += "var xhttp = new XMLHttpRequest();\n";
   ptr += "xhttp.onreadystatechange = function() {\n";
   ptr += "if (this.readyState == 4 && this.status == 200) {\n";
   ptr += "document.getElementById(\"webpage\").innerHTML =this.responseText}\n";
   ptr += "};\n";
   ptr += "\"GET\", \"/\", true);\n";
   ptr += "xhttp.send();\n";
   ptr += "}\n";
   ptr += "</script>\n";
   //ajax refresh end
   ptr += "</head>\n";
   ptr += "<body>\n";
   ptr += "<div id=\"webpage\">\n";
   ptr += "<h1>AZ-Delivery</h1>\n";
   ptr += "<h2>Environmental Control</h2>\n";
   ptr += "<p>Temperature: ";
   ptr += (t);
   ptr += " *C ";
   ptr += "</p>";
   ptr += "<p>Humidity: ";
   ptr += (h);
   ptr += " %";
   ptr += "</p>";
   ptr += "<p>Gas concentration: ";
   ptr += (sensorValue);
   if(sensorValue > 100) {
     ptr += "<p>gas detected!";   }
   ptr += "</div>\n";
   ptr += "</body>\n";
   ptr += "</html>\n";
   return ptr;

Our garden shed is built quite robustly and the door is secured with a safety lock. That's why I've given up motion detectors or other sensors to prevent theft. It would be easy to adjust the code for that.

In the second part I would like to show how I can display the transmitted data not only in the browser on the PC or smartphone, but also with the help of a Raspberry Pi or a Micro Controller. This has the advantage that you can even connect a buzzer and/or LED to trigger an alarm.

Esp-8266Projects for beginnersSmart home


Andreas Wolter

Andreas Wolter

Norbert Dunker: das zweite Bild ist ein Fritzing Schaltplan und sollte theoretisch direkt nachgebaut werden können.

Norbert Dunker

Norbert Dunker

Mit den Fotos komme ich nicht ganz zurecht gibt es auch eine Schaltung

Juergen Eggers

Juergen Eggers

Herr Kühnel hat im Gegensatz zu AZ-Delivery und dem Entwickler des Board´s ganz nah aus meiner Heimat das Problem mit der Temperaturkompensation thematisiert und auch Lösungsansätze gezeigt. Warum haben die Schweizer im Gegensatz zu den Wolfsburgern bessere Lösungsansätze. Ich empfehle die Kühnel-sche Lösund auf Seiten AZ zu verfolgen, mit der bisherigen Beispiel Datei ist das AZ-Envy kein Freude-spender, übrigens auch nicht die Tasmota Variante, die zwar MQTT beherrscht aber bei der Temperatur eindeutig “schummelt” Vielen Dank Herr Kühnel, auch für die Ausarbeitung bei den Gassensoren.

Claus  Kühnel

Claus Kühnel

Ich habe etwas sehr ähnliches mit einem AZ-Envy aufgebaut. Das kompakte Board ist für diesen Zweck sehr gut geeignet. Detail unter

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN