Go to file
Robert Foss d2fc878207 Merge remote-tracking branch 'origin' into dev 2015-11-11 20:05:19 -05:00
.settings add spi, and some minor fix 2015-01-18 11:46:15 +08:00
app Use #if instead of #ifdef. 2015-08-03 01:09:49 +02:00
bin serial input now accept non-ascii chars 2014-12-30 19:00:36 +08:00
examples Merge branch 'master' into dev 2015-04-06 00:06:08 +08:00
include Fixed RAM_CONST_ATTR macro. 2015-07-08 17:51:44 +10:00
ld Fix and support Wi-Fi smart link (esp-touch and air-kiss). 2015-07-04 02:27:51 +08:00
lib Fix and support Wi-Fi smart link (esp-touch and air-kiss). 2015-07-04 02:27:51 +08:00
lua_examples added more displays: 2015-07-20 23:11:13 +02:00
lua_modules adding lua_module for Grove Digital Light Sensor (http://www.seeedstudio.com/wiki/Grove_-_Digital_Light_Sensor) 2015-07-21 18:56:29 +02:00
pre_build Merge branch 'dev' of https://github.com/nodemcu/nodemcu-firmware 2015-03-16 01:13:03 +08:00
tools Workaround for DIO mode leaving flash in write-protect. 2015-07-02 20:11:03 +10:00
.cproject move eclipse project file to root, now eclipse can see all files 2015-01-12 21:04:43 +08:00
.project move eclipse project file to root, now eclipse can see all files 2015-01-12 21:04:43 +08:00
.travis.yml Keep SDK internally rather than fetching the one from GeorgHahn, fixes #497 2015-06-24 21:47:34 +02:00
CONTRIBUTING.md Move contributing guidelines to repository 2015-02-26 20:13:27 -05:00
LICENSE update doc 2014-11-21 00:10:01 +08:00
Makefile Makefile fix: "-Os" was overridden by "-O2" 2015-05-01 17:39:47 +02:00
README.md Add notice to use json module in readme, due to the merged PR #584 2015-08-02 23:58:37 +08:00

README.md

NodeMCU

version 0.9.5

Join the chat at https://gitter.im/nodemcu/nodemcu-firmware Build Status Download

###A lua based firmware for wifi-soc esp8266 Build on ESP8266 sdk 0.9.5
Lua core based on eLua project
cjson based on lua-cjson
File system based on spiffs
Open source development kit for NodeMCU nodemcu-devkit
Flash tool for NodeMCU nodemcu-flasher

wiki: NodeMCU wiki
api: NodeMCU api
home: nodemcu.com
bbs: Chinese bbs
docs: NodeMCU docs
Tencent QQ group: 309957875

Summary

  • Easy to access wireless router
  • Based on Lua 5.1.4 (without debug, os module.)
  • Event-Drive programming preferred.
  • Build-in json, file, timer, pwm, i2c, spi, 1-wire, net, mqtt, coap, gpio, wifi, adc, uart and system api.
  • GPIO pin re-mapped, use the index to access gpio, i2c, pwm.
  • Both Integer(less memory usage) and Float version firmware provided.

To Do List (pull requests are very welcomed)

  • loadable c module
  • fix wifi smart connect
  • add spi module (done)
  • add mqtt module (done)
  • add coap module (done)
  • cross compiler (done)

Change log

2015-06-27
fixed ap/station-ap cannot connect to the device.
added wifi.ap.getconfig().
fixed net.dns.getdnsserver().
added new base64 lua example.
added node.bootreason() to inspect boot cause.
optimization of u8g.

Change log

2015-06-25
move constants to ROM. Frees up 16k+ of RAM.
add dhtlib for DHT11/21/22/33/44, port from Arduino.
add 433MHz transmission.
add crypto library.
re-add ws2812.write().
add wifi.getchannel.
changed wifi_setip() to allow setting SoftAP gateway to 0.0.0.0.
added net.dns.setdnsserver and net.dns.getdnsserver.
add support for lm92 temperature sensor.
implement getStrWidth() and setFontLineSpacingFactor().
add -Os flag to release and debug builds.
changed output format of table that is output by wifi_scan_done.
added ability to set scan configuration to wifi.sta.getap.
added function wifi.sta.getconfig().
allow connecting to unsecured WiFi networks.
add setphymode and getphymode to wifi module.
add multicastJoin and multicastLeave to net module.
add Yeelink Modules.

2015-03-31
polish mqtt module, add queue for mqtt module.
add reconnect option to mqtt.connect api, :connect( host, port, secure, auto_reconnect, function(client) )
move node.readvdd33 to adc.readvdd33.
tools/esptool.py supported NodeMCU devkit automatic flash.

more change log

##GPIO NEW TABLE ( Build 20141219 and later)

IO indexESP8266 pinIO indexESP8266 pin
0 [*]GPIO168GPIO15
1GPIO59GPIO3
2GPIO410GPIO1
3GPIO011GPIO9
4GPIO212GPIO10
5GPIO14
6GPIO12
7GPIO13
#### [*] D0(GPIO16) can only be used as gpio read/write. no interrupt supported. no pwm/i2c/ow supported.

#Build option ####file ./app/include/user_modules.h

#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

#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
#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.

Please try Marcel's NodeMCU custom builds cloud service and you can get your own firmware.

#Flash the firmware nodemcu_latest.bin: 0x00000
for most esp8266 modules, just pull GPIO0 down and restart.
You can use the nodemcu-flasher to burn the firmware.

Or, if you build your own bin from source code.
0x00000.bin: 0x00000
0x10000.bin: 0x10000

Better run file.format() after flash

#Connect the hardware in serial baudrate:9600

#Write Lua script to filesystem ####Esplorer Victor Brutskiy's 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 is written in C# and support Windows. This software is opensource and can write lua files to filesystem.

#Start play

####Connect to your ap

    ip = wifi.sta.getip()
    print(ip)
    --nil
    wifi.setmode(wifi.STATION)
    wifi.sta.config("SSID","password")
    ip = wifi.sta.getip()
    print(ip)
    --192.168.18.110

####Manipulate hardware like a arduino

    pin = 1
    gpio.mode(pin,gpio.OUTPUT)
    gpio.write(pin,gpio.HIGH)
    print(gpio.read(pin))

####Write network application in nodejs style

    -- A simple http client
    conn=net.createConnection(net.TCP, 0)
    conn:on("receive", function(conn, payload) print(payload) end )
    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")

####Or a simple http server

    -- A simple http server
    srv=net.createServer(net.TCP)
    srv:listen(80,function(conn)
      conn:on("receive",function(conn,payload)
        print(payload)
        conn:send("<h1> Hello, NodeMcu.</h1>")
      end)
      conn:on("sent",function(conn) conn:close() end)
    end)

####Connect to MQTT Broker

-- 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

UDP client and server

-- a udp server
s=net.createServer(net.UDP)
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")
cu:send("hello")

####Do something shining

  function led(r,g,b)
    pwm.setduty(1,r)
    pwm.setduty(2,g)
    pwm.setduty(3,b)
  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)
  led(512,0,0) -- red
  led(0,0,512) -- blue

