notes on making an Arduino UNO communicate with a PC via Bluetooth (using the Arduino IDE, a HC-05 module and python)

This post explores how to establish communication between an Arduino and a PC running Ubuntu via Bluetooth using a HC-05 module plugged to an Arduino UNO, the Arduino IDE, and some Python. It provides an illustrative use case covering HC-05 module configuration, Arduino code implementation for message transmission, and Python-based PC connection and data handling. The post emphasizes a practical implementation, including some setup commands and coding examples.

Example


Materials

- Arduino UNO
- USB cable
- HC-05
- 1kΩ resistor
- 2kΩ resistor
- breadboard
- wires
- PC with Bluetooth connectivity 

-------------------------------------------------------------------------------------------------
Part I 
[configure the HC-05 module plugged to the Arduino]
(check reference [1] for additional information)

Sketch I

void setup() {
}

void loop() {
}

Setup I

I.1) enter AT commands mode (press and hold the button while powering the hc-05)

I.2) confirm AT commands mode (test sending "AT" through the serial monitor) 

    Example Output
    OK


I.
3) get address of hc-05 via the following AT command:

    AT+ADDR?

    Example address
    +ADDR:1122:33:445566

I.4) get name of hc-05 via the following AT command:

    AT+NAME?

    Example address
    hc112233445566

TO DO
(set role, set mode and address to bind to)

-------------------------------------------------------------------------------------------------
Part II 
[make the Arduino regularly send an illustrative message via hc-05 bluetooth module]
(Arduino IDE) 

Setup II
Sketch II

#include <SoftwareSerial.h>

//Create software serial object to communicate with HC-05
SoftwareSerial mySerial(3, 2); //HC-05 Tx & Rx is connected to Arduino #3 & #2


void setup()
{
//Begin serial communication with Arduino and HC-05
mySerial.begin(9600);
Serial.begin(115200);
}

void loop()
{
unsigned long currentMillis = millis();
unsigned long seconds = currentMillis / 1000;
unsigned long minutes = seconds / 60;

if ( int(minutes) <= 0 ) {
mySerial.println(String(int(seconds))+" sec.");
Serial.println(String(int(seconds))+" sec.");
}
else {
mySerial.println(String(int(minutes))+" min.");
Serial.println(String(int(minutes))+" min.");
}
delay(2000);
}

-------------------------------------------------------------------------------------------------
Part III 
[establish a connection between the Uno board and the PC via bluetooth and make the later read the message being sent by the Arduino via hc-05 adapter]
(python)
-tested on a system running ubuntu-

III.1) check requirements (possibly system dependent) 

sudo apt-get install libbluetooth-dev
pip install git+https://github.com/pybluez/pybluez.git#egg=pyblue
(...)
III.2) try to connect to hc-05 module and read the stream
(pyhton)
import socket

baddr = "11:22:33:44:55:66"
channel = 1
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
print(baddr)
s.connect((baddr,channel))

print ("Accepted connection from "+baddr)

try:
while 1:
data = s.recv(4096)
if data:
dataout = str(data).replace('\\r\\n\'', '')
print(dataout.replace('b\'', ''))
except:
print("Closing socket")
s.close()


Comments