Python scripts stops working after some time

Hey guys,

since I’ve learned a lot in this forum, I’m facing a problem which i can’t solve by searching the forum.

I’m using 2 different python scripts in my OpenHab installation. The first one is for a DHT22 sensor, which i’m using for a longer time with no problems. The second one is for 6x HC-SR04 sensors which control my garage door.
Unfortunately the scripts stop working after ~2 hours. The scripts are not called anymore by OpenHab. When I call them via shell, they start to work for some time (the items are updated again) and stop again.

The log files don’t put up any errors regarding my scripts.

Since I call the second script for each of the 6 HC-SR04 every second, i think this might cause some problems. Is there a limit for the calls of python scripts?

I hope you can guide me to the mistake in my project :thinking:

Best Regards

OpenHab Version: 2.5.0~S1486-1

  1. DHT22 Sensor
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
#import Adafruit_BMP.BMP085 as BMP085
import Adafruit_DHT
import sys, time
 
# constants
DHT_PIN = 4                    # GPIO nr
DHT_SENSOR = Adafruit_DHT.DHT22     
 
if __name__ == '__main__':
    if len(sys.argv) > 1:
        call = sys.argv[1].lower()
 
        if call == 'temperature':
            temperature = None
            while temperature == None:
               _, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
               if temperature == None:
                   time.sleep(1.5)
            print(temperature)
 
        elif call == 'humidity':
            humidity = None
            while humidity == None:
               humidity, _ = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
               if humidity == None:
                   time.sleep(1.5)
            print(humidity)
            
        elif call == 'pressure':
            #sensor = BMP085.BMP085()
            print(100.0)
  1. HC-SR04 Sensors (6x), which use the same python script (just the GPIO are changed)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import RPi.GPIO as GPIO
import time

if __name__ == '__main__':
    #GPIO Modus (BOARD / BCM)
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)

    #GPIO Pins zuweisen
    GPIO_TRIGGER = 25
    GPIO_ECHO = 8

    #Richtung der GPIO-Pins festlegen (IN / OUT) l
    GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
    GPIO.setup(GPIO_ECHO, GPIO.IN)

    # setze Trigger auf HIGH
    GPIO.output(GPIO_TRIGGER, True)

    # setze Trigger nach 0.01ms aus LOW
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)

    StartZeit = time.time()
    StopZeit = time.time()

    # speichere Startzeit
    while GPIO.input(GPIO_ECHO) == 0:
        StartZeit = time.time()

    # speichere Ankunftszeit
    while GPIO.input(GPIO_ECHO) == 1:
        StopZeit = time.time()

    # Zeit Differenz zwischen Start und Ankunft
    TimeElapsed = StopZeit - StartZeit
    # mit der Schallgeschwindigkeit (34300 cm/s) multiplizieren
    # und durch 2 teilen, da hin und zurueck
    distanz = (TimeElapsed * 34300) / 2
    # GPIO.cleanup()
    print (distanz)

Things:

Thing exec:command:weatherstation_temperature "Temperatur"    [command="/etc/openhab2/scripts/weatherstation.py temperature", transform="REGEX((.*?))", interval=60, timeout=10, autorun=true]
Thing exec:command:weatherstation_humidity "Luftfeuchtigkeit" [command="/etc/openhab2/scripts/weatherstation.py humidity",    transform="REGEX((.*?))", interval=60, timeout=10, autorun=true]

Thing exec:command:uss_sensor1 "Polo 1"    [command="/etc/openhab2/scripts/uss_sensor4.py", transform="REGEX((.*?))", interval=1, timeout=10, autorun=true]
Thing exec:command:uss_sensor2 "Polo 2"    [command="/etc/openhab2/scripts/uss_sensor5.py", interval=1, timeout=10, autorun=true]
Thing exec:command:uss_sensor3 "Polo 3"    [command="/etc/openhab2/scripts/uss_sensor6.py", interval=1, timeout=10, autorun=true]
Thing exec:command:uss_sensor4 "Audi 1"    [command="/etc/openhab2/scripts/uss_sensor1.py", interval=1, timeout=10, autorun=true]
Thing exec:command:uss_sensor5 "Audi 2"    [command="/etc/openhab2/scripts/uss_sensor2.py", interval=1, timeout=10, autorun=true]
Thing exec:command:uss_sensor6 "Audi 3"    [command="/etc/openhab2/scripts/uss_sensor3.py", interval=1, timeout=10, autorun=true]

items:

//for the first script
Number Weatherstation_Temperature_unfiltered "ungefilterte Temperatur [%.1f °C]" <temperature> 
Number Weatherstation_Humidity_unfiltered    "ungefilterte Luftfeuchtigkeit [%.1f %%]" <humidity>

String temperature_out { channel="exec:command:weatherstation_temperature:output" }
String humidity_out    { channel="exec:command:weatherstation_humidity:output" }

//for the second script

String Polo_uss_1 "Polo uss 1 [%d]" { channel="exec:command:uss_sensor1:output" } 
String Polo_uss_2 "Polo uss 2 [%d]" { channel="exec:command:uss_sensor3:output" }
String Polo_uss_3 "Polo uss 3 [%d]" { channel="exec:command:uss_sensor2:output" }
String Audi_uss_1 "Audi uss 1 [%d]" { channel="exec:command:uss_sensor4:output" }
String Audi_uss_2 "Audi uss 2 [%d]" { channel="exec:command:uss_sensor5:output" }
String Audi_uss_3 "Audi uss 3 [%d]" { channel="exec:command:uss_sensor6:output" }

