I want to send data between an ESP-32 (NODEMCU-32S) and an Arduino Uno but I haven't found any source on how to do that, so I tried this code:
ESP-32 code:
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin(21, 22); /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
}
void loop() {
Wire.beginTransmission(8);
Wire.write(3); /* sends hello string */
Wire.endTransmission(); /* stop transmitting */
Wire.requestFrom(8, 18); /* request & read data of size 13 from slave */
while (Wire.available()) {
char c = Wire.read();
Serial.print(c);
}
}
And Arduino code:
#include <Wire.h>
void setup() {
Wire.begin(8); /* join i2c bus with address 8 */
Wire.onReceive(receiveEvent); /* register receive event */
Wire.onRequest(requestEvent); /* register request event */
Serial.begin(9600); /* start serial for debug */
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
void receiveEvent(int howMany) {
while (0 < Wire.available()) {
int c = Wire.read(); /* receive byte as a character */
Serial.print(c); /* print the character */
}
}
// function that executes whenever data is requested from master
void requestEvent() {
Wire.write("hello esp");
}
But I found that no data was sent.
I connected pin 21 to A4 and 22 to A5 directly.
I cannot find where the problem is. Also, I don't know whether the device address is correct or not, since when I tried an I2C scanner it didn't discover any device.
Also, I hope that there is a similar method to have two-way UART communication between the Arduino and ESP as I found none since I need both chips to send data to each other.