{"id":89319,"date":"2019-09-17T12:43:14","date_gmt":"2019-09-17T12:43:14","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=89319"},"modified":"2019-09-21T09:59:30","modified_gmt":"2019-09-21T09:59:30","slug":"micropython-esp32-esp8266-access-point-ap","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-access-point-ap\/","title":{"rendered":"MicroPython: ESP32\/ESP8266 Access Point (AP)"},"content":{"rendered":"\n<p>Learn how to set your ESP32 or ESP8266 boards as an Access Point (AP) using MicroPython firmware. This allows you to connect directly to your ESP boards via Wi-Fi without a wireless router.  <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/ESP32-ESP8266-Access-Point-MicroPython.png?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython: ESP32\/ESP8266 Access Point (AP)\" class=\"wp-image-89323\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/ESP32-ESP8266-Access-Point-MicroPython.png?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/ESP32-ESP8266-Access-Point-MicroPython.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/ESP32-ESP8266-Access-Point-MicroPython.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/ESP32-ESP8266-Access-Point-MicroPython.png?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>Use the following snippet to set your ESP32 or ESP8266 as an access point using MicroPython:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ap = network.WLAN(network.AP_IF)\nap.active(True)\nap.config(essid=ssid, password=password)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Thonny IDE:<ul><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Installing and getting started with Thonny IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/flashing-micropython-firmware-esptool-py-esp32-esp8266\/\">Flashing MicroPython Firmware with esptool.py<\/a><\/li><\/ul><\/li><li>uPyCraft IDE:<ul><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">Getting Started with uPyCraft IDE<\/a><\/li><li>Install uPyCraft IDE (<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\">Windows<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\">Mac OS X<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\">Linux<\/a>)<\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li><\/ul><\/li><\/ul>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about MicroPython:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266 eBook<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP32\/ESP8266 Station and Access Point<\/h2>\n\n\n\n<p>In most of our web server projects with MicroPython, we connect the ESP32 or the ESP8266 to a wireless router. In this configuration, we can access the ESP board through the local network.<\/p>\n\n\n\n<p>In this scenario, the router acts as an access point and the ESP boards are set as a station. So, you need to be connected to your router (local network) to control the ESP32 or ESP8266.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"362\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/ESP32-esp8266-station.jpg?resize=750%2C362&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32\/ESP8266 Station MicroPython firmware\" class=\"wp-image-89324\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/ESP32-esp8266-station.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/ESP32-esp8266-station.jpg?resize=300%2C145&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>In some cases, this might not be the best configuration (when you don\u2019t have a router nearby). But if you set the ESP boards as an access point (hotspot), you can be connected to them using any device with Wi-Fi capabilities without the need to connect to your router.<\/p>\n\n\n\n<p>Basically, when you set the ESP32 or ESP8266 as an access point you create its own Wi\u2011Fi network and nearby Wi-Fi devices (stations) can connect to it (like your smartphone or your computer).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"446\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/ESP32-esp8266-access-point.jpg?resize=750%2C446&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32\/ESP8266 Access Point (AP) MicroPython firmware\" class=\"wp-image-89325\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/ESP32-esp8266-access-point.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/ESP32-esp8266-access-point.jpg?resize=300%2C178&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>In this tutorial, we\u2019ll show you how to set the ESP32 and ESP8266 as an access point in your web server projects. This way, you don\u2019t need to be connected to a router to control them.<\/p>\n\n\n\n<p>Because the ESP doesn\u2019t connect further to a wired network (like your router), it is called soft-AP (<strong>soft Access Point<\/strong>).<\/p>\n\n\n\n<p>This means that if you try to load libraries or use firmware from the internet, it will not work. It also doesn\u2019t work if you try to make HTTP requests to services on the internet like publishing sensor readings to the cloud.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP32\/ESP8266 MicroPython Access Point (AP) for Web Server<\/h2>\n\n\n\n<p>For demonstration purposes, we&#8217;ll create a simple &#8220;Hello, World! web server. To learn more about how to create a web server with the ESP32 or ESP8266, you can read the following tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-micropython-web-server\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"ESP32\/ESP8266 MicroPython Web Server \u2013 Control Outputs (opens in a new tab)\">ESP32\/ESP8266 MicroPython Web Server \u2013 Control Outputs<\/a><\/li><\/ul>\n\n\n\n<p>Copy the following code to your <em>boot.py<\/em> file and upload it to your board.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\n\ntry:\n  import usocket as socket\nexcept:\n  import socket\n\nimport network\n\nimport esp\nesp.osdebug(None)\n\nimport gc\ngc.collect()\n\nssid = 'MicroPython-AP'\npassword = '123456789'\n\nap = network.WLAN(network.AP_IF)\nap.active(True)\nap.config(essid=ssid, password=password)\n\nwhile ap.active() == False:\n  pass\n\nprint('Connection successful')\nprint(ap.ifconfig())\n\ndef web_page():\n  html = &quot;&quot;&quot;&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;&lt;\/head&gt;\n  &lt;body&gt;&lt;h1&gt;Hello, World!&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;&quot;&quot;&quot;\n  return html\n\ns = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\ns.bind(('', 80))\ns.listen(5)\n\nwhile True:\n  conn, addr = s.accept()\n  print('Got a connection from %s' % str(addr))\n  request = conn.recv(1024)\n  print('Content = %s' % str(request))\n  response = web_page()\n  conn.send(response)\n  conn.close()\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP-MicroPython\/esp_access_point_boot.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Customize the SSID and Password<\/h3>\n\n\n\n<p>You need to define a SSID name and a password to your access point. In this example, we\u2019re setting the SSID name to&nbsp;<span class=\"rnthl rntliteral\">MicroPython-AP<\/span>, but you can modify the name to whatever you want. The password is&nbsp;<span class=\"rnthl rntliteral\">123456789<\/span>, but you can also modify it. <\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ssid = 'MicroPython-AP'\npassword = '123456789'<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Setting an Access Point with MicroPython<\/h3>\n\n\n\n<p>Then, create an access point using the following line of code:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ap = network.WLAN(network.AP_IF)<\/code><\/pre>\n\n\n\n<p>Activate the access point:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ap.active(True)<\/code><\/pre>\n\n\n\n<p>Configure the access point with the ssid and password you&#8217;ve defined earlier:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>ap.config(essid=ssid, password=password)<\/code><\/pre>\n\n\n\n<p>The following lines print the access point IP address<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>print('Connection successful')\nprint(ap.ifconfig())<\/code><\/pre>\n\n\n\n<p>By default, the IP address is <strong>192.168.4.1<\/strong><\/p>\n\n\n\n<p>Now, the access point is created.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Socket Server<\/h3>\n\n\n\n<p>For demonstrations purposes, we&#8217;re creating a socket server that displays an &#8220;Hello, Wolrd!&#8221; message.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def web_page():\n  html = \"\"\"&lt;html>&lt;head>&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&lt;\/head>&lt;body>&lt;h1>Hello, World!&lt;\/h1>&lt;\/body>&lt;\/html>\"\"\"\n  return html\n\ns = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\ns.bind(('', 80))\ns.listen(5)\n\nwhile True:\n  conn, addr = s.accept()\n  print('Got a connection from %s' % str(addr))\n  request = conn.recv(1024)\n  print('Content = %s' % str(request))\n  response = web_page()\n  conn.send(response)\n  conn.close()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Connecting to the Access Point (AP)<\/h2>\n\n\n\n<p>After uploading the code as<em> boot.py<\/em> to your ESP32 or ESP8266 board, in your smartphone open your Wi-Fi settings and tap the&nbsp;<strong>MicroPython-AP<\/strong> network: <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"633\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/micropython-access-point-connecting.png?resize=300%2C633&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Connecting to ESP32 or ESP8266 Access Point (AP) using MicroPython\" class=\"wp-image-89349\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/micropython-access-point-connecting.png?w=300&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/micropython-access-point-connecting.png?resize=142%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 142w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure><\/div>\n\n\n\n<p>Enter the password you\u2019ve defined earlier.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"221\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/connect-to-micropython-access-point-insert-password.png?resize=300%2C221&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Connect to MicroPython Access Point Insert Password\" class=\"wp-image-89350\"\/><\/figure><\/div>\n\n\n\n<p>Open your web browser and type the IP address&nbsp;<strong>http:\/\/192.168.4.1<\/strong>. The web server page should load: <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"223\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/Web-Server-Access-Point-ESP32-ESP8266-MicroPython.png?resize=300%2C223&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython Access Point (AP) web browser test\" class=\"wp-image-89351\"\/><\/figure><\/div>\n\n\n\n<p>You should have a similar message showing up on the Shell:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1072\" height=\"211\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/MicroPython-Access-Point-Shell.png?resize=1072%2C211&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython Access Point (AP) web browser debugging information\" class=\"wp-image-89355\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/MicroPython-Access-Point-Shell.png?w=1072&amp;quality=100&amp;strip=all&amp;ssl=1 1072w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/MicroPython-Access-Point-Shell.png?resize=300%2C59&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/MicroPython-Access-Point-Shell.png?resize=768%2C151&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/MicroPython-Access-Point-Shell.png?resize=1024%2C202&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1072px) 100vw, 1072px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial you&#8217;ve learned how to set your EPS32 or ESP8266 boards as soft access point (soft-AP) using MicroPython. If you want to learn how to do the same using Arduino IDE, read the following guides:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-access-point-ap-web-server\/\">ESP32 Access Point (AP) for Web Server using Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-access-point-ap-web-server\/\">ESP8266 NodeMCU Access Point (AP) for Web Server using Arduino IDE<\/a><\/li><\/ul>\n\n\n\n<p>Learn more about MicroPython with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-gpios-esp32-esp8266\/\">MicroPython with ESP32 and ESP8266: Interacting with GPIOs<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-micropython-web-server\/\">ESP32\/ESP8266 MicroPython Web Server \u2013 Control Outputs<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-mqtt-esp32-esp8266\/\">MicroPython \u2013 Getting Started with MQTT on ESP32\/ESP8266<\/a><\/li><li><strong><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266 (eBook)<\/a><\/strong><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to set your ESP32 or ESP8266 boards as an Access Point (AP) using MicroPython firmware. This allows you to connect directly to your ESP boards via Wi-Fi without &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython: ESP32\/ESP8266 Access Point (AP)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-access-point-ap\/#more-89319\" aria-label=\"Read more about MicroPython: ESP32\/ESP8266 Access Point (AP)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":89323,"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":[281,265,309,264,1],"tags":[],"class_list":["post-89319","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp8266-project","category-0-esp32-micropython","category-project","category-uncategorized"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/09\/ESP32-ESP8266-Access-Point-MicroPython.png?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/89319","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=89319"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/89319\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/89323"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=89319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=89319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=89319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}