{"id":212,"date":"2017-05-03T06:23:45","date_gmt":"2017-05-03T13:23:45","guid":{"rendered":"http:\/\/www.kenwalger.com\/blog\/?p=212"},"modified":"2019-01-18T16:42:32","modified_gmt":"2019-01-19T00:42:32","slug":"micropython-and-nodemcu-esp8266","status":"publish","type":"post","link":"https:\/\/www.kenwalger.com\/blog\/iot\/micropython-and-nodemcu-esp8266\/","title":{"rendered":"MicroPython and the NodeMCU ESP8266"},"content":{"rendered":"<p>In a <a href=\"http:\/\/www.kenwalger.com\/blog\/python\/an-overview-of-micropython\/\" target=\"_blank\" rel=\"noopener\">previous post<\/a>, I looked briefly at <a href=\"http:\/\/www.micropython.org\" target=\"_blank\" rel=\"noopener\">MicroPython<\/a> and it&#8217;s place and role in the Internet of Things (IoT) arena. In this post, I&#8217;d like to walk through the process of getting a <a href=\"http:\/\/nodemcu.com\/index_en.html\" target=\"_blank\" rel=\"noopener\">NodeMCU<\/a> ESP8266 device flashed and up and running with MicroPython. Then we&#8217;ll do the, almost required, IoT version of &#8220;Hello World&#8221;&#8230; a blinking LED light.<\/p>\n<p>There are multiple micro-controller options available for using MicroPython so why, you may ask, have I decided on the NodeMCU? First and foremost they are inexpensive. You can find the NodeMCU ESP8266 development boards on <a href=\"https:\/\/www.ebay.com\/p\/?iid=191607499973&amp;lpid=82&amp;&amp;&amp;ul_noapp=true&amp;chn=ps\" target=\"_blank\" rel=\"noopener\">eBay<\/a> for under $5.00.\u00a0Granted that is from a factory overseas, but for under $10.00 you can get one on <a href=\"https:\/\/www.amazon.com\/gp\/product\/B01N0B48NI\/ref=as_li_tl?ie=UTF8&amp;tag=kenwalgersite-20&amp;camp=1789&amp;creative=9325&amp;linkCode=as2&amp;creativeASIN=B01N0B48NI&amp;linkId=dc1e9f82e2beb42bca36616a22b8820d\" target=\"_blank\" rel=\"noopener\">Amazon<\/a>. The second reason I like this board, in particular, is that it has LED lights built into the board so I don&#8217;t necessarily need to break out LED bulbs and resistors. Another big plus is that it is WiFi ready and with MicroPython that means that one can set up a web interface with the board and also readily make truly connected projects without additional hardware.<\/p>\n<p>All that said and done I&#8217;ll be using a basic <a href=\"https:\/\/www.amazon.com\/gp\/product\/B01N0B48NI\/ref=as_li_tl?ie=UTF8&amp;tag=kenwalgersite-20&amp;camp=1789&amp;creative=9325&amp;linkCode=as2&amp;creativeASIN=B01N0B48NI&amp;linkId=dc1e9f82e2beb42bca36616a22b8820d\" target=\"_blank\" rel=\"noopener\">NodeMCU ESP8266<\/a>\u00a0device for this tutorial. Feel free to use a different physical device with the same chip, just know that some settings\u00a0<em>may<\/em> be different based on the physical device configuration. The board has a USB interface which, when connected to a PC provides power.<\/p>\n<h3>Firmware<\/h3>\n<p>We obviously need to get a copy of the MicroPython firmware. It comes in a <code>.bin<\/code> file format that can be loaded directly to the ESP8266 device and can be downloaded from the MicroPython site <a href=\"http:\/\/micropython.org\/download#esp8266\" target=\"_blank\" rel=\"noopener\">here<\/a>. For this tutorial, I will be using the <code>esp8266-20170108-v1.8.7.bin<\/code> version of firmware. You will want to download that to your computer and remember where\u00a0it is saved.<\/p>\n<p>Now we need to deploy the firmware to our device. If you are using the above-mentioned board, deploying the firmware should be relatively straight forward. If you are using a different ESP8266 board, check with the manufacturer of your board for the proper flashing technique. For the rest of us, we will want to start with the <a href=\"https:\/\/github.com\/espressif\/esptool\/\">esptool<\/a> and use it to move the firmware over to our device. The tool can either be downloaded from GitHub directly or can be installed using pip with:<\/p>\n<pre>python -m pip install esptool\n<\/pre>\n<p>This version of esptool supports Python version 3.4 or newer.<\/p>\n<h4>Serial Ports<\/h4>\n<p>Once we have that installed on our system and the device connected via the USB port, we should, for best practice, clear the device by erasing its current state. Assuming that the port name of the device is <code>COM4<\/code>, we can accomplish that with the following command:<\/p>\n<pre>esptool.py --port COM4 erase_flash<\/pre>\n<p>Just substitute COM4 for whichever communication port your particular device is using. It may be something similar to <code>\/dev\/ttyUSB1<\/code> on a non Windows machine and from a terminal port, you can use the command <code>ls \/dev\/tty.*<\/code> to detect the port. Also, depending on your ESP8266 device you might need to install the <a href=\"https:\/\/www.silabs.com\/products\/development-tools\/software\/usb-to-uart-bridge-vcp-drivers\" target=\"_blank\" rel=\"noopener\">USB to UART Bridge VCP Drivers<\/a>\u00a0the be able to detect the device via a serial connection. Once the device has been flashed (it takes less than 10 seconds on my machine) we upload the firmware to the device with the following command:<\/p>\n<pre>esptool.py --port COM4 --baud 460800 write_flash --flash_size=detect 0 esp8266-20170108-v1.8.7.bin\n<\/pre>\n<p>The filename of the firmware should match the downloaded firmware. Assuming you don&#8217;t get any errors during the flashing process, you now have a NodeMCU ESP8266 device running MicroPython. Pretty slick, eh?<\/p>\n<h3>Serial Terminal<\/h3>\n<p>We can now connect to the device using a serial terminal tool, like <a href=\"http:\/\/www.putty.org\/\" target=\"_blank\" rel=\"noopener\">PuTTY<\/a> for Windows, the screen program on Mac, or something like picocom for Linux. I&#8217;ll be using PuTTY since I&#8217;m currently on a Windows 10 machine.<\/p>\n<p>We&#8217;ll want to select our serial port again, in my case it is COM4 and set the speed, or baud rate, to 115200. Then we can open the connection and we should be prompted with a Python read-evaluate-print-loop (REPL) interface as shown:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/REPL-prompt.png\" target=\"_blank\" rel=\"noopener\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"217\" data-permalink=\"https:\/\/www.kenwalger.com\/blog\/iot\/micropython-and-nodemcu-esp8266\/attachment\/repl-prompt\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/REPL-prompt.png?fit=661%2C418&amp;ssl=1\" data-orig-size=\"661,418\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"REPL-prompt\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/REPL-prompt.png?fit=300%2C190&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/REPL-prompt.png?fit=661%2C418&amp;ssl=1\" class=\"alignnone size-medium wp-image-217\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/REPL-prompt-300x190.png?resize=300%2C190\" alt=\"\" width=\"300\" height=\"190\" srcset=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/REPL-prompt.png?resize=300%2C190&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/05\/REPL-prompt.png?w=661&amp;ssl=1 661w\" sizes=\"auto, (max-width: 300px) 85vw, 300px\" \/><\/a><\/p>\n<p>We can start entering in Python commands on the device itself. Let&#8217;s have our device do the IoT version of &#8220;Hello World&#8221; and turn on one of the on-board LED lights. These lights are controlled through General-Purpose Input\/Output (GPIO) pins and there are two of them on board. GPIO 2\u00a0controls a small blue\u00a0LED on the board.<\/p>\n<p>In the REPL then, we want to be able to turn the light on and off. That can be done by altering the state (electrical charge) provided to the GPIO pins, typically by changing the state from low to high and back to low.<\/p>\n<p>Here is something that I discovered when working with this particular LED and GPIO setting and board. On this board, the LED on GPIO 2 (and 16 for that matter) is wired between the pin and the power, so when we set the pin state to low, the light is activated, and when set to high, it is off. There is active development on this issue and firmware updates may have already addressed the issue. However, if one connects an external LED light to, say GPIO 5 along with the required resistor (~300+ ohms), we get the expected light on with a high state, and off with a low state.<\/p>\n<h3>MicroPython code &#8211; Try it Out!<\/h3>\n<p>Okay, so technical issues aside, let&#8217;s look at the code necessary to turn on our LED. First, we&#8217;ll need access to the board&#8217;s hardware. We can import a package called <code>machine<\/code> which provides the necessary software interface. Then we can change the state of a given GPIO pin&#8230;<\/p>\n<pre>&gt;&gt;&gt; import machine\n>&gt;&gt; pin = machine.Pin(2, machine.Pin.OUT)\n>&gt;&gt; pin.high() \u00a0# light off\n>&gt;&gt; pin.low() \u00a0# light on\n>&gt;&gt; pin.high() # light off again\n<\/pre>\n<p>There we have it! We have taken a NodeMCU 8266 device, flashed it with MicroPython, accessed it through the serial port, and with minimal code, turned the on-board LED lights on and off.<\/p>\n<p>Congratulations on your first venture into IoT and Python! There is a lot more that can be done with this $5.00 board and I think I will spend some time experimenting with it a bit. I&#8217;ll post my results and findings here as I work through the process.<\/p>\n<hr \/>\n<p><em>Follow me on Twitter <a href=\"https:\/\/www.twitter.com\/kenwalger\" target=\"_blank\" rel=\"noopener\">@kenwalger<\/a> to get the latest updates on my postings.<\/em><\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F212&#038;t=MicroPython%20and%20the%20NodeMCU%20ESP8266&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F212&#038;p&#091;images&#093;&#091;0&#093;=https%3A%2F%2Fi0.wp.com%2Fwww.kenwalger.com%2Fblog%2Fwp-content%2Fuploads%2F2017%2F04%2Fmicro-python-e1493094682987.png%3Ffit%3D125%252C125%26ssl%3D1&#038;p&#091;title&#093;=MicroPython%20and%20the%20NodeMCU%20ESP8266\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/facebook.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F212&#038;text=Hey%20check%20this%20out\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/twitter.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox\" data-provider=\"reddit\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Reddit\" href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F212&#038;title=MicroPython%20and%20the%20NodeMCU%20ESP8266\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"reddit\" title=\"Share on Reddit\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/reddit.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F212&#038;title=MicroPython%20and%20the%20NodeMCU%20ESP8266\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=MicroPython%20and%20the%20NodeMCU%20ESP8266&#038;body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F212\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/mail.png?resize=48%2C48&#038;ssl=1\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>In a previous post, I looked briefly at MicroPython and it&#8217;s place and role in the Internet of Things (IoT) arena. In this post, I&#8217;d like to walk through the process of getting a NodeMCU ESP8266 device flashed and up and running with MicroPython. Then we&#8217;ll do the, almost required, IoT version of &#8220;Hello World&#8221;&#8230; &hellip; <a href=\"https:\/\/www.kenwalger.com\/blog\/iot\/micropython-and-nodemcu-esp8266\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;MicroPython and the NodeMCU ESP8266&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":197,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"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":[27,28],"tags":[36,35],"yst_prominent_words":[903,249,327,274,321,132,908,902,900,905,1449,319,257,239,314,317,328,324,904,901],"class_list":["post-212","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-iot","category-micropython","tag-esp8266","tag-nodemcu","pmpro-has-access"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/04\/micro-python-e1493094682987.png?fit=125%2C125&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8lx70-3q","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/comments?post=212"}],"version-history":[{"count":16,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/212\/revisions"}],"predecessor-version":[{"id":1003,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/212\/revisions\/1003"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media\/197"}],"wp:attachment":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/categories?post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/tags?post=212"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}