Now that Ive incorporated i2c in my robot design I now see that i2c will hang intermittently . Subsequent search find that this is a problem for people that want stable i2c connections. I have a NodeMCU master sending and recieving data from a Arduino Nano slave The hang can occur anywhere between 1 to 6 hours. I know this might seem like a long time to some but I dont know what my expectations should be.
I've tried to vary the timing from sending data every .1 seconds to every 2 seconds and the results are the same.
Is there a form of a "Wire.reset()" (or equivalent) function that can restart the I2c connection without having to do a full hald reset of the nano ?
This is what Im doing now and it works but I lose my state info on the nano and I dont want to instrument a work around before I see if ther is a way to gracefully restart i2c in software.
Or maybe should I use the I2c master Library that includes a timeout function?
Any way here is the "relevent" i2c code:
Master on NodeMCU
#include <Wire.h>
#include <i2cSimpleTransfer.h>
struct SLAVE_DATA {
float sensor1;
float sensor2;
float sensor3;
float sensor4;
float sensor5;
uint8_t sensor6;
uint8_t sensor7;
uint8_t sensor8;
char sensor9;
char sensor10;
uint8_t sensor11;
uint16_t sensor12;
};
void setup(void) {
Serial.begin(115200);
Wire.begin(D1,D2);
}
void loop(void)
{
delay(500); // Tried different delays however nothing changes
//do stuff...
// Send Data
Wire.beginTransmission(8); /* begin with device address 8 */
Wire.write(rcmd); /* sends hello string */
Wire.endTransmission(); /* stop transmitting */
delay (100);
// Read Data
delay(100);
Wire.requestFrom(8, sizeof slave_data ); /* request & read data of size 13 from slave */
if ( Wire.available() == sizeof(slave_data) ) {
i2cSimpleRead(slave_data);
}
delay (100);
}
Slave on Nano
#include <Wire.h>
#include <i2cSimpleTransfer.h>
struct SLAVE_DATA {
float sensor1; // use specific declarations to ensure communication between 16bit and 32bit controllers
float sensor2;
float sensor3;
float sensor4;
float sensor5;
uint8_t sensor6;
uint8_t sensor7;
uint8_t sensor8;
char sensor9 = '0';
char sensor10;
uint8_t sensor11;
uint16_t sensor12;
};
SLAVE_DATA slave_data;
void setup()
{
Wire.begin(8);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
delay(100);
}
void loop(void)
{
delay(500); // Tried different delays however nothing changes
// do stuff...
void receiveEvent(int howMany) {
while (0 <Wire.available()) {
char c = Wire.read();
rcmd=c;
}
}
void requestEvent() {
i2cSimpleWrite(slave_data);
}
}