Seite 6 von 11
Re: Ultraschallsensor in OpenHAB intigrieren
Verfasst: 11. Jul 2020 01:45
von Pepe1907
udo1toni hat geschrieben: ↑11. Jul 2020 01:02
Aus irgendeinem Grund scheint derBefehl gar nicht erst ausgeführt zu werden. Ich würde da jetzt erst mal auf die whitelist tippen, da hat es schön mehrfach Probleme gegeben.
Hast Du mal die whitelist einfach neu abgespeichert? Am einfachsten geht das von der Konsole aus per
Gesendet von iPad mit Tapatalk
Einfach nur den Befehl per Konsole aufrufen oder vorher löschen , dann mittels Touch erstellen und per nano ergänzen?
Re: Ultraschallsensor in OpenHAB intigrieren
Verfasst: 11. Jul 2020 14:28
von udo1toni
Einfach nur den Befehl ausführen. Das sollte dazu führen, dass das laufende openHAB die Datei neu einliest, da sich der Zeitstempel der Datei geändert hat (da gibt es einen Dienst in karaf, der die Verzeichnisse daraufhin überwacht...)
Gesendet von iPad mit Tapatalk
Re: Ultraschallsensor in OpenHAB intigrieren
Verfasst: 11. Jul 2020 14:55
von Pepe1907
udo1toni hat geschrieben: ↑11. Jul 2020 14:28
Einfach nur den Befehl ausführen. Das sollte dazu führen, dass das laufende openHAB die Datei neu einliest, da sich der Zeitstempel der Datei geändert hat (da gibt es einen Dienst in karaf, der die Verzeichnisse daraufhin überwacht...)
Gesendet von iPad mit Tapatalk
Leider auch nicht der gewünschte Erfolg.
Ich als Laie denke aber dass er das Skript ausführt, da man eine Meldung bekommt, wenn man es händisch startet,
dass das Skript schon läuft.
Code: Alles auswählen
[14:55:06] openhabian@openhab:~$ sudo python /etc/openhab2/scripts/hc.py
[sudo] Passwort für openhabian:
/etc/openhab2/scripts/hc.py:9: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
110.9 cm
Wenn ich aber den Switch, also {channel="exec:command:teichstand:run"}, auf 0 schalte und dann sieht die Konsole so aus
Code: Alles auswählen
[14:55:44] openhabian@openhab:~$ sudo python /etc/openhab2/scripts/hc.py
111.4 cm
Re: Ultraschallsensor in OpenHAB intigrieren
Verfasst: 11. Jul 2020 14:58
von udo1toni
Nein.
Die Meldung besagt, dass die Port schon benutzt werden.
Diese Meldung kann aber auch kommen, wenn schlicht Rechte fehlen.
Ob das Script ausgeführt wird, kannst Du nur feststellen, indem Du im Script Logging einrichtest und dieses Log anschließend prüfst.
Ich schau nachher mal, ob ich Dir Beispielcode schicken kann (geht vom Laptop besser als vom iPad...)
Gesendet von iPad mit Tapatalk
Re: Ultraschallsensor in OpenHAB intigrieren
Verfasst: 11. Jul 2020 15:22
von Pepe1907
udo1toni hat geschrieben: ↑11. Jul 2020 14:58
Nein.
Die Meldung besagt, dass die Port schon benutzt werden.
Diese Meldung kann aber auch kommen, wenn schlicht Rechte fehlen.
Ob das Script ausgeführt wird, kannst Du nur feststellen, indem Du im Script Logging einrichtest und dieses Log anschließend prüfst.
Ich schau nachher mal, ob ich Dir Beispielcode schicken kann (geht vom Laptop besser als vom iPad...)
Gesendet von iPad mit Tapatalk
Vielen lieben Dank.
Dann werde ich mal gespannt warten.
Habe selbst mal recherchiert.
hc.py
Code: Alles auswählen
#Bibliotheken
import RPi.GPIO as GPIO
import time
import logging
logging.basicConfig(filename='example.log', filemode='w', level=logging.INFO)
#GPIO definieren (Modus, Pins, Output)
GPIO.setmode(GPIO.BCM)
GPIO_TRIGGER = 18
GPIO_ECHO = 24
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def do_something():
logging.info('Skript gestartet')
def entfernung():
# Trig High setzen
GPIO.output(GPIO_TRIGGER, True)
# Trig Low setzen (nach 0.01ms)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
Startzeit = time.time()
Endzeit = time.time()
# Start/Stop Zeit ermitteln
while GPIO.input(GPIO_ECHO) == 0:
Startzeit = time.time()
while GPIO.input(GPIO_ECHO) == 1:
Endzeit = time.time()
# Vergangene Zeit
Zeitdifferenz = Endzeit - Startzeit
# Schallgeschwindigkeit (34300 cm/s) einbeziehen
entfernung = 133 - (Zeitdifferenz * 34300) / 2
return entfernung
if __name__ == '__main__':
try:
while True:
distanz = entfernung()
print (" %.1f cm" % distanz)
time.sleep(20)
# Programm beenden
except KeyboardInterrupt:
print("Programm abgebrochen")
GPIO.cleanup()
example.log wurde erstellt bleibt aber leer egal ob ich händisch oder per exec binding laufen lasse.
Re: Ultraschallsensor in OpenHAB intigrieren
Verfasst: 11. Jul 2020 15:33
von udo1toni
Ja, das passt schon fast.
Du hast aber das Problem, dass Du zwar eine Routine anlegst, in der Du eine Logzeile ausgibst, dieese Routine wird aber niemals ausgeführt (do_something)
Stattdessen musst Du natürlich den Befehl logging.info('irgendeine Message') z.B. in die Routine entfernung() einbauen, z.B. unmittelbar vor der Zeile
Eine andere Sache, die mir gerade auffällt: Das Programm wartet auf eine Tastatureingabe, um beendet zu werden. Das muss natürlich weg! So, wie das Programm momentan geschrieben ist, wird es gestartet und läuft ewig. also statt
Code: Alles auswählen
if __name__ == '__main__':
try:
while True:
distanz = entfernung()
print (" %.1f cm" % distanz)
time.sleep(20)
# Programm beenden
except KeyboardInterrupt:
print("Programm abgebrochen")
GPIO.cleanup()
einfach
Code: Alles auswählen
if __name__ == '__main__':
try:
distanz = entfernung()
print (" %.1f cm" % distanz)
GPIO.cleanup()
Und schau mal mittels
ob da vielleicht eine (oder gar mehrere) Instanz(en) des Programms läuft/laufen.
Um ohne Neustart alle loszuwerden, kannst Du mit
die einzelnen Instanzen los werden, also z.B.
für das Programm mit der PID 10456.
Re: Ultraschallsensor in OpenHAB intigrieren
Verfasst: 11. Jul 2020 15:50
von Pepe1907
Vielen Dank
Skirpt sieht jetzt wie folgt aus.
Code: Alles auswählen
#Bibliotheken
import RPi.GPIO as GPIO
import time
import logging
logging.basicConfig(filename='teich.log', filemode='w', level=logging.INFO)
#GPIO definieren (Modus, Pins, Output)
GPIO.setmode(GPIO.BCM)
GPIO_TRIGGER = 18
GPIO_ECHO = 24
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def entfernung():
logging.info ('Skript gestartet')
# Trig High setzen
GPIO.output(GPIO_TRIGGER, True)
# Trig Low setzen (nach 0.01ms)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
Startzeit = time.time()
Endzeit = time.time()
# Start/Stop Zeit ermitteln
while GPIO.input(GPIO_ECHO) == 0:
Startzeit = time.time()
while GPIO.input(GPIO_ECHO) == 1:
Endzeit = time.time()
# Vergangene Zeit
Zeitdifferenz = Endzeit - Startzeit
# Schallgeschwindigkeit (34300 cm/s) einbeziehen
entfernung = 133 - (Zeitdifferenz * 34300) / 2
return entfernung
if __name__ == '__main__':
try:
while True:
distanz = entfernung()
print (" %.1f cm" % distanz)
time.sleep(20)
GPIO.cleanup()
Re: Ultraschallsensor in OpenHAB intigrieren
Verfasst: 11. Jul 2020 15:51
von Pepe1907
Code: Alles auswählen
[15:49:22] openhabian@openhab:~$ ps aux | grep -i python
root 562 0.0 1.5 38708 15740 ? Ssl Jul10 0:00 /usr/bin/python 3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
openhab 978 0.0 0.0 1940 372 ? S Jul10 0:00 sh -c sudo /usr /bin/python /etc/openhab2/scripts/hc.py
root 979 0.0 0.3 10028 3300 ? S Jul10 0:00 sudo /usr/bin/p ython /etc/openhab2/scripts/hc.py
openhab 980 0.0 0.0 1940 356 ? S Jul10 0:00 sh -c sudo /usr /bin/python /etc/openhab2/scripts/hc.py
root 982 0.0 0.3 10028 3240 ? S Jul10 0:00 sudo /usr/bin/p ython /etc/openhab2/scripts/hc.py
root 992 2.7 0.5 12040 5048 ? R Jul10 29:50 /usr/bin/python /etc/openhab2/scripts/hc.py
root 993 2.6 0.4 12040 4940 ? R Jul10 28:40 /usr/bin/python /etc/openhab2/scripts/hc.py
openhab+ 1927 0.0 0.0 7360 528 pts/0 S+ 15:50 0:00 grep --color=au to -i python
openhab 4652 0.0 0.0 1940 368 ? S Jul10 0:00 sh -c sudo /usr /bin/python /etc/openhab2/scripts/hc.py
root 4654 0.0 0.3 10028 3224 ? S Jul10 0:00 sudo /usr/bin/p ython /etc/openhab2/scripts/hc.py
root 4660 2.8 0.5 12040 5052 ? R Jul10 28:57 /usr/bin/python /etc/openhab2/scripts/hc.py
openhab 31727 0.0 0.0 1940 404 ? S 14:56 0:00 sh -c sudo /usr /bin/python /etc/openhab2/scripts/hc.py
root 31729 0.0 0.3 10028 3336 ? S 14:56 0:00 sudo /usr/bin/p ython /etc/openhab2/scripts/hc.py
root 31736 52.4 0.5 12040 5048 ? R 14:56 28:02 /usr/bin/python /etc/openhab2/scripts/hc.py
openhab 32741 0.0 0.0 1940 408 ? S 15:16 0:00 sh -c sudo /usr /bin/python /etc/openhab2/scripts/hc.py
root 32742 0.0 0.3 10028 3456 ? S 15:16 0:00 sudo /usr/bin/p ython /etc/openhab2/scripts/hc.py
root 32750 49.2 0.5 12448 5476 ? R 15:16 16:33 /usr/bin/python /etc/openhab2/scripts/hc.py
[15:50:17] openhabian@openhab:~$ sudo kill <PID>
-bash: Syntaxfehler beim unerwarteten Wort `newline'
kann sie leider nicht schließen
Re: Ultraschallsensor in OpenHAB intigrieren
Verfasst: 11. Jul 2020 15:57
von Pepe1907
Pepe1907 hat geschrieben: ↑11. Jul 2020 15:51
Code: Alles auswählen
[15:49:22] openhabian@openhab:~$ ps aux | grep -i python
root 562 0.0 1.5 38708 15740 ? Ssl Jul10 0:00 /usr/bin/python 3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
openhab 978 0.0 0.0 1940 372 ? S Jul10 0:00 sh -c sudo /usr /bin/python /etc/openhab2/scripts/hc.py
root 979 0.0 0.3 10028 3300 ? S Jul10 0:00 sudo /usr/bin/p ython /etc/openhab2/scripts/hc.py
openhab 980 0.0 0.0 1940 356 ? S Jul10 0:00 sh -c sudo /usr /bin/python /etc/openhab2/scripts/hc.py
root 982 0.0 0.3 10028 3240 ? S Jul10 0:00 sudo /usr/bin/p ython /etc/openhab2/scripts/hc.py
root 992 2.7 0.5 12040 5048 ? R Jul10 29:50 /usr/bin/python /etc/openhab2/scripts/hc.py
root 993 2.6 0.4 12040 4940 ? R Jul10 28:40 /usr/bin/python /etc/openhab2/scripts/hc.py
openhab+ 1927 0.0 0.0 7360 528 pts/0 S+ 15:50 0:00 grep --color=au to -i python
openhab 4652 0.0 0.0 1940 368 ? S Jul10 0:00 sh -c sudo /usr /bin/python /etc/openhab2/scripts/hc.py
root 4654 0.0 0.3 10028 3224 ? S Jul10 0:00 sudo /usr/bin/p ython /etc/openhab2/scripts/hc.py
root 4660 2.8 0.5 12040 5052 ? R Jul10 28:57 /usr/bin/python /etc/openhab2/scripts/hc.py
openhab 31727 0.0 0.0 1940 404 ? S 14:56 0:00 sh -c sudo /usr /bin/python /etc/openhab2/scripts/hc.py
root 31729 0.0 0.3 10028 3336 ? S 14:56 0:00 sudo /usr/bin/p ython /etc/openhab2/scripts/hc.py
root 31736 52.4 0.5 12040 5048 ? R 14:56 28:02 /usr/bin/python /etc/openhab2/scripts/hc.py
openhab 32741 0.0 0.0 1940 408 ? S 15:16 0:00 sh -c sudo /usr /bin/python /etc/openhab2/scripts/hc.py
root 32742 0.0 0.3 10028 3456 ? S 15:16 0:00 sudo /usr/bin/p ython /etc/openhab2/scripts/hc.py
root 32750 49.2 0.5 12448 5476 ? R 15:16 16:33 /usr/bin/python /etc/openhab2/scripts/hc.py
[15:50:17] openhabian@openhab:~$ sudo kill <PID>
-bash: Syntaxfehler beim unerwarteten Wort `newline'
kann sie leider nicht schließen
Habe jetzt alle hänidsch geschlossen.
Code: Alles auswählen
[16:07:21] openhabian@openhab:~$ sudo python /etc/openhab2/scripts/hc.py
File "/etc/openhab2/scripts/hc.py", line 49
^
IndentationError: unexpected unindent
Nur leider kann ich das Skript nicht mehr starten
und das passiert im openhab.log
2020-07-11 16:20:25.576 [vent.ItemStateChangedEvent] - teichmessung changed from OFF to ON
2020-07-11 16:20:25.749 [vent.ItemStateChangedEvent] - teichmessung changed from ON to OFF
==> /var/log/openhab2/openhab.log <==
2020-07-11 16:20:25.756 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Teichstand': For input string: "File "/etc/openhab2/scripts/hc.py", line 49
^
IndentationError: unexpected unindent
File "/etc/openhab2/scripts/hc.py", line 49
^
Re: Ultraschallsensor in OpenHAB intigrieren
Verfasst: 11. Jul 2020 16:20
von udo1toni
Schau Dir bitte mal genau an, was ich gepostet habe.
Eventuell muss sogar noch das try: weg.
Dann sähe es so aus:
Code: Alles auswählen
if __name__ == '__main__':
distanz = entfernung()
print (" %.1f cm" % distanz)
GPIO.cleanup()