Gartenhäuschen mit Micro Controller und Sensoren überwachen - Teil 2
in the first part I described how the D1 Mini (ESP8266) with temperature sensor DHT22 and Gassensor MQ-2 as Web Server sends measured values ​​from our garden cottage into domestic Wi-Fi.

These values ​​are displayed in the browser on the PC or the smartphone (see picture) when you enter the IP address of the Web Server (here: http://192.168.178.100) into the address line.

The web server is programmed to output the word "alarm" when a temperature of 50 ° C or a gas concentration of 100 is exceeded. But you have to look active on the corresponding page to get everything displayed. I did not care, I also wanted to be notified with an acoustic and optical signal. So I had to evaluate the website with a Micro Controller or Micro Computer, filter out the measured values ​​and, if necessary, trigger the alarm with a buzzer and LED.

Required hardware for the first part

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 sensor and humidity sensor

1

MQ-2 gas sensor smoke sensor air quality module

MINI BREADBOARD, JUMPER CABLE


Required hardware for the second part

1

Raspberry Pi 3b, 4b or Zero W (WLAN-capable)

1

1.3 inch OLED I2C 128 x 64 pixel display

MINI BREADBOARD, JUMPER CABLE

Active buzzer

Red LED with series resistor 200 to 330 ohms


Excessive is the use of a micro controller, as it works after switching on "on fingering". But I did not want to feel right away to use my D1 board for the query. Only after the help of a blogger colleague did I make it. Therefore, I first introduce the (for me) faster solution with the Raspberry Pi and move the sketch for the D1 Board as Web Client on the third part of the blog series.

I have already made the reading out of a text-based website in connection with the download of drivers and program libraries more frequently. With CURL ( „ClientforURLs" is aCommand lineTool for downloading or uploading files via an Internet address) I can download the complete HTML text from the ESP Web Server with the IP address 192.168.178.100 in the terminal. (The corresponding line in the program with the IP address you must of course customize for your Web server).

After many lines with formatting, we will find the searched measured values ​​for temperature, rel. Humidity (English Humidity) from the DHT22 and for the gas concentration from the MQ-2 at the end.

With the redirection> Text.txt I save the entire text for further editing in a file.

 curl 192.168.178.100> text.txt

For the filtering out of the measured values, checking the exceeding of limit values ​​and, if necessary, the tripping of the alarm I use a self-developed Python program. For the execution of the above-mentioned terminal command, the program module OS is required, ie

 import OS

and then

 OS.system('Curl 192.168.178.100> text.txt')

To assign the stored text to the website of a variable, the following commands are needed:

 f=open("text.txt","r")
 data = f.reading()
 f.close

With the string methods Find (), slicing [from: to], split () and strip () finally the measured values ​​are filtered out.

Here is the Python program with Web Client and Alarm (Download):

 import OS
 import time
 import RPI.Gpio as Gpio
 
 buzzer=21
 Gpio.setwarings(False)
 Gpio.setmode(Gpio.Bcm)
 Gpio.set up(buzzer,Gpio.OUT)
 
 def alarm():
     print("Alarm")
     for I in ratchet(2):
         Gpio.output(buzzer,True)
         time.sleep(0.2)
         Gpio.output(buzzer,False)
         time.sleep(0.1)
         Gpio.output(buzzer,True)
         time.sleep(0.2)
         Gpio.output(buzzer,False)
         time.sleep(1)
         
 While True:
     try:
         OS.system('Curl 192.168.178.100> text.txt')
         f=open("text.txt","r")
         data = f.reading()
         f.close
         #Print (data)
         print()
         index=data.find("Temperature")
         #Print (index)
         Data1=data[index:-23]
         print(Data1)
         values=Data1.split(":")
         print(values)
         t=float(values[1][0:6].strip())
         print("T =",t,"° C")
         H=int(values[2][1:3])
         print("H =",H," %")
         G=int(values[3][1:4])
         print("G =",G,"PPM")
         IF (t>25.0 OR G>100):
             alarm()        
         time.sleep(1)
 
     except:
         time.sleep(10)

The output of the data takes place in the Python shell, when the limit values ​​are exceeded, the BUZZER and LED are activated to GPIO21.

To operate the Raspi in the "Headless Mode", ie without keyboard and monitor, makes sense to integrate a small OLED display for data output into the arrangement. That 1.3 inch OLED I2C 128 x 64 pixel display Isolated with four connections for 3.3V (pin 1) and GND (pin 9) and SDA (pin 3) and SCL (pin 5). For this I downloaded the program library luma.core from RM Hull von Github.
If your Raspberry Pi OS is not up to date, the following commands may be necessary:

  $ Sudo usermod -a -G I2C, SPI, GPIO PI
  $ Sudo APT Install Python3-Dev Python3-Pip Libreietype6-Dev Libjpeg-Dev Build-Essential
  $ Sudo APT Install Libsdl-Dev LibportMidi-Dev LibsDL-TTF2.0-Dev LibsDL-Mixer1.2-DEV LibsDL-Image1.2-Dev

The cloning of the repository takes place with:

   $ git Clone https://github.com/rm-hull/luma.examples.git
   $ CD luma.examples
   $ Sudo -H pip install -ce .

In the directory OLED with his other subdirectories, we have no write rights first. Due to the internal dependencies on the program module demo_opts.py and the subdirectory fonts, however, makes sense to write our program in the subdirectory examples. We pick us up the write rights:

 sudo chmod 777 -c -R / Home / Pi / OLED
Please do not forget to activate both SSH and I2C in the Raspberry PI configuration on the Interfaces tab.

Here is the circuit diagram:

and the full Python program that uses parts of the luma.core Example Terminal.py: (Download)

 #! / usr / bin / env python
 # - * - Coding: UTF-8 - * -
 # Web Client to Read Environmental Data
 # By Bernd Albrecht for AZ-Delivery
 #
 # luma.core: Copyright (C) 2014-2020 Richard Hull and Contributors
 # See License.RST for details.
 
 import OS
 import time
 from pathlib import Path
 from demo_opts import get_device
 from luma.core.virtual import terminal
 from Pile import Imaginary
 import RPI.Gpio as Gpio
 
 buzzer=21
 Gpio.setwarings(False)
 Gpio.setmode(Gpio.Bcm)
 Gpio.set up(buzzer,Gpio.OUT)
 
 def make_font(Surname, size):
     font_path = string(Path(__File__).resolve().parent.Joinpath('Fonts', Surname))
     return Imaginary.truetype(font_path, size)
 
 def alarm():
     print("Alarm")
     for I in ratchet(2):
         Gpio.output(buzzer,True)
         time.sleep(0.2)
         Gpio.output(buzzer,False)
         time.sleep(0.1)
         Gpio.output(buzzer,True)
         time.sleep(0.2)
         Gpio.output(buzzer,False)
         time.sleep(1)
 
 def sensor():
     global t,H,G,Time
     try:
         OS.system('Curl 192.168.178.100> text.txt')
         f=open("text.txt","r")
         data = f.reading()
         f.close
         
     except:
         t=H=G=0
         time.sleep(10)
 
     #Print (data)
     print()
     index=data.find("Temperature")
     #Print (index)
     Data1=data[index:-23]
     print(Data1)
     values=Data1.split(":")
     print(values)
     t=float(values[1][0:6].strip())
     print("t = ",t," °C")
     h=int(werte[2][1:3])
     print("h = ",h," %")
     g=int(werte[3][1:4])
     print("g = ",g)
     if (t>25.0 or g>100):
         alarm()        
     time.sleep(1)
 
 def main():
     #       for fontname, size in [(None, None), ("ProggyTiny.ttf", 16), ('ChiKareGo.ttf', 16)]:
     for fontname, size in [("ProggyTiny.ttf", 16)]:
         print()
         print(fontname, size)
         font = make_font(fontname, size) if fontname else None
         term = terminal(device, font)
             
     while True:
             sensor()
 
             term.clear()
             term.animate = False
             term.clear()
             term.puts("Temperature:")
             term.puts(str(t))
             term.puts("°C")
             term.println()
             term.println()
             term.puts("Humidity: ")
             term.puts(str(h))
             term.puts(" %")
             term.println()
             term.println()
             term.puts("Gas: ")
             term.puts(str(g))
             term.println()          
             term.flush()
             time.sleep(2)
 
 if __name__ == "__main__":
     try:
         device = get_device()
         main()
     except KeyboardInterrupt:
         pass

Für den Programmstart haben Sie grundsätzlich drei Möglichkeiten:

  1. Normaler Start mit Tastatur und Monitor, anschließend “abnabeln“.
  2. Start über SSH (secure shell) vom PC z.B. mit dem Programm Putty
  3. Einrichten von Autostart (beschrieben im Blog Robot Car mit Raspberry Pi)

Es reicht jedoch nicht, das Programm zu laden und zu starten, weil die Voreinstellungen für ein anderes Display mit dem Treiber SSD1306 festgelegt sind. Deshalb muss für unser 1,3 Zoll OLED I2C 128 x 64 Pixel Display beim Programmaufruf der Parameter --display sh1106 übergeben werden.

Die weiteren Voreinstellungen für Interface: I2C und Dimensions: 128 x 64 sind richtig. Wenn Sie ein anderes Display verwenden, lohnt der Blick in die Datei README.rst im Verzeichnis luma.examples.

Im dritten Teil werde ich die Auswertung der Messwerte mit einem D1 Board (oder einem beliebigen anderen ESP8266 Micro Controller) vorstellen.


Download als pdf

DisplaysEsp-8266Projects for beginnersRaspberry piSmart home

3 comments

Bernd Albrecht

Bernd Albrecht

@ Sylvio Beck:
In seinem Blog vom 12. August 2020 zeigt Markus Pohle die Verwendung von IFTTT (if this than that).
https://www.az-delivery.de/blogs/azdelivery-blog-fur-arduino-und-raspberry-pi/enc28j60-ethernet-modul-fur-arduino-esp-und-raspi
Nachteil beider Lösungen: Man benötigt einen Internet-Dienstleister für das Versenden einer Nachricht. Das wollte ich vermeiden.

Wolfgang Herz

Wolfgang Herz

Sehr schönes Projekt. Sicherlich lässt sich da etwas optimieren. _Vielleicht ist der Vorschlag von Hr. Beck auch als Beispiel abbildbar. Trotzdem finde ich die direkte Anzeige im Display gut. Es macht Spaß derart tolle Projekt im AZ-Delivery zu haben! Ich habe bisher viel dazugelernt.

Danke

Sylvio Beck

Sylvio Beck

Schönes Projekt
Aber viel zu kompliziert viel einfacher wäre es wenn das Gerät im Garten Anomalien selbst erkennt und mit Hilfe eines Web Dienstes wie notifymydevice.com eine push Meldung auf das Handy sendet.

Die Daten die der Server sammelt können auch direkt per ftp upload ins Web oder eigene nas hochgeladen werden.

Bei Interesse könnt ich den entsprechenden Code zur Verfügung stellen.

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