參考difi esp8266物聯(lián)網(wǎng)mqtt開發(fā)板lua教程_第1頁
參考difi esp8266物聯(lián)網(wǎng)mqtt開發(fā)板lua教程_第2頁
參考difi esp8266物聯(lián)網(wǎng)mqtt開發(fā)板lua教程_第3頁
參考difi esp8266物聯(lián)網(wǎng)mqtt開發(fā)板lua教程_第4頁
參考difi esp8266物聯(lián)網(wǎng)mqtt開發(fā)板lua教程_第5頁
已閱讀5頁,還剩33頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、迪發(fā)物聯(lián)DiFi ESP8266 物聯(lián)網(wǎng)開發(fā)板 Lua(V1.0)Copyright20160722一、準(zhǔn)備工作硬件:帶串口的 ESP8266 開發(fā)板一塊(建議 NodeMCU 或 DiFi ESP8266 V1/V2),如果開發(fā)板,建議 ESP 模塊器大小至少為 4MByte,型號 ESP-12F。DiFi ESP8266V1/V2地址。電腦:這個就不用寫了,現(xiàn)在能見到的電腦基本上都能跑,系統(tǒng)版本 Win7 64 位。賬號:請打開通過用戶獲取合法的用戶名和,接下來要用到這個用戶名和。每個用戶可以免費使用兩個 Topic,如訂閱 TopicB,ESP8266開發(fā)板訂閱 TopicA,如下圖:開

2、發(fā)板 Lua 代碼調(diào)試(ESPlorer 單文件帶環(huán)境版-64 位.exe)、ESP8266 開發(fā)板固件燒寫(ESP8266Flasher.exe)、基于 PC 的 MQTT 客戶端(MQTT.fx)、基于安卓的MQTT 客戶端(MyMQTT.apk),這里給出云ht/s/1geVdqHd。固件:可以從網(wǎng)盤ht/s/1geVdqHd,對固件有點了解的話也可以編譯 http:/,也可以源碼自行編譯。NodeMCU 編程參考文檔:。1迪發(fā)物聯(lián)二、WiFi 模塊2.1 燒錄固件先使用 ESP8266Flasher.exe 將 bin 固件(上面網(wǎng)盤中已經(jīng)給出)燒入開發(fā)板。2迪發(fā)物聯(lián)燒入過程中如果過程

3、中不會出現(xiàn)任何問題。問題請盡量網(wǎng)上尋找解決辦法,DiFi ESP8266 V1/V2 開發(fā)板燒入2.2 ESPlorer 使用指導(dǎo)打開 ESPlorer.exe,連接 ESP8266 開發(fā)板至 PC,打開串口,按下復(fù)位鍵如果顯示上面信息表示固件燒錄成功!,否則請重新燒寫固件。接下來編寫 Lua 程序,并將文件上傳至 ESP 開發(fā)板。3迪發(fā)物聯(lián)兩個文件編輯好后,分別點擊 save,就可以上傳至開發(fā)板 ESP8266 模塊中,上傳完畢,按下板子上的復(fù)位按鈕,模塊重啟,輸出信息如下4迪發(fā)物聯(lián)信息表示連接 DiFi MQTT 服務(wù)器成功,向 tangtopicA 發(fā)送“A”數(shù)據(jù)成功,訂閱 tangto

4、picB成功,收到(使用 MyMQTT)發(fā)來的數(shù)據(jù)“jgjgh”,也可以使用 PCMQTT.fx 來進(jìn)行測試,具體測試方法見以下章節(jié)。三、NodeMCU Lua 例程3.1 連接網(wǎng)絡(luò)-init.luaif true theng_mac=nilpr (set up wifi mode)wifi.setmode(wifi.SION)-根據(jù)你的無線路由器來設(shè)置上網(wǎng)用戶名和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-這里設(shè)置你的 WIFI 名字和)5迪發(fā)物聯(lián)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)-這里設(shè)置自動執(zhí)行的程序。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()-這里設(shè)置你的 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迪發(fā)物聯(lián)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 服務(wù)端(建議使用chrome 瀏覽器)-init.luapr (set up wifi) w

9、ifi.setmode(wifi.SION)wifi.sta.config(WiFi 名稱,WiFiwifi.sta.connect()-這里設(shè)置你的 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迪發(fā)物聯(lián)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迪發(fā)物聯(lián)-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迪發(fā)物聯(lián)-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迪發(fā)物聯(lián)-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迪發(fā)物聯(lián)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迪發(fā)物聯(lián)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迪發(fā)物聯(lián)3.9溫濕度、按鍵、紅外開關(guān)、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迪發(fā)物聯(lián)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()-這里設(shè)置你的 WIFI 名字和)tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()if wifi.sta.getip()= nil thenpr (IP unavaiable, Waiting.) else tmr.stop(1)15迪發(fā)物聯(lián)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)測試時可以用“網(wǎng)絡(luò)調(diào)試助手”,PC 上可以用“USR-TCP232-Test.exe”,和 PC 都作為 UDP Server3.11 連接D

33、iFi MQTT 服務(wù)器并收發(fā)數(shù)據(jù),可通過串口輸入輸出-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-這里設(shè)置你的 WIFI 名字和)16迪發(fā)物聯(lián)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迪發(fā)物聯(lián)3.12 網(wǎng)絡(luò)授時(相當(dāng)準(zhǔn)確)-init.luapr (set up wifi) wifi.setmo

35、de(wifi.SION)wifi.sta.config(WiFi 名稱,WiFiwifi.sta.connect()-這里設(shè)置你的 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 區(qū)時區(qū)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迪發(fā)物聯(lián)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迪發(fā)物聯(lián).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迪發(fā)物聯(lián)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)-你的無線網(wǎng)名稱和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迪發(fā)物聯(lián)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迪發(fā)物聯(lián)end)-端或網(wǎng)絡(luò)端發(fā)送數(shù)據(jù)格式為 1000,0,10003.15 通過 MQTT控制 LED、繼電器開關(guān)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迪發(fā)物聯(lián)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)端發(fā)送 on 或 off 即可25迪發(fā)物聯(lián)3.16 Socket 客戶端發(fā)送溫度給服務(wù)端-init.luapr (set up wifi) wifi.setmode(wifi.SION)-你的無線網(wǎng)名稱和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,) -根據(jù)具體情況設(shè)置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迪發(fā)物聯(lián)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 客戶端發(fā)送溫度給服務(wù)端(

56、丟失自動重連)-init.luapr (set up wifi)27迪發(fā)物聯(lián)wifi.setmode(wifi.SION)-你的無線網(wǎng)名稱和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迪發(fā)物聯(lián)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 開發(fā)板,十分鐘連接貝殼物聯(lián),實現(xiàn)控制-init.luapr

60、(set up wifi) wifi.setmode(wifi.SION)-你的無線網(wǎng)名稱和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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論