Sending AT command with code gives no result

Hi, I’m trying to programmatically send an AT command to my wifi module (ESP8266). It all worked fine when I manually send AT commands to the wifi module through the Serial Monitor. But I’m having difficulties doing this in code.

This is what I have:

#include <SoftwareSerial.h>

SoftwareSerial esp(10, 11); // RX, TX

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

    test();
}


void loop() { }

void test()
{
    esp.println("Sending AT command");
    esp.println(sendATcommand("AT", 2000));
}

String sendATcommand(const char *toSend, unsigned long milliseconds) {
  String result;
  Serial.print("Sending: ");
  Serial.println(toSend);
  esp.println(toSend);
  unsigned long startTime = millis();
  Serial.print("Received: ");
  
  while (millis() - startTime < milliseconds) {
    if (esp.available()) {
      char c = esp.read();
      Serial.write(c);
      result += c;  // append to the result string
    }
  }
  
  Serial.println();  // new line after timeout.
  return result;
}

It should send a simple AT command and I expect an OK response. But I’m not receiving anything. All I see in the Serial monitor is this:

Sending: AT
Received:

What am I doing wrong?

You are printing the result to the ESP, not to Serial.

At this point, you really shouldn't be using AT commands. I'd recommend looking into programming the ESP directly:
A Beginner's Guide to the ESP8266 & How to flash the ESP8266 using an Arduino UNO

Pieter

PieterP:
You are printing the result to the ESP, not to Serial.

At this point, you really shouldn’t be using AT commands. I’d recommend looking into programming the ESP directly:
A Beginner’s Guide to the ESP8266 & How to flash the ESP8266 using an Arduino UNO

Pieter

Do you mean this line?

esp.println(sendATcommand("AT", 2000));

If I change that to this:

Serial.println(sendATcommand("AT", 2000));

Then I get the same result.

I’m also looking into programming the ESP directly. But that is still a bit to advanced for me. Still reading up on that. In the mean time I’d like to get this to work using AT commands. This is just for learning purposes.

Just remove the println around sendATcommand.

How do you have it wired up? Did you connect TX to pin 10 and RX to pin 11? Are you using a level shifter on the RX line?

Yes I’m sure it’s wired up correctly. Just checked it again.

I changed my code to something simple like this:

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

void setup() { 
  // Open serial communications and wait for port to open:
  Serial.begin(9600); 
  while (!Serial) {
  ; // wait for serial port to connect. Needed for native USB port only
  }
  
  Serial.println("Started"); 
  // set the data rate for the SoftwareSerial port 
  esp8266.begin(9600);
  esp8266.write("AT\r\n"); 
}

void loop() 
{ 
  if (esp8266.available())
  { 
    Serial.write(esp8266.read()); 
  }
  
  if (Serial.available()) 
  {
    esp8266.write(Serial.read()); 
  }
}

I see that it says “Started”. But nothing after that… Again, I expect to see “OK” in the Serial monitor window, but I get nothing after that.

Try this:

const char a=“AT\r\n”;
for(byte i=0;i<=strlen(a);i++) Serial.write(a);

surepic:
Try this:

const char a=“AT\r\n”;
for(byte i=0;i<=strlen(a);i++) Serial.write(a);

That makes absolutely no sense.

Use this:

esp8266.println(F("AT"));

Why makes no sense? I am running webserver right now on esp without library the only reliable way was write via for loop. And you cant use println or print with esp in at command mode.

Edit:
Now i know from where confusion is coming from.

Serial.write(a[i]);

surepic:
Why makes no sense? I am running webserver right now on esp without library the only reliable way was write via for loop. And you cant use println or print with esp in at command mode.

Edit:
Now i know from where confusion is coming from.

Serial.write(a[i]);

Even then it’s not a good way to print strings. The Arduino Core provides a Print class that handles all that for you, there is absolutely no need to explicitly write out for-loops.

If your code doesn’t work without it, you have other problems.

You could argue that the default implementation is not very effecient, because it iterates over the string twice.
A solution would be:

  const char *a = "AT\r\n";
  while (*a)
    Serial.write((uint8_t)*a++);

Actually im passing everything to the function with for loop which is dealing with serial write strings. Your solution looks better though.

Be sure that you connected arduino software serial TX pin to ESP8266 RX pin
and ESP8266 TX pin to arduino software serial RX pin.

GrOnThOs:
Be sure that you connected arduino software serial TX pin to ESP8266 RX pin
and ESP8266 TX pin to arduino software serial RX pin.

Wow, thanks a million. That was the issue.

On the Arduino I first hooked port0 (RX) to the RX pin of the ESP module, and port1 (TX) to the TX pin. With that I was able to communicate with the module through the Serial Monitor.

So later on I did the same, but I used ports 2 and 3 on my arduino. Had port2 setup as RX and port 3 as TX. I'm not sure why the first method works, but I'm happy that it does now,

Thanks again.

Shopeng:
Yes I'm sure it's wired up correctly. Just checked it again.

That's why you should always triple-check your wiring :slight_smile:

PieterP:
That's why you should always triple-check your wiring :slight_smile:

Yeah, but I was under the impression that RX should connect to RX and TX to TX. And that was the case. Didn't know it had to be RX to TX and TX to RX :slight_smile:

Transmit goes to receive, receive goes to transmit.