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);
}
asked Sep 29, 2018 at 13:16
17
  • try to print back to Serial the command variable Commented Sep 29, 2018 at 13:21
  • i did , the command was fine just AT command it responds with busy p... then error Commented Sep 29, 2018 at 13:22
  • do you have CR/LF in Serial Monitor? then replace println(command) with print(command) Commented Sep 29, 2018 at 13:23
  • sorry , i don't know about CR/LF . i tried print and it just responds with error Commented Sep 29, 2018 at 13:26
  • in Serial Monitor one of the settings is line ending. LN & CR Commented 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".

answered Sep 29, 2018 at 13:35
2
  • i don;t know the right way to read serial input can u please demonstrate as i took the code from internet . Commented 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 Commented Sep 29, 2018 at 13:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.