How to check for ESP8266 WiFi connection in loop() (SOLVED)

I need to send a message via the Internet when my device starts up but I don't know how I can check if it has connected to an AP yet and is ready to send. The ESP runs in station mode and is programmed to connect to a specific ssid/password.
Is there a way to make sure that a connection exists such that I can set a flag or such to use in loop() to send the message?
Or can I stay in setup() until a connection has been established?
How would this be done?

OK,
I tested this:

bool StartMessageSent = false;

void loop()
{
   ...
    //Send startup report, only once
    if (StartMessageSent == false)
    {
        if (WiFi.status() == WL_CONNECTED)
        {
            SendStartReport();
            StartMessageSent = true; //Do not try sending anymore
        }
    }
}

But the report is not posted so I don't know if I got it right. In any case the call is not executed on the webserver.

The SendStartReport() is a simple http connection to a php file with some parameters.

The bulk function of the device is to post temp/humid data to another php file on the server and this works as intended.

I just let setup() run until a connection is made. This seems to be OK since my programs are hard-coded to just one particular ssid.

void setup()
{
Serial.begin(115200);
delay(100);
Serial.print("Connecting to ");
Serial.println("xxxxxxxx"); //your ssid
WiFi.mode(WIFI_STA);
WiFi.begin("xxxxxxxx","xxxxxxxxx"); //your password
//You will hang up in this loop until you connect...
while (WiFi.status() != WL_CONNECTED)
{ delay(200); Serial.print("."); }
Serial.print("\r\nConnected: local ip address is http://");
Serial.println(WiFi.localIP());
Serial.println();
//
//Your other setup stuff here...
//
}

1 Like

OK,thanks!
But would not the way with the flags I shown also work from loop()?

It might be a problem in formatting the data to be sent...
I need to put the IP address into a string for transfer to the php file on the website.
In your example I see:

Serial.print("\r\nConnected: local ip address is http://"); 
Serial.println(WiFi.localIP()); // <== here!
Serial.println();

Is there a way to put the IP address into a string so I can compose the string needed to send the data to the php file?

In my working temperature report function I do this, where sensor, temp and humid are simple numeric arguments (int, float, float) and it works fine:

    String dataline = "http://xxxxxx/php/tempreport.php";
    dataline += "?source=" + String(sensor);
    dataline += "&temp=" + String(temp, 1); //1 decimal places
    dataline += "&humid=" + String(humid, 1);

But an IP address is much more complex so I don't know an easy way to get it done...
I have tried this, but it might have failed here, hard to say:

    char ipbuf[16];
    String macaddr = WiFi.macAddress();
    
    IP2str(ipbuf, WiFi.localIP());  //Own function to convert IP address to char array

    String dataline = "http://xxxxxxx/php/ipreport.php";
    dataline += "?hostname=" + String(WiFi.hostname());
    dataline += "&localaddr=" + String(ipbuf);  //<== Maybe failing?
    dataline += "&macaddr=" + macaddr;

Debugging is difficult because I only have network access to the device so serial debug messages are impossible.
I update the device firmware via the webupdater function included in the sketch.

I have composed the call string that the function "should" result in and using a browser I executed it and it works correctly and generates the email I am looking for. But when I run it as shown earlier in the sketch there is no email...

Note: The php file on the server is used on all of my Raspberry Pi units to report their IP address when starting up, so I know for sure that the php works.

IPAddress.toString()

Problem solved!
It turns out that the temp logging and start report scripts on the webserver used different protocols!
In the temp logging it used _GET and this worked, whereas the start report script used _POST and this does not work with the ESP function I made.
So I copied the start report script on the webserver to a new file specifically used for the ESP8266 devices and here I changed _POST to _GET like this:

    $esphostname = $_GET['hostname'];
    $espaddress = $_GET['ipaddr'];
    $espmacaddr = $_GET['macaddr'];

Now it works fine, no real problem with the Arduino sketch code....
I also simplified my reporter code in the sketch as follows (the serial debug messages can be removed of course):

void SendStartReport()
{
    HTTPClient http;

    //Build call string:
    String dataline = "http://www.xxxxxx.com/php/espreport.php";
    dataline += "?hostname=" + String(WiFi.hostname());
    dataline += "&ipaddr=" + WiFi.localIP().toString();
    dataline += "&macaddr=" + String(WiFi.macAddress());

    //Call espreport.php web script
    bool httpResult = http.begin(dataline);
    if(!httpResult)
    {
        SerialDebug.println("Invalid HTTP request:");
        SerialDebug.println(dataline);
    }
    else
    {
        int httpCode = http.GET();
        if (httpCode > 0)
        { // Request has been made
            SerialDebug.printf("HTTP status: %d Message: ", httpCode);
            String payload = http.getString();
            SerialDebug.println(payload);
            StartMessageSent = true;
        }
        else
        { // Request could not be made
            SerialDebug.printf("HTTP request failed. Error: %s\r\n", http.errorToString(httpCode).c_str());
        }
    }
    http.end();
}