0

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?

PabloUY
  • 15
  • 4
  • Have you tried using [Serial.write](https://www.arduino.cc/reference/en/language/functions/communication/serial/write/) rather than Serial.print – ukBaz Jul 11 '22 at 15:44
  • Thank you for the answer. Will try it and see what happens. – PabloUY Jul 11 '22 at 16:57
  • Hi, still receiving in pi 255, 254, 255, 254 and it should be only number 2. very strange.. Any ideas? Thank you. – PabloUY Jul 12 '22 at 12:01
  • Hi , I was passing the rx through a the breadboard. Now i connected tx and rx directly to the ports in the asrduino and I receive only the number 50, when I should reveive number 2. Any ideas? Thank you... – PabloUY Jul 12 '22 at 12:39
  • So you are doing `int.from_bytes(data, 'little')` and it equals `50`? This is likely because the byte being sent is `b'\x32'` which is the string of `2`. Maybe update your question with the code that you are using so people can see if you are doing `Serial.write(2)` or `Serial.write("2")` – ukBaz Jul 12 '22 at 13:05

1 Answers1

0

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.

J Z
  • 26
  • 4
  • A note of caution when sending data over Bluetooth is that integers as strings takes more bytes and so longer to transmit. To send `254` and `255` as strings is 7 bytes `[50, 53, 52, 44, 50, 53, 53]`. If sent as a binary struct of uint8's then it is 2 bytes `[254, 255]` – ukBaz Jul 13 '22 at 10:47
  • Thank you for helping. I finally got it. The conversions were ok. The problem was that I connected the hc5 via 2 resistors to reduce the voltage to 3.3. I connected both tx and rx directly yo arduino and now all is fine. All working perfectly, just esxactly as the serial monitor. I can read and write perfectly with serial.Serialdata.decode() and the int.frombytes depending the situation. Thank you all. – PabloUY Jul 13 '22 at 14:41