Last active
March 29, 2023 18:37
-
-
Save vkartk/d67f38578d4eed1b71ab926cb811c70e to your computer and use it in GitHub Desktop.
This is a code example that demonstrates how to connect to multiple Wi-Fi networks and set up a hotspot using an ESP8266 microcontroller. The code uses the ESP8266WiFi and ESP8266WiFiMulti libraries to add two Wi-Fi networks and attempt to connect to them. If both networks are unavailable, the code creates a hotspot with the SSID "nodemcu-hotspo…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* MultipleWiFiWithBackupHotspot.c | |
* | |
* Connect to Multiple Wi-Fi Networks and Set Up a Hotspot as a Backup with ESP8266 | |
* | |
* This is a code example that demonstrates how to connect to multiple Wi-Fi networks and set up a hotspot using an ESP8266 microcontroller. | |
* The code uses the ESP8266WiFi and ESP8266WiFiMulti libraries to add two Wi-Fi networks and attempt to connect to them. | |
* If both networks are unavailable, the code creates a hotspot with the SSID "nodemcu-hotspot" and password "password". | |
* The code is useful for applications where the ESP8266 needs to connect to multiple Wi-Fi networks and have a backup hotspot as a fail-safe mechanism. | |
* | |
* Author: Karthik V | |
*/ | |
#include <ESP8266WiFi.h> // Include the ESP8266 WiFi library | |
#include <ESP8266WiFiMulti.h> // Include the ESP8266 WiFiMulti library | |
const char* ssid1 = "your_wifi_ssid1"; // Replace "your_wifi_ssid1" with your first Wi-Fi network SSID | |
const char* password1 = "your_wifi_password1"; // Replace "your_wifi_password1" with your first Wi-Fi network password | |
const char* ssid2 = "your_wifi_ssid2"; // Replace "your_wifi_ssid2" with your second Wi-Fi network SSID | |
const char* password2 = "your_wifi_password2"; // Replace "your_wifi_password2" with your second Wi-Fi network password | |
ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class | |
void setup() { | |
Serial.begin(9600); // Start the serial communication | |
wifiMulti.addAP(ssid1, password1); // Add the first Wi-Fi network to the ESP8266WiFiMulti instance | |
wifiMulti.addAP(ssid2, password2); // Add the second Wi-Fi network to the ESP8266WiFiMulti instance | |
Serial.println(""); | |
Serial.print("Connecting to Wi-Fi"); | |
int attempts = 0; // Initialize the number of connection attempts | |
while (wifiMulti.run() != WL_CONNECTED && attempts < 10) { // Wait for connection or timeout after 10 attempts | |
Serial.print("."); // Print a dot to the serial monitor | |
delay(1000); | |
attempts++; | |
} | |
if (wifiMulti.run() == WL_CONNECTED) { // Check if the ESP8266 is connected to Wi-Fi | |
Serial.println(""); | |
Serial.println("Connected to Wi-Fi"); | |
} else { | |
Serial.println(""); | |
Serial.println("Timed out, creating hotspot..."); | |
WiFi.softAP("nodemcu-hotspot", "password"); // Create a hotspot with the SSID "nodemcu-hotspot" and password "password" | |
} | |
} | |
void loop() { | |
// Your code goes here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment