{"id":1953,"date":"2020-02-02T11:13:57","date_gmt":"2020-02-02T03:13:57","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1953"},"modified":"2020-05-12T14:14:45","modified_gmt":"2020-05-12T06:14:45","slug":"how-to-setup-micropython-webrepl-on-your-esp32-development-board","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-setup-micropython-webrepl-on-your-esp32-development-board\/","title":{"rendered":"How to setup MicroPython WebREPL on your ESP32 development board"},"content":{"rendered":"<p>When I wrote about <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-setup-micropython-on-your-esp32-development-board-to-run-python-applications\/\" rel=\"noopener\" target=\"_blank\">setting up MicroPython on an ESP development board<\/a>, I came across the <a href=\"https:\/\/docs.micropython.org\/en\/latest\/esp8266\/tutorial\/repl.html#webrepl-a-prompt-over-wifi\" rel=\"noopener\" target=\"_blank\">WebREPL<\/a>. <\/p>\n<p>If you setup MicroPython WebREPL on your ESP32 board, then you can interact with your ESP32 board wirelessly.<\/p>\n<p>In case you need it, this is how to setup MicroPython WebREPL on your ESP32 development board.<\/p>\n<h2>Components of MicroPython WebREPL<\/h2>\n<p>Before setting up MicroPython WebREPL on our ESP32 development board, let's look at the components of MicroPython WebREPL. <\/p>\n<p>All in all, there is the <strong>server component<\/strong> on our ESP32 board that interacts with the hardware. When activated, this server component listens for commands coming from <strong>WebREPL clients<\/strong> through WebSocket. <\/p>\n<p>For example, we can use the <a href=\"http:\/\/micropython.org\/webrepl\/\" rel=\"noopener\" target=\"_blank\">MicroPython WebREPL client<\/a> from a computer that is connected to the same network as our ESP32 board and interact with it.<br \/>\n<img decoding=\"async\" width=\"800\" height=\"456\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Micropython-WebREPL-client-running-from-micropython.org_.gif\" alt=\"MicroPython WebREPL client running from micropython.org\" class=\"aligncenter size-full wp-image-1954\" \/><\/p>\n<p>Since the client is a web page loaded on a browser, we only need to setup the WebREPL server on our ESP32 board.<\/p>\n<h2>Setting up MicroPython WebREPL server on our ESP32 development board<\/h2>\n<p>First, <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-setup-micropython-on-your-esp32-development-board-to-run-python-applications\/\" rel=\"noopener\" target=\"_blank\">setup MicroPython on your ESP32 development board<\/a> if you have not done so.<\/p>\n<p>Once you have done so, you will be able to access MicroPython REPL over serial with a terminal emulator program. For example, you can use the following command to access MicroPython REPL over serial on macOS:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nscreen -port \/dev\/tty.SLAB_USBtoUART 115200\r\n<\/pre>\n<p>At this point in time, you will be able to enable WebREPL on your ESP32 board. In order to do so, enter the following Python codes into the REPL prompt:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nimport webrepl_setup\r\n<\/pre>\n<p>Once you have done so, follow the instructions to enable WebREPL on your ESP32 board:<br \/>\n<img decoding=\"async\" width=\"1140\" height=\"730\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/MicroPython-webrepl_setup-sequence-on-ESP32.gif\" alt=\"MicroPython webrepl_setup sequence on ESP32\" class=\"aligncenter size-full wp-image-1955\" \/><\/p>\n<p>When your ESP32 board rebooted successfully, you will find <strong>webrepl_cfg.py<\/strong> with content similar to the following:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nPASS = 'abcd1234'\r\n<\/pre>\n<p>In addition to that, <strong>boot.py<\/strong> now contains the following codes:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# This file is executed on every boot (including wake-boot from deepsleep)\r\n#import esp\r\n#esp.osdebug(None)\r\nimport webrepl\r\nwebrepl.start()\r\n<\/pre>\n<p>Given that, whenever your ESP32 board starts MicroPython, it will start WebREPL as well.<\/p>\n<p>However, since there is no network connection between your computer and the ESP32 board, you cannot connect to your WebREPL server yet.<\/p>\n<h2>Connecting your ESP32 board to your router network<\/h2>\n<p>Therefore, you need to connect your ESP32 board to your router's <strong>2.4GHz network<\/strong>. Given that, terminate your terminal emulator program session. If you are using <code>screen<\/code>, then you will press <strong>Ctrl-A+K<\/strong>.<\/p>\n<p>Once the serial connection is released, use <code>rshell<\/code> to connect to your ESP32 board via serial connection:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nrshell -p \/dev\/tty.SLAB_USBtoUART -b 115200 --editor nano\r\n<\/pre>\n<p>When you do so, you will be able to edit files on the ESP32 board with the <code>nano<\/code> editor.<\/p>\n<p>Once rshell starts, run the following command to edit <code>boot.py<\/code> on your ESP32 board:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nedit \/pyboard\/boot.py\r\n<\/pre>\n<p>When your nano editor loads <code>boot.py<\/code>, edit it to look like the following:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef do_connect(ssid, pwd):\r\n    import network\r\n    sta_if = network.WLAN(network.STA_IF)\r\n    if not sta_if.isconnected():\r\n        print('connecting to network...')\r\n        sta_if.active(True)\r\n        sta_if.connect(ssid, pwd)\r\n        while not sta_if.isconnected():\r\n            pass\r\n    print('network config:', sta_if.ifconfig())\r\n\r\n# This file is executed on every boot (including wake-boot from deepsleep)\r\n#import esp\r\n#esp.osdebug(None)\r\n\r\n# Attempt to connect to WiFi network\r\ndo_connect('your_ssid', 'your_password')\r\n\r\nimport webrepl\r\nwebrepl.start()\r\n<\/pre>\n<p>Change <strong>your_ssid<\/strong> and <strong>your_password<\/strong> to the credentials (SSID, WiFi password) for your 2.4GHz WiFi network. When you are done with the changes, press <strong>Ctl-X<\/strong>, <strong>Y<\/strong> and <strong>Enter<\/strong> to save the changes.<\/p>\n<p>After you have edited <strong>boot.py<\/strong>, enter into MicroPython REPL:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nrepl\r\n<\/pre>\n<p>Once Python REPL loads, press <strong>Ctrl-D<\/strong> to restart your ESP32 board. When your ESP32 development board had restarted, you should find some output similar to the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nMPY: soft reboot\r\nnetwork config: ('192.168.1.131', '255.255.255.0', '192.168.1.1', '192.168.1.1')\r\nWebREPL daemon started on ws:\/\/192.168.1.131:8266\r\nStarted webrepl in normal mode\r\nMicroPython v1.12-68-g3032ae115 on 2020-01-15; ESP32 module with ESP32\r\nType &quot;help()&quot; for more information.\r\n<\/pre>\n<p>In this case, I will use <strong>ws:\/\/192.168.1.131:8266<\/strong> to connect to my ESP32 development board. If your ESP32 development board had connected to your WiFi network successfully, then you should get the URL to connect to your board. <\/p>\n<h2>Using the MicroPython WebREPL client to interact with your ESP32 development board<\/h2>\n<p>At this point in time, you can interact with your ESP32 development board with the <a href=\"http:\/\/micropython.org\/webrepl\/\" rel=\"noopener\" target=\"_blank\">MicroPython WebREPL client<\/a>. Given that, access <a href=\"http:\/\/micropython.org\/webrepl\/\" rel=\"noopener\" target=\"_blank\">MicroPython WebREPL client<\/a> from a browser within the same network as your ESP32 development board. When the MicroPython WebREPL client loads on your browser, enter the web socket URL into the text field on the top left corner:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/MicroPython-WebREPL-client-with-192.168.1.131-as-the-IP-address-and-8266-as-the-port-to-connect-to-the-WebREPL-server-on-an-ESP32-board.gif\" alt=\"MicroPython WebREPL client with 192.168.1.131 as the IP address and 8266 as the port to connect to the WebREPL server on an ESP32 board\" class=\"aligncenter size-full wp-image-1957\" \/><\/p>\n<p>After you click <strong>Connect<\/strong>, you will be prompted to enter the password (for eg: abcd1234) that you had configured earlier. When you have done so, you can interact with the WebREPL server on your ESP32 board.<br \/>\n<img decoding=\"async\" width=\"800\" height=\"611\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Connecting-to-and-interacting-with-MicroPython-WebREPL-with-a-browser-directed-at-192.168.1.131.gif\" alt=\"Connecting to and interacting with MicroPython WebREPL with a browser directed at 192.168.1.131\" class=\"aligncenter size-full wp-image-1959\" \/><\/p>\n<p>In addition to entering Python commands, you can also send files and get files with the controls on the right.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-vv\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-vv&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-vv&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1953&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When I wrote about <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-setup-micropython-on-your-esp32-development-board-to-run-python-applications\/\" rel=\"noopener\" target=\"_blank\">setting up MicroPython on an ESP development board<\/a>, I came across the <a href=\"https:\/\/docs.micropython.org\/en\/latest\/esp8266\/tutorial\/repl.html#webrepl-a-prompt-over-wifi\" rel=\"noopener\" target=\"_blank\">WebREPL<\/a>. <\/p>\n<p>If you setup MicroPython WebREPL on your ESP32 board, then you can interact with your ESP32 board wirelessly.<\/p>\n<p>In case you need it, this is how to setup MicroPython WebREPL on your ESP32 development board.<\/p>\n","protected":false},"author":1,"featured_media":1959,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[630,484,718,722,226,721,719,720],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Connecting-to-and-interacting-with-MicroPython-WebREPL-with-a-browser-directed-at-192.168.1.131.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-vv","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1953"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1953"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1953\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1959"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}