UART Communication ESP8266 - ArduinoUNO

Hello !
I think I’ve run into a problem with my project and I need a bit of guidance .
Im using an Arduino UNO to read data from some sensors and I’m sending that data to an ESP8266 through UART .
So something like :

#include <Arduino.h>

   #include <SoftwareSerial.h>

   SoftwareSerial espSerial(5, 6); // I made the pin 5 = Rx , 6 =Tx .

   espSerial.begin(9600);

   void loop() {

       espSerial.println(data);

   }

And on the ESP side, Im using the Rx/Tx pins and Im just reading from the serial :

Serial.readStringUntil('\n');

This is just an example , not exactly my code .
Anyway , my problem is that I need to receive data on my esp AND send some other data back to Arduino , at the same time .

Can I achieve this with UART ? Or do I need to change my aproach to I2C ?
And if so, can I get some guidance , please ?

ESP8266 is an IC.

Which module do you have? ESP-01 or something else?

.

I have the NodeMCU ESP8266 .

https://lastminuteengineers.com/esp8266-nodemcu-arduino-tutorial/

Anyway , my problem is that I need to receive data on my esp AND send some other data back to Arduino , at the same time .

What do you mean "at the same time"?

wickfm:
I have the NodeMCU ESP8266 .

https://lastminuteengineers.com/esp8266-nodemcu-arduino-tutorial/

Unless your sensors at 5V only, the module you have is actually more powerful than the Arduino Uno.

cattledog:
What do you mean "at the same time"?

Im sending some readings that Arduino is doing once every 3 seconds . But this can vary , because I've built a small OS scheduler to do that .So I can choose my interval , but its probably gonna be somewhere around 1-3 seconds .

On the other side , the ESP , where I have a small webpage built to showcase those values , I also have some buttons . I want to send commands when I press a button back to the arduino to activate a motor, or something .

For example : A button that when pressed will send "1" through my serial and my Arduino is ready at all time to read it and execute something

ieee488:
Unless your sensors at 5V only, the module you have is actually more powerful than the Arduino Uno.

Its not the first time people are saying that I dont even need the Arduino , but Im using it because im following a project requirements + the esp doesnt have enough analog pins ( but ya, this argument is not very good because can just buy those extenders or how do you call them )

I want to send commands when I press a button back to the arduino

The UART on the node mcu can send and receive serial data. Indeed, the name tells you that.

Universal Asynchronous Receiver/Transmitter.

cattledog:
The UART on the node mcu can send and receive serial data. Indeed, the name tells you that.

Universal Asynchronous Receiver/Transmitter.

I never said it cant. I wasnt sure because from what I tried, it didnt work .
Can you help me ? The code that sends the data from Arduino to ESP is posted . I tried the same technique to send back and it wasnt working .

I wasn't sure because from what I tried, it didn't work .
Can you help me ? The code that sends the data from Arduino to ESP is posted . I tried the same technique to send back and it wasn't working .

If you have the Arduino talking to the Node MCU you have done the hard part and must have solved all the 5v to 3.3v level shifting issues.

To send from the Node MCU back to the Arduino, you should be able to connect directly from the TX pin on the Node MCU to the RX pin (D0) of the Arduino and use Serial.begin() and Serial.print() on the Node MCU. Alternatively you can connect the TX1 pin (D4) on the Node MCU to the RX pin of the Arduino and use Serial1.begin() and Serial1.print() on the Node MCU. Grounds should be connected.

Node MCU code

void setup() {
  Serial1.begin(115200);
  Serial.begin(115200);
}

void loop() {
  Serial1.println("Hello World");//connect D4 of Node MCU to Arduino RX
  Serial.println("Hello World"); //connect TX of Node MCU to Arduino RX
  delay(1000);

}

Arduino code reads "Hello World"

void setup() {
  Serial.begin(115200);
  Serial.println("Starting");
}

void loop() {
  char buffer[20] = "";
  if (Serial.available())
  {
    Serial.readBytesUntil('\n', buffer, 20);
    Serial.println(buffer);
  }
}

