I2C com. between NodeMCU ( ESP8266 ) and Arduino MEGA

Hi @ all,

I have a problem with I2C communication.

Situation:

NodeMCU ( ESP8266 ) as Master ( Pin D1 and D2 )
Arduino MEGA as Slave ( Pin SDA20 and SCL21 )

Tested with and without PullUp resistors on both lines.

I can send a message from the Master to the slave without any Problems.
But if I try to send something from Slave to Master, the Master receives nothing.

Codesnippet from Master:

Setup()
{

Wire.begin();
//Wire.setClockStretchLimit(40000); // If I comment in, nothing change
}

bool sent = false;

loop()
{
int test = Wire.available(); // To see is something coming from Slave
Serial.println(test);

// The following message will only send once
if(!sent){
Wire.beginTransmission(SLAVE_ADDRESS);
Wire.write(“Hallo”);
Wire.endTransmission();
sent = true;
}
}

Codesnippet from Slave:

Setup()
{

Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveI2C);
}

void receiveI2C(int howMany)
{
String message = “”;
while (Wire.available() > 0) {
char c = Wire.read();
message += c;
}

Wire.write(“Danke”);
}

So the slave receives the word “Hallo” and send back “Danke” to Master.
But the Master never receive something.
The output of Wire.available(); on Master is always 0.

Any Idea why ?

Regards
Stefan

I'm having the same problem... Hope you'll find the answer...

i made it work with this examples: http://www.electronicwings.com/nodemcu/nodemcu-i2c-with-arduino-ide https://thewanderingengineer.com/2015/05/06/sending-16-bit-and-32-bit-numbers-with-arduino-i2c/

but, it was very tricky sometimes depending of the data types you are sending.

Finally a used this super helpful examples: https://github.com/Scott216/I2C-Examples

you can send some packets together using different data types only in one request.