参考difi esp8266物联网mqtt开发板lua教程_第1页
参考difi esp8266物联网mqtt开发板lua教程_第2页
参考difi esp8266物联网mqtt开发板lua教程_第3页
参考difi esp8266物联网mqtt开发板lua教程_第4页
参考difi esp8266物联网mqtt开发板lua教程_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、迪发物联DiFi ESP8266 物联网开发板 Lua(V1.0)Copyright20160722一、准备工作硬件:带串口的 ESP8266 开发板一块(建议 NodeMCU 或 DiFi ESP8266 V1/V2),如果开发板,建议 ESP 模块器大小至少为 4MByte,型号 ESP-12F。DiFi ESP8266V1/V2地址。电脑:这个就不用写了,现在能见到的电脑基本上都能跑,系统版本 Win7 64 位。账号:请打开通过用户获取合法的用户名和,接下来要用到这个用户名和。每个用户可以免费使用两个 Topic,如订阅 TopicB,ESP8266开发板订阅 TopicA,如下图:开

2、发板 Lua 代码调试(ESPlorer 单文件带环境版-64 位.exe)、ESP8266 开发板固件烧写(ESP8266Flasher.exe)、基于 PC 的 MQTT 客户端(MQTT.fx)、基于安卓的MQTT 客户端(MyMQTT.apk),这里给出云ht/s/1geVdqHd。固件:可以从网盘ht/s/1geVdqHd,对固件有点了解的话也可以编译 http:/,也可以源码自行编译。NodeMCU 编程参考文档:。1迪发物联二、WiFi 模块2.1 烧录固件先使用 ESP8266Flasher.exe 将 bin 固件(上面网盘中已经给出)烧入开发板。2迪发物联烧入过程中如果过程

3、中不会出现任何问题。问题请尽量网上寻找解决办法,DiFi ESP8266 V1/V2 开发板烧入2.2 ESPlorer 使用指导打开 ESPlorer.exe,连接 ESP8266 开发板至 PC,打开串口,按下复位键如果显示上面信息表示固件烧录成功!,否则请重新烧写固件。接下来编写 Lua 程序,并将文件上传至 ESP 开发板。3迪发物联两个文件编辑好后,分别点击 save,就可以上传至开发板 ESP8266 模块中,上传完毕,按下板子上的复位按钮,模块重启,输出信息如下4迪发物联信息表示连接 DiFi MQTT 服务器成功,向 tangtopicA 发送“A”数据成功,订阅 tangto

