Connect Your ESP8266 To Any Available Wi-Fi network (2023)

Connect Your ESP8266 To Any Available Wi-Fi network (1)

The ESP8266 is a microcontroller developed by Espressif Systems. Known as a WiFi module, this microcontroller can be used to perform various WiFi-related activities, with applications in home automation and beyond. Ranging in price and features, there are many types of ESP8266 modules available – but all are incredibly useful in the IoT world.

Regardless of the IoT application you’ve developed, there are two ways to connect your ESP8266 to the cloud. First, you can input your WiFi credentials in the ESP8266’s firmware to establish the required connection and start sending data. A second way – which we’ll cover step-by-step below – is by building your own access point into the board, creating a universal firmware that will establish a connection to any available network with just the press of a button.


Step 1. Hardware setup

Note: The Ubidots team made some modifications in the ConfigManager Library to implement a routine that launches the AP mode by simply pressing an external reset button.

Depending on the ESP8266 module you choose, you may need to assign the reset pin using this library version. The default button settings are assigned to PIN 5; if using a NodeMCU, you must connect the button into the D1 pin.

Connect Your ESP8266 To Any Available Wi-Fi network (2)

Step 2. Set up the Arduino IDE with your device

Before using any ESP8266 device, you’ll have to install the boards into the Arduino IDE. Follow the steps below to compile the board.

If you haven’t already, start by downloading the Arduino IDE.

  1. Open the Arduino IDE. Select Files -> Preferences and enter the URL below the Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.

NOTE: If you’re a Mac user, Arduino and File contain different drop-down functions compared to Windows users. You’ll also need to install the following driver to upload firmware to your NodeMCU.

2. Open the Boards Manager from Tools -> Board -> Boards Manager and install the ESP8266 platform. To find the correct device, search for ESP8266 within the search bar.

3. Select the ESP8266 module you’re using. In this case, we decided to use the NodeMCU 1.0 (ESP-12 Module). To select the board, go to Tools > Board > Select the board.

Additionally, to communicate with the NodeMCU, we’ll also need to select the port com. Go to Tools -> Port and select the appropriate PORT for your device.

To keep everything running fast and smooth, let’s make sure the upload speed is optimized to 115200. Go to Tools -> Upload Speed -> 115200.

4. Head over to GitHub and download the ConfigManager library. Click the green button labeled “Clone or download” and select “Download ZIP“.

Connect Your ESP8266 To Any Available Wi-Fi network (3)

5. Now, go back in the Arduino IDE and click Sketch -> Include Library -> Add .ZIP Library.

6. Select the .ZIP file of ConfigManager and then “Accept” or “Choose”.

If successful you’ll see the following message in the Arduino IDE: "Library added to your library. Check "Include Library" menu"

7. Next, go to Sketch/Program -> Include Library -> Library Manager and install the PubSubClient library. To find the correct library, search PubSubClient within the search bar.

Connect Your ESP8266 To Any Available Wi-Fi network (4)

8. Now, reboot the Arduino IDE prior to upload.

9. Once you have the ESP8266 platform and required libraries installed, install the Arduino ESP8266 filesystem uploader. Follow all the repository’s installation steps and return to this guide.

10. After your uploader is installed, create a new sketch to work in and save it. Our example is called AP_ESP8266:

Connect Your ESP8266 To Any Available Wi-Fi network (5)

11. Next, go to the sketch directory and create a new folder called data.

Connect Your ESP8266 To Any Available Wi-Fi network (6)

12. Once the directory is created, download this HTML file and attached it to the directory. This file will be used in the file system.

Connect Your ESP8266 To Any Available Wi-Fi network (7)

13. Next, we need to upload the file to the ESP8266 flash file system. To begin, make sure you have selected a board port and closed Serial Monitor.

14. Select Tools > ESP8266 Sketch Data Upload to start uploading files to the ESP8266 flash file system.

Connect Your ESP8266 To Any Available Wi-Fi network (8)

When done, the IDE status bar should display SPIFFS Image Uploaded:

Connect Your ESP8266 To Any Available Wi-Fi network (9)

15.Now, paste the below code in the Arduino IDE. Once pasted, assign your desired device and variable labels, as well as your unique Ubidots TOKEN. If you don’t know your Ubidots TOKEN, find out how to get one here.

Copy and paste the below code into the Arduino IDE, including your specific device and variable parameters.