cattledog:
Arduino code reads “Hello World”

Are you sure this is working ? Because I’ve tried both combination and no luck. Maybe I’m doing something wrong, I dont know.
Can you give me a code that sends and also recieves for Arduino/ESP 8266 ?
So far I was using SoftwareSerial to send from Arduino to ESP and I was using pins (5,6) connected to Rx/Tx of the ESP . Maybe we can keep this for the code that sends back to Arduino

What I’ve tried :

Arduino Code :

#include <Arduino.h>
#include <SoftwareSerial.h>

SoftwareSerial comm(5,6);


void setup() {
  comm.begin(9600);
}

void loop() {
  comm.println("From Arduino"); // send to ESP

  char buffer[20] = "";
  if (Serial.available())
  {
    Serial.readBytesUntil('\n', buffer, 20);
    Serial.println(buffer); // recieve from ESP
  }

  delay(1000);
}

ESP code :

#include <Arduino.h>

void setup()
{
  Serial1.begin(9600);

}

void loop()
{
  Serial1.println("From ESP"); // send to Arduino 

  char buffer[20] = "";
  if (Serial.available())
  {
    Serial.readBytesUntil('\n', buffer, 20);
    Serial.println(buffer); // recieve from Arduino 
  }
  delay(1000);
}

Are you sure this is working ? Because I've tried both combination and no luck. Maybe I'm doing something wrong, I dont know.

The previously posted code worked with a Wemos D1 and an Arduino Uno. This should be equivalent to the Node MCU and a Nano.

So far I was using SoftwareSerial to send from Arduino to ESP and I was using pins (5,6) connected to Rx/Tx of the ESP . Maybe we can keep this for the code that sends back to Arduino

You should be able to connect the ESP TX pin to the software serial RX pin of the Arduino.

Can you give me a code that sends and also recieves for Arduino/ESP 8266 ?

I'm not certain of what you are asking for?

I would recommend that you review Robin2's tutorial on Serial Input Basics. It will help you with code to send and receive.

cattledog:
I'm not certain of what you are asking for?

Basically , 2 different projects ( because I use 2 diff boards ) that talks with each other .
My arduino sends sensor readings to the ESP once every 3 seconds through SoftwareSerial , and in the ESP code I just simply read like

Serial.readStringUntil('\n');

Now I want to send some data from the ESP back to the Arduino .
Basically I'm confused. Will the ESP send data with ?

Serial.println("1")

Will the ESP send data with ?

Code: [Select]
Serial.println("1")

Yes, it should, with the TTL output on the TX pin of the Node Mcu.

However the code you posted above uses

Serial1.begin(9600);

In this case output will be on D4 (according to the pin out diagrams I see) which is TX1.

Do you have a scope to look at what is going on at the TX pin of the node MCU. Because of the 3.3v to 5v (node MCU > Arduino) issue, there is some possibility that the HIGH and LOW signal levels are not at the threshold levels for the Arduino.

Are the grounds of the two devices connected?

cattledog:
Are the grounds of the two devices connected?

Yes.
I've manage to send data from ESP to Arduino using the normal Rx/Tx pins . Now my problem is just to send and receive , without the 2 channels to interfere . Im thinking something like a handshake system . To send a signal saying something like * Arduino talks , ESP listen * , then vice versa. I dont really know how to implement it

I am very unclear on what you can and can not do successfully..

I've manage to send data from ESP to Arduino using the normal Rx/Tx pins .

The code that sends the data from Arduino to ESP is posted . I tried the same technique to send back and it wasn't working .

In your most recently posted code, the software serial on the Arduino sends to the node mcu TX/RX. The node mcu is sending back on Serial1. Is D4 connected to the software serial RX?

Once you sort out the hardware connections, the software will be straightforward.

I'm thinking something like a handshake system

Have you reviewed the serial input basics tutorial? I strongly encourage you to follow the methods of that tutorial.

