Below code waits for commands from Android Bluetooth Terminal (ie Blue Term): 0 (dim LED) or 1 (light LED).
- When this program is ran in RPI Putty terminal as python 5EE-Bluetooth-LED.py it performs as designed. However when run as python3 5EE-Bluetooth-LED.py nothing happens. Why would that be abd what is needed to run as python3?
- When this program is ran in Thonny, nothing happens either but I suspect that is because there too python3 is ran. Is that so, and how can this be solved?
When run as python3, the screen output is (after having entered first a "1" and then a "0" in the terminal:
Hello World
debug 1
debug 2
debug 3
debug 4
Accepted connecion from ('18:94:c6:95:de:0d', 1)
Received: b'1'
Received: b'0'
print("Hello World")
import bluetooth
import RPi.GPIO as GPIO #calling for header file which helps in using GPIOs of PI
LED=21
GPIO.setmode(GPIO.BCM) #programming the GPIO by BCM pin numbers. (like PIN40 as GPIO21)
GPIO.setwarnings(False)
GPIO.setup(LED,GPIO.OUT) #initialize GPIO21 (LED) as an output Pin
GPIO.output(LED,0)
print("debug 1")
server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
port = 0
print("debug 2")
server_socket.bind(("",port))
print("debug 3")
server_socket.listen(1)
print("debug 4")
client_socket,address = server_socket.accept()
print ("Accepted connection from ",address)
while 1:
data = client_socket.recv(1024)
print ("Received: %s" % data)
if data == "0": #if '0' is sent from the Android App, turn OFF the LED
print ("GPIO 21 LOW, LED OFF")
GPIO.output(LED,0)
if data == "1": #if '1' is sent from the Android App, turn OFF the LED
print ("GPIO 21 HIGH, LED ON")
GPIO.output(LED,1)
if data == "q":
print ("Quit")
break
client_socket.close()
server_socket.close()