My setup: a RP3 running Debian 12 Bookwork and Python 3.11.2.
I have a capacitive touch sensor wired to the RPi3. The ground, power supply, and output signal are connected to the Pi's GND, 3V3, and GPIO17 respectively. It works.
Here's my Python script named touch_play_random.py located in /home/pi/:
I've done a lot of fiddling to try and solve two problems, but I don't want to over-explain and so will try to keep this brief.
The first problem is that I can't seem to get this script to run at boot. I've tried the systemd and the crontab methods, but there's always some glitch with mpg123 that fouls things up. If you want more (oh, so much more) explanation I can give it, but my hunch is that it might be better for me to just follow someone's instructions from scratch.
If I can solve that first problem, then the second one may not need to be solved.
Second problem is that, since I haven't been able to get my script to run at boot, I figured I'd just log in once and enter this in a terminal window:
Initially everything's fine, but then after a few minutes of sitting idle, touching the sensor only causes the audio to play. The MQTT message won't publish.
Any insights very much appreciated.
I have a capacitive touch sensor wired to the RPi3. The ground, power supply, and output signal are connected to the Pi's GND, 3V3, and GPIO17 respectively. It works.
Here's my Python script named touch_play_random.py located in /home/pi/:
Code:
import RPi.GPIO as GPIOimport osimport timeimport randomimport paho.mqtt.client as mqtt# MQTT SetupMQTT_BROKER = '192.168.1.35'MQTT_PORT = 1883MQTT_TOPIC = 'home/ham_radio_touch'MQTT_USERNAME = <redacted>MQTT_PASSWORD = <redacted>client = mqtt.Client()client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)client.connect(MQTT_BROKER, MQTT_PORT, 60)# GPIO SetupTOUCH_PIN = 17 # The GPIO pin connected to the sensor outputAUDIO_FILES_DIR = "/home/pi/audio" # Directory containing the audio filesAUDIO_FILES = [f"{i:02}.mp3" for i in range(1, 30)] # List of audio files (01.mp3, 02.mp3, ..., 29.mp3)GPIO.setmode(GPIO.BCM)GPIO.setup(TOUCH_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)# State definitionsIDLE = 0TOUCH_DETECTED = 1DEBOUNCE_TIME = 2 # secondsprevious_state = IDLElast_touch_time = time.time()def play_audio(): selected_file = random.choice(AUDIO_FILES) os.system(f'mpg123 {os.path.join(AUDIO_FILES_DIR, selected_file)}')try: while True: current_state = GPIO.input(TOUCH_PIN) current_time = time.time() if current_state == GPIO.HIGH and previous_state == IDLE and (current_time - last_touch_time > DEBOUNCE_TIME): client.publish(MQTT_TOPIC, 'ON') play_audio() previous_state = TOUCH_DETECTED last_touch_time = current_time elif current_state == GPIO.LOW and previous_state == TOUCH_DETECTED and (current_time - last_touch_time > DEBOUNCE_TIME): client.publish(MQTT_TOPIC, 'OFF') previous_state = IDLE last_touch_time = current_time time.sleep(0.1)except KeyboardInterrupt: GPIO.cleanup() client.disconnect()
The first problem is that I can't seem to get this script to run at boot. I've tried the systemd and the crontab methods, but there's always some glitch with mpg123 that fouls things up. If you want more (oh, so much more) explanation I can give it, but my hunch is that it might be better for me to just follow someone's instructions from scratch.
If I can solve that first problem, then the second one may not need to be solved.
Second problem is that, since I haven't been able to get my script to run at boot, I figured I'd just log in once and enter this in a terminal window:
Code:
python3 touch_play_random.py
Any insights very much appreciated.
Statistics: Posted by grantalewis — Tue Jul 23, 2024 6:23 pm