How to connect Esp8266 to a secure wifi without password

I'm trying to connect to a secure Wi-Fi network, and the admin has already whitelisted my device on the router server. While I can connect to the Wi-Fi, the Arduino Cloud isn't connecting, and I can't control the ESP8266 through the IoT Remote mobile app.

It seems that the WiFiConnectionHandler ArduinoIoTPreferredConnection(); function might be causing the issue. I tried passing NULL values, but it's not working, and the following output appears on the serial monitor. I also attempted using static IPs (as suggested by GPT), but that didn't resolve the problem. How can we solve this? Is there an alternative way, possibly through the Arduino IDE, to control the ESP8266 via the IoT Remote app?

Serial Monitor Output :

(Pass = " " OR NULL)

�HP��>dh��xI<�Connecting to Wi-Fi network...
Connected to Wi-Fi
IP Address: 10.10.10.1XX
Subnet Mask: 255.255.255.0
Connecting to Arduino IoT Cloud...
Connecting to Arduino IoT Cloud...

(Pass = 0)
Connecting to Wi-Fi network...
Connecting to Wi-Fi network...

did you try a simple sketch to ensure the connection was indeed successful? (without arduino cloud)

what type of secure network?

I have seen where Wi-Fi is slow to connect and it still tries to connect to the cloud anyway. It will keep retrying until Wi-Fi is connected and keep trying Cloud until connected, but it may take 30 seconds or so.

I agree that you should take your sketches one step at a time and just try to connect to Wi-Fi.

You might try:

WiFi.begin(ssid)

1 Like

It is a university wifi, using MKR module

Can you share some code so I can check if the connection was successful?

I used the code to check and verify if it connected to wifi

WiFi.begin(ssid, password);

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}

It does print Connected to wifi, when the PASS = NULL, but arduino is not able to connect, that is the main problem

One thing to keep in mind is that you're dealing with two (at least) aspects of the connection process. WiFi connection appears to work just fine. You can verify this by pinging your ESP8266 from a different device on the same subnet.

The likely issue is that your ESP fails to connect to the outside world, either because it has not router/gateway address that it can use to connect to anything outside the local 10.10.10.x subnet, or because there's a problem with domain name resolution (DNS). Does the ESP8266 connect to IoT cloud using an IP address or a server name? The former bypasses any DNS trouble.

Did you set a router address? This will be needed to connect to anything outside the local network. On a 10.10.10.x network, there will usually be a router on 10.10.10.1, but there's no guarantee. Ask the network administrator, or check your phone or computer that's connected to the same network and note which router address it got from DHCP.

Btw, I don't see why a static IP address would be needed or particularly helpful in this case. If you use DHCP you won't have a problem with router or DNS addresses etc. since this will come as part of the DHCP lease.

you could use a sample code to test if you can reach a time server for example

#include <WiFi.h>
#include <esp_sntp.h> // https://github.com/espressif/esp-idf/blob/v5.2/components/lwip/include/apps/esp_sntp.h (doc https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/system_time.html)

const char *ssid = "******";
const char *wifiPassword = "********";
const char* ntpServer = "pool.ntp.org";

bool ntpSyncCompleted = false;
bool wifiConnected = false;
time_t lastNtpSync;

void ntpSyncCallback(struct timeval *tv) {
  Serial.println("NTP synchronized");
  lastNtpSync = time(NULL);
  Serial.print("NTP sync completed on ");
  struct tm *pTime = localtime(&lastNtpSync);
  Serial.println(pTime, "%d/%m/%Y %H:%M:%S"); // https://github.com/espressif/arduino-esp32/blob/ccacb7e3d1dd0e58b309c83e5ebc2302ce97c7b2/cores/esp32/Print.h#L98
  ntpSyncCompleted = true;
}

void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
  Serial.println("...Network connection established");
}

void WiFiStationGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
  Serial.print("Obtained IP address = ");
  Serial.println(WiFi.localIP());
  wifiConnected = true;
}

void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
  wifiConnected = false;
  Serial.print("Disconnected from the network. (reason: ");
  Serial.println(info.wifi_sta_disconnected.reason);
  Serial.println(")\nAttempting to reconnect.");
  WiFi.begin(ssid, wifiPassword); 
}

// function that displays the current time every second (can force display by passing true)
bool displayTime(bool forceDisplay = false) {
  static unsigned long lastDisplay = -2000;
  if (ntpSyncCompleted)
    if (forceDisplay || (millis() - lastDisplay >= 1000)) {
      time_t timestamp = time( NULL );
      struct tm *pTime = localtime(&timestamp );
      Serial.println(pTime, "%d/%m/%Y %H:%M:%S"); // https://github.com/espressif/arduino-esp32/blob/ccacb7e3d1dd0e58b309c83e5ebc2302ce97c7b2/cores/esp32/Print.h#L98
      lastDisplay = millis();
    }
  return ntpSyncCompleted;
}

void setup() {
  Serial.begin(115200);
  Serial.println("NTP Time Management Demonstration");

  Serial.println("Connecting to WiFi network");
  // callbacks to know if the network is functional
  WiFi.onEvent(WiFiStationConnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);
  WiFi.onEvent(WiFiStationGotIP, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
  WiFi.onEvent(WiFiStationDisconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);

  // configure the current time reading by NTP, setting the timezone:
  // last Sunday of March at 2:00: GMT+2
  // last Sunday of October at 3:00: GMT+1
  configTzTime("CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00", ntpServer);

  // a callback to be notified that we successfully synchronized time with the NTP server
  sntp_set_time_sync_notification_cb(ntpSyncCallback);

  WiFi.begin(ssid, wifiPassword);
}

void loop() {
  displayTime();

  // If your code requires WiFi, you can test it
  if (wifiConnected) {
    // •••
  }

  // here is the code that doesn't require WiFi
}

if all works OK, after a short while you should see the time being printed in the serial monitor

(the timezone is for France but it does not matter)

Hi So I was trying to pass only wifi name in WiFiConnectionHandler ArduinoIoTPreferredConnection(); and password as null

It shows the following on serial monitor

Ramada (Pass = " ",NULL)

�HP��>dh��xI<�Connecting to Wi-Fi network...
Connected to Wi-Fi
IP Address: 10.10.10.156
Subnet Mask: 255.255.255.0
Connecting to Arduino IoT Cloud...
Connecting to Arduino IoT Cloud...

As you can see it connect to the wifi, but not to the cloud. But should I just pass the IP address in the Arduino function as server name? Please let me know

Thanks I will check this

That's a space, not 'empty'

My comment about connecting with the IP address was not about establishing the wifi connection, but establishing the connection with the server you want to exchange data with. Please look into the basics of IP and DNS for further reference.

Yes esp8266 is not able to connect to ardunio. I have checked the IP address which the wifi provides when esp connect, how can I use this (IP address) in the sketch so that ESP can connect to arduino cloud without SSID and password being passed in the function
WiFiConnectionHandler ArduinoIoTPreferredConnection();?

or is there any other function that can be used to connect to arduino cloud, that uses IP address as variable?

The IP address your ESP receives evidently won't be useful in connecting to the server at the other end. You need the IP address if that server and then connect with that instead of trying to connect to the server by name. Again, please refer to the basics of IP and name resolution/DNS.

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