前陣子樹莓派因爲沒外殼不當心短路燒掉了。最近得手nodeMCU的板子,不只便宜還支持串口交互式調試,非常方便。尋思着實現個自動化控制玩玩。自從「穹頂之下」的信息被普及,不只要出門帶口罩,還要家裏開空氣淨化器。因而照着知乎也DIY了一個。但做爲一個程序員,不搗鼓一點硬件軟件怎麼能夠,因而花了兩三天搗鼓了lua,順便配了個web端的語音播報,軟硬件組合達到了指望的效果。(順便吐槽一下tinyOS的組件式編程,文檔也少得可憐)php
硬件:html
軟件:前端
--家庭空氣質量管家 --sensor爲傳感器data口 --power爲繼電器控制口 --D0 CH1 0 --D1 CH2 0 --D2 CH4 0 --D3 MQ-DATA 1 --D4 DHT-DATA 0 --D5 CH3 0 --D6 DHT-VCC 1 --RX PM-TX Air={ pm010=0,pm025=0,pm100=0, temp=0,humi=0,smoke=1, gpio_pm=0,gpio_temp=0,gpio_humi=0,gpio_smoke=0, humi_power=0, pm_power=1, smoke_power=2, mq_sensor=3, dht_sensor=4, temp_power=5 } --引腳初始化 for pi=0,6,1 do gpio.mode(pi, gpio.OUTPUT,(pi==6)and gpio.PULLUP or gpio.FLOAT) gpio.write(pi,(pi==3 or pi==6)and gpio.HIGH or gpio.LOW) end --網絡 wifi.setmode(wifi.STATION) wifi.sta.config("SSID","PASSWORD") --串口監聽 uart.setup( 0, 9600, 8, 0, 1, 0 ) uart.on("data", function(data) if(string.len(data)==32 and string.byte(data)==66) then --顆粒 Air.pm010=tonumber(string.byte(data,11))*256+tonumber(string.byte(data,12)) Air.pm025=tonumber(string.byte(data,13))*256+tonumber(string.byte(data,14)) Air.pm100=tonumber(string.byte(data,15))*256+tonumber(string.byte(data,16)) --溫溼度 _,Air.temp,Air.humi, _, _ =dht.read(Air.dht_sensor) --煙霧 Air.smoke=gpio.read(Air.mq_sensor) --繼電器 Air.gpio_pm,Air.gpio_temp,Air.gpio_humi,Air.gpio_smoke=gpio.read(Air.pm_power),gpio.read(Air.temp_power),gpio.read(Air.humi_power),gpio.read(Air.smoke_power) end end, 0) --http srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive", function(client,request) local buf = "{" local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP") if(method == nil)then _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP") end local _GET = {} if (vars ~= nil)then for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do _GET[k] = v end end if(_GET.action == "read")then for key,value in pairs(Air) do buf=buf..key..":"..value.."," end elseif(_GET.action == "write")then gpio.write(_GET.pin,_GET.level) elseif(_GET.action == "debug")then node.input(_GET.data) end client:send(buf.."error:0}") client:close() collectgarbage() end) end)