Can we use the same code for standard Arduino library, like wire library, for ESP32 and ESP8266, or do we have to do something different?
Code using the Arduino Wire library is generally portable between processors, but there are pitfalls to watch for. One that I'm aware of is a difference in the Wire libraries for the ESP32 and ESP8266. The TwoWire::begin() method for the ESP32 has prototype:
bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency);
whereas for the ESP8266 it is:
void TwoWire::begin(int sda, int scl, uint8_t address);
So, a call to begin() using the ESP#2 library will set the clock using the third parameter, whereas on the ESP8266 it will truncate the third parameter and use it as an address.
If you wish code to be portable between the ESP32 and ESP8266, then the fix for this particular issue is to call
TwoWire::begin( my_SDA_pin, my_SCL_pin ) (specifying only two parameters:the SDA and SCL pins), and use a separate call to
TwoWire::setClock( my_clock_speed ) to set the clock.
There may be other differences I'm not aware of...