Thing loging in wrong LAN

To connect thing to network, normally you must provide SSID and password. With IoT Cloud it is enough to write correct password only, you do not need the correct SSID. Is it only for me or somebody else have this security problem?

What are you talking about? the mgical "aether" that radiowaves travel in?

You can change the SSID but until you change the password, but you will be logged in the same LAN, however the IoT Cloud will show that you are logged in the provided SSID LAN.
I have two LAN with different SSID with the same password. They are placed 15 km one from another. When I prepared device for another SSID, IoT Cloud shows that I'm logged into the distant one however router show that I'm logged in near one.

Probably you have stored both credentials on the same device or both WLAN are configured identical and you hunt a ghost.

I changed SSID to neighbour's SSID and IoT Cloud show that I'm logged in that LAN. But it's not true.

How did you check the network stuff?

From Serial Monitor
��=���***** Arduino IoT Cloud - configuration info *****
Device ID: 21e9929c-fcba-4f6e-88cb-53acda4408f1
MQTT Broker: mqtts-up.iot.arduino.cc:8884
WiFi.status(): 3
Duration 2247
389 mm
Connected to "Wireless-G"

$�?��-���***** Arduino IoT Cloud - configuration info *****
Device ID: 21e9929c-fcba-4f6e-88cb-53acda4408f1
MQTT Broker: mqtts-up.iot.arduino.cc:8884
WiFi.status(): 3
Duration 0
0 mm
Connected to "NotExistingLAN"

May be it is only for NodeMCU 1.0 (ESP-12E Module)?

Hi @Daumants. 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:

So if the board is able to connect autonomously to the Wi-Fi AP using its stored settings before the Thing program executes ArduinoCloud.begin(ArduinoIoTPreferredConnection); then you get the behavior you noticed.

Something that might be confusing is that the library always prints the SSID from SECRET_SSID in that "Connected to ..." Serial Monitor message, not the actual SSID the board is 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.

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() {}

If you would like to erase the stored Wi-Fi settings, 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() {}

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);

Thank you for so nice explanation!
Only this one confusing thing is not good.

Ther is a gottcha:
WiFi.begin(SSID,PWD) connects always to the given SSID.
WiFi.begin(); without SSID connects to whatever SSID is available/stored.

You are welcome. I'm glad if I was able to be of assistance.

Regards,
Per

OK, but in IoT Cloud sketch *.ino there is not WiFi.begin().
Only in thingProperties.h is
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

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