####And blink it

  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
  end)

####If you want to run something when system started

  --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.

####With below code, you can telnet to your esp8266 now

    -- 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
       node.output(s_output, 0)   -- re-direct output to function s_ouput.
       c:on("receive",function(c,l)
          node.input(l)           -- works like pcall(loadstring(l)) but support multiple separate line
       end)
       c:on("disconnection",function(c)
          node.output(nil)        -- un-regist the redirect output function, output goes to serial
       end)
       print("Welcome to NodeMcu world.")
    end)

####Use DS18B20 module extends your esp8266

    -- read temperature with DS18B20
    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

####Operate a display with u8glib u8glib is a graphics library with support for many different displays. The integration in nodemcu is developed for SSD1306 based display attached via the I2C port. Further display types and SPI connectivity will be added in the future.

U8glib v1.18.1

#####I2C connection Hook up SDA and SCL to any free GPIOs. Eg. 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:

sda = 5
scl = 6
i2c.setup(0, sda, scl, i2c.SLOW)

#####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 u8g_graphics_test.lua:

spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0)

#####Library usage 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:

sla = 0x3c
disp = u8g.ssd1306_128x64_i2c(sla)

SSD1306 via SPI:

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)

This object provides all of u8glib's methods to control the display. Again, refer to u8g_graphics_test.lua to get an impression how this is achieved with Lua code. Visit the u8glib homepage 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:

#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.

#####Fonts 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 and recompile. Simply add the desired fonts to the font table:

#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.

#####Bitmaps 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. 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: Convert from XBM to MONO format and upload the binary result with nodemcu-uploader.py.

#####Unimplemented functions

  • Cursor handling
    • disableCursor()
    • enableCursor()
    • setCursorColor()
    • setCursorFont()
    • setCursorPos()
    • setCursorStyle()
  • General functions
    • setContrast()
    • setPrintPos()
    • setHardwareBackup()
    • setRGB()
    • setDefaultMidColor()

####Control a WS2812 based light strip

	-- 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

-- 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")

####cjson

-- 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"}]'