Search code examples
pythonarduinobluetoothraspberry-pi4

Receiving data from arduino with bluettoth HC-05 module to python - wierd numbers recived and conversion


I asked this question before to know how to convert the byes to ints here: and now I have a different situation.

Send data to from Arduino to Raspberry using bluetooth hc-05 and python - Byte conversion

The HC-05 is connected to pin 1 and 2 in arduino TX to RX and RX to TX (PI, Arduino).

The solution kindly sent was the function int.frombytes. That worked ok. But the only numbers I could see were 255, 254, 255, 10, 255, 254.

I tried unplugging the usb cable form the pc and powering the arduino from a battery and same thing happened.

Now, I modified the sketch from arduino to send only the number 2 and in raspi python I still receive 255, 254, 255, 10, 254, 255.

Has anyone had the same problem?


Solution

  • You were using: input = serialData.read(). I used getData = str(ser.readline()) and it worked for me. Input is in utf-8, whereas getData is a string. I feel like in Python a string is much easier to process and convert into integer than utf-8.

    To process the string into integers, i did:

    getData= str(ser.readline())
    data = getData[0:][2:-5] ##2 and -5 (for list slicing) may need to be adjusted, after you read what is in getData
    getData = int(getData)
    

    Readline() separates data by line. If you want to send multiple data in each line on the serial monitor:

    getData= str(ser.readline())
    data = getData[0:][2:-5]
    processed = data.split(",")
    processed[0] = int(processed[0])
    processed[1] = int(processed[1])
    

    As you can see I separated my data by commas (,), when I sent my data in Arduino IDE:

    Serial.print(angle); //angle is my processed[0]
    Serial.print(",");
    Serial.println(distance); //distance is my processed[1]
    

    I learnt this from: https://www.learnrobotics.org/blog/arduino-data-logger-csv/ Hope it helps.