From 9802d0853eae0f1395d92cc3c95761cf51501091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20K=C3=B6pferl?= Date: Fri, 19 Jun 2020 17:39:41 +0200 Subject: [PATCH] Unit-Files erstellt und Skript soweit --- install/install.sh | 32 ++++++++++-- src/systemd/temp+feuchte-sammler.service | 9 ++++ src/systemd/weight-sammler.service | 9 ++++ src/temp+feucht-DHT22.py | 65 +++++++++++++----------- 4 files changed, 80 insertions(+), 35 deletions(-) create mode 100644 src/systemd/temp+feuchte-sammler.service create mode 100644 src/systemd/weight-sammler.service diff --git a/install/install.sh b/install/install.sh index 3e806f9..dfddf67 100644 --- a/install/install.sh +++ b/install/install.sh @@ -11,16 +11,19 @@ if [ -z "$GRAFANADB" ] then exit 1 fi - +if [ "$EUID" -ne 0 ] + then echo "Bitte mit sudo laufen lassen!" + exit +fi # generell update -sudo apt-get update -sudo apt-get upgrade +apt-get update +apt-get upgrade # wir brauchen python3 -sudo apt install python3 +apt install python3 # wir brauchen pip3 -sudo apt-get install python3-pip +apt-get install python3-pip #Nö: sudo pip3 install --upgrade setuptools # Run the following command to install the Raspberry PI GPIO library: @@ -28,3 +31,22 @@ sudo apt-get install python3-pip #pip3 install adafruit-blinka sudo pip3 install Adafruit_DHT + + +# Skripte nach usr/bin kopieren +cp src/temp+feucht-DHT22.py /usr/bin/temp+feucht-DHT22.py +cp src/weight-datageneration.py /usr/bin/weight-datageneration.py + +# systemd unit files kopieren und chmod +cp src/systemd/temp+feuchte-sammler.service /etc/systemd/system/temp+feuchte-sammler.service +chmod 644 /etc/systemd/system/temp+feuchte-sammler.service +cp src/systemd/weight-sammler.service /etc/systemd/system/weight-sammler.service +chmod 644 /etc/systemd/system/weight-sammler.service + +# services enablen - starten so automatisch +systemctl enable temp+feuchte-sammler +systemctl enable weight-sammler + +# und starten +systemctl start temp+feuchte-sammler +systemctl start weight-sammler diff --git a/src/systemd/temp+feuchte-sammler.service b/src/systemd/temp+feuchte-sammler.service new file mode 100644 index 0000000..158dab6 --- /dev/null +++ b/src/systemd/temp+feuchte-sammler.service @@ -0,0 +1,9 @@ +[Unit] +Description=Temperatur und Luftfeuchte-Sammler-service. + +[Service] +Type=simple +ExecStart=/usr/bin/python3 /usr/bin/temp+feucht-DHT22.py + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/src/systemd/weight-sammler.service b/src/systemd/weight-sammler.service new file mode 100644 index 0000000..7c2216e --- /dev/null +++ b/src/systemd/weight-sammler.service @@ -0,0 +1,9 @@ +[Unit] +Description=Gewicht-Sammler-service. + +[Service] +Type=simple +ExecStart=/usr/bin/python3 /usr/bin/weight-datageneration.py + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/src/temp+feucht-DHT22.py b/src/temp+feucht-DHT22.py index 8162846..be4e8c7 100644 --- a/src/temp+feucht-DHT22.py +++ b/src/temp+feucht-DHT22.py @@ -1,43 +1,48 @@ #!/usr/bin/python # Author: Robert Köpferl # Skript, um Sensoren des Typs AD2302 auszulesen und die umgerechneten -# Temperatur und Feuchte-Werte zu pushen +# Temperatur und Luftfeuchte-Werte zu pushen import sys - -import Adafruit_DHT +import datetime +import time +#import Adafruit_DHT # Einfach alle Sensoren der Reihe nach auslesen - -sensor = [ -{'Name': 'Bienen0_o', 'Pin': 24, 'Ziel': 'B0-o'}, -{'Name': 'Bienen0_u', 'Pin': 26, 'Ziel': 'B0-u'}, -{'Name': 'Bienen1_o', 'Pin': 32, 'Ziel': 'B1-o'}, -{'Name': 'Bienen1_u', 'Pin': 36, 'Ziel': 'B1-u'}, -{'Name': 'Bienen2_o', 'Pin': 38, 'Ziel': 'B2-o'}, -{'Name': 'Bienen2_u', 'Pin': 40, 'Ziel': 'B2-u'}, +sensoren = [ +{'Name': 'Bienen0_o', 'GPIO': 8, 'Pin': 24, 'Ziel': 'B0-o'}, +{'Name': 'Bienen0_u', 'GPIO': 7, 'Pin': 26, 'Ziel': 'B0-u'}, +{'Name': 'Bienen1_o', 'GPIO': 12, 'Pin': 32, 'Ziel': 'B1-o'}, +{'Name': 'Bienen1_u', 'GPIO': 16, 'Pin': 36, 'Ziel': 'B1-u'}, +{'Name': 'Bienen2_o', 'GPIO': 20, 'Pin': 38, 'Ziel': 'B2-o'}, +{'Name': 'Bienen2_u', 'GPIO': 21, 'Pin': 40, 'Ziel': 'B2-u'}, ] +intervall = 10 +jez = datetime.datetime.now().isoformat() -* GPIO8 (24) (Bienen0) - Sensor A -* GPIO7 (26) (Bienen0) - Sensor B -* GPIO12 (32) (Bienen1) - Sensor A -* GPIO16 (36) (Bienen1) - Sensor B -* GPIO20 (38) (Bienen2) - Sensor A -* GPIO21 (40) (Bienen2) - Sensor B +print(f"{jez}: Starten von AD2302-Auslese-Skript. Läuft dauerhaft, periodisches Abfragen im Intervall: {intervall}s. Folgende Sensoren konfiguriert:") -# Try to grab a sensor reading. Use the read_retry method which will retry up -# to 15 times to get a sensor reading (waiting 2 seconds between each retry). -humidity, temperature = Adafruit_DHT.read(sensor, pin) +for sensor in sensoren: + print( sensor['Name'], "@ GPIO:", sensor['GPIO'], "/ Pin: ", sensor['Pin']) +while True: + for sensor in sensoren: + gpio = sensor['GPIO'] + # Try to grab a sensor reading. Use the read_retry method which will retry up + # to 15 times to get a sensor reading (waiting 2 seconds between each retry). + # using read fails immediately + #humidity, temperature = Adafruit_DHT.read('22', gpio) + humidity, temperature = None,None - -# Note that sometimes you won't get a reading and -# the results will be null (because Linux can't -# guarantee the timing of calls to read the sensor). -# If this happens try again! -if humidity is not None and temperature is not None: - print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity)) -else: - print('Failed to get reading. Try again!') - sys.exit(1) + # Note that sometimes you won't get a reading and + # the results will be null (because Linux can't + # guarantee the timing of calls to read the sensor). + # If this happens try again! + if humidity is not None and temperature is not None: + print('Temp={0:0.1f}°C Humidity={1:0.1f}%'.format(temperature, humidity)) + else: + jez = datetime.datetime.now().isoformat() + print(f"{jez} Fehler bei Sensor {sensor['Name']} Pin:{sensor['Pin']} - keine Werte erhalten.") + # eine Runde pennen + time.sleep(intervall)