0

I have a weird problem , as I.m trying to connect to esp8266 01 with Arduino Uno when i try sending commands from the serial it doesn't work but if i send it in a function in the setup phase it works

#include <SoftwareSerial.h>

SoftwareSerial esp8266(2, 3);

void setup()
{
Serial.begin(9600);
esp8266.begin(9600);
delay(1000);
//sendData("AT", 1000);
//sendData("AT+CWMODE=1", 1000);
}

void loop() {
// put your main code here, to run repeatedly:
if(esp8266.available()) // check if the esp is sending a message
  {
      while(esp8266.available())
      {
          // The esp has data so display its output to the serial window
          char c = esp8266.read(); // read the next character.
          Serial.write(c);
      }
  }
  if(Serial.available())
  {
    delay(1000);
    String command="";
    while(Serial.available()) // read the command character by character
    {
        command+=(char)Serial.read();
    }
    esp8266.println(command); // send the read character to the esp8266
  }
}

void sendData(String command, const int timeout) {
 String response = "";
 esp8266.println(command);
 long int time = millis();
 while ((time + timeout) > millis())
   while (esp8266.available()) {
     char c  = esp8266.read();
     response += c;
    }
 Serial.println(response);
}
17
  • try to print back to Serial the command variable
    – Juraj
    Sep 29, 2018 at 13:21
  • i did , the command was fine just AT command it responds with busy p... then error Sep 29, 2018 at 13:22
  • do you have CR/LF in Serial Monitor? then replace println(command) with print(command)
    – Juraj
    Sep 29, 2018 at 13:23
  • sorry , i don't know about CR/LF . i tried print and it just responds with error Sep 29, 2018 at 13:26
  • in Serial Monitor one of the settings is line ending. LN & CR
    – Juraj
    Sep 29, 2018 at 13:28

1 Answer 1

0

I take your code as a test sketch and will not comment on the terrible way you read Serial input.

The AT firmware requires \r\n as command ending characters. You can set them in Serial Monitor as option LN & CR, but then use print to send the command to esp8266.

Or set in Serial Monitor "No line ending" and let in sketch the println to send the command. println adds \r\n to printed string.

If you had only "New line" set in Serial Monitor and used println the resulting command was for AT firmware disturbing "AT\n\r\n".

2
  • i don;t know the right way to read serial input can u please demonstrate as i took the code from internet . Sep 29, 2018 at 13:38
  • I fixed that problem now i send AT commands and it responds well my problem now is connecting to an access point Sep 29, 2018 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.