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);
}
1 Answer 1
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".
-
i don;t know the right way to read serial input can u please demonstrate as i took the code from internet .Ali El-Boghdady– Ali El-Boghdady2018年09月29日 13:38:48 +00:00Commented 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 pointAli El-Boghdady– Ali El-Boghdady2018年09月29日 13:40:19 +00:00Commented Sep 29, 2018 at 13:40
command
variableprintln(command)
withprint(command)