I2C send variables between Mega and ESP32

Hello. I'm trying to send variables from the Mega one ESP32. I thought that I can use the SerialTransfer library but I'm getting some weird results by running the example code.

For the Master (Mega):

#include "I2CTransfer.h"


I2CTransfer myTransfer;

struct STRUCT {
  char z;
  float y;
} testStruct;

char arr[] = "hello";


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

  myTransfer.begin(Wire);

  testStruct.z = '$';
  testStruct.y = 4.5;
}


void loop()
{
  // use this variable to keep track of how many
  // bytes we're stuffing in the transmit buffer
  uint16_t sendSize = 0;

  ///////////////////////////////////////// Stuff buffer with struct
  sendSize = myTransfer.txObj(testStruct, sendSize);

  ///////////////////////////////////////// Stuff buffer with array
  sendSize = myTransfer.txObj(arr, sendSize);

  ///////////////////////////////////////// Send buffer
  myTransfer.sendData(sendSize);
  delay(1000);
}

Slave (ESP32):

#include "I2CTransfer.h"


I2CTransfer myTransfer;

struct STRUCT {
  char z;
  float y;
} testStruct;

char arr[6];


/////////////////////////////////////////////////////////////////// Callbacks
void hi()
{
  // use this variable to keep track of how many
  // bytes we've processed from the receive buffer
  uint16_t recSize = 0;

  recSize = myTransfer.rxObj(testStruct, recSize);
  Serial.print(testStruct.z);
  Serial.print(testStruct.y);
  Serial.print(" | ");

  recSize = myTransfer.rxObj(arr, recSize);
  Serial.println(arr);
}

// supplied as a reference - persistent allocation required
const functionPtr callbackArr[] = { hi };
///////////////////////////////////////////////////////////////////


void setup()
{
  Serial.begin(115200);
  Wire.begin(0);

  ///////////////////////////////////////////////////////////////// Config Parameters
  configST myConfig;
  myConfig.debug        = true;
  myConfig.callbacks    = callbackArr;
  myConfig.callbacksLen = sizeof(callbackArr) / sizeof(functionPtr);
  /////////////////////////////////////////////////////////////////
  
  myTransfer.begin(Wire, myConfig);
}


void loop()
{
  // Do nothing
}

On the serial port of the esp32 I was expecting to get the following line:
$4.5 | hello

instead I'm getting this
001

Can anyone advise me firstly on how to get the examples sketched work and secondly how to send variables from the Mega to the ESP32.

I believe an ESP can only be an I2C-M.

Why not use asynchronous ("Serial") ?

1. Connect 5V-MEGA and 3.3V-ESP32 as per Fig-1 using level shifter.


Figure-1:

2. Upload the following sketches and check that ESP32 is found as I2C-Slave.
I2C-Master-MEGA Sketch:

#include<Wire.h>

void setup() 
{
  Serial.begin(9600);
  Wire.begin();
  //--------------
  Wire.beginTransmission(0x23);
  byte busStatus = Wire.endTransmission();
  if(busStatus != 0)
  {
    Serial.print("ESP32 is not found!");
    while(1); //wait for ever
  }
  Serial.println("ESP32 is found.");
}

void loop() {}

I2C-ESP32-Slave Sketch:

#include<Wire.h>

void setup() 
{
  Serial.begin(9600);
  Wire.begin(0x23);
}

void loop() 
{

}

3. If Step-2 is ok, the add codes with both sketches to send "Hello" from Master to Slave's SM2 at 1-sec interval.

4. If Step-4 is ok, then add codes with both sketches to receive 0x1234 from Slave and show it on SM1 at 1-sec interval.

5. If Step-4 is ok, then add codes with both sketches to receive a struct type data from Slave.

What happens when you run the I2C scanner. Also where are the pull up resistors on the ESP32 side? Generally the default resistor value is 4.7K (5V) or 3.3K (3V3) to force about 1 ma through the bus. This helps reduce its sensitivity to noise.

Have you tried yourself to see that ESP32 does not work as I2C-Slave?

The Arduino Mega board has three spare hardware serial/UART ports (Serial1, Serial2, Serial3)
The ESP32 has a spare serial/UART port (Serial2).
You can connect them via a level shifter with serial/UART ports.

The SerialTransfer library does not solve problems that you may encounter with the I2C bus.

Have you used your sketch in Post No.3 ?

To the extent that it is possible, it's no walk in the park --
Inter-Integrated Circuit (I2C) - ESP32 - — ESP-IDF Programming Guide latest documentation (espressif.com)

Yes! This is the result:

ESP32 is found.

image

Transfer any data?

Received from Slave: 36
Received from Slave: 36
Received from Slave: 36
Received from Master: 12
Received from Master: 12
Received from Master: 12

Master-MEGA Sketch

#include<Wire.h>

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  //--------------
  Wire.beginTransmission(0x23);
  byte busStatus = Wire.endTransmission();
  if (busStatus != 0)
  {
    Serial.print("ESP32 is not found!");
    while (1); //wait for ever
  }
  Serial.println("ESP32 is found.");
}

void loop()
{
  Wire.beginTransmission(0x23);
  Wire.write(0x12);
  Wire.endTransmission();
  //--------------------------
  Wire.requestFrom(0x23, 1);
  byte y = Wire.read();
  Serial.print("Received from Slave: ");
  Serial.println(y, HEX);
  delay(1000);
}

Slave-ESP32 Sketch:

#include<Wire.h>
volatile bool flag = false;
byte y;

void setup()
{
  Serial.begin(9600);
  Wire.begin(0x23);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(sendEvent);
}

void loop()
{
  if (flag == true)
  {
    Serial.print("Received from Master: ");
    Serial.println(y, HEX);
    flag = false;
  }
}

void receiveEvent(int howMany)
{
  y = Wire.read();
  flag = true;
}

void sendEvent()
{
  Wire.write(0x36);
}

Apparently it's a lot easier than they let on in the espressif link.

Arduino Dev Team has done excellent job to justfy the name "ESP32 Arduino."

Vs. Nano ESP32?

I don't know who maintains the libraries - or if they're different / same, some mix.

1 Like

Also, Arduino UNO R4 WiFi.

I have everything wired up as shown in your diagram. I have run the code to provided and everything works fine.
From here I am lost.I don't have the coding skill for the next steps.
I only need to send some variables from the Mega to the esp32. I don't need to send data from the esp32 to the Mega.

What type of data you want to send from MEGA to ESP32 -- give examples? You may follow the sketches of post #10.

I have several temperature and humidity sensors and I want to see data from Mega (where the sensors are connected) to the Esp32.

On the SM of ESP32?

What type of temperature and humidity sensors you have -- post their pictures?

Give search to net saying -- tutorial on sending floatt type data from MEGA to ESP32 using I2C Bus. Collect codes from there and integrate them with the sketches of post #10.

Maybe also look here (and similar) for issues related to cross platform transfer of data which can raise problems like data type representation, byte alignment, endienness etc. Data handling when mixing 8-bit and 32-bit boards

I have 5 DHT22 sensors wired to the Mega and I want to sent the data to the ESP32 so I can post it to the internet. this is why I need to send the data as variables so I can compose, on the ESP32, a string to post it on the internet. This is part of a larger project so I can't wire the sensors directly to the ESP32.