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);
}
command
variableprintln(command)
withprint(command)