Unable to get ESP8266 AT codes to work from sketch

have a problem using AT commands via my Sketch.

I flashed the ESP-01 unit I have, and when connecting the unit directly to my RX and TX pins on the Arduino mega, I can use the AT commands via serial monitor. However as soon as I try to move the pins and use SoftwareSerial.h on any other pins, I can communicate with the module.

I have looked at examples, from really basic to full on web services, and none of them work, if I connect the RX and TX from the ESP to my Arduino in the correct locations as defined. The AT commands are not processed.

I have tried swapping the RX and TX connections, still with no results. I know the AT commands work, as I can manually enter them, just not using a sketch.

Below is one of the may examples I tried with no results at all.

    #include <SoftwareSerial.h>
    
    SoftwareSerial mySerial(2,3); //RX,TX
    
    void setup(){
      Serial.begin(115200);
      mySerial.begin(115200);
      delay(1000);
      mySerial.write("AT\r\n");
      long b=mySerial.read();
      Serial.println(b);
      delay(1000);
      mySerial.print("AT\r\n");
      b=mySerial.read();
      Serial.println(b);
      delay(1000);
      mySerial.println("AT\r\n");
      b=mySerial.read();
      Serial.println(b);
      delay(1000);
     
    }
    
    void loop(){
      if(Serial.available()>0){
        byte b=Serial.read();
        mySerial.println(b);
      }
      if(mySerial.available()>0){
        byte b=mySerial.read();
        Serial.println(b);
      }
    }

Looking at serial monitor I get these results.

09:32:47.346 -> -1
09:32:48.346 -> -1
09:32:49.331 -> -1

When I connect directly to my RX and TX pins I can run AT commands like below

    
    09:54:32.148 -> AT
    
    09:54:32.148 -> 
    09:54:32.148 -> OK
    09:54:39.175 -> AT+GMR
    
    09:54:39.175 -> AT version:1.1.0.0(May 11 2016 18:09:56)
    09:54:39.175 -> SDK version:1.5.4(baaeaebb)
    09:54:39.175 -> Ai-Thinker Technology Co. Ltd.
    09:54:39.175 -> Jun 13 2016 11:29:20
    09:54:39.175 -> OK

IIdon't know where I am going wrong here, for some reason it does not seem like my ESP-01 likes "Software Serial". I have 5 units all of them giving the same results. Showing me its something I am doing wrong. With all of this said, how do I get my AT commands to work from my sketch? do i have to reset the unit in setup, or is my code just completely wrong?

This is just testing, AT commands. If I get this to work I can add the rest of the code. Since there is no point in writing a entire sketch if I cant get the basic command to work.

read() returns -1 if nothing is available. you have to wait for the response

@tonystark67, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

===

115200 is high for SoftwareSerial; try 9600 and work your way up from there.

You should be forwarding the bytes with .write(b); because .println(b); will show the byte as a decimal number and send a newline character after each.

The Arduino MEGA has THREE spare hardware serial ports: Serial1, Serial2, and Serial3. Use one of them instead of Software Serial and your baud rate won't be a problem.