I2C between ESP32 and ESP8266

I am looking at using I2C to transfer data between an ESP32 as the master and an ESP8266 as the slave. When my sketches to transfer data did not work I created 2 simple sketches as follows

ESP32 master

// Wire Master Writer ESP32

//SDA GPIO21
//SCL GPIO22

#include <Arduino.h>
#include <Wire.h>

void setup()
{
  Serial.begin(115200);
  Wire.begin(99);
}

void loop()
{
  static byte x;
  Wire.beginTransmission(99);
  Wire.write("x is ");
  Wire.write(x);
  Wire.endTransmission();
  Serial.println(x);
  x++;
  delay(500);
}

ESP8266 I2C scanner

// Wire Scanner ESP8266

//SDA D2
//SCL D1

#include <Arduino.h>
#include <Wire.h>

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  Serial.println("\nI2C Scanner");
}

void loop()
{
  byte error, address;
  int nDevices;
  Serial.println("Scanning...");
  nDevices = 0;
  for (address = 1; address < 127; address++)
  {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address < 16)
      {
        Serial.print("0");
      }
      Serial.println(address, HEX);
      nDevices++;
    }
    else if (error == 4)
    {
      Serial.print("Unknow error at address 0x");
      if (address < 16)
      {
        Serial.print("0");
      }
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0)
  {
    Serial.println("No I2C devices found\n");
  }
  else
  {
    Serial.println("done\n");
  }
  delay(5000);

The scanner reports that no I2C devices are found

I have checked the wiring between the devices and all looks OK using the pins mentioned in the comments for each sketch and there is a common GND connection between them. Adding 10K pullup resistors to the SDA and SCL lines makes no difference

Running the scanner on either board with an I2C device attached finds the device and correctly reports its address, so I am confident that the correct pins are being used on both of them

Can anyone see anything obviously wrong either with the hardware setup or the sketches ?

You mention the ESP32 as a master, but the Wire.begin(99); statement sets it up as a slave with address 99.
Similarly, the Wire.begin(); statement for the ESP8266 sets it up as the master.

Have you read about I2C Slave mode for a ESP32 ?
Is it implemented ? If it is, can it be used in the same way as the Arduino ? Are there tutorials ?

There seems to be a dearth of information about I2C slave mode on ESPs. I have certainly seem mention of the lack of slave mode on the ESP8266 and I can't get it working on the ESP32 either. As a quick reality check I tried it on a pair of Nanos and it worked straight away

I will try again but as it happens I have used a serial connection instead of I2C where I wanted it

A serial connection will be much easier to program.
Alternatively ESP8266 / ESP32 offers a wireless protocol called ESP-NOW.
As long as you don't want to use WiFi-WLAN and ESP-NOW in parallel this would be an option too.

unfortunetely ESP-NOW can only be used mutually exclusive. Which means ESP-NOW or connecting to a WiFiRouter

best regards Stefan

I have read that if the same channel is used for Wifi and ESP-NOW, that they can work both. I have not tested it.

As long as you don't want to use WiFi-WLAN and ESP-NOW in parallel this would be an option too.

That is the very reason why I wanted to use I2C

Despite reading that it is possible I have not managed to get the two WiFi based solutions working at the same time

Where have you read it is possible ? I mean the ESP32 as I2C Slave.
As far as I know someone got it working, but not with the normal libraries. I don't know the current status for the ESP32, so perhaps they have implemented it.

This could help, but I have no personal experience (but I'm quite good at using Google). GitHub - gutierrezps/ESP32_I2C_Slave: I2C slave library for ESP32

It has a very modest version number of 0.1.0

Where have you read it is possible ? I mean the ESP32 as I2C Slave.

Sorry, but I don't remember and as it did not work I did not make a note

What I do remember is that it involved finding the channel number of the WiFi and forcing ESP NOW to use that. This is a likely candidate ESP32: ESP-NOW and Wi-Fi Web Server Dashboard (Arduino) | Random Nerd Tutorials and I will try it again as it is not clear when they updated the tutorial

Has using the ESP32 I2C API been considered? I2C Driver - ESP32 - — ESP-IDF Programming Guide latest documentation, bypassing, for the ESP32, the Arduino ESP32 core. I have used, in the Arduino IDE, both the I2C and SPI API in the Arduino IDE, both work. I prefer the SPI API with its duplex and background data transfers.

Has using the ESP32 I2C API been considered?

That's way deeper than I want or need to go at the moment