NodeMCU Firmware Documentation

repository·release·Indexed 27 days ago

https://github.com/nodemcu/nodemcu-firmware

Lua-based firmware for ESP8266 and ESP32 WiFi SoCs, providing an asynchronous, event-driven programming environment. Includes documentation on the uzlib compression library, luaOTA for over-the-air updates, timezone management via the tz module, and various hardware sensor modules such as BH1750, DS18B20, and DS3231.

Tokens
108K
Snippets
377
Records
706
Agent score
92%

What's inside NodeMCU Firmware

  1. Overview of uzlib compression and decompression

    release

    uzlib is a modified LZ77 compression library designed for embedded systems like the ESP8266. It provides Deflate-compatible compression and decompression with minimal code size and runtime memory requirements.

    Compression

    • Compresses data to a Deflate-compatible bitstream.
    • Uses a static Deflate Huffman tree encoding (resulting in a lower compression ratio than standard Zlib Deflate).
    • Warning: Compression is performed in RAM and requires approximately 4 bytes of RAM per 1 byte of input data. It should only be used for small records on the ESP8266.

    Decompression

    • Can decompress any valid Deflate, Zlib, and Gzip bitstream that is less than 16Kb in size.
    • Can decompress any arbitrary length stream that was compressed using the uzlib compressor.
  2. Overview of NodeMCU Firmware

    release

    NodeMCU is an open-source, Lua-based firmware designed for the ESP8266 WiFi SOC from Espressif. It uses an on-module flash-based SPIFFS file system and is implemented in C on top of the Espressif NON-OS SDK.

    Key features:

    • Programming Language: Based on Lua 5.1.4 or Lua 5.3 (with certain modules like debug, io, os, and most of math removed).
    • Programming Model: Asynchronous and event-driven, similar to Node.js.
    • LFS Support: Lua Flash Store (LFS) allows Lua code and read-only constants to execute directly from flash memory, freeing up RAM for read-write data. This enables applications with up to 256Kb of Lua code.
    • Hardware Support:
      • release and dev branches target the ESP8266.
      • dev-esp32 branch targets the ESP32.
  3. Use spiffsimg to manipulate SPI Flash File System disk images

    release
    The spiffsimg tool allows you to prepare SPIFFS (SPI Flash File System) disk images offline. Instead of uploading files one-by-one to a microprocessor via an application, you can create a complete image and flash the entire image onto the device's storage at once.
  4. Use the rtctime module for advanced timekeeping

    release

    The rtctime module provides high-precision timekeeping (microsecond precision) based on the Unix Epoch. It is designed to work with the sntp module to maintain accurate time across deep sleep cycles, which is essential for battery-powered sensor nodes.

    Key features:

    • Maintains time during deep sleep (when using rtctime.dsleep()).
    • Automatically compensates for clock drift caused by temperature changes and different CPU frequencies.
    • Uses RTC memory slots 0-9; calling rtctime.set() or sntp.sync() will occupy these slots.

    Note: Time is lost if the module undergoes an unexpected reset.

  5. mDNS Module usage notes

    release

    The mdns module acts as an mDNS server. It is used to allow systems to identify themselves and the services they provide on a local area network (Bonjour/Zeroconf).

    If you need to implement an mDNS client (to query existing mDNS services), this module does not support that functionality. You may want to look at udaygin/nodemcu-mdns-client instead.

  6. Use the Struct module to convert Lua values to and from C structs

    release

    The struct module provides facilities to pack Lua values into binary strings (simulating C structs) and unpack binary strings back into Lua values.

    Note: float (f) and double (d) conversions are only available in NodeMCU builds that support floating point.

  7. Understand the NodeMCU Lua implementation

    release

    NodeMCU implements Lua 5.1 optimized for embedded systems via the eLua fork. It is designed to run on the ESP8266 SoC and is layered over the Espressif SDK.

    Key characteristics include:

    • LTR (Lua Tiny RAM) technique: Uses read-only tables and constants for library modules to reduce RAM footprint by approximately 20-25KB.
    • Resource constraints: To maintain stability, the firmware has omitted the debug and math libraries to reduce the runtime footprint. Use % for modulo and ^ for power operations.
    • Library replacements: Standard io and os libraries are not available. They are replaced by the NodeMCU node and file libraries.
  8. Understand NodeMCU Lua implementation and compatibility

    release

    NodeMCU provides a Lua-based runtime for ESP8266 and ESP32 architectures. It supports two versions of the Lua language:

    • Lua 5.1 (Deprecated/Frozen support)
    • Lua 5.3 (Recommended)

    Developers should primarily follow the Lua 5.3 Reference Manual (LRM). While NodeMCU aims for compatibility between 5.1 and 5.3 by back-porting enhancements and using compatibility options, breaking changes inherent to the Lua language versions may still occur when switching between them.