From 700de9c9f408885277fa1d55f50936c11fa7d3e4 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 27 Feb 2025 11:07:52 +0100 Subject: [PATCH] update --- loop/SARA_send_data_v2.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/loop/SARA_send_data_v2.py b/loop/SARA_send_data_v2.py index 7a13040..74e3cb0 100755 --- a/loop/SARA_send_data_v2.py +++ b/loop/SARA_send_data_v2.py @@ -95,6 +95,7 @@ import busio import re import os import traceback +import threading import sys import sqlite3 import RPi.GPIO as GPIO @@ -130,6 +131,9 @@ uSpot_profile_id = 1 conn = sqlite3.connect("/var/www/nebuleair_pro_4g/sqlite/sensors.db") cursor = conn.cursor() +_gpio_lock = threading.Lock() # Global lock for GPIO access + + def blink_led(pin, blink_count, delay=1): """ Blink an LED on a specified GPIO pin. @@ -139,22 +143,21 @@ def blink_led(pin, blink_count, delay=1): blink_count (int): Number of times the LED should blink. delay (float): Time in seconds for the LED to stay ON or OFF (default is 1 second). """ - # GPIO setup - GPIO.setwarnings(False) - GPIO.setmode(GPIO.BCM) # Use BCM numbering - GPIO.setup(pin, GPIO.OUT) # Set the specified pin as an output + with _gpio_lock: + GPIO.setwarnings(False) + GPIO.setmode(GPIO.BCM) # Use BCM numbering + GPIO.setup(pin, GPIO.OUT) # Ensure pin is set as OUTPUT - try: - for _ in range(blink_count): - GPIO.output(pin, GPIO.HIGH) # Turn the LED on - #print(f"LED on GPIO {pin} is ON") - time.sleep(delay) # Wait for the specified delay - GPIO.output(pin, GPIO.LOW) # Turn the LED off - #print(f"LED on GPIO {pin} is OFF") - time.sleep(delay) # Wait for the specified delay - finally: - GPIO.cleanup(pin) # Clean up the specific pin to reset its state - print(f"GPIO {pin} cleaned up") + try: + for _ in range(blink_count): + GPIO.output(pin, GPIO.HIGH) # Turn LED on + time.sleep(delay) + GPIO.output(pin, GPIO.LOW) # Turn LED off + time.sleep(delay) + + finally: + GPIO.output(pin, GPIO.LOW) # Ensure LED is off + print(f"LED on GPIO {pin} turned OFF (cleanup avoided)") #get data from config def load_config(config_file):