2015-11-16 14:00:12 +01:00
# **NodeMCU 1.4.0** #
2015-03-05 02:22:02 +01:00
2015-03-15 08:01:07 +01:00
[![Join the chat at https://gitter.im/nodemcu/nodemcu-firmware ](https://badges.gitter.im/Join%20Chat.svg )](https://gitter.im/nodemcu/nodemcu-firmware?utm_source=badge& utm_medium=badge& utm_campaign=pr-badge& utm_content=badge)
2015-11-16 14:57:42 +01:00
[![Build Status ](https://travis-ci.org/nodemcu/nodemcu-firmware.svg )](https://travis-ci.org/nodemcu/nodemcu-firmware)
2015-03-05 02:22:02 +01:00
2014-11-12 17:17:08 +01:00
###A lua based firmware for wifi-soc esp8266
2015-11-16 17:11:48 +01:00
- Build on [ESP8266 sdk 1.4.0 ](http://bbs.espressif.com/viewtopic.php?f=46&t=1124 )
- Lua core based on [eLua project ](http://www.eluaproject.net/ )
- cjson based on [lua-cjson ](https://github.com/mpx/lua-cjson )
- File system based on [spiffs ](https://github.com/pellepl/spiffs )
- Open source development kit for NodeMCU [nodemcu-devkit ](https://github.com/nodemcu/nodemcu-devkit )
- Flash tool for NodeMCU [nodemcu-flasher ](https://github.com/nodemcu/nodemcu-flasher )
### Resources
| Resource | Location |
| -------------- | -------------- |
| Developer Wiki | https://github.com/nodemcu/nodemcu-firmware/wiki |
| api | [NodeMCU api ](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en ) |
| home | [nodemcu.com ](http://www.nodemcu.com ) |
| bbs | [Chinese BBS ](http://bbs.nodemcu.com ) |
| docs | [NodeMCU docs ](http://www.nodemcu.com/docs/ ) |
| Tencent QQ group | 309957875 |
2014-12-22 12:35:05 +01:00
2015-01-06 14:52:14 +01:00
# Summary
2015-11-16 14:54:46 +01:00
- Easy to program wireless node and/or Access Point
2015-03-06 09:34:24 +01:00
- Based on Lua 5.1.4 (without *debug, os* module.)
2015-11-16 14:54:46 +01:00
- Event-driven programming model preferred
- Built-in modules: node, json, file, timer, pwm, i2c, spi, onewire, net, mqtt, coap, gpio, wifi, adc, uart, bit, u8g, ucg, ws2801, ws2812, crypto, dht, rtc, sntp, bmp085, tls2561, hx711 and system api.
2015-11-16 14:05:21 +01:00
- Both Integer (less memory usage) and Float version firmware provided.
2015-01-06 14:52:14 +01:00
2015-11-16 14:05:21 +01:00
# To Do List (pull requests are very welcome)
2015-11-16 14:54:46 +01:00
2015-03-06 09:34:24 +01:00
- loadable c module
2015-01-06 14:52:14 +01:00
- fix wifi smart connect
2015-01-24 16:36:04 +01:00
- add spi module (done)
- add mqtt module (done)
2015-03-06 09:34:24 +01:00
- add coap module (done)
2015-02-13 12:35:05 +01:00
- cross compiler (done)
2015-01-06 14:52:14 +01:00
2015-11-16 14:15:10 +01:00
# Programming Examples
2014-12-19 09:56:07 +01:00
2015-11-16 14:15:10 +01:00
Because Lua is a high level language and several modules are built into the firmware, you can very easily program your ESP8266. Here are some examples!
2015-06-22 12:26:54 +02:00
2015-11-16 14:15:10 +01:00
## Connect to your AP
2014-10-13 14:24:52 +02:00
2014-11-12 17:17:08 +01:00
```lua
2015-01-16 14:04:38 +01:00
ip = wifi.sta.getip()
print(ip)
--nil
2014-11-07 05:59:19 +01:00
wifi.setmode(wifi.STATION)
2015-11-16 15:04:44 +01:00
wifi.sta.config("SSID", "password")
2015-01-16 14:04:38 +01:00
ip = wifi.sta.getip()
print(ip)
2014-11-12 17:17:08 +01:00
--192.168.18.110
2014-11-07 05:59:19 +01:00
```
2014-10-13 14:24:52 +02:00
2015-11-16 14:15:10 +01:00
## Manipulate hardware like an Arduino
2015-03-03 10:20:02 +01:00
2014-11-12 17:17:08 +01:00
```lua
pin = 1
2015-11-16 15:04:44 +01:00
gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, gpio.HIGH)
2014-11-12 17:17:08 +01:00
print(gpio.read(pin))
2014-11-07 05:59:19 +01:00
```
2014-10-13 14:24:52 +02:00
2015-11-16 14:15:10 +01:00
## Write a network application in Node.js style
2015-03-03 10:20:02 +01:00
2014-11-12 17:17:08 +01:00
```lua
-- A simple http client
2015-03-03 10:20:02 +01:00
conn=net.createConnection(net.TCP, 0)
2015-11-16 15:04:44 +01:00
conn:on("receive", function(conn, payload) print(payload) end)
conn:connect(80, "115.239.210.27")
2014-11-12 17:17:08 +01:00
conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n"
2015-11-16 15:04:44 +01:00
.. "Connection: keep-alive\r\nAccept: */* \r\n\r\n")
2014-11-07 05:59:19 +01:00
```
2014-10-13 14:24:52 +02:00
2015-11-16 14:15:10 +01:00
## Or a simple HTTP server
2015-03-03 10:20:02 +01:00
2015-01-26 16:50:40 +01:00
```lua
-- A simple http server
2015-03-03 10:20:02 +01:00
srv=net.createServer(net.TCP)
2015-11-16 15:04:44 +01:00
srv:listen(80, function(conn)
conn:on("receive", function(conn,payload)
2015-03-03 10:20:02 +01:00
print(payload)
2015-11-16 15:04:44 +01:00
conn:send("< h1 > Hello, NodeMCU.< / h1 > ")
2015-03-03 10:20:02 +01:00
end)
2015-11-16 15:04:44 +01:00
conn:on("sent", function(conn) conn:close() end)
2015-01-26 16:50:40 +01:00
end)
```
2015-11-16 14:15:10 +01:00
## Connect to MQTT broker
2015-01-23 04:48:05 +01:00
```lua
-- init mqtt client with keepalive timer 120sec
m = mqtt.Client("clientid", 120, "user", "password")
-- setup Last Will and Testament (optional)
2015-03-03 10:20:02 +01:00
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
2015-11-16 15:04:44 +01:00
-- to topic "/lwt" if client doesn't send keepalive packet
2015-01-23 04:48:05 +01:00
m:lwt("/lwt", "offline", 0, 0)
2015-11-16 15:04:44 +01:00
m:on("connect", function(con) print("connected") end)
m:on("offline", function(con) print("offline") end)
2015-01-23 04:48:05 +01:00
-- on publish message receive event
2015-03-03 10:20:02 +01:00
m:on("message", function(conn, topic, data)
2015-11-16 15:04:44 +01:00
print(topic .. ":")
2015-01-23 04:48:05 +01:00
if data ~= nil then
print(data)
end
end)
2015-11-16 15:04:44 +01:00
-- m:connect(host, port, secure, auto_reconnect, function(client) end)
2015-03-30 18:36:44 +02:00
-- for secure: m:connect("192.168.11.118", 1880, 1, 0)
-- for auto-reconnect: m:connect("192.168.11.118", 1880, 0, 1)
m:connect("192.168.11.118", 1880, 0, 0, function(conn) print("connected") end)
2015-01-23 04:48:05 +01:00
2015-11-16 15:04:44 +01:00
-- subscribe to topic with qos = 0
m:subscribe("/topic", 0, function(conn) print("subscribe success") end)
-- or subscribe multiple topics (topic/0, qos = 0; topic/1, qos = 1; topic2, qos = 2)
2015-02-02 10:58:54 +01:00
-- m:subscribe({["topic/0"]=0,["topic/1"]=1,topic2=2}, function(conn) print("subscribe success") end)
2015-01-23 04:48:05 +01:00
-- publish a message with data = hello, QoS = 0, retain = 0
2015-11-16 15:04:44 +01:00
m:publish("/topic", "hello", 0, 0, function(conn) print("sent") end)
2015-01-23 04:48:05 +01:00
2015-11-16 15:04:44 +01:00
m:close(); -- if auto-reconnect == 1, it will disable auto-reconnect and then disconnect from host.
2015-01-23 04:48:05 +01:00
-- you can call m:connect again
```
2015-11-16 14:15:10 +01:00
## UDP client and server
2014-11-12 17:17:08 +01:00
```lua
2015-01-26 16:50:40 +01:00
-- a udp server
2015-03-03 10:20:02 +01:00
s=net.createServer(net.UDP)
2015-11-16 15:04:44 +01:00
s:on("receive", function(s, c) print(c) end)
2015-01-26 16:50:40 +01:00
s:listen(5683)
-- a udp client
2015-03-03 10:20:02 +01:00
cu=net.createConnection(net.UDP)
2015-11-16 15:04:44 +01:00
cu:on("receive", function(cu, c) print(c) end)
cu:connect(5683, "192.168.18.101")
2015-01-26 16:50:40 +01:00
cu:send("hello")
2014-11-12 17:17:08 +01:00
```
2015-11-16 14:15:10 +01:00
## Do something shiny with an RGB LED
2014-11-12 17:17:08 +01:00
```lua
2015-11-16 15:04:44 +01:00
function led(r, g, b)
pwm.setduty(1, r)
pwm.setduty(2, g)
pwm.setduty(3, b)
2014-11-12 17:17:08 +01:00
end
2015-11-16 15:04:44 +01:00
pwm.setup(1, 500, 512)
pwm.setup(2, 500, 512)
pwm.setup(3, 500, 512)
2015-03-03 10:20:02 +01:00
pwm.start(1)
pwm.start(2)
2014-12-19 09:56:07 +01:00
pwm.start(3)
2015-11-16 15:04:44 +01:00
led(512, 0, 0) -- red
led(0, 0, 512) -- blue
2014-11-12 17:17:08 +01:00
```
2015-11-16 14:15:10 +01:00
## And blink it
2014-11-12 17:17:08 +01:00
```lua
lighton=0
2015-11-16 15:04:44 +01:00
tmr.alarm(1, 1000, 1, function()
2015-03-03 10:20:02 +01:00
if lighton==0 then
lighton=1
2015-11-16 15:04:44 +01:00
led(512, 512, 512)
2015-03-03 10:20:02 +01:00
else
lighton=0
2015-11-16 15:04:44 +01:00
led(0, 0, 0)
2015-03-03 10:20:02 +01:00
end
2014-11-12 17:17:08 +01:00
end)
```
2015-11-16 14:15:10 +01:00
## If you want to run something when the system boots
2014-11-12 17:17:08 +01:00
```lua
2015-11-16 15:04:44 +01:00
--init.lua will be executed
file.open("init.lua", "w")
2014-11-12 17:17:08 +01:00
file.writeline([[print("Hello, do this at the beginning.")]])
file.close()
node.restart() -- this will restart the module.
```
2014-11-18 12:58:48 +01:00
2015-11-16 14:15:10 +01:00
## Add a simple telnet server to the Lua interpreter
2014-11-18 12:58:48 +01:00
```lua
-- a simple telnet server
2015-11-16 15:04:44 +01:00
s=net.createServer(net.TCP, 180)
s:listen(2323, function(c)
2015-03-03 10:20:02 +01:00
function s_output(str)
if(c~=nil)
then c:send(str)
end
end
2014-11-18 12:58:48 +01:00
node.output(s_output, 0) -- re-direct output to function s_ouput.
2015-11-16 15:04:44 +01:00
c:on("receive", function(c, l)
node.input(l) -- works like pcall(loadstring(l)) but support multiples separate lines
2015-03-03 10:20:02 +01:00
end)
2015-11-16 15:04:44 +01:00
c:on("disconnection", function(c)
node.output(nil) -- un-register the redirect output function, output goes to serial
2015-03-03 10:20:02 +01:00
end)
2015-11-16 15:04:44 +01:00
print("Welcome to NodeMCU world.")
2014-11-18 12:58:48 +01:00
end)
```
2014-12-09 07:41:14 +01:00
2015-11-16 14:40:59 +01:00
# Building the firmware
2015-11-16 14:15:10 +01:00
2015-11-16 14:40:59 +01:00
There are several options for building the NodeMCU firmware.
## Online firmware custom build
2015-11-16 17:11:48 +01:00
Please try Marcel's [NodeMCU custom builds ](http://frightanic.com/nodemcu-custom-build ) cloud service and you can choose only the modules you need, and download the firmware once built.
2015-11-16 14:40:59 +01:00
NodeMCU custom builds can build from the master branch (0.9.6; deprecated) and dev
branch (1.4.0).
## Docker containerised build
See https://hub.docker.com/r/asmaps/nodemcu-builder/
This docker image includes the build toolchain and SDK. You just run the docker
image with your checked-out NodeMCU firmware repository (this one). The docker
image can also flash the firmware to your device.
You will need to see BUILD OPTIONS below, to configure the firmware before building.
## Build it yourself
See BUILD OPTIONS below, to configure the firmware before building.
### Minimum requirements:
- unrar
- GNU autoconf, automake, libtool
- GNU gcc, g++, make
- GNU flex, bison, gawk, sed
- python, python-serial, libexpat-dev
- srecord
2015-11-16 16:44:49 +01:00
- The esp-open-sdk from https://github.com/pfalcon/esp-open-sdk
2015-11-16 14:40:59 +01:00
### Build instructions:
2015-11-16 17:09:57 +01:00
Assuming NodeMCU firmware is checked-out to `/opt/nodemcu-firmware` :
2015-11-16 14:40:59 +01:00
```sh
git clone --recursive https://github.com/pfalcon/esp-open-sdk.git /opt/esp-open-sdk
cd /opt/esp-open-sdk
make STANDALONE=y
PATH=/opt/esp-open-sdk/xtensa-lx106-elf/bin:$PATH
cd /opt/nodemcu-firmware
make
```
# BUILD OPTIONS
Disable modules you won't be using, to reduce firmware size on flash and
free more RAM. The ESP8266 is quite limited in available RAM, and running
out can cause a system panic.
2015-11-16 17:09:57 +01:00
## Edit `app/include/user_modules.h`
2015-11-16 14:40:59 +01:00
2015-11-16 14:49:41 +01:00
Comment-out the #define statement for unused modules. Example:
2015-11-16 14:15:10 +01:00
```c
#ifdef LUA_USE_MODULES
#define LUA_USE_MODULES_NODE
#define LUA_USE_MODULES_FILE
#define LUA_USE_MODULES_GPIO
#define LUA_USE_MODULES_WIFI
#define LUA_USE_MODULES_NET
#define LUA_USE_MODULES_PWM
#define LUA_USE_MODULES_I2C
#define LUA_USE_MODULES_SPI
#define LUA_USE_MODULES_TMR
#define LUA_USE_MODULES_ADC
#define LUA_USE_MODULES_UART
#define LUA_USE_MODULES_OW
#define LUA_USE_MODULES_BIT
#define LUA_USE_MODULES_MQTT
// #define LUA_USE_MODULES_COAP
2015-11-16 14:49:41 +01:00
// #define LUA_USE_MODULES_U8G
// #define LUA_USE_MODULES_WS2801
// #define LUA_USE_MODULES_WS2812
// #define LUA_USE_MODULES_CJSON
#define LUA_USE_MODULES_CRYPTO
#define LUA_USE_MODULES_RC
#define LUA_USE_MODULES_DHT
#define LUA_USE_MODULES_RTCMEM
#define LUA_USE_MODULES_RTCTIME
#define LUA_USE_MODULES_RTCFIFO
#define LUA_USE_MODULES_SNTP
// #define LUA_USE_MODULES_BMP085
#define LUA_USE_MODULES_TSL2561
// #define LUA_USE_MODULES_HX711
2015-11-16 14:15:10 +01:00
#endif /* LUA_USE_MODULES */
```
2015-11-16 14:49:41 +01:00
## Tagging your build
Identify your firmware builds by editing `app/include/user_version.h`
```c
#define NODE_VERSION "NodeMCU 1.4.0+myname"
#ifndef BUILD_DATE
#define BUILD_DATE "YYYYMMDD"
#endif
```
2015-11-16 16:44:49 +01:00
# Flash the firmware
## Flash tools for Windows
2015-11-16 14:15:10 +01:00
You can use the [nodemcu-flasher ](https://github.com/nodemcu/nodemcu-flasher ) to burn the firmware.
2015-11-16 16:44:49 +01:00
## Flash tools for Linux
Esptool is a python utility which can read and write the flash in an ESP8266 device. See https://github.com/themadinventor/esptool
## Preparing the hardware for firmware upgrade
To enable ESP8266 firmware flashing, the GPIO0 pin must be pulled low before
the device is reset. Conversely, for a normal boot, GPIO0 must be pulled high
or floating.
If you have a [NodeMCU Development Kit ](http://www.nodemcu.com/index_en.html ) then
you don't need to do anything, as the USB connection can pull GPIO0
low by asserting DTR, and reset your board by asserting RTS.
If you have an ESP-01 or other device without inbuilt USB, you will need to
enable flashing yourself by pulling GPIO0 low or pressing a "flash" switch.
## Files to burn to the flash
If you got your firmware from [NodeMCU custom builds ](http://frightanic.com/nodemcu-custom-build ) then you can flash that file directly to address 0x00000.
Otherwise, if you built your own firmware from source code:
- bin/0x00000.bin to 0x00000
- bin/0x10000.bin to 0x10000
2015-11-16 17:09:57 +01:00
Also, in some special circumstances, you may need to flash `blank.bin` or `esp_init_data_default.bin` to various addresses on the flash (depending on flash size and type).
2015-11-16 14:15:10 +01:00
2015-11-16 16:44:49 +01:00
If upgrading from `spiffs` version 0.3.2 to 0.3.3 or later, or after flashing any new firmware, you should run `file.format()` to re-format your flash filesystem.
2015-11-16 14:15:10 +01:00
2015-11-16 17:09:57 +01:00
# Connecting to your NodeMCU device
NodeMCU serial interface uses 9600 baud at boot time. To increase the speed after booting, issue `uart.setup(0, 115200, 8, 0, 1, 1 )` (ESPlorer will do this automatically when changing the speed in the dropdown list).
If the device panics and resets, errors will be written to the serial interface at 115200 bps.
2015-11-16 14:15:10 +01:00
#Write Lua script to filesystem
####Esplorer
Victor Brutskiy's [Esplorer ](https://github.com/4refr0nt/ESPlorer ) support most platforms such as Linux, Windows, Mac OS, etc. This software is opensource and can write lua/lc files to filesystem.
####NodeMCU Studio
[NodeMCU Studio ](https://github.com/nodemcu/nodemcu-studio-csharp ) is written in C# and support Windows. This software is opensource and can write lua files to filesystem.
2015-11-16 14:40:59 +01:00
# OPTIONAL MODULES
2015-11-16 14:15:10 +01:00
2014-12-09 07:45:37 +01:00
####Use DS18B20 module extends your esp8266
2014-12-09 07:41:14 +01:00
```lua
-- read temperature with DS18B20
2015-02-13 08:11:59 +01:00
node.compile("ds18b20.lua") -- run this only once to compile and save to "ds18b20.lc"
2014-12-09 07:41:14 +01:00
t=require("ds18b20")
t.setup(9)
addrs=t.addrs()
-- Total DS18B20 numbers, assume it is 2
print(table.getn(addrs))
-- The first DS18B20
print(t.read(addrs[1],t.C))
print(t.read(addrs[1],t.F))
print(t.read(addrs[1],t.K))
-- The second DS18B20
print(t.read(addrs[2],t.C))
print(t.read(addrs[2],t.F))
print(t.read(addrs[2],t.K))
-- Just read
print(t.read())
-- Just read as centigrade
print(t.read(nil,t.C))
-- Don't forget to release it after use
t = nil
2014-12-23 09:24:53 +01:00
ds18b20 = nil
2015-03-03 10:20:02 +01:00
package.loaded["ds18b20"]=nil
2014-12-09 07:41:14 +01:00
```
2015-02-05 00:42:00 +01:00
2015-07-21 21:32:42 +02:00
####Operate a display with u8glib
2015-08-10 22:15:27 +02:00
u8glib is a graphics library with support for many different displays. The nodemcu firmware supports a subset of these.
Both I2C and SPI:
* sh1106_128x64
* ssd1306 - 128x64 and 64x48 variants
* ssd1309_128x64
* ssd1327_96x96_gr
* uc1611 - dogm240 and dogxl240 variants
SPI only:
* ld7032_60x32
* pcd8544_84x48
* pcf8812_96x65
* ssd1322_nhd31oled - bw and gr variants
* ssd1325_nhd27oled - bw and gr variants
* ssd1351_128x128 - gh and hicolor variants
* st7565_64128n - variants 64128n, dogm128/132, lm6059/lm6063, c12832/c12864
* uc1601_c128032
* uc1608 - 240x128 and 240x64 variants
* uc1610_dogxl160 - bw and gr variants
* uc1611 - dogm240 and dogxl240 variants
* uc1701 - dogs102 and mini12864 variants
2015-02-05 00:42:00 +01:00
2015-07-20 22:11:20 +02:00
U8glib v1.18.1
2015-02-08 21:32:32 +01:00
2015-02-05 00:42:00 +01:00
#####I2C connection
2015-07-21 21:32:42 +02:00
Hook up SDA and SCL to any free GPIOs. Eg. [u8g_graphics_test.lua ](lua_examples/u8glib/u8g_graphics_test.lua ) expects SDA=5 (GPIO14) and SCL=6 (GPIO12). They are used to set up nodemcu's I2C driver before accessing the display:
2015-02-05 00:42:00 +01:00
```lua
sda = 5
scl = 6
i2c.setup(0, sda, scl, i2c.SLOW)
```
2015-03-11 22:20:28 +01:00
#####SPI connection
The HSPI module is used, so certain pins are fixed:
* HSPI CLK = GPIO14
* HSPI MOSI = GPIO13
2015-03-15 22:08:35 +01:00
* HSPI MISO = GPIO12 (not used)
2015-03-11 22:20:28 +01:00
All other pins can be assigned to any available GPIO:
* CS
* D/C
2015-03-15 22:08:35 +01:00
* RES (optional for some displays)
2015-03-11 22:20:28 +01:00
2015-07-21 21:32:42 +02:00
Also refer to the initialization sequence eg in [u8g_graphics_test.lua ](lua_examples/u8glib/u8g_graphics_test.lua ):
2015-03-11 22:20:28 +01:00
```lua
2015-10-18 17:40:11 +02:00
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
2015-03-11 22:20:28 +01:00
```
2015-02-05 00:42:00 +01:00
#####Library usage
2015-03-11 22:20:28 +01:00
The Lua bindings for this library closely follow u8glib's object oriented C++ API. Based on the u8g class, you create an object for your display type.
SSD1306 via I2C:
2015-02-05 00:42:00 +01:00
```lua
sla = 0x3c
disp = u8g.ssd1306_128x64_i2c(sla)
```
2015-03-11 22:20:28 +01:00
SSD1306 via SPI:
```lua
2015-03-17 21:03:15 +01:00
cs = 8 -- GPIO15, pull-down 10k to GND
dc = 4 -- GPIO2
res = 0 -- GPIO16, RES is optional YMMV
2015-07-20 23:11:13 +02:00
disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res)
2015-03-11 22:20:28 +01:00
```
2015-02-05 00:42:00 +01:00
This object provides all of u8glib's methods to control the display.
2015-07-21 21:32:42 +02:00
Again, refer to [u8g_graphics_test.lua ](lua_examples/u8glib/u8g_graphics_test.lua ) to get an impression how this is achieved with Lua code. Visit the [u8glib homepage ](https://github.com/olikraus/u8glib ) for technical details.
#####Displays
I2C and HW SPI based displays with support in u8glib can be enabled. To get access to the respective constructors, add the desired entries to the I2C or SPI display tables in [app/include/u8g_config.h ](app/include/u8g_config.h ):
```c
#define U8G_DISPLAY_TABLE_I2C \
U8G_DISPLAY_TABLE_ENTRY(ssd1306_128x64_i2c) \
#define U8G_DISPLAY_TABLE_SPI \
U8G_DISPLAY_TABLE_ENTRY(ssd1306_128x64_hw_spi) \
U8G_DISPLAY_TABLE_ENTRY(pcd8544_84x48_hw_spi) \
U8G_DISPLAY_TABLE_ENTRY(pcf8812_96x65_hw_spi) \
```
An exhaustive list of available displays can be found in the [u8g module wiki entry ](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#u8g-module ).
2015-02-05 00:42:00 +01:00
#####Fonts
2015-07-21 21:32:42 +02:00
u8glib comes with a wide range of fonts for small displays. Since they need to be compiled into the firmware image, you'd need to include them in [app/include/u8g_config.h ](app/include/u8g_config.h ) and recompile. Simply add the desired fonts to the font table:
2015-02-05 00:42:00 +01:00
```c
#define U8G_FONT_TABLE \
U8G_FONT_TABLE_ENTRY(font_6x10) \
U8G_FONT_TABLE_ENTRY(font_chikita)
```
They'll be available as `u8g.<font_name>` in Lua.
2015-02-08 21:32:32 +01:00
2015-03-11 22:20:28 +01:00
#####Bitmaps
2015-07-21 21:32:42 +02:00
Bitmaps and XBMs are supplied as strings to `drawBitmap()` and `drawXBM()` . This off-loads all data handling from the u8g module to generic methods for binary files. See [u8g_bitmaps.lua ](lua_examples/u8glib/u8g_bitmaps.lua ).
2015-03-21 22:58:58 +01:00
In contrast to the source code based inclusion of XBMs into u8glib, it's required to provide precompiled binary files. This can be performed online with [Online-Utility's Image Converter ](http://www.online-utility.org/image_converter.jsp ): Convert from XBM to MONO format and upload the binary result with [nodemcu-uploader.py ](https://github.com/kmpm/nodemcu-uploader ).
2015-03-11 22:20:28 +01:00
2015-02-08 21:32:32 +01:00
#####Unimplemented functions
- [ ] Cursor handling
- [ ] disableCursor()
- [ ] enableCursor()
- [ ] setCursorColor()
- [ ] setCursorFont()
- [ ] setCursorPos()
- [ ] setCursorStyle()
- [ ] General functions
- [ ] setContrast()
- [ ] setPrintPos()
- [ ] setHardwareBackup()
- [ ] setRGB()
2015-05-15 21:32:28 +02:00
- [ ] setDefaultMidColor()
2015-02-10 21:40:29 +01:00
2015-08-09 20:48:58 +02:00
####Operate a display with ucglib
Ucglib is a graphics library with support for color TFT displays.
Ucglib v1.3.3
#####SPI connection
The HSPI module is used, so certain pins are fixed:
* HSPI CLK = GPIO14
* HSPI MOSI = GPIO13
* HSPI MISO = GPIO12 (not used)
All other pins can be assigned to any available GPIO:
* CS
* D/C
* RES (optional for some displays)
Also refer to the initialization sequence eg in [GraphicsTest.lua ](lua_examples/ucglib/GraphicsRest.lua ):
```lua
2015-10-18 17:40:11 +02:00
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
2015-08-09 20:48:58 +02:00
```
#####Library usage
The Lua bindings for this library closely follow ucglib's object oriented C++ API. Based on the ucg class, you create an object for your display type.
ILI9341 via SPI:
```lua
cs = 8 -- GPIO15, pull-down 10k to GND
dc = 4 -- GPIO2
res = 0 -- GPIO16, RES is optional YMMV
disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res)
```
This object provides all of ucglib's methods to control the display.
Again, refer to [GraphicsTest.lua ](lua_examples/ucglib/GraphicsTest.lua ) to get an impression how this is achieved with Lua code. Visit the [ucglib homepage ](https://github.com/olikraus/ucglib ) for technical details.
#####Displays
To get access to the display constructors, add the desired entries to the display table in [app/include/ucg_config.h ](app/include/ucg_config.h ):
```c
#define UCG_DISPLAY_TABLE \
UCG_DISPLAY_TABLE_ENTRY(ili9341_18x240x320_hw_spi, ucg_dev_ili9341_18x240x320, ucg_ext_ili9341_18) \
UCG_DISPLAY_TABLE_ENTRY(st7735_18x128x160_hw_spi, ucg_dev_st7735_18x128x160, ucg_ext_st7735_18) \
```
#####Fonts
ucglib comes with a wide range of fonts for small displays. Since they need to be compiled into the firmware image, you'd need to include them in [app/include/ucg_config.h ](app/include/ucg_config.h ) and recompile. Simply add the desired fonts to the font table:
```c
#define UCG_FONT_TABLE \
UCG_FONT_TABLE_ENTRY(font_7x13B_tr) \
UCG_FONT_TABLE_ENTRY(font_helvB12_hr) \
UCG_FONT_TABLE_ENTRY(font_helvB18_hr) \
UCG_FONT_TABLE_ENTRY(font_ncenR12_tr) \
UCG_FONT_TABLE_ENTRY(font_ncenR14_hr)
```
They'll be available as `ucg.<font_name>` in Lua.
2015-02-10 21:40:29 +01:00
2015-02-05 19:04:09 +01:00
####Control a WS2812 based light strip
```lua
2015-03-03 10:20:02 +01:00
-- set the color of one LED on GPIO2 to red
ws2812.writergb(4, string.char(255, 0, 0))
-- set the color of 10 LEDs on GPIO0 to blue
ws2812.writergb(3, string.char(0, 0, 255):rep(10))
2015-02-05 19:04:09 +01:00
-- first LED green, second LED white
2015-03-03 10:20:02 +01:00
ws2812.writergb(4, string.char(0, 255, 0, 255, 255, 255))
2015-02-05 19:04:09 +01:00
```
2015-03-15 13:12:29 +01:00
####coap client and server
```lua
-- use copper addon for firefox
cs=coap.Server()
cs:listen(5683)
myvar=1
cs:var("myvar") -- get coap://192.168.18.103:5683/v1/v/myvar will return the value of myvar: 1
-- function should tack one string, return one string.
function myfun(payload)
print("myfun called")
respond = "hello"
return respond
end
cs:func("myfun") -- post coap://192.168.18.103:5683/v1/f/myfun will call myfun
cc = coap.Client()
cc:get(coap.CON, "coap://192.168.18.100:5683/.well-known/core")
cc:post(coap.NON, "coap://192.168.18.100:5683/", "Hello")
2015-03-15 22:08:35 +01:00
```
2015-03-18 07:22:17 +01:00
####cjson
```lua
2015-08-02 17:58:37 +02:00
-- Note that when cjson deal with large content, it may fails a memory allocation, and leaks a bit of memory.
-- so it's better to detect that and schedule a restart.
--
2015-03-18 07:22:17 +01:00
-- Translate Lua value to/from JSON
-- text = cjson.encode(value)
-- value = cjson.decode(text)
json_text = '[ true, { "foo": "bar" } ]'
value = cjson.decode(json_text)
-- Returns: { true, { foo = "bar" } }
value = { true, { foo = "bar" } }
json_text = cjson.encode(value)
-- Returns: '[true,{"foo":"bar"}]'
```
2015-10-09 05:43:57 +02:00
####Read an HX711 load cell ADC.
Note: currently only chanel A with gain 128 is supported.
The HX711 is an inexpensive 24bit ADC with programmable 128x, 64x, and 32x gain.
```lua
2015-10-31 07:27:31 +01:00
-- Initialize the hx711 with clk on pin 5 and data on pin 6
2015-10-09 05:43:57 +02:00
hx711.init(5,6)
-- Read ch A with 128 gain.
2015-10-31 07:27:31 +01:00
raw_data = hx711.read(0)
2015-10-09 05:43:57 +02:00
```