Bidirectional I2C communication between ESP32 and Arduino nano

Hello, I've had some luck using the I2C_anything Library to send multiple floating variables from an ESP32 Master to 2 slave arduino Nano's. In the example below, the first slave is sending AC current readings to the ESP.

Master ESP32 code;

#include <Wire.h>
#include<I2C_Anything.h>

float x1; 
float x2;
double c1;
float c2;

void setup()
{
  Wire.begin();
  Serial.begin(9600);

}

void loop(){
transmission();

}

void transmission(){
  Wire.requestFrom(0x08, 8);
  I2C_readAnything(x1);
  I2C_readAnything(c1);
  Serial.println("Data received from Slave1...!");
  Serial.println(x1, 2);
  Serial.println(c1, 2);
  //----------------------
  Wire.requestFrom(0x09, 8);
  I2C_readAnything(x2);
  I2C_readAnything(c2);
  Serial.println("Data received from Slave2...!");
  Serial.println(x2, 2);
  Serial.println(c2, 2);
  delay(100);
}

Slave Arduino 1

#include <Wire.h>
#include<I2C_Anything.h>
#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance
float x1;
double c1;
  
void setup()
{
  emon1.current(1, 111.1);
  Serial.begin(9600);
  Wire.begin(0x08);
  Wire.onRequest(sendEvent);
}

void loop()
{
 
  double Irms = emon1.calcIrms(1480);
  x1 = 19.36;
  c1 = Irms;
  
  delay(100);  
}

void sendEvent(int howMany)
{
  I2C_writeAnything(x1);
  I2C_writeAnything(c1);
}

that code was manipulated from here; [I2C] Master Code For Master Arduino Connected to 3 Slave Arduino Unos - Networking, Protocols, and Devices - Arduino Forum

However, I was wondering if there was any way to be able to send additional floating variables from the ESP32 back to the Slaves using I2C, so there is bidirectional communication? Perhaps there's another thread out there with someone achieving this that I could be linked to?

thank you!

I found this very helpful example Bidirectional I2C communication with the I2C_anything library - #5 by E40racer - Networking, Protocols, and Devices - Arduino Forum.

However, it only compiles for the ESP8266 board, and not the ESP32. I get the following error for the ESP32;

"undefined reference to `TwoWire::onReceive(void (*)(int))'"

It seems like the Wire.onReceive function doesn't work with the 32, but does with the 8266.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.