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