My phone did not show the board is connecting to hotspot

I have an issue regarding to my connection between my arduino board and phone hotspot. I have changed to new network but after uploaded the sketch code, my phone did not show the board is connecting but the arduino still can able to upload the data to the cloud. Is that normal?

Hi @jovincyc0825.

Background

The way the ESP8266 handles Wi-Fi credentials can be quite confusing. The ESP8266 automatically stores Wi-Fi settings in non-volatile memory. The board will then attempt to connect to the AP using those settings:

https://arduino-esp8266.readthedocs.io/en/2.5.0/esp8266wifi/generic-class.html#persistent

The "Arduino_ConnectionHandler" library, which is used to handle the network connections for Arduino Cloud Thing sketches checks to see if the board is already connected to the network, and only initializes a new connection if it doesn't already have one:

This means that if network you were using prior to changing the Thing's "Network" configuration to the phone hotspot is still accessible, then the board will connect to that prior network instead of to the phone's hotspot. This is one possible explanation for the unexpected behavior you observed.

Investigation

If you open Serial Monitor while the Thing sketch is running, you will see some information about the progress of the Devices connection to the network. For example:

***** Arduino IoT Cloud - configuration info *****
Device ID: e89e4b28-afcf-4e5f-b988-6a11b8af8ed4
MQTT Broker: iot.arduino.cc:8884
WiFi.status(): 0
Connected to "some-ssid"
Connected to Arduino IoT Cloud
Thing ID: 0869e139-9299-4605-a539-d9b3976cf6a4

The "Connected to "some-ssid"" part of this information is not trustworthy though. The reason is that the library simply prints the SSID from the "Wi-Fi Name" field of the Thing's Network configuration (i.e., SECRET_SSID) in that "Connected to ..." Serial Monitor message, not the SSID the board actually connected to. As I explained above, these will not necessarily be the same thing and in this case the message printed to Serial Monitor will be false.

In order to determine whether your board is actually connecting to the intended SSID, I suggest you perform this experiment:

  1. Add the following code to the loop function of your Thing sketch in Cloud Editor:
    static bool ssidPrinted = false;
    if (ArduinoCloud.connected() && !ssidPrinted) {
      Serial.print("Actually connected to SSID: ");
      Serial.println(WiFi.SSID());
      ssidPrinted = true;
    }
    
  2. Upload the updated Thing sketch to your ESP8266 board.
  3. Wait for the upload to finish successfully.
  4. Click on "Serial Monitor" in the Cloud Editor toolbar.
    A Serial Monitor window will open.
  5. Watch the Serial Monitor window until you see a line printed that says:
    Actually connected to SSID: <actual SSID>
    
    (where <actual SSID> is the SSID of the network the board actually connected to)

Was the SSID shown in that line you saw in step (5) the SSID of your phone hotspot, or was it something else?


You can now remove those lines you added to your Thing sketch, as they were only needed for the purposes of this investigation. They don't do any harm (other than using up a little bit of memory), so if you like to have this information printed in Serial Monitor so that you can be confident the board connected to the intended network, then you are also welcome to leave the lines in your sketch.


Resolution

If the SSID was something other than that of the phone hotspot network, the way to proceed is to erase the stored Wi-Fi settings to force the ESP8266 to connect to the intended network.

To do that, upload this sketch to your ESP8266 board:

void setup() {
  Serial.begin(9600);
  Serial.println("Erasing stored Wi-Fi settings.");
  if(ESP.eraseConfig()){
    Serial.println("Done!");
  } else {
    Serial.println("FAILED");
  }
}

void loop() {}

Wait for the upload to finish successfully. You can then open Serial Monitor to verify that the settings were erased successfully.

At this point, you can upload the Thing sketch to the ESP8266 again and it will attempt to connect to the intended network. However, you might want to take one additional measure to ensure this type of confusing situation can't happen again:

If you would like to prevent the board from storing the settings again, add these lines to the top of the setup function in your Thing sketches (as well as any other non-Thing sketches where you call WiFi.begin):

WiFi.mode(WIFI_STA);
WiFi.persistent(false);

Additional Information

This behavior is not specific to Arduino Cloud Thing sketches. Here is a minimal demonstration sketch that will produce the same behavior (except for printing the true SSID) if your board already has usable settings stored:

#include <ESP8266WiFi.h>

void setup() {
  Serial.begin(9600);
  Serial.println("Giving time for board to connect autonomously:");
  for (int countdown = 10; countdown >= 0; countdown--) {
    Serial.println(countdown);
    delay(1000);
  }
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.begin("foo", "bar");  // These invalid settings will not be used if the board was able to connect using its stored settings.
    Serial.print("Connecting");
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println();
  } else {
    Serial.println("Using existing connection.");
  }

  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());
}

void loop() {}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.