Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8082

Python • Re: RP Zero 2W GPIO and TX/RX not working simultaneously

$
0
0
Sure - Here are the relevant functions - I have omitted some unrelated functions for clarity.

import socket
import sys
import json
import serial
import time
import shutil
import os
from gpiozero import Button
# import RPi.GPIO as GPIO
import subprocess
from data_new import Astro_Feed,Ow_Feed


# # GPIO Pin setup
CONFIG_SIGNAL_PIN = 22 # Adjust based on your setup
# SCORPIO_RESET_PIN =
#
GPIO.setmode(GPIO.BCM)
GPIO.setup(CONFIG_SIGNAL_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# # GPIO.setup(SCORPIO_RESET_PIN, GPIO.OUT)
# # GPIO.input(SCORPIO_RESET_PIN) == GPIO.HIGH

CK_Button = Button(23)
# Set up general variables
port = '/dev/ttyS0'
baudrate = 9600

def check_for_buttonpress():
# Monitor the signal pin to switch between AP and Client mode
print('Checking Button Press')
# if GPIO.input(CONFIG_SIGNAL_PIN) == GPIO.LOW:
if CK_Button.is_pressed:
reset_mode()
start_ap_mode()
print('Starting AP Mode')
else:
start_station_mode()
print('Starting Station Mode')

time.sleep(1) # Avoid excessive CPU usage

def send_data():

try:
ser = serial.Serial(port, baudrate)
ser.reset_output_buffer()
print(f"Serial port {port} opened successfully")

with open('/tmp/display_data.json', "r") as data:

json_data = json.load(data)

data_string = json.dumps(json_data)

ser.write(data_string.encode('utf-8'))
print(f"Sent: {json_data}")

# Receive response
if ser.in_waiting > 0:
received_data = ser.readline().decode('utf-8').strip()
print(f"Received: {received_data}")

except serial.SerialException as e:
print(f"Error opening serial port: {e}")
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
finally:
# Close the serial port
if 'ser' in locals() and ser.is_open:
ser.close()
print("Serial port closed")

check_for_buttonpress()
# Give it a moment
time.sleep(2)
send_data()

Statistics: Posted by dsimpson3030 — Mon Mar 10, 2025 5:41 pm



Viewing all articles
Browse latest Browse all 8082

Trending Articles