nodemcu-firmware/README.md

547 lines
18 KiB
Markdown
Raw Normal View History

# **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-03-10 16:19:30 +01:00
[![Build Status](https://travis-ci.org/nodemcu/nodemcu-firmware.svg)](https://travis-ci.org/nodemcu/nodemcu-firmware) [![Download](https://img.shields.io/badge/download-~400k-orange.svg)](https://github.com/nodemcu/nodemcu-firmware/releases/latest)
2015-03-05 02:22:02 +01:00
2014-11-12 17:17:08 +01:00
###A lua based firmware for wifi-soc esp8266
Build on [ESP8266 sdk 1.4.0](http://bbs.espressif.com/viewtopic.php?f=46&t=1124)<br />
2014-12-22 12:37:49 +01:00
Lua core based on [eLua project](http://www.eluaproject.net/)<br />
2015-03-16 17:24:30 +01:00
cjson based on [lua-cjson](https://github.com/mpx/lua-cjson)<br />
2014-12-22 12:37:49 +01:00
File system based on [spiffs](https://github.com/pellepl/spiffs)<br />
Open source development kit for NodeMCU [nodemcu-devkit](https://github.com/nodemcu/nodemcu-devkit)<br />
Flash tool for NodeMCU [nodemcu-flasher](https://github.com/nodemcu/nodemcu-flasher)<br />
2015-11-16 14:05:21 +01:00
Developer Wiki: (https://github.com/nodemcu/nodemcu-firmware/wiki)<br />
2015-03-06 09:34:24 +01:00
api: [NodeMCU api](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en)<br />
2014-12-22 12:37:49 +01:00
home: [nodemcu.com](http://www.nodemcu.com)<br />
2015-11-16 14:05:21 +01:00
bbs: [Chinese BBS](http://bbs.nodemcu.com)<br />
2015-03-06 09:38:28 +01:00
docs: [NodeMCU docs](http://www.nodemcu.com/docs/)<br />
Tencent QQ group: 309957875<br />
2015-01-06 14:52:14 +01:00
# Summary
- Easy to access wireless router
2015-03-06 09:34:24 +01:00
- Based on Lua 5.1.4 (without *debug, os* module.)
2015-11-16 14:05:21 +01:00
- Event-driven programming preferred
- Built-in modules: json, file, timer, pwm, i2c, spi, 1-wire, net, mqtt, coap, gpio, wifi, adc, uart and system api.
- GPIO pins re-mapped: use the index to access gpio, i2c, pwm.
- 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-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
##GPIO NEW TABLE ( Build 20141219 and later)
<a id="new_gpio_map"></a>
<table>
<tr>
<th scope="col">IO index</th><th scope="col">ESP8266 pin</th><th scope="col">IO index</th><th scope="col">ESP8266 pin</th>
</tr>
<tr>
2015-09-10 15:38:00 +02:00
<td>0 [*]</td><td>GPIO16</td><td>8</td><td>GPIO15 (SPI CS)</td>
</tr>
<tr>
2015-09-10 15:38:00 +02:00
<td>1</td><td>GPIO5</td><td>9</td><td>GPIO3 (UART RX)</td>
</tr>
<tr>
2015-09-10 15:38:00 +02:00
<td>2</td><td>GPIO4</td><td>10</td><td>GPIO1 (UART TX)</td>
</tr>
<tr>
<td>3</td><td>GPIO0</td><td>11</td><td>GPIO9</td>
</tr>
<tr>
<td>4</td><td>GPIO2</td><td>12</td><td>GPIO10</td>
</tr>
<tr>
2015-09-10 15:38:00 +02:00
<td>5</td><td>GPIO14 (SPI CLK)</td><td></td><td></td>
</tr>
<tr>
2015-09-10 15:38:00 +02:00
<td>6</td><td>GPIO12 (SPI MISO)</td><td></td><td></td>
</tr>
<tr>
2015-09-10 15:38:00 +02:00
<td>7</td><td>GPIO13 (SPI MOSI)</td><td></td><td></td>
</tr>
</table>
#### [*] D0(GPIO16) can only be used as gpio read/write. no interrupt supported. no pwm/i2c/ow supported.
#Build option
2015-03-06 09:34:24 +01:00
####file ./app/include/user_modules.h
```c
2015-03-06 09:34:24 +01:00
#define LUA_USE_BUILTIN_STRING // for string.xxx()
#define LUA_USE_BUILTIN_TABLE // for table.xxx()
#define LUA_USE_BUILTIN_COROUTINE // for coroutine.xxx()
#define LUA_USE_BUILTIN_MATH // for math.xxx(), partially work
// #define LUA_USE_BUILTIN_IO // for io.xxx(), partially work
// #define LUA_USE_BUILTIN_OS // for os.xxx(), not work
// #define LUA_USE_BUILTIN_DEBUG // for debug.xxx(), not work
#define LUA_USE_MODULES
2015-03-06 09:34:24 +01:00
#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
2015-03-06 09:34:24 +01:00
#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
2015-03-06 09:34:24 +01:00
#define LUA_USE_MODULES_MQTT
// #define LUA_USE_MODULES_COAP
2015-03-06 09:34:24 +01:00
#define LUA_USE_MODULES_U8G
#define LUA_USE_MODULES_WS2812
#define LUA_USE_MODULES_CJSON
#endif /* LUA_USE_MODULES */
```
#Online firmware custom build
For many application, some modules are not used, remove them can free many memory.<br />
Please try Marcel's [NodeMCU custom builds](http://frightanic.com/nodemcu-custom-build) cloud service and you can get your own firmware.<br />
2014-11-07 05:59:19 +01:00
2014-11-12 17:17:08 +01:00
#Flash the firmware
nodemcu_latest.bin: 0x00000<br />
for most esp8266 modules, just pull GPIO0 down and restart.<br />
You can use the [nodemcu-flasher](https://github.com/nodemcu/nodemcu-flasher) to burn the firmware.
2014-11-11 05:31:45 +01:00
2014-12-22 13:20:20 +01:00
Or, if you build your own bin from source code.<br />
2015-01-24 16:36:04 +01:00
0x00000.bin: 0x00000<br />
0x10000.bin: 0x10000<br />
2014-12-22 13:20:20 +01:00
*Better run file.format() after flash*
2014-12-22 13:20:20 +01:00
2014-11-12 17:17:08 +01:00
#Connect the hardware in serial
baudrate:9600
2014-11-11 05:31:45 +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.
2014-11-12 17:17:08 +01:00
#Start play
2014-11-11 05:31:45 +01:00
2014-11-12 17:17:08 +01:00
####Connect to your ap
2014-10-13 14:24:52 +02:00
2014-11-12 17:17:08 +01:00
```lua
ip = wifi.sta.getip()
print(ip)
--nil
2014-11-07 05:59:19 +01:00
wifi.setmode(wifi.STATION)
2014-11-12 17:17:08 +01:00
wifi.sta.config("SSID","password")
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
2014-11-12 17:17:08 +01:00
####Manipulate hardware like a arduino
2014-11-12 17:17:08 +01:00
```lua
pin = 1
gpio.mode(pin,gpio.OUTPUT)
gpio.write(pin,gpio.HIGH)
print(gpio.read(pin))
2014-11-07 05:59:19 +01:00
```
2014-10-13 14:24:52 +02:00
2014-11-12 17:17:08 +01:00
####Write network application in nodejs style
2014-11-12 17:17:08 +01:00
```lua
-- A simple http client
conn=net.createConnection(net.TCP, 0)
2014-11-25 12:28:44 +01:00
conn:on("receive", function(conn, payload) print(payload) end )
2014-11-12 17:17:08 +01:00
conn:connect(80,"115.239.210.27")
conn:send("GET / HTTP/1.1\r\nHost: www.baidu.com\r\n"
.."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-01-26 16:50:40 +01:00
####Or a simple http server
2015-01-26 16:50:40 +01:00
```lua
-- A simple http server
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
2015-01-26 16:50:40 +01:00
conn:send("<h1> Hello, NodeMcu.</h1>")
end)
2015-01-26 16:50:40 +01:00
conn:on("sent",function(conn) conn:close() end)
end)
```
####Connect to MQTT Broker
```lua
-- init mqtt client with keepalive timer 120sec
m = mqtt.Client("clientid", 120, "user", "password")
-- setup Last Will and Testament (optional)
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
-- to topic "/lwt" if client don't send keepalive packet
m:lwt("/lwt", "offline", 0, 0)
m:on("connect", function(con) print ("connected") end)
m:on("offline", function(con) print ("offline") end)
-- on publish message receive event
m:on("message", function(conn, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
-- m:connect( host, port, secure, auto_reconnect, function(client) )
-- 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)
-- subscribe topic with qos = 0
m:subscribe("/topic",0, function(conn) print("subscribe success") end)
-- or subscribe multiple topic (topic/0, qos = 0; topic/1, qos = 1; topic2 , qos = 2)
-- m:subscribe({["topic/0"]=0,["topic/1"]=1,topic2=2}, function(conn) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
m:publish("/topic","hello",0,0, function(conn) print("sent") end)
m:close(); -- if auto-reconnect == 1, will disable auto-reconnect and then disconnect from host.
-- you can call m:connect again
```
2015-01-26 16:50:40 +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
s=net.createServer(net.UDP)
2015-01-26 16:50:40 +01:00
s:on("receive",function(s,c) print(c) end)
s:listen(5683)
-- a udp client
cu=net.createConnection(net.UDP)
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
```
####Do something shining
```lua
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
pwm.setup(1,500,512)
pwm.setup(2,500,512)
pwm.setup(3,500,512)
pwm.start(1)
pwm.start(2)
pwm.start(3)
2014-11-29 16:33:45 +01:00
led(512,0,0) -- red
led(0,0,512) -- blue
2014-11-12 17:17:08 +01:00
```
####And blink it
```lua
lighton=0
tmr.alarm(1,1000,1,function()
if lighton==0 then
lighton=1
led(512,512,512)
else
lighton=0
led(0,0,0)
end
2014-11-12 17:17:08 +01:00
end)
```
####If you want to run something when system started
```lua
--init.lua will be excuted
file.open("init.lua","w")
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
####With below code, you can telnet to your esp8266 now
```lua
-- a simple telnet server
s=net.createServer(net.TCP,180)
s:listen(2323,function(c)
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.
c:on("receive",function(c,l)
2014-11-18 12:58:48 +01:00
node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
end)
c:on("disconnection",function(c)
2014-11-18 12:58:48 +01:00
node.output(nil) -- un-regist the redirect output function, output goes to serial
end)
2014-11-22 14:24:51 +01:00
print("Welcome to NodeMcu world.")
2014-11-18 12:58:48 +01:00
end)
```
####Use DS18B20 module extends your esp8266
```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"
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
ds18b20 = nil
package.loaded["ds18b20"]=nil
```
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-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
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-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
#####Unimplemented functions
- [ ] Cursor handling
- [ ] disableCursor()
- [ ] enableCursor()
- [ ] setCursorColor()
- [ ] setCursorFont()
- [ ] setCursorPos()
- [ ] setCursorStyle()
- [ ] General functions
- [ ] setContrast()
- [ ] setPrintPos()
- [ ] setHardwareBackup()
- [ ] setRGB()
- [ ] setDefaultMidColor()
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.
####Control a WS2812 based light strip
```lua
-- 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))
-- first LED green, second LED white
ws2812.writergb(4, string.char(0, 255, 0, 255, 255, 255))
```
####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
```
####cjson
```lua
-- 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.
--
-- 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
```