element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Arduino Bluetooth send data to Python stuck
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 1 reply
  • Answers 1 answer
  • Subscribers 390 subscribers
  • Views 2178 views
  • Users 0 members are here
Related

Arduino Bluetooth send data to Python stuck

i7-ryzen
i7-ryzen over 6 years ago

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.

  • Sign in to reply
  • Cancel
  • neilk
    0 neilk over 6 years ago

    Hi,

    You have defined, for use with SoftwareSerial

    1. constint rxpin = 0; 
    2. constint txpin = 1;

     

    These are actually the pins already used by the built in hardware serial port. That's why you are seeing the Serial Monitor Output on Bluetooth!

     

    If you want to communicate with your HC05 independently of the Serial Monitor, you must specify 2 different pins.for use with SoftwareSerial, and connect your HC05 to those pins.

     

    Hope this helps

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube