ESP8266 http call after wifi begin

Hi, my esp8266 is set to WIFI_AP_STA and one endpoint is available on server side (ESP is listening to some /FOO)

When I call FOO from my smartphone, passing SSID and PSW, I want my ESP to connect to wifi and then register calling a HTTP client

this is ethe code

WiFi.begin(ssid, pass);

long loopCount = 0;
long loopMaxCount = 10;
while (WiFi.status() != WL_CONNECTED && ++loopCount <= loopMaxCount) {

wl_status_t status = WiFi.status();

debugLog(“connectToWiFIAndRegister”, String(loopCount) + "of " + String(loopMaxCount) +
“: WiFi status: " + String(status) + " (” + WiFiStatusToString(status) + “)”);

delay(1000);
}

if (WiFi.status() != WL_CONNECTED)
{
return “ERROR”; // TODO returns the right error (like SSID not found)
}

registerDevice()

The registerDevice function cals some http url passing data and wating for 200 response

The problem is that the registerDevice always throw exception

But connect ESP to WIFI on setup , then registerDevice works fine

Can you help me?

The problem is that the registerDevice always throw exception

You did not post ALL of your code, or ANY of the details of the exception.

Can you help me?

What do you think?

Here it is

server.on("/connectandcheck", handleConnectAndCheck);

void handleConnectAndCheck()
{

WiFi.begin(ssid, pass);

long loopCount = 0;
long loopMaxCount = 25;
while (++loopCount <= loopMaxCount) {

if (WiFi.status() == WL_CONNECTED)
break;

delay(1000);
}

if (WiFi.status() != WL_CONNECTED)
{
WiFi.disconnect(); // to erase all WiFi info
server.send(500, “text/plain”, WiFi.status());
return;
}

WiFiClient client;

if (client.connect(host, 80))
{
client.print(String(“GET /”) + " HTTP/1.1\r\n" +
"Host: " + host + “\r\n” +
“Connection: close\r\n” +
“\r\n”
);

String ret = “”;

while (client.connected() || client.available())
{
if (client.available())
{
String line = client.readStringUntil(’\n’);
ret += line;
}
}
client.stop();

server.send(200, “text/plain”, “OK”);
}
else
{
debugSerialPrintln(“handleConnectAndCheck”, “connection failed!”);
client.stop();

server.send(500, “text/plain”, “TESTHOST ERROR”);
}
}

this call, using any client like POSTMAN for example, often returns ERROR
The problem seems to be related to the changes of channel on ESP

  1. ESP starts in AP on channel 1...
  2. client connects to ESP AP
  3. ESP start connecting and changes the channel to 2, 3, ... and immediately the client disconnects from

so the ERROR is probably due to the fact that the client disconnect from ESP

Is there any way to avoid this?

thanks

station and AP of the esp8266 must be on the same chanel. so if you connect as station to some AP, the SoftAP of the esp8266 resets and the browser disconnects. why do you connect to AP in HTTP request handler?

Thank you for reply

My goal is to create a sort of SYNCRONOUS method of ConectAndRegister

This method should return to the client with message OK or KO

any way to do that? from WIFI Connection to HTTP call?

mattdev:
Thank you for reply

My goal is to create a sort of SYNCRONOUS method of ConectAndRegister

This method should return to the client with message OK or KO

any way to do that? from WIFI Connection to HTTP call?

it is not possible

Juraj:
it is not possible

ok thanks so what's the best practice for that?

I mean.... I need to do the MOST COMMON use case of the IoT (I mean, fron user prospective)

  • Open APP
  • connect Smarthone to ESP AP
  • send SSID & PSW to ESP

waiting for response like

  • SSID NOT FOUND

or, if the above is passed

  • PASSWORD WRONG

or, if the above is passed

  • NO internet connection

or, if the above is passed

OK, registered!

what0s the right approach to do that?

thanks for helping

mattdev:
ok thanks so what's the best practice for that?

I mean.... I need to do the MOST COMMON use case of the IoT (I mean, fron user prospective)

  • Open APP
  • connect Smarthone to ESP AP
  • send SSID & PSW to ESP

waiting for response like

  • SSID NOT FOUND

or, if the above is passed

  • PASSWORD WRONG

or, if the above is passed

  • NO internet connection

or, if the above is passed

OK, registered!

what0s the right approach to do that?

thanks for helping

see WiFiManager library or WiFiLink firmware's WebServer

Juraj:
see WiFiManager library or WiFiLink firmware's WebServer

Hi, thanks for the link

I'm tryng to figure out what's the right CLIENT/SERVER process

As far as I undestand, the ide could be

  1. ESP starts in AP
  2. CLIENT connects to ESP
  3. CLIENT call ESP enpoint http://192.168.1.1/connect?SSID=...
  4. ESP returns immediately 200/OK
  5. then the CLIENT start looping http://192.168.1.1/isConnected untilthe ESP returns something (OK or ERROR)

is my assumption correct?

Is yes, In this case it won't work, as after point #4, the CLIENT automatically disconnects from ESP (due to the channel change), so the calls at poin #5 will always fail

What's your opinion?

thanks for your help

mattdev:
Hi, thanks for the link

I'm tryng to figure out what's the right CLIENT/SERVER process

As far as I undestand, the ide could be

  1. ESP starts in AP
  2. CLIENT connects to ESP
  3. CLIENT call ESP enpoint http://192.168.1.1/connect?SSID=...
  4. ESP returns immediately 200/OK
  5. then the CLIENT start looping http://192.168.1.1/isConnected untilthe ESP returns something (OK or ERROR)

is my assumption correct?

Is yes, In this case it won't work, as after point #4, the CLIENT automatically disconnects from ESP (due to the channel change), so the calls at poin #5 will always fail

What's your opinion?

thanks for your help

it will not fail. it will reconnect for new request

Juraj:
it will not fail. it will reconnect for new request

Hi, not exaclty on smartphones

based on my multiple experiments:

  1. ESP starts in AP
  2. CLIENT connects to ESP
  3. CLIENT call ESP enpoint http://192.168.1.1/connect?SSID=...
  4. ESP returns immediately 200/OK

--> ESP starts connection process and change channel

HERE; the smartphone disconnects from ESP and does not reconnects... most of the time smartphone reconnects to HOME WIFI

  1. then the CLIENT start looping http://192.168.1.1/isConnected untilthe ESP returns something (OK or ERROR)

--> the CALL fails because the smarthpone is connected to HOME WIFI, it is not connected to ESP anymore

Am I missing anything?

Are you facing/expecting a diferent behaviour?

Thanks

mattdev:
Hi, not exaclty on smartphones

based on my multiple experiments:

  1. ESP starts in AP
  2. CLIENT connects to ESP
  3. CLIENT call ESP enpoint http://192.168.1.1/connect?SSID=...
  4. ESP returns immediately 200/OK

--> ESP starts connection process and change channel

HERE; the smartphone disconnects from ESP and does not reconnects... most of the time smartphone reconnects to HOME WIFI

  1. then the CLIENT start looping http://192.168.1.1/isConnected untilthe ESP returns something (OK or ERROR)

--> the CALL fails because the smarthpone is connected to HOME WIFI, it is not connected to ESP anymore

Am I missing anything?

Are you facing/expecting a diferent behaviour?

Thanks

I didn't use it on phone.
try the WiFiManager library