Post topics, source code that relate to the Arduino Platform

User avatar
By tomporter114
#25380 Hello,

I am trying to write a function to return true if the ESP8266 is connected to a Wifi network and false if it isn't.


I am using software serial on an Arduino Nano

my initial idea was something like this (forgive if there are some small differences, dont have the ability to C+P just now):

Code: Select allboolean ConnTest()
{
String received;
ESP.print("AT+CWJAP?\r\n");

while (ESP.available())
{
char c = ESP.read();
received += c;

if (c == '\n')
{
Serial.println (received);
}
if (received == "ERROR")
{ return false }
else
{return true}
}
}


the last part was a little different, to do with parsing the string and checking if it came back with the connected response (dont remember it off the top of my head), but what I am really looking for is a way to query the ESP and react according to its response but no matter what I do I cannot get the correct response back.

I've been searching for days with no luck so any help would be appreciated.

Thank you,
User avatar
By martinayotte
#25404 I hate AT commands, but if I didn't have choice to use them, I would use "AT+CIFSR=?", it will return 1 or 2 IP addresses, depending if ESP is in STA mode or STA+AP, I would just ignore the AP address, and if I have an STA address too, that would mean that I'm connected to a router ...
User avatar
By tomporter114
#25427 Thanks for the advice, however, when I query the esp with AT+CIFSR=?, it always comes back with "OK", whether I am connected to my network or not.

I think the crux of what I am trying to accomplish is to be able to send a(ny) serial command to the esp and then save its response to a string without the data that I have just sent confusing the Serial.read() function. Is there any way to do this without pinpoint accuracy with delay() functions?

This question might be better for the arduino.cc forum...

I will post my function here now I have acces to it...

Code: Select all  boolean ConnTest(){
  String received = "";
  ESP.print("AT+CWJAP?\r\n");
  delay(2000);
 
  while (ESP.available()) 
  {
    char f = ESP.read();
    received += f;

    if (f == '\n')
    {
      Serial.print ("received = ");
      Serial.println (received);
    }
    if( received == "ERROR")
    {
      return false;
    }
    else
    {
      return true;
    }
  }

 
 
 
}
User avatar
By tomporter114
#25430 ok,

I think I have solved my problem (I have managed to create the boolean to return false if not connected)

this is using two functions, the first of which I did not write and I cant remember/find where I got it from so I apologise for not credting:

Code: Select all  String sendData(String command, const int timeout, boolean debug)
{
    String response = "";
    ESP.print(command);                                                          // send the read character to the esp8266

    long int time = millis();   
    while( (time+timeout) > millis())
    {
      while(ESP.available())
      {
        char c = ESP.read(); // read the next character.
        response+=c;   
      } 
    }
        if(debug)
    {     
      Serial.print(response);
    }   
    return response;   
}

  boolean ConnTest(){
 
  String comp = sendData("AT+CWJAP?\r\n",1000,DEBUG).substring(14);

  //Serial.println(comp);                                              // for debugging

  if (comp == "ERROR\r\n")
  {
    return false;
  }
  else
  {
    return true;
  }
}


and all I had to do was sleep on it :-)