gutierrezps / ESP32_I2C_Slave

I2C slave library for ESP32
GNU Lesser General Public License v2.1
81 stars 20 forks source link

"Max attempts" error after a few minutes - Master reader / slave sender #14

Closed BartoszGum closed 2 years ago

BartoszGum commented 3 years ago

Hi, I have a code with communication between two ESP-WROOM 32U 240MHz / 4MB Flash. During sending first 4-5 times everything is ok, but next I'm getting error "Max attempts" or "Slave not found". Master restart doesn't fix a problem, so I think It must be problem with slave (his restart fix problem).

It's my code: (a little chaotic, because I'm trying different things to repair this error)

Master:

`

 void I2CFun(void * param) {

vTaskDelay(2000);
String message = "";
Serial.println("Wywoluje x.begin();");
x.setClock(10000);
bool status = (x.begin(16, 4));
vTaskDelay(2000);
scanI2C();

for (;;){

    if (!status) {
        Serial.println("Wywoluje x.begin();");
        status = (x.begin(16, 4));
    }

    WireSlaveRequest slaveReq(x, 0x04, MAX_SLAVE_RESPONSE_LENGTH);
    slaveReq.setRetryDelay(100);
    slaveReq.setAttempts(255);

    bool success = slaveReq.request();
    String temp = "";
    if (success) {
        while (1 < slaveReq.available()) {   // pętla przez wszystkie bajty oprócz ostatniego
            char c = slaveReq.read(); // otrzymaj bajt jako znak
            temp += c;
                          // wypisuje znak
        }
        Serial.println(temp);
        saveData(temp);
        deserializeJson(sensorsData, temp);
    }
    else {
        Serial.println(slaveReq.lastStatusToString());
        Serial.println("Wywoluje x.begin();");
        I2C_ClearBus();
        vTaskDelay(2000);
        status = (x.begin(16, 4));
        vTaskDelay(2000);
    }
    //deserializeJson(sensorsData, message);

    vTaskDelay(1000*60*3);
}

I2CTask = NULL;
vTaskDelete(NULL);
   }

`

and Slave:

`

void I2CMasterFun(void * param) {

unsigned long timeToBeginAgain = millis() + (1000 * 60 * 5);

vTaskDelay(1000);
bool status = (WireSlave.begin(13, 27, 0x04));
vTaskDelay(10);
if (!status) {
    Serial.println("I2C Slave init failed");
    while (1) delay(100);
}
else {
    Serial.println("I2C Slave init OK");
}

WireSlave.onRequest(requestEvent);

for (;;) {

    if (millis() >= timeToBeginAgain) {
        I2C_ClearBus();
        status = (WireSlave.begin(13, 27, 0x04));
        WireSlave.onRequest(requestEvent);

        WireSlave.clearWriteError();
        WireSlave.flush();
        Serial.print("PEEK: ");
        Serial.println(WireSlave.peek());

        timeToBeginAgain = millis() + (1000 * 60 * 2);
    }

    WireSlave.update();
    vTaskDelay(5);

}

vTaskDelete(NULL);
     }

`

gutierrezps commented 2 years ago

Unfortunately, I couldn't address this issue in a timely manner because I haven't been working with the ESP32 ever since. In this meantime, I2C Slave support has been added to the official ESP32 framework as you can see in the updated README of this repo. Therefore this library is no longer needed and its development will be ceased.

gutierrezps commented 2 years ago

In your particular case, I can see you're setting up the I2C slave on every loop, and that's wrong. You should setup only once, and only call the update() method on the main loop, as in the slave examples.