The receiving code is always listening, along with doing any other activities in the sketch, and when a message is received, it sets a flag (newData = true) for you to act on. One action which is certainly possible is to send a reply.

In essence the newData variable being true or false is a boolean control which works like a handshake.

cattledog:
I am very unclear on what you can and can not do successfully..

I change my connections . Now I'm simply using Rx/Tx of each boards to communicate .
From UNO to ESP is working ok .

Serial.println(data); // to send
String a;
a = Serial.readStringUntil('\n'); // to receive on ESP

Now I'm just trying to do the same thing , but backwards , from ESP to UNO ( just this , im not sending any data to the esp anymore ) .
And I'm using the same principle to send , but I'm using example #1 from this to read .

And well ,it's not working . I'm either getting garbage values or simply nothing . I made sure both serials are working at the same baud rate ( 9600) and all the connections are correct .
Note ( I dont if it's important ) : both boards are connected to a single PC . I usually disconnect one to upload the other one , and vice-versa . Using PlatformIO

Now I'm simply using Rx/Tx of each boards to communicate .
From UNO to ESP is working ok .

I usually disconnect one to upload the other one , and vice-versa

If you are going to use the hardware serial connection, my recommendation is to remove the connecting wires completely when you load the programs. I'm not clear what you mean by "disconnect" and if that refers to the usb connection to the computer or the rx/tx connection between the units. The single pc is not a problem and in fact it helps that the usb grounds provide a common ground at the pc.

Now I'm just trying to do the same thing , but backwards , from ESP to UNO (just this , im not sending any data to the esp anymore ) .
And I'm using the same principle to send , but I'm using example #1 from this to read .

Yes, using the example from the serial basics tutorial is the correct thing to do on the receiver. The methods of that tutorial are rock solid.

What is the sending code? It needs to have some delay or periodic sending and can't just be outputting freely in loop() with no timing.

If you are making a unidirectional connection with send from the esp to receive on the uno its best to not have a connection between the uno tx and the esp rx.

Do you have any better results with the esp using Serial1 with output on D4?

Using PlatformIO

I use the Arduino IDE and have no idea if your environment can be an issue.

cattledog:
If you are making a unidirectional connection with send from the esp to receive on the uno its best to not have a connection between the uno tx and the esp rx.

Yes, Im doing that.

Arduino :

#include <Arduino.h>

// Example 1 - Receiving single characters

char receivedChar;
boolean newData = false;

void recvOneChar();
void showNewData();

void setup()
{
  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
}

void loop()
{
  recvOneChar();
  showNewData();
}

void recvOneChar()
{
  if (Serial.available() > 0)
  {
    receivedChar = Serial.read();
    newData = true;
  }
}

void showNewData()
{
  if (newData == true)
  {
    Serial.print("This just in ... ");
    Serial.println(receivedChar);
    newData = false;
  }
}

ESP :

#include <Arduino.h>

void setup()
{
  Serial.begin(9600);
  // Serial1.begin(9600);
}

void loop() { 
  // Serial1.println("1"); // SENDING TO UNO
  Serial.println("1"); // SENDING TO UNO
  delay(500);
}

I’ve used both , but no succes . On 3 different arduino/esp boards aswell .
Does the Rx pin on Arduino have to be turned on when I’m connecting the wires to send ? Because in my case,is not .

Anyway , thanks for sticking with me and trying to solve haha . Its pretty stupid , but it just not working

When I run the two programs just posted, I see this as expected on the uno. The two blanks are the cr/lf in the message which are not printable characters.

This just in ... 1
This just in ... 
This just in ...

Does the Rx pin on Arduino have to be turned on when I'm connecting the wires to send ? Because in my case,is not .

What does this mean? Serial.begin() should be sufficient.

Anyway , thanks for sticking with me and trying to solve haha . Its pretty stupid , but it just not working

Perhaps it is worth exploring the Arduino IDE as compared to PlatformIO. I would think that the #include Arduino.h would be adequate, but I really don't know anything about PlatformIO.