0
votes

I want to communicate between Arduino Uno R3 and Esp8266 Esp-01. in this case, I want to communicate or want to send a data (string using serial software) from esp8266 esp-01 to arduino uno.

the problem is, how to insert RX and TX pins in softwareserial settings, I have tried using the sample code that has been provided, but it can't.

my code as below

FOR ESP8266 ESP-01

#include <SoftwareSerial.h>
SoftwareSerial mhaUno(12, 12, false, 256);

void setup() {
  Serial.begin(9600);
  pinMode(0, OUTPUT);
}

void loop() {
  Serial.write("hello from esp");
  delay(1000);

  String IncomingString = "";
  boolean StringReady = false;

  while(mhaUno.available()){
    IncomingString = mhaUno.readString();
    StringReady = true;
  }

  if(StringReady) {
    if(IncomingString == "hello from uno") {
      digitalWrite(0, HIGH);
    } else {
      digitalWrite(0, LOW);
    }
  }
}

FOR ARDUINO UNO R3

#include <SoftwareSerial.h>
SoftwareSerial esp8266(2,3); //RX, TX

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {
  Serial.write("hello from uno");
  delay(1000);

  String IncomingString = "";
  boolean StringReady = false;

  while(esp8266.available()){
    IncomingString = esp8266.readString();
    StringReady = true;
  }

  if(StringReady) {
    if(IncomingString == "hello from esp") {
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);
    }
  }
}

can you help me find a problem in my case?

thank you ^_^

1

1 Answers

0
votes

That code looks like this Instructable, though there are many snippets out there. Before addressing the specifics, a few corrections:

  1. Physical connection: You should have a voltage divider going from the Uno's software-defined TX to the ESP's software-defined RX (Uno has 5V high logic level, while ESP is 3.3V and works better when its pins are not driven much above that).
  2. Variables names are normally all lowercase - upper identifies classes like Serial, SoftwareSerial, String.

Apart from that, I would try to replicate that example's configuration:

  1. Use mhaUno.write() and esp8266.write(), since each device's software-defined pins are presumably connected to the other's software-defined pins, not the other's default pins. Currently, the custom TX pins are unused.
  2. If this is not the case and you really do mean to send to the Uno's SoftwareSerial from the ESP's default Serial, then try matching the baud rates for both libraries (as the example does).
  3. Otherwise, continuing from 3, use distinct pins for TX and RX on the Uno.
  4. Documentation for the SoftwareSerial constructor does not mention a fourth argument on the constructor, just SoftwareSerial(rxPin, txPin, inverse_logic). Nor does the example; try without it
  5. A single unexpected character on the serial line will disable your notification LED; consider requiring a specific string to turn it off, so that an innocuous command like esp8266.write("hello from esp\n") (or println()) leaves it on. Better yet, use Serial to print what SoftwareSerial receives to the computer terminal, instead of relying on blinks.

You can find similar questions on the Arduino StackExchange.