I've been struggling to send sensor values from arduino to raspberry pi 3b+. I'm using NRF24L01+ module to communicate. I'm trying to send accelerometer values (double type) from arduino to raspberry pi. A part of Arduino code that's sending values:
typedef struct {
double rollx;
double pitchy;
}myStruct;
myStruct duomenys;
duomenys.rollx = kalAngleX;
duomenys.pitchy = kalAngleY;
radio.write(&duomenys,sizeof(duomenys));
Serial.print("Roll: ");
Serial.println(duomenys.rollx);
Serial.print("Pitch: ");
Serial.println(duomenys.pitchy);
Serial.print("\t");
Here's the arduino serial monitor output:
Pitch: -12.98
Roll: 89.85
Pitch: -12.97
Roll: 89.85
Pitch: -12.96
Roll: 89.86
However on the raspberry side, I'm not able to Unpack the received structure (Note: I'm pretty new to python). What I've tried is:
while True:
while not radio.available(0):
## print("Nepareina")
time.sleep(1)
duomenys = []
radio.read(duomenys, radio.getDynamicPayloadSize())
data = struct.unpack('ff',duomenys)
rollx = data [0]
pitchy = data[1]
print(rollx)
print(" ")
print(pitchy)
When compiling this code, I'm getting an error:
Traceback (most recent call last):
File "/home/pi/Desktop/NRF24L01/receiveArduino.py", line 41, in <module>
data = struct.unpack('ff',duomenys)
TypeError: a bytes-like object is required, not 'list'
If I change the line
data = struct.unpack('ff',duomenys)
to
data = struct.unpack('ff',bytes(duomenys))
I get an error:
Traceback (most recent call last):
File "/home/pi/Desktop/NRF24L01/receiveArduino.py", line 41, in <module>
data = struct.unpack('ff',bytes(duomenys))
struct.error: unpack requires a bytes object of length 8
If anyone has any suggestions on how to read the received struct in python, feel free to share.
EDIT: Updated the arduino serial monitor output. Previously had posted the wrong output.
EDIT: This is the NRF24L01library I'm using. https://github.com/matmany/lib_nrf24
Hey I do not know if you solved it or not. I was having the same issue and I managed to solve it like this:
Instead of declaring duomenys = []
... try duomenys = bytearray(nbBytesData)
, where nbBytesData should be the number of bytes you are expecting (I'm assuming 8 bytes)
Then the data = struct.unpack('ff',duomenys)
should work.