Read ESP8266-01 serial print through Arduino on IDE Serial Monitor

Hi, I would transmit ESP8266-01 serial.println messages to Arduino in order to output them on serial monitor and/or execute code depending on the message arrivated to Arduino. I tried with

#include <SoftwareSerial.h>      //Including the AltSoftSerial library
SoftwareSerial esp (8,9);

void setup() {
Serial.begin(9600);
esp.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println(esp.read());
}

and

#include <AltSoftSerial.h>      //Including the AltSoftSerial library
AltSoftSerial esp;

void setup() {
Serial.begin(9600);
esp.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println(esp.read());
}

without success... Any suggestions?

Are you certain that the ESP8266 is actually sending anything?

If so, are you certain the the ESP8266 is communicating at 9600 baud?

Yes, I am!

Ah, I forgot to say that it prints "-1" in loop!

This is the code updated on the ESP :

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

/* Set these to your desired credentials. */
const char *ssid = "APOG";
const char *password = "98761234";

ESP8266WebServer server(80);

/* Just a little test message.  Go to http://192.168.4.1 in a web browser
  connected to this access point to see it.
*/
void handleRoot() {
 server.send(200, "text/html", "<h1>You are connected</h1>");
}

void setup() {
 delay(1000);
 Serial.begin(115200);
 Serial.println();
 Serial.print("Configuring access point...");
 /* You can remove the password parameter if you want the AP to be open. */
 WiFi.mode(WIFI_AP);
 WiFi.softAP(ssid, password);

 IPAddress myIP = WiFi.softAPIP();
 Serial.print("AP IP address: ");
 Serial.println(myIP);
//  server.on("/genericArgs", handleGenericArgs); //Associate the handler function to the path
server.on("/specificArgs", handleSpecificArg);   //Associate the handler function to the path

server.begin();                                       //Start the server
Serial.println("Server listening");    

}

void loop() {
 server.handleClient();
}

void handleSpecificArg() { 

String message = "";

if (server.arg("Temperature")== ""){     //Parameter not found

message = "Temperature Argument not found";

}else{     //Parameter found

message = "Temperature Argument = ";
message += server.arg("Temperature");     //Gets the value of the query parameter

}

server.send(200, "text/plain", message);          //Returns the HTTP response
Serial.println(message);

}

I tried to connect the esp to pc via USB adaptor and it prints!

I also tried to edit the baud rate to 115200 without success...!

It's definitely not going to work if you have the ESP8266 configured to run at 115200 but the other board running at 9600. The baud rates must match. Also, SoftwareSerial library won't work reliably at 115200 so 9600 is a safe rate to get things working.

How do you have things wired up?

void loop() {
  // put your main code here, to run repeatedly:
Serial.println(esp.read());
}

You get -1 because you are reading the serial port even if there is nothing in the buffer. The serial read function returns -1 for an empty receive buffer. To check if there is something in the buffer to read use the available() function.

Here is a great tutorial on reading from a serial port.

@pert True, you are right, sorry! I wired:

Arduino Esp

8 Rx
9 - 0.2k - 0.2k - Tx
GND - external GND - GND

Esp chpd and VCC : external 3.3V - 1A

@groundFungus I have already read it but I could not understand how to apply it to this code :frowning:

Edit: in fact, printing esp.available() it shows "0"

@groundFungus You were right! I edited the code uploaded to Arduino in this way:

#include <AltSoftSerial.h>      //Including the AltSoftSerial library
AltSoftSerial esp;

const byte BUF_SIZE = 128;
char receivedChars[BUF_SIZE];   // an array to store the received data

boolean newData = false;

void setup() {
  Serial.begin(9600);
  esp.begin(9600);
}

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

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (esp.available() > 0 && newData == false) {
        rc = esp.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= BUF_SIZE) {
                ndx = BUF_SIZE - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.println(receivedChars);
        newData = false;
    }
}

And now it works! Thanks!!

Cricri:
Arduino Esp

8 Rx
9 - 0.2k - 0.2k - Tx
GND - external GND - GND

This is wrong. You need to make the connections RX-TX, TX-RX but the above is RX-RX, TX-TX.