Serial Communication From Arduino to ESP8266

Hi All,

I know this question must have been asked many times and it should be very basic but for some reason, I could not solve the issue, hence the post.

General info:
I have an Arduino Uno and I would like to create serial communication between the Arduino and ESP8266 board. I am using an ESP-01 adapter which takes care of the voltage shifts and allows me to use the 5v instead of the 3v required.

The ESP board seems to work without any issue, I have uploaded a very simple sketch which uploads some data to ThingSpeak servers. here is the code for it.

#include <ESP8266WiFi.h>


void setup() {
  Serial.begin(9600);
  Serial.println();

  WiFi.begin("AP Name", "Pass");


  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());

  randomSeed(50);
  
  
}

void loop() {


  
  const char* host = "api.thingspeak.com";
  String tmpr ;
  String humid ;

while(Serial.available())
{
  
  if(Serial.find("Data"))
  {
    WiFiClient client;
    if (client.connect(host, 80))
    {
      //Serial string should be at this format "Data:Temp:Humid"
      String incommingStr = Serial.readString();
      incommingStr.trim();
      Serial.println("String is : " + incommingStr);
      int inx = incommingStr.indexOf(":");
      Serial.println("inx = " + String(inx));
      int inx2 = 0;
      if(inx>=0)
      {
        inx2 = incommingStr.lastIndexOf(":");
        Serial.println("inx2 = " + String(inx2));
        tmpr = incommingStr.substring(inx+1,inx2);
        humid = incommingStr.substring(inx2+1);
        }
      

      Serial.println(String(tmpr) + " : Temp");
      Serial.println(String(humid)  + " : Humid");
      
      String postStr="update?api_key=XXXXX&field1=" + String(tmpr) + "&field2=" + String(humid);
      String apiKey= "XXXXX";

      Serial.println("We are connected to the host ...");// we are connected to the host!
        client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(postStr.length());
        client.print("\n\n");
        client.print(postStr);
        
              Serial.println("Response");
              while (client.connected())
              {
                if (client.available())
                {
                  String line = client.readStringUntil('\n');
                  Serial.println(line);
                }
              }
              client.stop();
              Serial.println("\n[Disconnected]");
    }
    else
    {
      Serial.println("Connection to the host failed...");// connection failure
      client.stop();
    }
  }
}
  
}

The Problem:

This code works without any issue as long as I am connecting the ESP board to a USB port on my computer using a USB adapter and sending serial string by a serial monitor application. If I connect the board to the Arduino board, I can see the ESP output messages in the serial monitor that monitors the serial output of the Arduino. (the massages are showing it is connected to my WiFi network successfully) But I can not send any thing either using the serial monitor or in the code from Arduino to the ESP.

Here is the code that I have on the Arduino Side which uses SoftwareSerial library to communicate.

#include <SoftwareSerial.h>

SoftwareSerial ESPserial(2, 3); // RX | TX

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  ESPserial.begin(9600);

  ESPserial.println("Data:24:45"); //this is just a test but nothing seems to be sent to the ESP board
  Serial.println("Started....");

}

void loop() {
  // put your main code here, to run repeatedly:
    while(ESPserial.available())
    {
       Serial.write( ESPserial.read()) ;
      }
    while(Serial.available())
    {
      ESPserial.write( Serial.read() ); // THIS LINE DOES NOT SEEM TO WORK
      }

}

I am very new to C++ programing and Arduino and would appreciate any suggestion.

Regards,

If you see the output from the ESP sketch on the Serial Monitor, it is because the ESP is connected to the hardware serial pins on the Arduino.

Using SoftwareSerial on the Arduino, to talk to pins that the ESP is not connected to is rather pointless.

@ehsan_ro, you need to post a simple diagram showing how you have everything connected. It does seem, as @PaulS has surmised, that you have something wrong.

...R

PaulS:
If you see the output from the ESP sketch on the Serial Monitor, it is because the ESP is connected to the hardware serial pins on the Arduino.

Using SoftwareSerial on the Arduino, to talk to pins that the ESP is not connected to is rather pointless.

PaulS: Thanks for your prompt response.
The TX and RX pins on the ESP Adapter are connected to Pins 2 and 3 on the Arduino port, not to the hardware serial pins (i.e., 0, and 1).

Robin2:
@ehsan_ro, you need to post a simple diagram showing how you have everything connected. It does seem, as @PaulS has surmised, that you have something wrong.

...R

Robin2: thanks for your response.
It is a very simple setup.

TX from ESP Adapter is connected to Arduino's Pin 2
RX from ESP Adapter is connected to Arduino's Pin 3

This is the adapter that I have used

The VCC and GRD from the ESP Adapter are connected to a separate 5 v source.
Hope this clarifies the issue.

it works if you send Data:24:45 from serial monitor to esp8266 over usb adapter, but the sketch in Uno doesn't passthru the response when you send the same string from the sketch?
Or the data sent from the sketch in setup() are processed, but then you want to send next string from Serial Monitor and it is not processed?

Juraj:
it works if you send Data:24:45 from serial monitor to esp8266 over usb adapter, but the sketch in Uno doesn't passthru the response when you send the same string from the sketch?

Correct. using esp8266 over usb adapter works but Uno does not pass through. Although I can see the messages from the ESP to the UNO.

Juraj:
Or the data sent from the sketch in setup() are processed, but then you want to send next string from Serial Monitor and it is not processed?

No. Neither data from the setup() nor from Serial Monitor (through UNO) work.

Thanks in advance.

check the wire and connections.

how do the esp8266 and uno powerup? can't be that the string from Uno sketch is sent too early?

Juraj:
check the wire and connections.

how do the esp8266 and uno powerup? can't be that the string from Uno sketch is sent too early?

Juraj: Thanks for your responses.

The wiring seem to be OK.
The Arduino is powered through the USB port and the ESP is powered separately via a bench supply.

Regardless of when I send the string from UNO to ESP, the message does not seem to go through.

Thanks

connect ground between uno and esp8266

ehsan_ro:
Juraj: Thanks for your responses.

The wiring seem to be OK.
The Arduino is powered through the USB port and the ESP is powered separately via a bench supply.

Regardless of when I send the string from UNO to ESP, the message does not seem to go through.

Thanks

connect ground between uno and esp8266

I just tried this. It did not solve the issue.
It also messed up the serial communication from ESP to Arduino which was working previously!

I test your sketches now and it doesn't work.

Edit: it works

Juraj:
I test your sketches now and it doesn't work.

Edit: it works

Did you connected the GRDs between Arduino and ESP?

ehsan_ro:
Did you connected the GRDs between Arduino and ESP?

of course, Serial needs common ground

Juraj:
of course, Serial needs common ground

Thanks Juraj for your great help.

This solved the issue.