Hello,
I'm trying to create a communication between Arduino nano 33 IOT and the Jetson Nano. I'm using the i2c bus and I want to send a structure to the Jetson Nano but when I receive data over the bus it's gibberish.
Jetson Nano pins:
GND, 27 (SDA), 28 (SDL)
Arduino Nano 33 IoT pins:
GND, A4 (SDA), A5 (SCL)
Arduino code:
#include <Wire.h>
int i2cAddress = 0x6a;
typedef struct {
int command;
int ack_Signature;
int x;
int y;
int z;
} JETSON_COMMANDS;
JETSON_COMMANDS SEND_CMD;
JETSON_COMMANDS RECEIVE_RESPONSE_CMD;
void setup() {
// Setup the Serial
Serial.begin(9600);
while(!Serial) { ; }
Wire.begin(i2cAddress);
Wire.onReceive(receiveJetsonEvent);
Wire.onRequest(sendJetsonEvent);
}
void loop() {
SEND_CMD.command = 1;
SEND_CMD.ack_Signature = 2;
SEND_CMD.x = 3;
SEND_CMD.y = 4;
SEND_CMD.z = 2;
sendJetsonEvent();
Serial.println("SEND COMMAND");
delay(5000);
}
void receiveJetsonEvent(int bytes) {
Serial.println(bytes);
Serial.println();
Wire.readBytes((byte*) &RECEIVE_RESPONSE_CMD, sizeof RECEIVE_RESPONSE_CMD);
Serial.println(RECEIVE_RESPONSE_CMD.command);
Serial.println(RECEIVE_RESPONSE_CMD.ack_Signature);
Serial.println(RECEIVE_RESPONSE_CMD.x);
Serial.println(RECEIVE_RESPONSE_CMD.y);
Serial.println(RECEIVE_RESPONSE_CMD.z);
Serial.println("*******************************");
}
// 2. sendJetsonEvent() //
void sendJetsonEvent() {
int count = Wire.write((byte*)& SEND_CMD, sizeof SEND_CMD);
Serial.println(count);
}
Jetson Nano - Python3 code:
import struct
import time
import sambas
BINARY_FORMAT = '5h'
bus = sambas.SMBus(0)
address = 0x6a
def receiveArduinoEvent(num_of_bytes):
data = bus.read_i2c_block_data(address, num_of_bytes)
return data
while True:
data = receiveArduinoEvent(20)
data_struct = struct.unpack(BINARY_FORMAT, data)
print(data)
print(data[0])
print(data[1])
print(data[2])
print(data[3])
print(data[4])
print('*******************')
time.sleep(2)
My problem is that the struct library returns a TypeError: a bytes-like object is required, not 'list'
I would expect to receive a struct of 5 ints with the values 1,2,3,4,2
Can someone extend a helping hand?
Thank you!