I have a slave arduino connected to a master arduino mega (through UART) and to a RPi (through USB). The slave arduino is set to monitor various SHT85 (through I2C) and NTC sensors. What I would like to do is to have the slave arduino respond to both commands from the master and the RPi.
The code I have works ok for the NTCs but it is sometimes problematic for the SHT sensors. From times to times (every ~10 minutes) some of the sensors do not send back all requested bytes. The functions I am using to write and read are the following:
//
// I2C write
void cmdI2Cwrite(int address, char *cmd) {
Wire.beginTransmission(address);
int c = 0;
for (int i = 0; i < strlen(cmd); i += 2) {
sscanf(&cmd[i], "%02x", &c);
Wire.write(c);
}
if (Wire.endTransmission() != 0) {
MySerial->println(F("end trasmission failed"));
}
if(address == 112)
{
if(cmd[1] == '0')
{
MySerial->println(F("OK - unlocked"));
IsLocked = false;
}
else
{
MySerial->println(F("OK - locked"));
IsLocked = true;
}
}
else
{
MySerial->println(F("OK"));
}
}
and for read:
// I2C read
void cmdI2Cread(int address, unsigned int nBytes) {
delay(15);
Wire.requestFrom(address, nBytes);
unsigned char c;
char cstr[4];
// check if the same number of bytes are received that are requested.
if (Wire.available() != nBytes) {
MySerial->println("Requested bytes not returned");
}
for (unsigned int i = 0; i < nBytes; i++) {
c = Wire.read();
sprintf(cstr, "%02x", c);
MySerial->print(cstr);
}
MySerial->println();
}
The command: Wire.available() != nBytes is sometimes satisfied and basically I receive no response from the SHT
Both the master and Rpi serials are set to a baud rate of 115200.
I have tried changing cables, shielding cables, reducing the baud rate, implementing some kind of lock mechanism
the problem doesn't seem to appear when I disconnect either the RPi or the master from the slave (so it seems there is some kind of cross talk.. not sure)
Any help would be appreciated
Thank you
-
the code is incomplete ... it does not compilejsotola– jsotola2023年05月22日 17:25:03 +00:00Commented May 22, 2023 at 17:25
-
which arduino are you using? ... first thing to try when facing communication problems is to reduce the baud ratejsotola– jsotola2023年05月22日 17:28:50 +00:00Commented May 22, 2023 at 17:28
-
Hello, Thank you for your reply! You are right the code is incomplete! The problem is that the it's rather long and it's a bit complicated to provide a working version here. But the main loop of the slave code where the communication switch from master arduino to rpi occurs should be sharedhiggsboson2012– higgsboson20122023年05月23日 14:51:24 +00:00Commented May 23, 2023 at 14:51
-
I am using arduino mega 2560 I have tried decreasing the baud rate for both serials to 9600 and I still get the same issuehiggsboson2012– higgsboson20122023年05月23日 14:55:59 +00:00Commented May 23, 2023 at 14:55
-
Sounds like you're using the same UART pins on your secondary arduino for communicating with the RPi and the primary arduino mega. The USB serial chip is connected to the RX/TX pins a.k.a. UART0 on an arduino. If you've got a Uno/Nano/Mini as a secondary arduino, you'll need SoftwareSerial on different pins to avoid crosstalk (two "clients" on an TX line may pull down the signal too much due to parallel pulldown resistors).orithena– orithena2023年05月26日 11:56:01 +00:00Commented May 26, 2023 at 11:56