4、picB成功,收到(使用 MyMQTT)发来的数据“jgjgh”,也可以使用 PCMQTT.fx 来进行测试,具体测试方法见以下章节。三、NodeMCU Lua 例程3.1 连接网络-init.luaif true theng_mac=nilpr (set up wifi mode)wifi.setmode(wifi.SION)-根据你的无线路由器来设置上网用户名和wifi.sta.config(WiFi 名称,WiFi wifi.sta.connect()cnt = 0tmr.alarm(1, 1000, tmr.ALARM_AUTO, function() if (wifi.sta.get

5、ip() = nil) and (cnt 20) thenpr (IP unavaiable, Waiting.)cnt = cnt + 1-这里设置你的 WIFI 名字和)5迪发物联elsetmr.stop(1)if (cnt 20) then pr (IP:.wifi.sta.getip() MAC=wifi.sta.getmac() mac=string.gsub(MAC,:,)g_mac = macpr (MAC:.mac)-dofile(gpio.lua)-这里设置自动执行的程序。elsepr (No Wifi Connected.)endendend)elseprpr pr pr(

6、n)(Please edit init.lua:)(Step 1: Modify wifi.sta.config() function in line 5 according settings of your wireless router.)(Step 2: Change the ilse sement in line 1 to if true.)end3.2 简单的 HTTP 客户端( 建议使用 USR-TCP232-Test.exe 做 TCPServer)-init.luapr (set up wifi) wifi.setmode(wifi.SION)wifi.sta.config(W

7、iFi 名称,WiFiwifi.sta.connect()-这里设置你的 WIFI 名字和)tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()if wifi.sta.getip()= nil thenpr (IP unavaiable, Waiting.) else tmr.stop(1)pr (Config done, IP is .wifi.sta.getip() pr (do HTTPC nt.lua)dofile(HTTPC nt.lua)endend)-HTTPC nt.lua-simple http c ntsrv = net.createC

8、onnection(net.TCP, 0)6迪发物联srv:on(receive, function(sck, c) pr (c) end)srv:connect(80,80) srv:on(connection, function(sck, c)- Wait for connection before sending.sck:send(GET / HTTP/1.1rnHost: 80rnConnection: keep-alivernAccept: */*rnrn) end)3.3 简单的 HTTP 服务端(建议使用chrome 浏览器)-init.luapr (set up wifi) w

9、ifi.setmode(wifi.SION)wifi.sta.config(WiFi 名称,WiFiwifi.sta.connect()-这里设置你的 WIFI 名字和)tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()if wifi.sta.getip()= nil thenpr (IP unavaiable, Waiting.) else tmr.stop(1)pr (Config done, IP is .wifi.sta.getip() pr (do HTTPServer.lua) dofile(HTTPServer.lua)endend)-HT

10、TPServer.luasrv=net.createServer(net.TCP) srv:listen(80, function(conn)conn:on(receive, function(conn, payload)pr (payload)conn:send(end)o,h1)end)7迪发物联3.4 按键(中断方式)长按检测led_pin = 6key_pin = 3gpio.mode(key_pio.)gpio.med_pio.OUTPUT)down_timer = 0up_timer = 0press_timer = 0i =0 gpio.trig(key_pin, down, f

11、unction() i=i+1gpio.wried_pio.LOW)if(i%2=1) then-pr (tmr.now()down_timer = tmr.now()/1000000else-pr (tmr.now()up_timer = tmr.now()/1000000 press_timer = up_timer - down_timer pr (press_timer)if(press_timer =5 ) then long_pressed = 1 pr else short_pressed = 1 pr (short press)end(long press)gpio.wried

12、_pio.HIGH)endend)3.5 按键(中断方式)选择 smartconfig 的方式(ESPTOUCH 或AIRKISS)-key.lua-0GPIO16-1GPIO5-2GPIO4-3GPIO0-4GPIO2-5GPIO14-6GPIO12-7GPIO13-8GPIO15-9GPIO38迪发物联-10GPIO1-11GPIO9-12GPIO10doledG_pin = 7ledR_pin = 6ledB_pin = 5beep_pin = 8key_pin = 3-IO13-IO12-IO14-IO15-IO5gpio.mgpio.m gpio.medG_edR_ edB_pio.

13、OUTPUT)pio.OUTPUT) pio.OUTPUT) pio.OUTPUT)gpio.mode(beep_gpio.mode(key_pio.)gpio.wrigpio.wri gpio.wriedG_edR_ edB_pio.HIGH)pio.HIGH) pio.HIGH) pio.LOW)gpio.write(beep_wifi.setmode(wifi.SION)down_timer = 0up_timer = 0press_timer = 0long_pressed = 0short_pressed = 0short_after_long = 0i =0 gpio.trig(k

14、ey_pin, down, function()i=i+1gpio.wrigpio.wriedG_edG_pio.HIGH)pio.LOW)tmr.delay(10000)gpio.wriedG_pio.HIGH)if(i%2=1) then-pr (tmr.now()down_timer = tmr.now()/1000000else-pr (tmr.now()up_timer = tmr.now()/1000000 press_timer = up_timer - down_timer9迪发物联-pr (press_timer)if(press_timer =5 ) then long_p

15、ressed = 1-pr (long_pressed = .long_pressed)-select_netconfigmode()elseshort_pressed = 1-pr (short_pressed = .short_pressed)if (long_pressed = 1 and short_pressed = 1) then short_after_long = short_after_long + 1-pr (short_after_long = .short_after_long) select_netconfigmode()endend-gpio.wri-gpio.wr

16、i-gpio.wriedG_edR_ edB_pio.HIGH)pio.HIGH) pio.HIGH)endend)function select_netconfigmode()if(long_pressed = 1 and short_after_long%2 = 1) then wifi.stopsmart()gpio.wrigpio.wri gpio.wriedG_edR_ edB_pio.LOW)pio.HIGH) pio.HIGH)pr (start smartconfig airkiss)wifi.startsmart(1,function(, password)pr (strin

17、g.format(A_-tmr.stop(0) long_pressed = 0short_pressed = 0short_after_long= 0:%s;PASSWORD:%s, password)gpio.wriend)edG_pio.HIGH)endif(short_after_long%2 = 0) then wifi.stopsmart()gpio.wrigpio.wri gpio.wriedG_edR_ edB_pio.HIGH)pio.HIGH) pio.LOW)pr (start smartconfig esptouch)wifi.startsmart(0,function

18、(, password)pr (string.format(E_:%s;PASSWORD:%s, password)10迪发物联-tmr.stop(0)long_pressed = 0short_pressed = 0short_after_long= 0gpio.wriend)edB_pio.HIGH)endendend3.6DHT11 温湿度-DHT11dht_pin = 1-gpio5sus, temp, humi, temp_dec, humi_dec = dht.read(dht_pin)if sus = dht.OK then-eger firmware using this ex

19、lepr (string.format(DHT Temperature:%d.%03d;Humidity:%d.%03drn,math.floor(temp), temp_dec, math.floor(humi), humi_dec)- Float firmware using this exlepr(DHT Temperature:.temp.;.Humidity:.humi)elseif spr elseif sprendus = dht.ERROR_CHECKSUM then( DHT Checksum error. )us = dht.ERROR_TIMEOUT then ( DHT

20、 timed out. )3.7 打包 JSON 以及JSONledG_pin = 7ledR_pin = 6ledB_pin = 5beep_pin = 8-IO13-IO12-IO14-IO15gpio.mgpio.m gpio.medG_edR_ edB_pio.OUTPUT)pio.OUTPUT) pio.OUTPUT)11迪发物联gpio.mode(beep_pio.OUTPUT)gpio.wrigpio.wri gpio.wriedG_edR_ edB_pio.HIGH)pio.HIGH) pio.HIGH) pio.LOW)gpio.write(beep_t = cjson.de

21、code(ledR:0,ledG:1,ledB:1,beep:0)for k,v in pairs(t) do-pr (k,v)if(k=ledR) then gpio.wriif(k=ledG) then gpio.wri if(k=ledB) then gpio.wriedR_pin,v) endedG_pin,v) end edB_pin,v) endif(k=beep) then gpio.write(beep_pin,v) endendtemp = 23hum = 45photor = 0irsw = 0key = 0ok, json = pcall(cjson.encode, Te

22、mp=temp, Hum=hum, PhotoR=photor, Irsw=irsw, Key=key)if ok thenprelsepr end(json)(failed to encode!)3.8温湿度打包成 JSON 以及JSONlledG_pin = 7ledR_pin = 6ledB_pin = 5beep_pin = 8dht_pin = 1-IO13-IO12-IO14-IO15-gpio5gpio.mgpio.m gpio.medG_edR_ edB_pio.OUTPUT)pio.OUTPUT) pio.OUTPUT) pio.OUTPUT)gpio.mode(beep_g

23、pio.wriedG_pio.HIGH)12迪发物联gpio.wrigpio.wriedR_edB_pio.HIGH)pio.HIGH) pio.LOW)gpio.write(beep_dot = cjson.decode(ledR:0,ledG:1,ledB:1,beep:0)for k,v in pairs(t) do-pr (k,v)if(k=ledR) then gpio.wriif(k=ledG) then gpio.wri if(k=ledB) then gpio.wriedR_pin,v) endedG_pin,v) end edB_pin,v) endif(k=beep) th

24、en gpio.write(beep_pin,v) endendtemp = 0humi = 0photor = 0irsw = 0key = 0sus, temp, humi, temp_dec, humi_dec = dht.read(dht_pin)if sus = dht.OK then- Float firmware using this exlepr(DHT Temperature:.temp.;.Humidity:.humi)elseif spr elseif sprendus = dht.ERROR_CHECKSUM then( DHT Checksum error. )us

25、= dht.ERROR_TIMEOUT then ( DHT timed out. )ok, json = pcall(cjson.encode, Temp=temp, Humi=humi, PhotoR=photor, Irsw=irsw, Key=key)if ok thenpr (json) elsepr (failed to encode!)endend13迪发物联3.9温湿度、按键、红外开关、ADC 打包成 JSON 以及JSONledG_pin = 7ledR_pin = 6ledB_pin = 5beep_pin = 8dht_pin = 1irsw_pin = 2key_pin

26、 = 3-GPIO13-GPIO12-GPIO14-GPIO15-GPIO5-GPIO4-GPIO0gpio.mgpio.m gpio.medG_edR_ edB_pio.OUTPUT)pio.OUTPUT) pio.OUTPUT) pio.OUTPUT)gpio.mode(beep_gpio.mode(irsw_gpio.mode(key_pio.INPUT,gpio.PULLUP)pio.INPUT,gpio.PULLUP)gpio.wrigpio.wri gpio.wriedG_edR_ edB_pio.HIGH)pio.HIGH) pio.HIGH) pio.LOW)gpio.writ

27、e(beep_dot = cjson.decode(ledR:1,ledG:1,ledB:1,beep:0)for k,v in pairs(t) do-pr (k,v)if(k=ledR) then gpio.wriif(k=ledG) then gpio.wri if(k=ledB) then gpio.wriedR_pin,v) endedG_pin,v) end edB_pin,v) endif(k=beep) then gpio.write(beep_pin,v) endendtemp = 0humi = 0photor = 0irsw = 0key = 0sus, temp, hu

28、mi, temp_dec, humi_dec = dht.read(dht_pin)if sus = dht.OK then- Float firmware using this exle14迪发物联prpr(Temp:.temp)(Humi:.humi)elseif spr elseif sprendus = dht.ERROR_CHECKSUM then( DHT Checksum error. )us = dht.ERROR_TIMEOUT then ( DHT timed out. )adc.force_init_mode(adc.INIT_ADC)photor = adc.read(

29、0)pr (PhotoR:.photor)irsw = gpio.read(irsw_pin)pr (Irsw:.irsw)key = gpio.read(key_pin)pr (Key:.key)ok, json = pcall(cjson.encode, Temp=temp, Humi=humi, PhotoR=photor, Irsw=irsw, Key=key)if ok thenpr (json) elsepr (failed to encode!)endend3.10DHT11 温湿度 NodeMCU 做 UDP Cnt,或 PC 做 UDPServer-init.luapr (s

30、et up wifi) wifi.setmode(wifi.SION)wifi.sta.config(WiFi 名称,WiFiwifi.sta.connect()-这里设置你的 WIFI 名字和)tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()if wifi.sta.getip()= nil thenpr (IP unavaiable, Waiting.) else tmr.stop(1)15迪发物联pr (Config done, IP is .wifi.sta.getip()pr (do UDPC nt.lua) dofile(UDPC nt.lu

31、a)endend)-UDPC nt.luadht_pin = 1-gpio5cu=net.createConnection(net.UDP)cu:on(receive,function(cu,c) pr (c) end) cu:connect(5683,55)cu:send(stard temp and humin)function SendData2UDPserver()sus, temp, humi, temp_dec, humi_dec = dht.read(dht_pin) if sus = dht.OK thenprelseif spr elseif sprend(DHT Temp:

32、.temp.; .Humi:.humi)us = dht.ERROR_CHECKSUM then ( DHT Checksum error. )us = dht.ERROR_TIMEOUT then( DHT timed out. )cu:send(DHT Temp:.temp.; .Humi:.humi.n)endtmr.register(0, 2000, tmr.ALARM_AUTO, SendData2UDPserver) tmr.start(0)测试时可以用“网络调试助手”,PC 上可以用“USR-TCP232-Test.exe”,和 PC 都作为 UDP Server3.11 连接D

33、iFi MQTT 服务器并收发数据,可通过串口输入输出-init.luaif true then -change to if true g_mac=nilpr (set up wifi mode)wifi.setmode(wifi.S-please configION)and password according to settings of your wireless router.wifi.sta.config(WiFi 名称,WiFi wifi.sta.connect()cnt = 0tmr.alarm(1, 1000, tmr.ALARM_AUTO, function() if (wi

34、fi.sta.getip() = nil) and (cnt 20) thenpr (IP unavaiable, Waiting.)cnt = cnt + 1-这里设置你的 WIFI 名字和)16迪发物联elsetmr.stop(1)if (cnt =90) then i=0 end end) gpio.write(4, gpio.HIGH)endtmr.register(2, 2000, tmr.ALARM_AUTO, sendPublishTimer)tmr.start(2)18迪发物联3.12 网络授时(相当准确)-init.luapr (set up wifi) wifi.setmo

35、de(wifi.SION)wifi.sta.config(WiFi 名称,WiFiwifi.sta.connect()-这里设置你的 WIFI 名字和)tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()if wifi.sta.getip()= nil thenpr (IP unavaiable, Waiting.) else tmr.stop(1)pr (Config done, IP is .wifi.sta.getip() pr (do GetTimer.lua) dofile(GetTimer.lua)endend)-GetTimer.luahos

36、t = port = 80 url = /httpPKG = HEAD . url . HTTP/1.1rnHost: . host . rnConnection: closernrncurGMT = bConnected = falsecalibratedAftowerOn = false tzone = 8- 东 8 区时区t = 0t = 1t2= 0tmrBtn = 2tmrCycled = 4ioPWR = 1ioCali = 2function adjustClock(hour, minute)local cchour = hour % 12 minute = minute % 6

37、0 cc = hour * 60 + minutepr (= Calibrated byernet =)pr (Adjust clock to . hour . : . minute)19迪发物联endfunction constructConnection()if bConnected = false then socket = nilsocket = net.createConnection(net.TCP, 0) socket:on(connection, function(sck, response)bConnected = truet =t + 1-pr (rn# Conneted!

38、tt&socket:, socket)socked(httpPKG)end)socket:on(disconnection, function(sck, response) bConnected = falsesocket:close() socket = nil-pr (# Socket disconneted, Count= .end)socket:on(receive, function(sck, response) hh = nilt ., Heap= . node.heap()curGMT = string.sub(response,string.find(response,Date

39、: ),string.find(response,Date: )+35)hh,mm,ss = string.match(curGMT, (%d+)%d*:(%d+)%d*:(%d+)%d* GMT) if hh = nil thenpr (# Geternet time from . host, (hh+tzone)%24).:.mm.:.ss)if (calibratedAftcalibratedAftowerOn = false) or (hh + tzone = 24) thenowerOn = trueadjustClock(hh+tzone, mm)endendend)socket:

40、connect(port, host)endendconstructConnection()tmr.alarm(tmrCycled,3660000,1,function() constructConnection() end)-可以将 3660000 改为 5000 试试tmr.start(tmrCycled)3.13控制LED 亮度LEDB = 5LEDR = 6LEDG = 720迪发物联.setup(LEDB, 500, 512).setup(LEDR, 500, 512).setup(LEDG, 500, 512).start(LEDB).start(LEDR).start(LEDG)

41、function led(r, g, b).setduty(LEDG, g).setduty(LEDB, b).setduty(LEDR, r)endled(0, 1023, 1023)-红色-led(1023, 0, 1023)-绿色-led(1023, 1023, 0)-绿色3.14 通过 MQTT控制 LED值broker = - IP or hostname of MQTT broker- MQTT port (default 1883)mqttport = 1883userID = x-用户名请到-请到usWD = xx- 请到获取c ntID = REALY = 0LEDB = 5

42、LEDR = 6LEDG = 7BEEP = 8gpio.mode(REALY, gpio.OUTPUT)gpio.mgpio.m gpio.mEDB, gpio.OUTPUT)EDR, gpio.OUTPUT) EDG, gpio.OUTPUT)gpio.mode(BEEP, gpio.OUTPUT)gpio.write(REALY, gpio.HIGH)gpio.wrigpio.wri gpio.wriEDB, gpio.HIGH)EDR, gpio.HIGH) EDG, gpio.HIGH)21迪发物联gpio.write(BEEP, gpio.LOW).setup(LEDB, 500,

43、 512).setup(LEDR, 500, 512).setup(LEDG, 500, 512).start(LEDB).start(LEDR).start(LEDG)color = wifi.setmode(wifi.SION)-你的无线网名称和wifi.sta.config(,x).setduty(LEDB, 512)function wifi_connect()ip = wifi.sta.getip() if ip = nill thenpr (Connected, ip is: . ip) tmr.stop(1).setduty(LEDB, 0)ready = 1elseready

44、= 0endendfunction mqtt_do()if ready = 1 thenm = mqtt.C nt(c ntID, 120, userID, us m:connect( broker , mqttport, 0, 1, function(conn)WD)pr (Connected to MQTT: . broker . : . mqttport . as . ctmr.stop(0) connected = 1; sub_mqtt() on_mqtt()end) endendntID )22迪发物联function sub_mqtt()m:subscribe(testtopic

45、, 0, function(conn) pr (Subscribed)end)endfunction on_mqtt() - req. endm:on(message, function(conn, topic, input) -red end&) red,green,blue = input:match(,+),(,+),(,+) colorr = tonumber(red)colorg = tonumber(green) colorb = tonumber(blue)pr (red: .colorr.ngreen: .colorg.nblue: .colorb)if (colorr = 0

46、) then.setduty(LEDR, colorr)elsepr (Incorrect red value) endif (colorg = 0) then.setduty(LEDG, colorg)elsepr (Incorrect green value) endif (colorb = 0) then.setduty(LEDB, colorb)elsepr (Incorrect blue value) endend)endtmr.alarm(0, 1000, 1, function()mqtt_do() tmr.delay(1000) end)tmr.alarm(1, 1111, 1

47、, function()wifi_connect()23迪发物联end)-端或网络端发送数据格式为 1000,0,10003.15 通过 MQTT控制 LED、继电器开关broker = - IP or hostname of MQTT broker- MQTT port (default 1883)-用户名请到mqttport = 1883userID = x-请到- 请到usWD = xx获取c ntID = REALY = 0LEDB = 5LEDR = 6LEDG = 7BEEP = 8gpio.mode(REALY, gpio.OUTPUT)gpio.mgpio.m gpio.mED

48、B, gpio.OUTPUT)EDR, gpio.OUTPUT) EDG, gpio.OUTPUT)gpio.mode(BEEP, gpio.OUTPUT)gpio.write(REALY, gpio.HIGH)gpio.wrigpio.wri gpio.wriEDB, gpio.HIGH)EDR, gpio.HIGH) EDG, gpio.HIGH)gpio.write(BEEP, gpio.LOW)wifi.setmode(wifi.Swifi.sta.config(ION),gufei1984)function wifi_connect()ip = wifi.sta.getip() if

49、 ip = nil thenpr (Connected, ip is: . ip) tmr.stop(1)ready = 1elseready = 0endend24迪发物联function mqtt_do()if ready = 1 thenm = mqtt.C nt(c ntID, 120, userID, us m:connect( broker , mqttport, 0, 1, function(conn)WD)pr (Connected to MQTT: . broker . : . mqttport . as . ctmr.stop(0) connected = 1; sub_m

50、qtt()m:on(message, function(conn, topic, input) pr (input)if input = off thenntID )gpio.wriEDG, gpio.HIGH)gpio.write(REALY, gpio.HIGH)elseif input = on thengpio.wriEDG, gpio.LOW)gpio.write(REALY, gpio.LOW)else-pr (error) endend) end)endendfunction sub_mqtt()m:subscribe(18fe34d6a895A, 0, function(con

51、n) pr (Subscribed)end)endtmr.alarm(0, 1000, 1, function()mqtt_do() tmr.delay(1000) end)tmr.alarm(1, 1111, 1, function()wifi_connect() end)端发送 on 或 off 即可25迪发物联3.16 Socket 客户端发送温度给服务端-init.luapr (set up wifi) wifi.setmode(wifi.SION)-你的无线网名称和wifi.sta.config(wifi.sta.connect(),x)tmr.alarm(1, 1000, tmr.

52、ALARM_AUTO, function()if wifi.sta.getip()= nil thenpr (IP unavaiable, Waiting.) else tmr.stop(1)pr (Config done, IP is .wifi.sta.getip() pr (do HTTPC nt.lua)dofile(HTTPC nt.lua)endend)-HTTPC nt.lua-simple http c nt-i = 0dht_pin = 1-gpio5srv = net.createConnection(net.TCP, 0)-srv:on(receive, function

53、(sck, c) pr (c) end) srv:connect(9999,) -根据具体情况设置srv:on(connection, function(sck, c)- Wait for connection before sending. pr (-connection)end)srv:on(receive, function(sck, c)- Wait for receive before sending. if c=read then-i = i + 1-sck:send(i = .i.rn)-pr (Send dao server)sus, temp, humi, temp_dec,

54、 humi_dec = dht.read(dht_pin)if sus = dht.OK thenprelseif spr elseif s(DHT Temp:.temp.; .Humi:.humi)us = dht.ERROR_CHECKSUM then ( DHT Checksum error. )us = dht.ERROR_TIMEOUT then26迪发物联pr ( DHT timed out. )endsck:send(Temp:.temp.;.Humi:.humi.rn)-sck:send(temp.rn)endend)srv:on(sent,function(sck, c)-

55、Wait for sent before sending.-sck:close()pr (-sent)end)srv:on(reconnection,function(sck, c)- Wait for reconnection before sending.-sck:close()pr (-reconnection)end)srv:on(disconnection,function(sck, c)- Wait for disconnection before sending.-sck:close()pr (-disconnection)end)3.17 Socket 客户端发送温度给服务端(

56、丢失自动重连)-init.luapr (set up wifi)27迪发物联wifi.setmode(wifi.SION)-你的无线网名称和wifi.sta.config(wifi.sta.connect(),x)tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()if wifi.sta.getip()= nil thenpr (IP unavaiable, Waiting.) else tmr.stop(1)pr (Config done, IP is .wifi.sta.getip() pr (do HTTPC nt.lua)dofile(HTTPC

57、nt.lua)endend)-HTTPC nt.luadht_pin = 1port = 9999-gpio5host = 85tempLast = 26local function run()pr (run)local srv = net.createConnection(net.TCP, 0)srv:connect(port,host)srv:on(connection, function(sck, c)pr (-connection)end)srv:on(disconnection,function(sck, c)pr (-disconnection)srv = niltmr.alarm

58、(1, 3000, 0, run) end)srv:on(receive, function(sck, c)pr (-receive)-if c=READrn then if c=READ thensus, temp, humi, temp_dec, humi_dec = dht.read(dht_pin) if sus = dht.OK thenpr (DHT Temp:.temp.; .Humi:.humi)tempLast = temp28迪发物联elseif spr elseif sprendus = dht.ERROR_CHECKSUM then( DHT Checksum erro

59、r. )us = dht.ERROR_TIMEOUT then ( DHT timed out. )if(temp=0) then temp = tempLast end-sck:send(Temp:.temp.;.Humi:.humi.rnrn) sck:send(temp.rn)endend)srv:on(sent,function(sck, c)pr (-sent)end)srv:on(reconnection,function(sck, c)pr (-reconnection)end)endrun()3.18 使用 DiFi 开发板,十分钟连接贝壳物联,实现控制-init.luapr

60、(set up wifi) wifi.setmode(wifi.SION)-你的无线网名称和wifi.sta.config(wifi.sta.connect(),x)tmr.alarm(0, 1000, tmr.ALARM_AUTO, function()if wifi.sta.getip()= nil thenpr (IP unavaiable, Waiting.) else tmr.stop(0)pr (Config done, IP is .wifi.sta.getip() pr (do kaiguan.lua) dofile(kaiguan.lua)endend)下面的 APIKEY、

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论