I am working on a project where I'm trying to implement 14 RDM630 RFID sensors that will send any recorded RFID tags to the Main RPi. Unfortunately, these sensors use RS232 communication, and it seems like it's not feasible to have 14 serial communication channels on one RPi. Thus, I've turned to Arduinos to translate the RS232 to strings for I2C to send. I have the tag reader working as desired on the Arduino. I also went through the I2C connection troubleshooting with GND, SDA, and SCL properly being connected (Errno 121 was a pain). I have no pull-up resistors since the RPi has them innately. I also have an oscilloscope set up to check the signals of SCL and SDA, as well as voltage levels in general. They all seem reasonable.
My main issue is that my Python code seems to be requesting constantly and not getting the correct byte values that I'd expect. I am expecting bytes that correspond to 0e5412345, but getting [0, 255, ..., 255]. Here's the code:
Raspberry Pi:
import time
import RPi.GPIO as GPIO
from smbus2 import SMBus, i2c_msg
bus = SMBus(1)
time.sleep(1)
addresses = [0x41] # eventually more
def read_data(ADDRESS):
while True:
Try:
data = bus.read_i2c_block_data(ADDRESS, 0, 9)
print(data)
time.sleep(0.1)
except IOError:
continue
return data
try:
while True:
for address in addresses:
rfid_data = read_data(address)
# do stuff here, later
except KeyboardInterrupt:
GPIO.cleanup()
pass
Arduino:
#include <Wire.h>
#include <rdm630.h>
#define SLAVE_ADDRESS 0x41
rdm630 rfid1(4, 6);
String tagID;
void setup() {
Serial.begin(9600);
Wire.begin(SLAVE_ADDRESS);
Serial.println("Initialization Done!");
digitalWrite(SDA,LOW);
digitalWrite(SCL,LOW);
Wire.onRequest(sendData);
}
void loop() { }
void sendData() {
readTag();
Wire.write(tagID.c_str()); // Send the tag ID .c_str()
}
void readTag() {
byte data[6];
byte length;
tagID = ""; // Clear the tag array before reading a new tag
for (int i = 0 ; i < 1000 ; i++) {
if (rfid1.available()) {
delay(1); // Delay before attempting to read buffer
rfid1.getData(data, length);
for (int j = 0; j < length; j++) {
// Print individual bytes of the tag data
// tagID += data[j];
tagID += String(data[j], HEX);
}
return;
}
}
}
-
please add the output data to your post ... no pictures of textjsotola– jsotola2024年05月29日 19:30:17 +00:00Commented May 29, 2024 at 19:30
-
you have no way of knowing what the arduino is sendingjsotola– jsotola2024年05月29日 19:30:46 +00:00Commented May 29, 2024 at 19:30
1 Answer 1
This won't work! The Pi supports I2C as master not slave.
There are programs which purport to support slave mode but you would need a separate address for each i.e. 14 slaves.