Issue
I'm kinda a newbie here, so thanx in advace for your help. I have and arduino with the HC-05 bluetooth module paired with a raspi 4. I have lots of sensors in the arduino so I wanted to transmit the data from the sensors from the arduino to the raspberry using python. The connection works ok and I started with some examples. I just wanted to read the serial output of the arduino, in the raspi via bluetooth and python. I found lots of examples and tried them but always got stuck in the conversion of the data the came from the arduino to the PI via bluetooth.
I just use a loop in the arduino to generate numbers that are sent to the pi via bluetooth. All I get in the pi python app is this: b'\xf9' b'\xff' b'\xfe' b'\xe1' b'\xfd' b'\xff' b'\xf8' b'\xff' b'\xff' b'\xff'
I can't even send a "Hello from Arduino" and get it right. I tried .decode and lots of examples and nothing happens.
Arduino Code:
//send numbers 0 to 255 to the Serial port
//Context: Arduino
//
void setup() {
//open serial connection at 9600
//
Serial.begin(9600);
}
//start program main loop
void loop() {
//count from 0 to 25
for (byte n = 0; n < 255; n++) {
//send current number to the serial port
Serial.print(n);
// pause
delay(50);
}
}
Python Code
#import PySerial module
#wait for data from serial port and print on console
#Context: Raspberry Pi
import serial
#assign serial port address
port = "/dev/rfcomm0"
#open serial port at 9600 baud rate and create serial communication object
serialData = serial.Serial(port,9600)
#main function
def run():
while True:
#wait for incoming data from the serial port
if (serialData.inWaiting() > 0):
#read incoming data
input = serialData.read()
#print incoming data
print (input.decode('utf-8', 'strict'))
run()
I can't get the numbers, imagine a string or data from environemtal sensor in the arduino.
I passed 3 days trying and I couldnt get a solution.
Would really appreciate your help.
Thanx
Pablo
I cannot convert the byte sent from the arduino via bluetooth to string.
If it use the serial USB it works ok with the .decode(), not from bluetooth used as serial.
.... edit...
HI I and thanx.
Hi and thank you. Something changed but I modified the sketch to send only number 2 and I continue getting 255 254 62 255 255 Serial monitor only shows number 2 but python shows those numbers
Please help, thanx
Pablo
Solution
In Python, to convert the bytes back to integers, there is the the int.from_bytes
functionality.
https://docs.python.org/3/library/stdtypes.html#int.from_bytes
So as an example, your first byte data above b'\xf9
would be:
pi@raspberry:~ $ python3
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> data = b'\xf9'
>>> print(int.from_bytes(data, 'little'))
249
If you have more complicated sensors you might want to send all its values as a struct.
Python has the struct library for interpreting bytes from packed binary data
For example if there were two int8
values packed into the bytes you could get them with:
pi@raspberry:~ $ python3
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> data = b'\xf9\xf3'
>>> value1, value2 = struct.unpack("<2B", data)
>>> print(f"Value1={value1}\nValue2={value2}")
Value1=249
Value2=243
Answered By - ukBaz Answer Checked By - Cary Denson (WPSolving Admin)