1.ESP8266 Chip Related information
ESP8266 's official website in: Http://www.espressif.com/zh-hans
NODEMCU's official website is in: http://nodemcu.com
NODEMCU Online Document Location: https://nodemcu.readthedocs.io/en/master/en/upload/
NODEMCU Open Source in: Https://github.com/nodemcu/nodemcu-firmware
NODEMCU Development tools in: https://frightanic.com/iot/tools-ides-nodemcu/
NODEMCU's Development forum: http://www.esp8266.com/
2. Using the NOMCU development example
Connecting to a router
Print (Wifi.sta.getip ())
--nil
wifi.setmode (wifi. Station)
wifi.sta.config ("SSID", "password")
print (Wifi.sta.getip ())
--192.168.18.110
Operate IO like Arduino
Pin = 1
gpio.mode (Pin,gpio. OUTPUT)
Gpio.write (Pin,gpio. High)
Gpio.mode (Pin,gpio. INPUT)
print (Gpio.read (PIN))
A simple HTTP client
--A Simple HTTP client
conn=net.createconnection (NET. TCP, False)
Conn:on ("Receive", function (conn, PL) print (PL) end)
conn:connect ("121.41.33.127")
Conn : Send ("get/http/1.1\r\nhost:www.nodemcu.com\r\n"
... " Connection:keep-alive\r\naccept: */*\r\n\r\n ")
A simpler 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 ("
PWM operation
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
Flashing LED
Lighton=0
Tmr.alarm (0,1000,1,function ()
if lighton==0 then
lighton=1
LED (512,512,512)
- -512/1024, 50% duty cycle
else
lighton=0
led (0,0,0)
end
End)
Startup file
--init.lua'll be excuted
file.open ("Init.lua", "W")
file.writeline ([[Print ("Hello world!")])
File.close ()
Node.restart () --This would restart the module.
Use a timer to loop the execution
Tmr.alarm (1,5000,1,function () print ("Alarm 1") end)
Tmr.alarm (0,1000,1,function () print ("Alarm 0") end)
Tmr.alarm (2,2000,1,function () print ("Alarm 2") end)
--After sometime
tmr.stop (0)
Pure Lua-written Telnet server
--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)
--like Pcall (LoadString (L)), support multiple separate lines
end)
C:on ("Disconnection", function (c)
node.output (nil)
--unregist redirect output function, output goes to Serial
end)
print ("Welcome to Nodemcu")
End
Interface with Sensor
--read temperature with Ds18b20
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