nodemcu-firmware/docs/en/upload.md

96 lines
5.0 KiB
Markdown
Raw Normal View History

2016-01-16 23:11:40 +01:00
As with [flashing](flash.md) there are several ways to upload code from your computer to the device.
!!! note
The NodeMCU serial interface uses 115'200bps at boot time. To change the speed after booting, issue `uart.setup(0,9600,8,0,1,1)`. If the device panics and resets at any time, errors will be written to the serial interface at 115'200 bps.
2016-01-31 14:31:15 +01:00
2016-06-13 21:51:48 +02:00
# Tools
Transferring application code to ESP8266/8285 is an essential task, one that you'll perform quite frequently. Hence, it does make sense to try a few different uploading tools until you find one you feel comfortable with. [https://frightanic.com/iot/tools-ides-nodemcu/](https://frightanic.com/iot/tools-ides-nodemcu/) lists almost a dozen classical uploaders - in addition to IDEs or IDE-like applications which of course transfer code as well.
The NodeMCU firmware team does not give any recommendations as for which uploader to use nor are there any "NodeMCU approved" tools. The below listed tools are just three, in no particular order, which seem popular and/or reasonably well maintained.
2016-06-13 21:51:48 +02:00
## ESPlorer
2016-01-16 23:11:40 +01:00
> The essential multiplatforms tools for any ESP8266 developer from luatool authors, including Lua for NodeMCU and MicroPython. Also, all AT commands are supported. Requires Java (Standard Edition - SE ver 7 and above) installed.
![ESPlorer](../img/ESPlorer.jpg "ESPlorer")
2016-01-31 14:31:15 +01:00
Source: [https://github.com/4refr0nt/ESPlorer](https://github.com/4refr0nt/ESPlorer)
2016-01-16 23:11:40 +01:00
Supported platforms: macOS, Linux, Windows, anything that runs Java
2016-01-16 23:11:40 +01:00
2016-06-13 21:51:48 +02:00
## nodemcu-uploader.py
2016-01-16 23:11:40 +01:00
> A simple tool for uploading files to the filesystem of an ESP8266 running NodeMCU as well as some other useful commands.
2016-01-31 14:31:15 +01:00
Source: [https://github.com/kmpm/nodemcu-uploader](https://github.com/kmpm/nodemcu-uploader)
2016-01-16 23:11:40 +01:00
Supported platforms: macOS, Linux, Windows, anything that runs Python
2016-01-16 23:11:40 +01:00
## NodeMCU-Tool
2016-01-16 23:11:40 +01:00
> Upload/Download Lua files to your ESP8266 module with NodeMCU firmware.
> Simple. Command Line. Cross-Platform. File Management. NodeMCU.
2016-01-16 23:11:40 +01:00
Source: [https://github.com/andidittrich/NodeMCU-Tool](https://github.com/andidittrich/NodeMCU-Tool)
2016-01-16 23:11:40 +01:00
Supported platforms: macOS, Linux Windows, anything that runs Node.js
2016-06-13 21:51:48 +02:00
# init.lua
You will see "lua: cannot open init.lua" printed to the serial console when the device boots after it's been freshly flashed. If NodeMCU finds a `init.lua` in the root of the file system it will execute it as part of the boot sequence (standard Lua feature). Hence, your application is initialized and triggered from `init.lua`. Usually you first set up the WiFi connection and only continue once that has been successful.
Be very careful not to lock yourself out! If there's a bug in your `init.lua` you may be stuck in an infinite reboot loop. It is, therefore, advisable to build a small delay into your startup sequence that would allow you to interrupt the sequence by e.g. deleting or renaming `init.lua` (see also [FAQ](lua-developer-faq.md#how-do-i-avoid-a-panic-loop-in-initlua)). Your `init.lua` is most likely going to be different than the one below but it's a good starting point for customizations:
```lua
-- load credentials, 'SSID' and 'PASSWORD' declared and initialize in there
dofile("credentials.lua")
function startup()
if file.open("init.lua") == nil then
print("init.lua deleted or renamed")
else
print("Running")
file.close("init.lua")
-- the actual application is stored in 'application.lua'
-- dofile("application.lua")
end
end
print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID, PASSWORD)
2016-07-28 07:07:39 +02:00
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default
tmr.create():alarm(1000, tmr.ALARM_AUTO, function(cb_timer)
2016-06-13 21:51:48 +02:00
if wifi.sta.getip() == nil then
print("Waiting for IP address...")
else
cb_timer:unregister()
2016-06-13 21:51:48 +02:00
print("WiFi connection established, IP address: " .. wifi.sta.getip())
print("You have 3 seconds to abort")
print("Waiting...")
2017-01-04 10:32:55 +01:00
tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
2016-06-13 21:51:48 +02:00
end
end)
```
Inspired by [https://github.com/ckuehnel/NodeMCU-applications](https://github.com/ckuehnel/NodeMCU-applications)
# Compiling Lua on your PC for Uploading
2016-06-29 22:12:24 +02:00
If you install `lua` on your development PC or Laptop then you can use the standard Lua
compiler to syntax check any Lua source before downloading it to the ESP8266 module. However,
2016-06-29 22:12:24 +02:00
the NodeMCU compiler output uses different data types (e.g. it supports ROMtables) so the
compiled output cannot run on the ESP8266.
Compiling source on one platform for use on another (e.g. Intel x38 Window to ESP8266) is
2016-06-29 22:12:24 +02:00
known as _cross-compilation_ and the NodeMCU firmware supports the compilation of `luac.cross`
on \*nix patforms which have Lua 5.1, the Lua filesystem module (lfs), and the essential
2016-06-29 22:12:24 +02:00
GCC tools. Simply change directory to the firmware root directoy and run the command:
lua tools/cross-lua.lua
This will generate a `luac.cross` executable in your root directory which can be used to
compile and to syntax-check Lua source on the Development machine for execution under
2016-06-29 22:12:24 +02:00
NodeMCU Lua on the ESP8266.