Laptop to Arduino Nano with HC-05 Bluetooth Module
After getting a serial connection to work between an Arduino Nano and Pi Zero in a previous project I purchased a HC-05 Bluetooth module so that the Pi can be removed from the enclosure, this should reduce the power requirement significantly resulting in increased battery life.
Power requirements :
Pi Zero, Arduino Nano 290mA
Arduino Nano, HC-05 module Bluetooth Module 50mA
The power reduction is huge but it is a bid of a mess when soldered onto veroboard. (I have ordered a few ESP32 for a future improved version.)
The wiring diagram is very similar, just replace the Pi Zero with the HC-05 as below. You can ignore everything to the right of the Nano, it is not required for Bluetooth but i am using it to retrieve a battery level reading.
When powered up the led on the HC-05 should flash rapidly indicating ready to connect. To test you can use the Android app "Serial Bluetooth Terminal". The default pairing code is "1234".
I will be using Python to communicate with the Nano and retrieve a variable from the Arduino so we should make sure we can connect to it through Python. You need to pair it with your device before you can connect.
sudo apt install bluetooth python-dev libbluetooth-dev
pip install pybluez
Test bluetooth install by using the sample code provided here.
If you have any errors check you python and pip version. Linux Mint seems to be Python 3 all the way and worked fine. Raspbian defaults to Python 2.7.16 (python --version) pip will install the module for the default, we need to install to Python3 with pip3 install pybluez.
It should find a device called HC-05, make a note of the mac address because we will need it later.
This part took an embarrassing amount of time to get right :/ It seems that the HC-05 will not send the small variable that i request in one go, it splits it seemingly at random into smaller chunks, the variable is an integer 1023. It sends fine over a direct serial connection but not over the HC-05. Below is the code showing the issue. I have commented out the correction code to show the problem.
You can see that the first request receives b'1' and the second request receives b'023\r\n'
b indicates it is a bytes value, the value between '' is part of the variable. \r is carriage return and \n is newline.
The working code is as follows :
import bluetooth
import time
import sys
bd_addr = "98:D3:61:F6:0B:E8" #mac address of HC-05
port = 1
sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
print ("Connected")
sock.settimeout(5.0) #will search for 5 seconds
value = 98 #"b" that we send to Arduino
count = 0
data = ""
while (count < 5): #this ensures we receive the complete variable
sock.send(value.to_bytes(8,'big')) #sends "b" over bluetooth to HC-05
rcv = sock.recv(1024) #receives data fron the socket
print ('raw = %s'%rcv) #the %s converts %rcv to string
data = data + rcv.decode("utf-8") #bytes to string
print("decoded = "+ data)
if r"\r\n" in str(rcv): #checks for \r\n in the bytes value
print("Got complete variable : "+ data)
int(data)
break
count += 1
sock.close()
The output is now useable and can be converted to int, data = int(data) etc
The difficult part for me was checking the received value for \r\n, i could not get it to work on the data string value but it worked on the rcv bytes value using str in the if statement. I really do not know why, if anyone can explain please comment below it would be very appreciated :)
Comments
Post a Comment