{"id":262,"date":"2016-11-23T18:27:50","date_gmt":"2016-11-23T17:27:50","guid":{"rendered":"http:\/\/www.vypers.it\/professione\/?page_id=262"},"modified":"2016-11-25T18:44:21","modified_gmt":"2016-11-25T17:44:21","slug":"build-firmware-nodemcu","status":"publish","type":"page","link":"https:\/\/www.vypers.it\/professione\/approfondimenti\/progettiextra\/esp8266-nodemcu\/build-firmware-nodemcu\/","title":{"rendered":"2) Build Firmware NodeMCU"},"content":{"rendered":"<div class=\"page-header\">\n<h1>Thanks to:<\/h1>\n<p>http:\/\/www.allaboutcircuits.com\/projects\/guts-of-the-iot-part-1-building-nodemcu-from-source-for-the-esp8266\/<\/p>\n<h1 id=\"6315\">Guts of the IoT: Building NodeMCU from Source for the ESP8266<\/h1>\n<p class=\"meta\">July 18, 2015 by <a class=\"author\" href=\"http:\/\/www.allaboutcircuits.com\/author\/patrick-lloyd\">Patrick Lloyd<\/a><\/p>\n<\/div>\n<hr \/>\n<div>\n<div class=\"sp-content-block\">\n<h4><\/h4>\n<h4><\/h4>\n<p><img decoding=\"async\" src=\"http:\/\/www.allaboutcircuits.com\/uploads\/articles\/mynodemcu.jpg\" alt=\"Comes in a variety of fancy colors depending on who you get it from.\" \/><\/p>\n<p>&nbsp;<\/p>\n<h4>Notes on Linux:<\/h4>\n<p>Provato su macchina virtuale Linux Lubuntu 16.10<br \/>\nPrevedere oltre 10G di disco virtuale (no default VirtualBox)<\/p>\n<h4>\u00a0Why Bother with NodeMCU and it&#8217;s Source Code?<\/h4>\n<p>NodeMCU\u00a0is based on the interpreted\u00a0Lua scripting language and allows\u00a0event-based programming of the ESP8266.\u00a0Firmware binaries can already be <a href=\"http:\/\/frightanic.com\/nodemcu-custom-build\/\" target=\"_blank\">customized and built online<\/a>, so strictly speaking, you don&#8217;t have to compile from source. However, since we&#8217;re not scared of getting our hands dirty, we&#8217;re going to learn how to strip away the cruft with more precision and\u00a0also give\u00a0ourselves more control and transparency over what&#8217;s\u00a0actually going in your hardware.<\/p>\n<p>So, with that said, the\u00a0first and possibly least exciting part of dealing with a new processor is getting the toolchain and its dependencies set up so it can actually be programmed. This tutorial will focus on downloading the NodeMCU firmware source and building tools\u00a0from github, selecting only the specific\u00a0modules we want, compiling the module, and flashing it to the ESP8266 from a Linux environment. The commands apply\u00a0specifically to Ubuntu 15.04 Vivid Vervet but could very easily be adapted to other versions and distributions &#8212; just be aware that there could be subltle differences.<\/p>\n<h1>Getting Ready to Get Ready<\/h1>\n<p>&nbsp;<\/p>\n<p>Install dependencies for compilation and communication to ESP8266:<\/p>\n<div><code>sudo apt-get install make unrar autoconf automake libtool gcc g++ gperf flex bison \\<br \/>\ntexinfo\u00a0gawk ncurses-dev libexpat-dev python python-serial sed git libtool-bin screen<\/code><\/div>\n<p>&nbsp;<\/p>\n<p>The software development kit that we&#8217;re going to use is the\u00a0<a href=\"https:\/\/github.com\/pfalcon\/esp-open-sdk\" target=\"_blank\">esp-open-sdk<\/a>. It contains the tools to cross compile code for the Xtensa processor as well as the hardware abstraction library and esptool, a python script that can convert and upload code to the device.<\/p>\n<p>&nbsp;<\/p>\n<p>3) Create a user-owned directory and clone the esp-open-sdk\u00a0repository from\u00a0Github:<\/p>\n<div><code>mkdir ~\/.opt &amp;&amp; cd ~\/.opt<\/code><\/div>\n<div><code>git clone --recursive <a href=\"https:\/\/github.com\/pfalcon\/esp-open-sdk.git\">https:\/\/github.com\/pfalcon\/esp-open-sdk.git<\/a> # --recursive since this repo contains more repos<\/code><\/div>\n<p>&nbsp;<\/p>\n<p>4) Compile a standalone version of the open SDK with\u00a0vendor SDK files built in. Then add the SDK&#8217;s bin directory to your PATH environment variable:<\/p>\n<div><code>cd esp-open-sdk<\/code><\/div>\n<div><code>make STANDALONE=y<\/code><\/div>\n<p>&nbsp;<\/p>\n<h5><img decoding=\"async\" src=\"http:\/\/www.allaboutcircuits.com\/uploads\/articles\/long_time_terminal.png\" alt=\"Sit back and relax.... it's gonna be a while.\" \/><br \/>\n<em>Sit back and relax&#8230;. it&#8217;s gonna be a while.<\/em><\/h5>\n<p>The\u00a0<code>make<\/code>\u00a0command will take a while so go grab a cup of coffee or something while you wait. It&#8217;s worth noting that while the standalone SDK is slightly easier to write code for locally, there are a few caveats. See the note from pfalcon\u00a0<a href=\"https:\/\/github.com\/pfalcon\/esp-open-sdk#building\" target=\"_blank\">here<\/a> on portability and licenses. Then add\u00a0the newly built tools&#8217; binary folder to the PATH environment variable. That way your system knows where to look when you try to compile Xtensa code.<\/p>\n<div><code>echo 'PATH=$PATH:\/opt\/esp-open-sdk\/xtensa-lx106-elf\/bin' &gt;&gt; ~\/.profile<\/code><\/div>\n<div><code>PATH=$PATH:~\/.opt\/esp-open-sdk\/xtensa-lx106-elf\/bin<\/code><\/div>\n<p>&nbsp;<\/p>\n<p>Now that we have the tools to make everything, now we can actually get the NodeMCU firmware and configure it to our specific needs.<\/p>\n<p>&nbsp;<\/p>\n<p>5) Clone the Nodemcu firmware:<\/p>\n<div><code>cd ~\/.opt<\/code><\/div>\n<div><code>git clone <a href=\"https:\/\/github.com\/nodemcu\/nodemcu-firmware.git\">https:\/\/github.com\/nodemcu\/nodemcu-firmware.git<\/a><\/code><\/div>\n<div><code>cd nodemcu-firmware<\/code><\/div>\n<p>&nbsp;<\/p>\n<p>Nuts and bolts time! A common problem many users of NodeMCU are discussing is running out of room on the chip for program. Since the ESP8266 only has 64 KiB of instruction RAM and\u00a096 KiB of data RAM, space is more or less at a premium. Why would you install I2C and\u00a0u8g\u00a0graphics libraries if you have no need for\u00a0them for this project?\u00a0By default, all the modules are built, so\u00a0we&#8217;re\u00a0going to comment out the ones we don&#8217;t want at this time:<\/p>\n<p>&nbsp;<\/p>\n<p>6) Select modules to compile and install<\/p>\n<p>Using your favorite editor (Sublime Text 3 with Vim key bindings thankyouverymuch), open <code><strong>..\/nodemcu-firmware\/app\/include\/user_modules.h<\/strong><\/code><\/p>\n<p>For this <em>Hello World<\/em> kind of a project, you only need the\u00a0<em>node, file, gpio, wifi, net, pwm, timer, adc, <\/em>and<em>uart\u00a0<\/em>modules.\u00a0I edited my <code><strong>user_modules.h<\/strong><\/code>\u00a0to look like this (I added some of my own comments for clarity):<\/p>\n<p>&nbsp;<\/p>\n<pre>                    <code class=\"hljs cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">ifndef<\/span> __USER_MODULES_H__<\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> __USER_MODULES_H__<\/span>\r\n\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_BUILTIN_STRING\t\t<span class=\"hljs-comment\">\/\/ for string.xxx()<\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_BUILTIN_TABLE\t\t<span class=\"hljs-comment\">\/\/ for table.xxx()<\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_BUILTIN_COROUTINE\t<span class=\"hljs-comment\">\/\/ for coroutine.xxx()<\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_BUILTIN_MATH\t\t<span class=\"hljs-comment\">\/\/ for math.xxx(), partially work<\/span><\/span>\r\n<span class=\"hljs-comment\">\/\/ #define LUA_USE_BUILTIN_IO \t\t\/\/ for io.xxx(), partially work<\/span>\r\n\r\n<span class=\"hljs-comment\">\/\/ #define LUA_USE_BUILTIN_OS\t\t\/\/ for os.xxx(), not work<\/span>\r\n<span class=\"hljs-comment\">\/\/ #define LUA_USE_BUILTIN_DEBUG\t\/\/ for debug.xxx(), not work<\/span>\r\n\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_MODULES<\/span>\r\n\r\n<span class=\"hljs-comment\">\/\/ Check out <\/span><a href=\"http:\/\/www.nodemcu.com\/docs\/\"><span class=\"hljs-comment\">http:\/\/www.nodemcu.com\/docs\/<\/span><\/a><span class=\"hljs-comment\"> for more info on what you need and what you don't. <\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">ifdef<\/span> LUA_USE_MODULES<\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_MODULES_NODE\t\t<span class=\"hljs-comment\">\/\/ NodeMCU system related functions (restart, info, etc)<\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_MODULES_FILE\t\t<span class=\"hljs-comment\">\/\/ File system for managing lua files <\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_MODULES_GPIO\t\t<span class=\"hljs-comment\">\/\/ General purpose I\/O. Good stuff!<\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_MODULES_WIFI\t\t<span class=\"hljs-comment\">\/\/ Nowhere near as cool of a chip without this<\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_MODULES_NET\t\t<span class=\"hljs-comment\">\/\/ Do things with TCP and UDP<\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_MODULES_PWM\t\t<span class=\"hljs-comment\">\/\/ Pulse width modulation -- gonna dim some LEDs!<\/span><\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_I2C\t\t\/\/ Peripheral communications library<\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_SPI\t\t\/\/ Peripheral communications library<\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_MODULES_TMR\t\t<span class=\"hljs-comment\">\/\/ Timer functions like delay() and all that <\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_MODULES_ADC\t\t<span class=\"hljs-comment\">\/\/ 10-bit analog-to-digital converter. Read pots &amp; batts.<\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">define<\/span> LUA_USE_MODULES_UART\t\t<span class=\"hljs-comment\">\/\/ Need it to talk to the durn thing through CH341<\/span><\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_OW\t\t\/\/ 1-Wire peripheral communications library<\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_BIT\t\t\/\/ Bitwise manipulation (xor, not, and, or, etc)<\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_MQTT\t\t\/\/ light weight messaging protocol on top of TCP\/IP<\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_COAP\t\t\/\/ IDK... No docs :'(<\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_U8G\t\t\/\/ Neato burrito graphics lib: github.com\/olikraus\/u8glib<\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_WS2801\t\/\/ IDK... No docs :'( More LEDs???<\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_WS2812\t\/\/ Cool RGB LED strips<\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_CJSON\t\t\/\/ Provides JSON support for Lua<\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_CRYPTO\t\/\/ Super secret spy stuff<\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_RC \t\t\/\/ IDK... No docs :'(<\/span>\r\n<span class=\"hljs-comment\">\/\/#define LUA_USE_MODULES_DHT\t\t\/\/ IDK... No docs :'(<\/span>\r\n\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">endif<\/span> <span class=\"hljs-comment\">\/* LUA_USE_MODULES *\/<\/span><\/span>\r\n\r\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">endif<\/span>\t<span class=\"hljs-comment\">\/* __USER_MODULES_H__ *\/<\/span><\/span>\r\n<\/code>\r\n<\/pre>\n<p id=\"file\" class=\"text-center\"><a class=\"btn btn-theme btn-assets \" href=\"http:\/\/www.allaboutcircuits.com\/login\/?redirect=http:\/\/www.allaboutcircuits.com\/projects\/guts-of-the-iot-part-1-building-nodemcu-from-source-for-the-esp8266\/\" rel=\"nofollow\" data-entry-id=\"1228\"><i class=\"fa fa-download\"><\/i> \u00a0 Download Code<\/a><\/p>\n<p>With the Devkit Version 0.9, we also have to modify the\u00a0<strong>..\/nodemcu-firmware\/app\/include\/user_config.h\u00a0<\/strong>to enable some v0.9-specific settings. Find line 4 and uncomment it\u00a0so it looks like this:<\/p>\n<div><code>#define DEVKIT_VERSION_0_9 1 \u00a0\u00a0 \u00a0\/\/ define this only if you use NodeMCU devkit v0.9<\/code><\/div>\n<h1><\/h1>\n<h4>Let&#8217;s Build It Already!<\/h4>\n<p>7) Compile NodeMCU:<\/p>\n<div><code>cd ~\/.opt\/nodemcu-firmware # make your way back to the firmware dir if you weren't there already<\/code><\/div>\n<div><code>make\u00a0# this might take a while<\/code><\/div>\n<p>&nbsp;<\/p>\n<p>8) Add user to dialout group and flash the firmware to the device:<\/p>\n<p>Before you can upload the compiled image or communicate with the device,\u00a0you have to have permission to\u00a0access to the serial port. \/dev\/ttyUSB0 is part of\u00a0the dialout\u00a0group, so to add yourself, type:<\/p>\n<div><code>sudo adduser $USER dialout<\/code><\/div>\n<p>&nbsp;<\/p>\n<p>Now you should be able to upload the NodeMCU image to your device:<\/p>\n<div><code>make flash # this assumes the tty device is \/dev\/ttyUSB0<\/code><\/div>\n<p>&nbsp;<\/p>\n<p>Phew! If everything worked properly and you didn&#8217;t see any errors, you are now ready to start prototyping!<\/p>\n<p>Note: in the guts of the\u00a0<code>make flash\u00a0<\/code>command<code>,<\/code>\u00a0the makefile actually calls a Python script called esptool.py to write\u00a0the compiled binaries\u00a0to specific parts of memory in ESP8266. <a href=\"https:\/\/github.com\/themadinventor\/esptool\/\">Esptool is its own separate project<\/a> but NodeMCU cloned it and incorporated it into theirs. The literall command that is called by &#8216;make flash&#8217; is:<\/p>\n<div><code>..\/tools\/esptool.py --port \/dev\/ttyUSB0 write_flash 0x00000 ..\/bin\/0x00000.bin 0x10000 ..\/bin\/0x10000.bin<\/code><\/div>\n<p>&nbsp;<\/p>\n<p>So if your CH341 happens to be at a different location than \/dev\/ttyUSB0 (or you used Frightanic&#8217;s custom build tool), just replace it with your specific device name and binaries.<\/p>\n<p>&nbsp;<\/p>\n<h4>Parting Notes<\/h4>\n<p>Now that the guts of our ESP8266 are now full of beautiful, Lua-interpreting\u00a0goodness, next time we can focus on actually getting it to do something useful. We\u00a0will discuss using Linux Screen to\u00a0connect to NodeMCU&#8217;s Lua interpreter\u00a0and how to\u00a0write scripts to interact with\u00a0WiFi and the internet,\u00a0as well as the onboard 10-bit ADC and GPIO. I&#8217;ll also show you Luatool and other programs\u00a0for interacting with the ESP8266 &#8212; very exciting times ahead.\u00a0See you next time!<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Thanks to: http:\/\/www.allaboutcircuits.com\/projects\/guts-of-the-iot-part-1-building-nodemcu-from-source-for-the-esp8266\/ Guts of the IoT: Building NodeMCU from Source for the ESP8266 July 18, 2015 by Patrick Lloyd &nbsp; Notes on Linux: Provato su macchina virtuale Linux Lubuntu 16.10 Prevedere oltre 10G di disco virtuale (no default VirtualBox) \u00a0Why Bother with NodeMCU and it&#8217;s Source Code? NodeMCU\u00a0is based on the interpreted\u00a0Lua scripting language &hellip; <a href=\"https:\/\/www.vypers.it\/professione\/approfondimenti\/progettiextra\/esp8266-nodemcu\/build-firmware-nodemcu\/\" class=\"more-link\">Continua la lettura di <span class=\"screen-reader-text\">2) Build Firmware NodeMCU<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":250,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-262","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.vypers.it\/professione\/wp-json\/wp\/v2\/pages\/262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vypers.it\/professione\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vypers.it\/professione\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vypers.it\/professione\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vypers.it\/professione\/wp-json\/wp\/v2\/comments?post=262"}],"version-history":[{"count":5,"href":"https:\/\/www.vypers.it\/professione\/wp-json\/wp\/v2\/pages\/262\/revisions"}],"predecessor-version":[{"id":277,"href":"https:\/\/www.vypers.it\/professione\/wp-json\/wp\/v2\/pages\/262\/revisions\/277"}],"up":[{"embeddable":true,"href":"https:\/\/www.vypers.it\/professione\/wp-json\/wp\/v2\/pages\/250"}],"wp:attachment":[{"href":"https:\/\/www.vypers.it\/professione\/wp-json\/wp\/v2\/media?parent=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}