{"id":1948,"date":"2020-01-15T23:49:09","date_gmt":"2020-01-15T15:49:09","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1948"},"modified":"2020-05-12T14:13:56","modified_gmt":"2020-05-12T06:13:56","slug":"how-to-setup-micropython-on-your-esp32-development-board-to-run-python-applications","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-setup-micropython-on-your-esp32-development-board-to-run-python-applications\/","title":{"rendered":"How to setup MicroPython on your ESP32 development board to run Python applications"},"content":{"rendered":"<p>When my friend <a href=\"https:\/\/youssefnotes.com\/\" rel=\"noopener\" target=\"_blank\">Youssef<\/a> shared a link to <a href=\"http:\/\/micropython.org\/\" rel=\"noopener\" target=\"_blank\">MicroPython<\/a>, I kept it at the back of my mind. Since I had found some time to sharpen my saw, I decided to take a look at it.<\/p>\n<p>If I get familiar MicroPython, then I can perform machine learning magic with an AIOT board like <a href=\"https:\/\/www.amazon.com\/Seeed-Studio-Sipeed-Maixduino-RISC-V\/dp\/B07SW9ZWQQ\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=62cae0a12f7445d3e481176dbd619052&language=en_US\" rel=\"noopener\" target=\"_blank\">Sipeed Maixduino Kit<\/a> with greater ease.<\/p>\n<p>Given that in mind, here are the steps that I took to setup MicroPython on my <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=ESP32+development+board&ref=nb_sb_noss_2&linkCode=ll2&tag=clivsperswebs-20&linkId=ffe715c4165e21ea1645738b5da04a8d&language=en_US\" rel=\"noopener\" target=\"_blank\">ESP32 development board<\/a> to kickstart my learning. <\/p>\n<p>In case you wish to setup MicroPython on your ESP32 development board to run Python applications, read on to find out more.<\/p>\n<h2>Downloading a copy of MicroPython for your ESP32 development board<\/h2>\n<p>First and foremost, we will need to download a copy of the MicroPython binary for our ESP32 development board. Therefore, head over to <a href=\"http:\/\/micropython.org\/download#esp32\" rel=\"noopener\" target=\"_blank\">MicroPython's download section for ESP32 boards<\/a>. For example, I had chosen to download <strong>esp32-idf4-20200115-v1.12-68-g3032ae115.bin<\/strong> into the <strong>~\/micropython\/images<\/strong> folder on my computer.<\/p>\n<h2>Installing a USB to UART bridge driver on your computer<\/h2>\n<p>In order to get the binary file onto our ESP32 development board, we will need a USB to UART driver. For example, if your development board has a CP210x chip to handle serial communication, you can install one of the <a href=\"https:\/\/www.silabs.com\/products\/development-tools\/software\/usb-to-uart-bridge-vcp-drivers\" rel=\"noopener\" target=\"_blank\">CP210x USB to UART Bridge VCP Drivers from Silicon Labs<\/a>. If you <a href=\"https:\/\/www.techcoil.com\/blog\/enabling-esp32-development-on-arduino-ide\/\" rel=\"noopener\" target=\"_blank\">enable ESP32 development on your Arduino IDE<\/a>, then you will have already installed the driver.<\/p>\n<h2>Preparing a Python environment with esptool installed<\/h2>\n<p>After your computer is able to communicate with your ESP32 development board, get a copy of <a href=\"https:\/\/github.com\/espressif\/esptool\" rel=\"noopener\" target=\"_blank\">esptool<\/a>. Since a Python virtual environment helps us isolate Python dependencies for different applications, you may want to install your esptool into one of them.<\/p>\n<p>Given these points, let's install our esptool into a Python 3 virtual environment. If you are using a linux or unix environment, <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-create-a-virtual-environment-for-your-python-3-application-with-python3-venv-in-linux-or-unix\/\" rel=\"noopener\" target=\"_blank\">this is how you can create a virtual environment for your Python 3 application with python3-venv in Linux or Unix<\/a>. However, if you are using Windows, <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-create-a-python-3-virtual-environment-in-windows-10\/\">this is how to create a Python 3 virtual environment in Windows 10<\/a>.<\/p>\n<p>Since I am using a Mac to create this tutorial, I first create a Python 3 virtual environment with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython3 -m venv ~\/micropython\/tools-env\r\n<\/pre>\n<p>After the virtual environment is created, I then activate the environment within my terminal session with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsource ~\/micropython\/tools-env\/bin\/activate\r\n<\/pre>\n<p>When the virtual environment is activated, I then install esptool with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install esptool\r\n<\/pre>\n<p>After the pip installation completes, we will be able to use the esptool by running the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nesptool.py\r\n<\/pre>\n<p>When you do so, you will see the help menu in the output.<\/p>\n<h2>Getting the MicroPython binary onto your ESP32 development board<\/h2>\n<p>At this point in time, you will have the necessary tools to get MicroPython onto your ESP32 development board.<\/p>\n<p>First, erase everything from your ESP32 board's flash with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nesptool.py --chip esp32 --port \/dev\/tty.SLAB_USBtoUART erase_flash\r\n<\/pre>\n<p>After the flash is erased, run the following command to put the MicroPython binary that you have downloaded onto your board:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nesptool.py --chip esp32 --port \/dev\/tty.SLAB_USBtoUART --baud 460800 write_flash -z 0x1000 ~\/micropython\/images\/esp32-idf4-20200115-v1.12-68-g3032ae115.bin\r\n<\/pre>\n<p>When the command completes, you will be able to run Python codes on your ESP32 board via MicroPython.<\/p>\n<h2>Verifying your MicroPython setup on your ESP32 development board<\/h2>\n<p>So how can we know if MicroPython is correctly installed on our ESP32 development board?<\/p>\n<p>When MicroPython is correctly installed, we will be able to access the REPL over serial with a terminal emulator program.<\/p>\n<p>For example, you can use the screen command if you are running macOS:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nscreen -port \/dev\/tty.SLAB_USBtoUART 115200\r\n<\/pre>\n<p>After you ran this command, you may find a blank screen. When you see the blank screen, hit <strong>Enter<\/strong> and you should be able to get the REPL prompt. If you want to see the MicroPython trace, then you can press <strong>Ctrl-D<\/strong> to initiate a soft reboot. At this point in time, you can try running some Python codes on the REPL:<br \/>\n<img decoding=\"async\" width=\"585\" height=\"365\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Using-screen-to-test-out-MicroPython-esp32-idf4-20200115-v1.12-68-g3032ae115.bin-on-ESP32-development-board.gif\" alt=\"Using screen to test out MicroPython esp32-idf4-20200115-v1.12-68-g3032ae115.bin on ESP32 development board\" class=\"aligncenter size-full wp-image-1950\" \/><\/p>\n<h2>Finding out the installed modules on your MicroPython installation<\/h2>\n<p>If you wish to find out the modules that are available with your MicroPython installation, you can enter the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nhelp('modules')\r\n<\/pre>\n<p>When I do so in my MicroPython REPL, I got the following output trace:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n__main__          gc                uctypes           urequests\r\n_boot             inisetup          uerrno            uselect\r\n_onewire          machine           uhashlib          usocket\r\n_thread           math              uhashlib          ussl\r\n_webrepl          micropython       uheapq            ustruct\r\napa106            neopixel          uio               utime\r\nbtree             network           ujson             utimeq\r\nbuiltins          ntptime           umqtt\/robust      uwebsocket\r\ncmath             onewire           umqtt\/simple      uzlib\r\ndht               sys               uos               webrepl\r\nds18x20           uarray            upip              webrepl_setup\r\nesp               ubinascii         upip_utarfile     websocket_helper\r\nesp32             ubluetooth        upysh\r\nflashbdev         ucollections      urandom\r\nframebuf          ucryptolib        ure\r\nPlus any modules on the filesystem\r\n<\/pre>\n<p>Given that, you can then check out <a href=\"http:\/\/docs.micropython.org\/en\/latest\/library\/index.html\" rel=\"noopener\" target=\"_blank\">MicroPython's libraries documentation<\/a> on how to use them.<\/p>\n<p>Before continuing with this post, quit your terminal emulator program and make sure that it does not lock the serial driver to your board. <\/p>\n<p>If you are using screen, you can press <strong>Ctrl-A+K<\/strong> to kill the session terminal. If you press <strong>Ctrl-A+D<\/strong> instead, then you will need to <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-terminate-screen-terminal-sessions-cleanly-on-macos\/\" rel=\"noopener\" target=\"_blank\">terminate your screen session cleanly<\/a>. <\/p>\n<h2>Getting Python scripts onto your ESP32 board<\/h2>\n<p>At this point in time, you can find <strong>boot.py<\/strong> when you run the following Python codes in your MicroPython REPL:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nimport os\r\nos.listdir()\r\n<\/pre>\n<p>When you setup MicroPython on your ESP32 board, it will run <strong>boot.py<\/strong> on every boot, including wake-boot from deep sleep, and <strong>main.py<\/strong> after that.<\/p>\n<p>Therefore, in order to run your own Python scripts on your ESP32 board, you need to get these two files into your ESP32 board. <\/p>\n<h3>Installing Remote MicroPython shell on your Python virtual environment<\/h3>\n<p>The <a href=\"https:\/\/github.com\/dhylands\/rshell\" rel=\"noopener\" target=\"_blank\">Remote MicroPython shell<\/a> is a useful tool to help us get our own Python scripts onto our ESP32 board. Therefore, you may want to install it into your Python environment with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install rshell\r\n<\/pre>\n<h3>Using rshell to connect to your ESP32 development board<\/h3>\n<p>After you have installed rshell into your Python virtual environment, run the following command to connect it to your board:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nrshell -p \/dev\/tty.SLAB_USBtoUART -b 115200\r\n<\/pre>\n<p>When rshell is able to connect to your MicroPython instance on your ESP32 board, you should see output similar to the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nUsing buffer-size of 32\r\nConnecting to \/dev\/tty.SLAB_USBtoUART (buffer-size 32)...\r\nTrying to connect to REPL  connected\r\nTesting if ubinascii.unhexlify exists ... Y\r\nRetrieving root directories ... \/boot.py\/\r\nSetting time ... Jan 15, 2020 15:19:09\r\nEvaluating board_name ... pyboard\r\nRetrieving time epoch ... Jan 01, 2000\r\nWelcome to rshell. Use Control-D (or the exit command) to exit rshell.\r\n<\/pre>\n<p>At this point in time, you will be able to use rshell to access the filesystems of your computer and your ESP32 board. Since the root folder of your ESP32 board is mapped to <strong>\/pyboard<\/strong>, you can list its contents via the following command call:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nls \/pyboard\r\n<\/pre>\n<p>When you do so, you will find <strong>boot.py<\/strong> in the output. <\/p>\n<h3>Copying files into your ESP32 development board via rshell<\/h3>\n<p>Given that the root folder of our ESP32 board is mappe to <strong>\/pyboard<\/strong>, we can use the <strong>cp<\/strong> command within rshell to get files into the board.<\/p>\n<p>For example, if we have <strong>~\/micropython\/codes\/boot.py<\/strong> on our host computer, we can run the following command to get it into the ESP32 board:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncp ~\/micropython\/codes\/boot.py \/pyboard\/boot.py\r\n<\/pre>\n<p>We can also copy everything in a folder onto the ESP32:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncp ~\/micropython\/codes\/* \/pyboard\/\r\n<\/pre>\n<p><img decoding=\"async\" width=\"600\" height=\"900\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/How-to-setup-MicroPython-on-your-ESP32-development-board-to-run-Python-applications.jpg\" alt=\"How to setup MicroPython on your ESP32 development board to run Python applications\" class=\"aligncenter size-full wp-image-1960\" \/><\/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-vq\" 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-vq&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-vq&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%2F1948&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 my friend <a href=\"https:\/\/youssefnotes.com\/\" rel=\"noopener\" target=\"_blank\">Youssef<\/a> shared a link to <a href=\"http:\/\/micropython.org\/\" rel=\"noopener\" target=\"_blank\">MicroPython<\/a>, I kept it at the back of my mind. Since I had found some time to sharpen my saw, I decided to take a look at it.<\/p>\n<p>If I get familiar MicroPython, then I can perform machine learning magic with an AIOT board like <a href=\"https:\/\/www.amazon.com\/Seeed-Studio-Sipeed-Maixduino-RISC-V\/dp\/B07SW9ZWQQ\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=62cae0a12f7445d3e481176dbd619052&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">Sipeed Maixduino Kit<\/a> with greater ease.<\/p>\n<p>Given that in mind, here are the steps that I took to setup MicroPython on my <a href=\"https:\/\/www.amazon.com\/s\/ref=as_li_ss_tl?k=ESP32+development+board&#038;ref=nb_sb_noss_2&#038;linkCode=ll2&#038;tag=clivsperswebs-20&#038;linkId=ffe715c4165e21ea1645738b5da04a8d&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">ESP32 development board<\/a> to kickstart my learning. <\/p>\n<p>In case you wish to setup MicroPython on your ESP32 development board to run Python applications, read on to find out more.<\/p>\n","protected":false},"author":1,"featured_media":1950,"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,718,226,719],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Using-screen-to-test-out-MicroPython-esp32-idf4-20200115-v1.12-68-g3032ae115.bin-on-ESP32-development-board.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-vq","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1948"}],"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=1948"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1948\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1950"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}