0

When running program below, the first line ("Hello World") is shown in the shell output.

Also, the Bluetooth connection is working fine (I can send "0" and "1" and they are well received in minicom therefor the required data is getting to the RPI).

But the second print command print ("Accepted from ", address) is not shown in the shell output window: why is that?

SOLVED (thks to Milliways): program execution to be done BEFORE connection made by Blue Term Android terminal program made the address printed out once connection established.

    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)
 
server_socket=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
 
port = 1
server_socket.bind(("",port))
server_socket.listen(1)
 
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()
3
  • Add more debug print statements and separate out the variables. print('a') client_socket,address = server_socket.accept() print('b') print ("Accepted connection from ") print(address) print('c')
    – CoderMike
    Mar 20, 2021 at 17:11
  • The most likely explanation is that there is NOTHING to print because server_socket.accept() is blocking execution.
    – Milliways
    Mar 21, 2021 at 6:50
  • Milliways has it right for my case: I just discovered that indeed no connection was made AFTER the program started. Once I started the program BEFORE connection with a Bluetooth terminal on my smartphone I did get the address printed out, as well as execution of the rest of the program.
    – Erik84750
    Mar 21, 2021 at 13:54

1 Answer 1

-1

Change your print statement to the following:

print(f"Accepted connection from {address}")

Please check how to print guide from here as well. I would suggest learning and using f-Strings.

2
  • OP’s print statement should work
    – CoderMike
    Mar 20, 2021 at 13:42
  • @CoderMike Yes, I missed the comma there. Should I delete this or what should I do?
    – Mr. Panda
    Mar 20, 2021 at 14:03

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.