Unit-Files erstellt und Skript soweit

This commit is contained in:
Robert Köpferl 2020-06-19 17:39:41 +02:00
parent 625f74026c
commit 9802d0853e
4 changed files with 80 additions and 35 deletions

View File

@ -11,16 +11,19 @@ if [ -z "$GRAFANADB" ] then
exit 1 exit 1
fi fi
if [ "$EUID" -ne 0 ]
then echo "Bitte mit sudo laufen lassen!"
exit
fi
# generell update # generell update
sudo apt-get update apt-get update
sudo apt-get upgrade apt-get upgrade
# wir brauchen python3 # wir brauchen python3
sudo apt install python3 apt install python3
# wir brauchen pip3 # wir brauchen pip3
sudo apt-get install python3-pip apt-get install python3-pip
#Nö: sudo pip3 install --upgrade setuptools #Nö: sudo pip3 install --upgrade setuptools
# Run the following command to install the Raspberry PI GPIO library: # 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 #pip3 install adafruit-blinka
sudo pip3 install Adafruit_DHT 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

View File

@ -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

View File

@ -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

View File

@ -1,43 +1,48 @@
#!/usr/bin/python #!/usr/bin/python
# Author: Robert Köpferl # Author: Robert Köpferl
# Skript, um Sensoren des Typs AD2302 auszulesen und die umgerechneten # 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 sys
import datetime
import Adafruit_DHT import time
#import Adafruit_DHT
# Einfach alle Sensoren der Reihe nach auslesen # Einfach alle Sensoren der Reihe nach auslesen
sensoren = [
sensor = [ {'Name': 'Bienen0_o', 'GPIO': 8, 'Pin': 24, 'Ziel': 'B0-o'},
{'Name': 'Bienen0_o', 'Pin': 24, 'Ziel': 'B0-o'}, {'Name': 'Bienen0_u', 'GPIO': 7, 'Pin': 26, 'Ziel': 'B0-u'},
{'Name': 'Bienen0_u', 'Pin': 26, 'Ziel': 'B0-u'}, {'Name': 'Bienen1_o', 'GPIO': 12, 'Pin': 32, 'Ziel': 'B1-o'},
{'Name': 'Bienen1_o', 'Pin': 32, 'Ziel': 'B1-o'}, {'Name': 'Bienen1_u', 'GPIO': 16, 'Pin': 36, 'Ziel': 'B1-u'},
{'Name': 'Bienen1_u', 'Pin': 36, 'Ziel': 'B1-u'}, {'Name': 'Bienen2_o', 'GPIO': 20, 'Pin': 38, 'Ziel': 'B2-o'},
{'Name': 'Bienen2_o', 'Pin': 38, 'Ziel': 'B2-o'}, {'Name': 'Bienen2_u', 'GPIO': 21, 'Pin': 40, 'Ziel': 'B2-u'},
{'Name': 'Bienen2_u', 'Pin': 40, 'Ziel': 'B2-u'},
] ]
intervall = 10
jez = datetime.datetime.now().isoformat()
* GPIO8 (24) (Bienen0) - Sensor A print(f"{jez}: Starten von AD2302-Auslese-Skript. Läuft dauerhaft, periodisches Abfragen im Intervall: {intervall}s. Folgende Sensoren konfiguriert:")
* 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
# Try to grab a sensor reading. Use the read_retry method which will retry up for sensor in sensoren:
# to 15 times to get a sensor reading (waiting 2 seconds between each retry). print( sensor['Name'], "@ GPIO:", sensor['GPIO'], "/ Pin: ", sensor['Pin'])
humidity, temperature = Adafruit_DHT.read(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
# Note that sometimes you won't get a reading and # the results will be null (because Linux can't
# the results will be null (because Linux can't # guarantee the timing of calls to read the sensor).
# guarantee the timing of calls to read the sensor). # If this happens try again!
# If this happens try again! if humidity is not None and temperature is not None:
if humidity is not None and temperature is not None: print('Temp={0:0.1f}°C Humidity={1:0.1f}%'.format(temperature, humidity))
print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity)) else:
else: jez = datetime.datetime.now().isoformat()
print('Failed to get reading. Try again!') print(f"{jez} Fehler bei Sensor {sensor['Name']} Pin:{sensor['Pin']} - keine Werte erhalten.")
sys.exit(1) # eine Runde pennen
time.sleep(intervall)