{"id":110863,"date":"2022-05-13T08:54:59","date_gmt":"2022-05-13T08:54:59","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=110863"},"modified":"2022-05-13T08:55:02","modified_gmt":"2022-05-13T08:55:02","slug":"esp32-wifimulti","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-wifimulti\/","title":{"rendered":"ESP32 WiFiMulti: Connect to the Strongest Wi-Fi Network (from a list of networks)"},"content":{"rendered":"\n<p>Learn how to use WiFiMulti with the ESP32. It allows you to register multiple networks (SSID\/password combinations). The ESP32 will connect to the Wi-Fi network with the strongest signal (RSSI). If the connection is lost, it will connect to the next network on the list.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/04\/ESP32-Wi-Fi-Multi.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 WiFiMulti Connect to the Strongest Wi-Fi Network from a list of networks\" class=\"wp-image-110879\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/04\/ESP32-Wi-Fi-Multi.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/04\/ESP32-Wi-Fi-Multi.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/04\/ESP32-Wi-Fi-Multi.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/04\/ESP32-Wi-Fi-Multi.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>Using WiFiMulti in your ESP32 IoT projects is useful if your board can have access to more than one Wi-Fi network. Implementing this feature in your projects is very simple and improves your projects significantly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP32 with WiFiMulti<\/h2>\n\n\n\n<p>You can easily add WiFiMulti to your ESP32 projects with just a few lines of code. You can find an example in your Arduino IDE. With an ESP32 board select (Tools &gt; Board), go to <strong>File <\/strong>&gt; <strong>Examples <\/strong>&gt; <strong>WiFi <\/strong>&gt; <strong>WifiMulti<\/strong>. <\/p>\n\n\n\n<p>Here are the essential steps to use WiFiMulti with the ESP32.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Include Libraries<\/h3>\n\n\n\n<p>First, you need to include both <span class=\"rnthl rntliteral\">WiFi.h<\/span> and <span class=\"rnthl rntliteral\">WiFiMulti.h<\/span> libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;WiFi.h>\n#include &lt;WiFiMulti.h><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">WiFiMulti Object<\/h3>\n\n\n\n<p>Then, you need to create a <span class=\"rnthl rntliteral\">WiFiMulti<\/span> object:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFiMulti wifiMulti;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Add List of Networks<\/h3>\n\n\n\n<p>Then, in the <span class=\"rnthl rntliteral\">setup()<\/span>, use the <span class=\"rnthl rntliteral\">addAp()<\/span> method on the <span class=\"rnthl rntliteral\">wifiMulti<\/span> object to add a network. The <span class=\"rnthl rntliteral\">addAP()<\/span> method accepts as arguments the network SSID and password. You should add at least one network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>wifiMulti.addAP(\"ssid_from_AP_1\", \"your_password_for_AP_1\");\nwifiMulti.addAP(\"ssid_from_AP_2\", \"your_password_for_AP_2\");\nwifiMulti.addAP(\"ssid_from_AP_3\", \"your_password_for_AP_3\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Connect to Wi-Fi<\/h3>\n\n\n\n<p>Finally, connect to Wi-Fi using the <span class=\"rnthl rntliteral\">run()<\/span> method. You can also print a message in case the Wi-Fi is disconnected.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(wifiMulti.run() != WL_CONNECTED) {\n  Serial.println(\"WiFi not connected!\");\n  delay(1000);\n}<\/code><\/pre>\n\n\n\n<p>You can run this snippet on the <span class=\"rnthl rntliteral\">loop()<\/span> section and if the ESP32 gets disconnected from a Wi-Fi network, it will automatically try to connect to the next strongest network on the list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP32 with WiFiMulti Example<\/h2>\n\n\n\n<p>For you to understand how WiFiMulti works with the ESP32, we created a simple example that does the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>scans for available wi-fi networks and prints their RSSI (so that you can check that the ESP32 is actually connecting to the strongest network on the list);<\/li><li>connects to the strongest wi-fi network from a list of provided networks;<\/li><li>in case it loses connection with the network, it will automatically connect to the next strongest network on the list.<\/li><\/ul>\n\n\n\n<p>To test this, you can copy the following code to your Arduino IDE. It is based on the <a href=\"https:\/\/github.com\/espressif\/arduino-esp32\/blob\/master\/libraries\/WiFi\/examples\/WiFiScan\/WiFiScan.ino\" target=\"_blank\" rel=\"noreferrer noopener\">WiFiScan<\/a> and <a href=\"https:\/\/github.com\/espressif\/arduino-esp32\/blob\/master\/libraries\/WiFi\/examples\/WiFiMulti\/WiFiMulti.ino\" target=\"_blank\" rel=\"noreferrer noopener\">WiFiMulti <\/a>examples provided in the Arduino core examples for the ESP32.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\r\n *  Based on the following examples:\r\n *  WiFi &gt; WiFiMulti: https:\/\/github.com\/espressif\/arduino-esp32\/blob\/master\/libraries\/WiFi\/examples\/WiFiMulti\/WiFiMulti.ino\r\n *  WiFi &gt; WiFiScan: https:\/\/github.com\/espressif\/arduino-esp32\/blob\/master\/libraries\/WiFi\/examples\/WiFiScan\/WiFiScan.ino\r\n *  Complete project details at our blog: https:\/\/RandomNerdTutorials.com\/\r\n *  \r\n *\/\r\n\r\n#include &lt;WiFi.h&gt;\r\n#include &lt;WiFiMulti.h&gt;\r\n\r\nWiFiMulti wifiMulti;\r\n\r\n\/\/ WiFi connect timeout per AP. Increase when connecting takes longer.\r\nconst uint32_t connectTimeoutMs = 10000;\r\n\r\nvoid setup(){\r\n  Serial.begin(115200);\r\n  delay(10);\r\n  WiFi.mode(WIFI_STA);\r\n  \r\n  \/\/ Add list of wifi networks\r\n  wifiMulti.addAP(&quot;ssid_from_AP_1&quot;, &quot;your_password_for_AP_1&quot;);\r\n  wifiMulti.addAP(&quot;ssid_from_AP_2&quot;, &quot;your_password_for_AP_2&quot;);\r\n  wifiMulti.addAP(&quot;ssid_from_AP_3&quot;, &quot;your_password_for_AP_3&quot;);\r\n\r\n  \/\/ WiFi.scanNetworks will return the number of networks found\r\n  int n = WiFi.scanNetworks();\r\n  Serial.println(&quot;scan done&quot;);\r\n  if (n == 0) {\r\n      Serial.println(&quot;no networks found&quot;);\r\n  } \r\n  else {\r\n    Serial.print(n);\r\n    Serial.println(&quot; networks found&quot;);\r\n    for (int i = 0; i &lt; n; ++i) {\r\n      \/\/ Print SSID and RSSI for each network found\r\n      Serial.print(i + 1);\r\n      Serial.print(&quot;: &quot;);\r\n      Serial.print(WiFi.SSID(i));\r\n      Serial.print(&quot; (&quot;);\r\n      Serial.print(WiFi.RSSI(i));\r\n      Serial.print(&quot;)&quot;);\r\n      Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?&quot; &quot;:&quot;*&quot;);\r\n      delay(10);\r\n    }\r\n  }\r\n\r\n  \/\/ Connect to Wi-Fi using wifiMulti (connects to the SSID with strongest connection)\r\n  Serial.println(&quot;Connecting Wifi...&quot;);\r\n  if(wifiMulti.run() == WL_CONNECTED) {\r\n    Serial.println(&quot;&quot;);\r\n    Serial.println(&quot;WiFi connected&quot;);\r\n    Serial.println(&quot;IP address: &quot;);\r\n    Serial.println(WiFi.localIP());\r\n  }\r\n}\r\n\r\nvoid loop(){\r\n  \/\/if the connection to the stongest hotstop is lost, it will connect to the next network on the list\r\n  if (wifiMulti.run(connectTimeoutMs) == WL_CONNECTED) {\r\n    Serial.print(&quot;WiFi connected: &quot;);\r\n    Serial.print(WiFi.SSID());\r\n    Serial.print(&quot; &quot;);\r\n    Serial.println(WiFi.RSSI());\r\n  }\r\n  else {\r\n    Serial.println(&quot;WiFi not connected!&quot;);\r\n  }\r\n  delay(1000);\r\n}\r\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_WiFiMulti.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Don&#8217;t forget to add a list of networks on the following lines. You can multiply those lines to add more networks.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>wifiMulti.addAP(\"ssid_from_AP_1\", \"your_password_for_AP_1\");\nwifiMulti.addAP(\"ssid_from_AP_2\", \"your_password_for_AP_2\");\nwifiMulti.addAP(\"ssid_from_AP_3\", \"your_password_for_AP_3\");<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> if you want to test this project, but at the moment, you only have access to one network, you can create a hotspot with your smartphone and add the hotspot name and password to the list of available networks. I tested this with my iPhone and it worked perfectly (<strong>you may need to remove spaces and special characters from the hotspot name<\/strong>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP32 with WiFiMulti Demonstration<\/h2>\n\n\n\n<p>After adding a list of networks to your code, you can upload it to your ESP32.<\/p>\n\n\n\n<p>Open the Serial Monitor at a baud rate of 115200 and press the ESP32 RST button to restart the board.<\/p>\n\n\n\n<p>First, it will show a list of nearby networks and corresponding RSSI. In my case, I have access to the first and third networks. In my case, the ESP32 connects to the iPhone network which is the strongest on the list (an RSSI closer to zero means a stronger signal).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"601\" height=\"661\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/04\/WiFi-Multi-Demonstration.png?resize=601%2C661&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"WiFiMulti Example with the ESP32 Scan and Connect to Network\" class=\"wp-image-110869\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/04\/WiFi-Multi-Demonstration.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/04\/WiFi-Multi-Demonstration.png?resize=273%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 273w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n\n<p>If I remove the iPhone hotspot, the connection will be lost and it will connect to the next strongest network on the list.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"601\" height=\"397\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/04\/WiFi-Multi-Demonstration-2.png?resize=601%2C397&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"WiFiMulti Example with the ESP32 Connect to the Next Network on the List\" class=\"wp-image-110871\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/04\/WiFi-Multi-Demonstration-2.png?w=601&amp;quality=100&amp;strip=all&amp;ssl=1 601w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/04\/WiFi-Multi-Demonstration-2.png?resize=300%2C198&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to use WiFiMulti with the ESP32 to add a list of networks that the ESP32 can connect to. It will connect to the network with the strongest signal (RSSI). If it loses connection with that network, it will automatically try to connect to the next network on the list.<\/p>\n\n\n\n<p>We hope you find this tutorial useful. We have other tutorials related to Wi-Fi functions with the ESP32 that you may find useful:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-useful-wi-fi-functions-arduino\/\">ESP32 Useful Wi-Fi Library Functions (Arduino IDE)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/solved-reconnect-esp32-to-wifi\/\">[SOLVED] Reconnect ESP32 to Wi-Fi Network After Lost Connection<\/a><\/li><\/ul>\n\n\n\n<p>Learn more about the ESP32 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build Web Servers with ESP32 and ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/firebase-esp32-esp8266-ebook\/\">Firebase Web App with ESP32 and ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">Free ESP32 Projects and Tutorials\u2026<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to use WiFiMulti with the ESP32. It allows you to register multiple networks (SSID\/password combinations). The ESP32 will connect to the Wi-Fi network with the strongest signal (RSSI). &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 WiFiMulti: Connect to the Strongest Wi-Fi Network (from a list of networks)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-wifimulti\/#more-110863\" aria-label=\"Read more about ESP32 WiFiMulti: Connect to the Strongest Wi-Fi Network (from a list of networks)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":110879,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,281,277,299,264],"tags":[],"class_list":["post-110863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/04\/ESP32-Wi-Fi-Multi.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/110863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=110863"}],"version-history":[{"count":12,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/110863\/revisions"}],"predecessor-version":[{"id":111098,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/110863\/revisions\/111098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/110879"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=110863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=110863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=110863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}