Hi, I have spent almost 3 days scouting the web for this solution but to not available.
I have an Arduino UNO R3 + bluetooth HC-05 and want to transmit information through Bluetooth to Laptop Win10 + Python Pybluez.
It appears that the only information I can receive using revc in Python are those send through Serial monitor and not Bluetooth. EVEN WHEN THE ARDUINO IS NOT PHYSICALLY CONNECTED TO THE COMPUTER (only through bluetooth), revc still only returns the values printed to Serial.
Arduino code
#include <SoftwareSerial.h> //Define the pins used for receiving //and transmitting information via Bluetooth const int rxpin = 0; const int txpin = 1; //Variable to store input value char rec= 'A'; //Connect the Bluetooth module SoftwareSerial bluetooth(rxpin, txpin); void setup(){ //Initialize Serial for debugging purposes Serial.begin(9600); Serial.println("Serial ready"); //Initialize the bluetooth bluetooth.begin(9600); bluetooth.println("Bluetooth ready"); } void loop(){ if(bluetooth.available()){ rec = bluetooth.read(); // read 1 char Serial.println(rec); // Printout throught Serial the Char just read. } if(rec == 'Z'){ // If rec char is Z Serial.println("Serial: Z"); // Printout throught Serial bluetooth.println("Bluetooth: Z"); // Printout throught Bluetooth rec = 'A'; // reset rec to A to avoid inf loop } //Wait ten milliseconds to decrease unnecessary hardware strain delay(10); }
Python Codedevices = bluetooth.discover_devices()
import bluetooth #Detect all Bluetooth devices and Create an array with all the MAC addresses print("Searching for devices...") nearby_ #Run through all the devices found and list their name num = 0 print("Select your device by entering its coresponding number...") for i in nearby_devices: num+=1 print(str(num) + ": " + bluetooth.lookup_name( i )) #Allow the user to select their Arduino selection = int(input("> ")) - 1 bd_addr = nearby_devices[selection] port = 1 # Show user selection print("You have selected " + bluetooth.lookup_name(nearby_devices[selection])) # Connect to bluetooth address and port sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM ) sock.connect((bd_addr, port)) data = "L" sock.send(data) data = "Z" sock.send(data) data = sock.recv(1024) print(data) # Print out appearsto be those of Serial.println and not bluetooth.println sock.getsockname() sock.getpeername() sock.close()
Basically, I want only the data transmitted through "bluetooth.println".
The code above returns the following in Python:
b'L\r\nZ\r\nSerial: Z\r\n'
Expected return to be:
Bluetooth: Z\r\n
Thanks
Message was edited by: Formatted code.