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
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.
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.
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.
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 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.