#include <ESP8266WiFi.h>#include <ConfigManager.h>#include <PubSubClient.h> /******* Define Constants **********/ namespace{ const char * AP_NAME = "Ubidots Access Point"; // Assigns your Access Point name const char * MQTT_SERVER = ""; const char * TOKEN = "...."; // Assigns your Ubidots TOKEN const char * DEVICE_LABEL = "my-device"; // Assigns your Device Label const char * VARIABLE_LABEL = "my-variable"; // Assigns your Variable Label int SENSOR = A0; } char topic[150];char payload[50];String clientMac = ""; unsigned char mac[6]; struct Config { char name[20]; bool enabled; int8 hour; } config;/******* Initialize a global instance **********/WiFiClient espClient; PubSubClient client(espClient); ConfigManager configManager; /***************Auxiliar Functions ********************/ void callback(char* topic, byte* payload, unsigned int length){ } void reconnect() { while (!client.connected()){ Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect(clientMac.c_str(), TOKEN, NULL)) { Serial.println("connected"); break; } else { configManager.reset(); Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 3 seconds"); for(uint8_t Blink=0; Blink<=3; Blink++){ digitalWrite(LED, LOW); delay(500); digitalWrite(LED, HIGH); delay(500); } } } }String macToStr(const uint8_t* mac) { String result; for (int i = 0; i < 6; ++i) { result += String(mac[i], 16); if (i < 5)result += ':'; } return result; } /********* Main Functions *************/void setup() { Serial.begin(115200); /* Declare PINs as input/output */ pinMode(SENSOR, INPUT); pinMode(PIN_RESET, INPUT); pinMode(LED, OUTPUT); /* Assign WiFi MAC address as MQTT client name */ WiFi.macAddress(mac); clientMac += macToStr(mac); /* Access Point configuration */ configManager.setAPName(AP_NAME); configManager.addParameter("name",, 20); configManager.addParameter("enabled", &config.enabled); configManager.addParameter("hour", &config.hour); configManager.begin(config); /* Set Sets the server details */ client.setServer(MQTT_SERVER, 1883); client.setCallback(callback); /* Build the topic request */ sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL); }void loop() { configManager.reset(); configManager.loop(); /* MQTT client reconnection */ if (!client.connected()) { reconnect(); } /* Sensor Reading */ int value = analogRead(SENSOR); /* Build the payload request */ sprintf(payload, "{\"%s\": %d}", VARIABLE_LABEL, value); /* Publish sensor value to Ubidots */ client.publish(topic, payload); client.loop(); delay(5000);}

16. After inputting your parameters, verify the code within the Arduino IDE. To do this, click the checkmark icon in the top left corner of your Arduino IDE.

Then, upload your code into your NodeMCU. Choose the right-arrow icon beside the checkmark.

Once the code is uploaded, you’ll see the below message in the Arduino IDE:

Connect Your ESP8266 To Any Available Wi-Fi network (10)

Your ESP8266 module is now ready to establish a connection with any available network by just pressing the button!

17. To verify the connection status, open the Serial monitor, press the button connected to your ESP module and hold it for 5 seconds until you see the message “Starting Access Point” in your serial monitor:

Connect Your ESP8266 To Any Available Wi-Fi network (11)

18. Now that the Access Point is created, you can establish connection from your phone. Under Wi-Fi networks, select Ubidots Access Point:

Connect Your ESP8266 To Any Available Wi-Fi network (12)

When the connection is established, it will redirect you to the page below. Input your Wi-Fi parameters and click save.

Connect Your ESP8266 To Any Available Wi-Fi network (13)

19. To verify the connection is established, go to the Serial Monitor:

Connect Your ESP8266 To Any Available Wi-Fi network (14)

Now, return to your Ubidots accounts to visualize the data received from your new device:

Connect Your ESP8266 To Any Available Wi-Fi network (15)


By following this guide, you’ll set up an Access Point that connects your device to any available network without setting the credentials into the firmware – thus creating a universal firmware that can be used anywhere!

Now it’s your turn to give it a try. Sign up with Ubidots for free today!


How to connect ESP8266 to WiFi network? ›

IoT ESP8266 Series: 1- Connect to WIFI Router
  1. Step 1: Download the Arduino IDE and ESP8266 NodeMCU Driver. ...
  2. Step 2: Configure ESP8266 NodeMCU As an Arduino. ...
  3. Step 3: Run Blinking LED Program. ...
  4. Step 4: Connect to a WIFI Network. ...
  5. Step 5: See Next Instructable. ...
  6. 8 People Made This Project!
  7. 13 Comments.

Why can't i connect to Wi-Fi on ESP8266? ›

The ESP8266 only operates on the 2.4GHz band and it is not compatible with the 5.8GHz band. You must ensure that the router you are trying to connect to has the 2.4GHz band enabled and that you are trying to connect to it. Depending on the router, you may needs to set the router AP band to 2.4GHz.

How do I control ESP8266 from anywhere? ›

The ESP8266 can be controlled from your local Wi-Fi network or from the internet (after port forwarding). The ESP-01 module has GPIO pins that can be programmed to turn an LED or a relay ON/OFF through the internet. The module can be programmed using an Arduino/USB-to-TTL converter through the serial pins (RX,TX).

Can ESP8266 connect to 5g Wi-Fi? ›

The ESP8266 has a built in WiFi interface that supports 802.11 b/g/n 2.4 GHz networking. 5GHz frequencies are not supported.

Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated: 12/12/2023

Views: 6424

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.