Raspberry Pi als Füllstandsmesser: Unterschied zwischen den Versionen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
| Zeile 1: | Zeile 1: | ||
{{WorkInProgress}} | {{WorkInProgress}} | ||
== Vorbereitung == | |||
Wir benötigen als erstes einen installierten Raspberry Pi mit Raspberry Pi OS. dies könnt ihr am besten nach dieser Anleitung machen: | |||
* [[Raspberry Pi OS Installation]] | |||
Ich habe zum konfigurieren das Script benutzt und alles auf Standard gelassen nur das Passwort habe ich festgelegt. Es wird kein Interface benötigt, bei den Tuningmaßnahmen könnt ihr selbst entscheiden, was ihr konfigurieren möchtet. | |||
Ich nutze hier einen Raspberry Pi zero w. Sollte aber auch für alle anderen Raspberry Pis umsetzbar sein. Wenn ihr einen gebooteten Raspberry Pi habt, loggt euch via SSH und dem root user an. | |||
Den HC-SR04 Ultraschall Sensor bitte nach dieser Anleitung anschließen (Ich habe die Version ohne Widerstände im Einsatz): | |||
* [[Raspberry Pi HC-SR04 Ultraschallsensor]] | |||
== distance.py Script == | == distance.py Script == | ||
Um das Script lauffähig zu bekommen müssen als erstes die Abhängigkeiten installiert werden:<syntaxhighlight lang="bash"> | Um das Script lauffähig zu bekommen müssen als erstes die Abhängigkeiten installiert werden:<syntaxhighlight lang="bash"> | ||
apt install | apt install python3-pip libgpiod2 python3-systemd python3-paho-mqtt -y | ||
pip3 install adafruit-circuitpython-hcsr04 | |||
pip3 install --upgrade setuptools | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang="bash"> | |||
chmod +x /usr/local/sbin/distance.py | |||
</syntaxhighlight> | |||
== Systemd Service == | == Systemd Service == | ||
Nun installieren wir das ganze noch mit einem systemd Daemon, damit das Script im Hintergrund läuft und es beim Reboot auch wieder startet. Dafür legen wir zuerst das Unit Service File an. | |||
<code>/etc/systemd/system/distance.service</code><syntaxhighlight lang="bash"> | |||
# systemd unit file for the Python Service | |||
[Unit] | |||
# Human readable name of the unit | |||
Description=Python Distance Sensor Service | |||
# Starting after System is online and docker is running | |||
# Only needed if MQTT is used | |||
Wants=network-online.target | |||
After=network-online.target | |||
[Service] | |||
# Command to execute when the service is started | |||
ExecStart=/usr/bin/python3 /usr/local/sbin/distance.py | |||
# Disable Python's buffering of STDOUT and STDERR, so that output from the | |||
# service shows up immediately in systemd's logs | |||
Environment=PYTHONUNBUFFERED=1 | |||
# Automatically restart the service if it crashes | |||
Restart=on-failure | |||
# Our service will notify systemd once it is up and running | |||
Type=notify | |||
# Use a dedicated user to run our service | |||
User=pi | |||
# Send CTRL+C tot python Script to terminate it clean | |||
KillSignal=SIGINT | |||
[Install] | |||
# Tell systemd to automatically start this service when the system boots | |||
# (assuming the service is enabled) | |||
WantedBy=default.target | |||
</syntaxhighlight> | |||
Nun lesen wir das Service File ein und starten es. Zum Abschluss aktivieren wir den Service noch, damit auch bei einem Reboot des Pi, dieser aktiv wird.<syntaxhighlight lang="bash"> | |||
systemctl daemon-reload | |||
systemctl start distance.service | |||
systemctl enable distance.service | |||
</syntaxhighlight> | |||
== Tuning == | |||
* Read Only Mode | |||
* Stromsparen | |||
* Logging deaktivieren | |||
{{#related:Raspberry Pi HC-SR04 Ultraschallsensor}} | {{#related:Raspberry Pi HC-SR04 Ultraschallsensor}} | ||
Version vom 3. November 2021, 12:33 Uhr
Work in Progress
Diese Seite wird erst noch aufgebaut. Bitte noch keine Änderungen vornehmen, ohne mit dem ursprünglichen Author zu sprechen!
Vielen Dank
Vorbereitung
Wir benötigen als erstes einen installierten Raspberry Pi mit Raspberry Pi OS. dies könnt ihr am besten nach dieser Anleitung machen:
Ich habe zum konfigurieren das Script benutzt und alles auf Standard gelassen nur das Passwort habe ich festgelegt. Es wird kein Interface benötigt, bei den Tuningmaßnahmen könnt ihr selbst entscheiden, was ihr konfigurieren möchtet.
Ich nutze hier einen Raspberry Pi zero w. Sollte aber auch für alle anderen Raspberry Pis umsetzbar sein. Wenn ihr einen gebooteten Raspberry Pi habt, loggt euch via SSH und dem root user an.
Den HC-SR04 Ultraschall Sensor bitte nach dieser Anleitung anschließen (Ich habe die Version ohne Widerstände im Einsatz):
distance.py Script
Um das Script lauffähig zu bekommen müssen als erstes die Abhängigkeiten installiert werden:
apt install python3-pip libgpiod2 python3-systemd python3-paho-mqtt -y
pip3 install adafruit-circuitpython-hcsr04
pip3 install --upgrade setuptools
chmod +x /usr/local/sbin/distance.py
Systemd Service
Nun installieren wir das ganze noch mit einem systemd Daemon, damit das Script im Hintergrund läuft und es beim Reboot auch wieder startet. Dafür legen wir zuerst das Unit Service File an.
/etc/systemd/system/distance.service
# systemd unit file for the Python Service
[Unit]
# Human readable name of the unit
Description=Python Distance Sensor Service
# Starting after System is online and docker is running
# Only needed if MQTT is used
Wants=network-online.target
After=network-online.target
[Service]
# Command to execute when the service is started
ExecStart=/usr/bin/python3 /usr/local/sbin/distance.py
# Disable Python's buffering of STDOUT and STDERR, so that output from the
# service shows up immediately in systemd's logs
Environment=PYTHONUNBUFFERED=1
# Automatically restart the service if it crashes
Restart=on-failure
# Our service will notify systemd once it is up and running
Type=notify
# Use a dedicated user to run our service
User=pi
# Send CTRL+C tot python Script to terminate it clean
KillSignal=SIGINT
[Install]
# Tell systemd to automatically start this service when the system boots
# (assuming the service is enabled)
WantedBy=default.target
Nun lesen wir das Service File ein und starten es. Zum Abschluss aktivieren wir den Service noch, damit auch bei einem Reboot des Pi, dieser aktiv wird.
systemctl daemon-reload
systemctl start distance.service
systemctl enable distance.service
Tuning
- Read Only Mode
- Stromsparen
- Logging deaktivieren