0

i am using NodeMCU (ESP8266) to communicate with another NodeMCU (ESP8266) using Serial Communication. Because i am using ESP-NOW to communicate and ESP-NOW and WiFi can not work together very well so trying with this serial communication option.

Here is how i connected the pins:

   Slave ->   Master
    D1    ->   D2
    D2    ->   D1
    GND   ->   GND

Here is how my sketch looks like:

Sender.ino

#include <Wire.h>

void setup() {
  Serial.begin(115200);
  Wire.begin(D1, D2); /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
}

void loop() {
  // put your main code here, to run repeatedly:
   Wire.beginTransmission(5); /* begin with device address 8 */
   Wire.write("Hello ESP 2");  /* sends hello string */
   Wire.endTransmission();    /* stop transmitting */
  
   Wire.requestFrom(5, 4); /* request & read data of size 13 from slave */
   while(Wire.available()){
      char c = Wire.read();
    Serial.print(c);
   }
   Serial.println();
   delay(1000);
}

Receiver.ino

#include <Wire.h>

void setup() {
 Wire.begin(5);            
 Wire.onReceive(receiveEvent); /* register receive event */
 Serial.begin(115200);           /* 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()) {
    char c = Wire.read();      /* receive byte as a character */
    Serial.print(c);           /* print the character */
  }
 Serial.println();             /* to newline */
}

But the communication is not working at all, not sure where i am wrong. It can code wise or may be via connection wise or it might be possible that NodeMCU to NodeMCU connection is not possible at all!

Thank you!

9
  • Your code does not reflect your hardware setup. I2C (Wire) communication over SDA/SCL pins is not the same as serial communication over crossed RX/TX pins. First you should decide what you really want. Serial communication or I2C. Then search the internet for examples. As you already use Serial for debugging (SerialMonitor?), you might want to use I2C to connect the two ESPs together. But that's not that easy, I2C uses a special electrical trasmission mode. You can not simply connect the pin pairs crossed to each other. You also must not use the same address for both devices. – Peter Paul Kiefer Jan 30 at 15:18
  • You also used Wire.begin(D2,D3) for Sender and Wire.begin(5) for Receiver. I suspect that's wrong. – Peter Paul Kiefer Jan 30 at 15:20
  • @Juraj actually i have send send data to my MQTT server connected in my LAN network. Which need wifi access and with ESP-NOW i can not do both. – user3201500 Jan 30 at 15:28
  • @Peter can you suggest how can i fix this? – user3201500 Jan 30 at 15:28
  • esp8266 Wire library doesn't work as slave – Juraj Jan 30 at 15:30
1

As already stated, your code uses the I2C interface (via the Wire library) and not the Serial interface (which is an Arduino term for UART). They work fundamentally different.

As you only communicate between 2 devices, which you both can program yourself. I would ditch the I2C interface. It doesn't give you any advantage here, but some disadvantages (for example, that you can't have real bidirectional communication, as the slave cannot do anything by its own, only, if the master asks for it).

Instead you should use a Serial (UART) interface. As the NodeMCU only has 1 full hardware Serial interface, which you already use for debugging, you could use the ESPSoftwareSerial library to create an extra Serial interface via software. It has limitations (in how many interfaces can listen at a time and how fast the communication can go), but most likely this will solve your problem. Also Serial is easier for beginners, than I2C.

2
  • So can you please suggest what is much better to process faster and easy to do communication two way? – user3201500 Jan 31 at 7:07
  • Also is there any example you can suggest with code and wire diagram for nodemcu to nodemcu so i can have better understanding – user3201500 Jan 31 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.