0

Below code waits for commands from Android Bluetooth Terminal (ie Blue Term): 0 (dim LED) or 1 (light LED).

  1. 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?
  2. 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()
7
  • 1
    Add more debug print statements. Could be changes in libraries between python2 and python3.
    – CoderMike
    Mar 21, 2021 at 18:40
  • I did per your suggestion. Result: on startup in python3 the code runs until and included server_socket.listen(1) Once the bluetooth terminal is connected then execution until and included while(1). And when a "1" is entered in the terminal then this is shown, and nothing else: "Received: b'1' Hence no execution of the subsequent if condition.
    – Erik84750
    Mar 22, 2021 at 5:56
  • Edit your question with the new debug code, include the actual debug output.
    – CoderMike
    Mar 22, 2021 at 7:07
  • "Edit your question with the new debug code, include the actual debug output." Done.
    – Erik84750
    Mar 22, 2021 at 10:21
  • SOLVED: the print output is b'1' or b'0' (why would that be?). So subsequently the if statement needs to change to if data == b'0' and if data == b'1'
    – Erik84750
    Mar 22, 2021 at 11:53

2 Answers 2

1

In python2: b"0" == u"0"

In python3: b"0" != u"0"

So your if statements don't match.

Probably best to do:

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)
elif data == "1":    #if '1' is sent from the Android App, turn OFF the LED
    print ("GPIO 21 HIGH, LED ON")
    GPIO.output(LED,1)
elif data == "q":
    print ("Quit")
    break
else:
    print("Unknown command: "+repr(data))

Then you'll be able to work out a bit more about what's happening.

0
https://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html

Very trivial, and the change in the print-syntax is probably the most widely known change, but still it is worth mentioning: Python 2’s print statement has been replaced by the print() function, meaning that we have to wrap the object that we want to print in parantheses.

Python 2 doesn’t have a problem with additional parantheses, but in contrast, Python 3 would raise a SyntaxError if we called the print function the Python 2-way without the parentheses.

Python 2 print 'Python', python_version() print 'Hello, World!' print('Hello, World!') print "text", ; print 'print more text on the same line' Python 2.7.6 Hello, World! Hello, World! text print more text on the same line Python 3 print('Python', python_version()) print('Hello, World!')

print("some text,", end="") print(' print more text on the same line') Python 3.4.1 Hello, World! some text, print more text on the same line print 'Hello, World!' File "", line 1 print 'Hello, World!' ^ SyntaxError: invalid syntax Note:

Printing “Hello, World” above via Python 2 looked quite “normal”. However, if we have multiple objects inside the parantheses, we will create a tuple, since print is a “statement” in Python 2, not a function call.

print 'Python', python_version() print('a', 'b') print 'a', 'b' Python 2.7.7 ('a', 'b') a b

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.