Probably. That’s a lot of calls.
Why do you need to do it every second?

I would have approached the problem the other way around
I would get the scripts to send the data to openHAB instead of openHAB calling for it.
I would use MQTT or the REST API in the scripts to send the data to OH

From reading the script, you are checking something like a distance with an ultrasound sensor or something like that. Again, why every second?

I need to check the distance every second to analyze the position of my garage doors. As they can be triggered automatically and manually, I have to poll the sensors.
I control the doors with the sensor (i.g. ventilation in my garage when the humidity is too high), so i need to be quick to stop the door at the right position.

I can give the MQTT solution a try, maybe this will solve the problem.

So you should be doing this inside the script.
OpenHAB is not made for real time use like this.

Make a script using mqtt.
It can control the doors
OpenHAB sends commands to it, like open door 1,close door 2…
The real time control of the door position is confined in the script.

I’ve switched my script to mqtt. The function is ok. I will leave the logic (rule) in OpenHab since i need some data from OpenHab.

I have still one problem: i can call just 5 of 6 sensors with my script. I think there might be the cause for my problems.
The 6. sensor will not be called. It doesn’t matter which sensor is the last one, the echo pin will no receive data :weary: It’s allways receiving a timeout.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import RPi.GPIO as GPIO
import time
import paho.mqtt.client as mqtt


if __name__ == '__main__':
    def on_connect(client, userdata, flags, rc):
        print("Connected with result code " + str(rc))

    client = mqtt.Client()
    client.on_connect = on_connect

    client.connect("localhost", 1883, 60)

    client.loop_start()

    while True:
        time.sleep(2)
        #GPIO Modus (BOARD / BCM)
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)

        for i in range(6):
            if i==0:
                GPIO_TRIGGER = 13
                GPIO_ECHO = 6
                KANAL = "Sensoren/Sensor1"
            elif i == 1:
                GPIO_TRIGGER =17
                GPIO_ECHO = 5
                KANAL = "Sensoren/Sensor2"
            elif i == 2:
                GPIO_TRIGGER =22
                GPIO_ECHO = 27
                KANAL = "Sensoren/Sensor3"
            elif i == 3:
                GPIO_TRIGGER = 7
                GPIO_ECHO = 12
                KANAL = "Sensoren/Sensor4"
            elif i == 4:
                GPIO_TRIGGER = 25
                GPIO_ECHO = 8
                KANAL = "Sensoren/Sensor5"
            elif i == 5:
                GPIO_TRIGGER = 9
                GPIO_ECHO = 10
                KANAL = "Sensoren/Sensor6"

            time.sleep(0.1)
            #Richtung der GPIO-Pins festlegen (IN / OUT) l
            GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
            GPIO.setup(GPIO_ECHO, GPIO.IN)

            # setze Trigger auf HIGH
            GPIO.output(GPIO_TRIGGER, True)

            # setze Trigger nach 0.01ms aus LOW
            time.sleep(0.00001)
            GPIO.output(GPIO_TRIGGER, False)

            StartZeit = time.time()
            StopZeit = time.time()

            # speichere Startzeit
            timeout = time.time() + 2   # 2 Sekunden Timeout
            while GPIO.input(GPIO_ECHO) == 0 and time.time() < timeout:
                StartZeit = time.time()

            # speichere Ankunftszeit
            timeout = time.time() + 2   # 2 Sekunden Timeout
            while GPIO.input(GPIO_ECHO) == 1 and time.time() < timeout:
                StopZeit = time.time()

            # Zeit Differenz zwischen Start und Ankunft
            TimeElapsed = StopZeit - StartZeit
            # mit der Schallgeschwindigkeit (34300 cm/s) multiplizieren
            # und durch 2 teilen, da hin und zurueck
            distanz = (TimeElapsed * 34300) / 2
            print (KANAL, distanz)
            client.publish(KANAL, distanz)
        
        GPIO.cleanup()

Terminal:

Connected with result code 0
('Sensoren/Sensor1', 87.8781795501709)
('Sensoren/Sensor2', 66.01085662841797)
('Sensoren/Sensor3', 136.08605861663818)
('Sensoren/Sensor4', 152.55197286605835)
('Sensoren/Sensor5', 121.05942964553833)
('Sensoren/Sensor6', -34300.03271102905)
('Sensoren/Sensor1', 60.35184860229492)
('Sensoren/Sensor2', 126.65301561355591)
('Sensoren/Sensor3', 145.9484338760376)
('Sensoren/Sensor4', 12.33205795288086)
('Sensoren/Sensor5', 122.43329286575317)
('Sensoren/Sensor6', -34300.01635551453)
('Sensoren/Sensor1', 64.17495012283325)
('Sensoren/Sensor2', 126.59986019134521)
('Sensoren/Sensor3', 125.62261819839478)
('Sensoren/Sensor4', 31.17769956588745)
('Sensoren/Sensor5', 132.39789009094238)
('Sensoren/Sensor6', -34300.0)

Changing the GPIO.cleanup() and GPIO.setmode(GPIO.BCM) might solve the problem when they are called every time in the for-part. I will check this.