The code asked Chat-gpt to write a Python code that sends a message to the app inventer and receives it in Raspberry Pi.. I just wanted to make sure that the transmission works properly because the errors keep coming out..
Just in case, I will also upload the code I asked gpt...
----python cod------
import serial
import RPi.GPIO as GPIO
import time
GPIO 핀 번호 설정
LED_PIN = 14
시리얼 통신 객체 생성
bluetooth_port = '/dev/ttyS0' # HC-05 모듈이 연결된 시리얼 포트 경로
bluetooth = serial.Serial(bluetooth_port, 9600, timeout=1)
GPIO 초기화
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
try:
print("Waiting for Bluetooth messages...")
while True:
if bluetooth.in_waiting > 0:
# Bluetooth로부터 데이터 수신
received_data = bluetooth.readline()
try:
# UTF-8로 디코딩 시도
received_data = received_data.decode('utf-8').strip()
print("Received message:", received_data)
# 수신된 메시지에 따라 LED 제어
if 'on' in received_data:
print("Turning LED ON...")
GPIO.output(LED_PIN, GPIO.HIGH) # LED 켜기
elif 'off' in received_data:
print("Turning LED OFF...")
GPIO.output(LED_PIN, GPIO.LOW) # LED 끄기
except UnicodeDecodeError:
print("UnicodeDecodeError: Cannot decode the received data")
continue
except KeyboardInterrupt:
print("Keyboard Interrupt. Exiting...")
finally:
# GPIO 리소스 해제
GPIO.cleanup()
# Bluetooth 시리얼 포트 닫기
bluetooth.close()
The above code connects App Inventor and Raspberry Pi with a Bluetooth module called hc-06 and attempts to communicate in series.
When you run the code and send a message from AppInventor to Raspberry Pi, you get the following error: (This is the contents of the terminal window.)
pi@raspberrypi:~/test $ sudo python seled.py
Waiting for Bluetooth messages...
Received message:
Received message:
Traceback (most recent call last):
File "/home/pi/test/seled.py", line 14, in
received_data = bluetooth.readline().decode().strip()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 1: invalid continuation byte