1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | -- D6 接 ds18b20 中 左为地 右VCC -- D7 接风扇调速(蓝线(最边上))。 4线风扇依次为:地、VCC、测速(1圈2方波)、PWN调速 ds18b20=require("ds18b20") ds18b20.setup(6) pwm.setup(7, 1000, 512) pwm.start(7) -- 温度越高转速越高 -- minT 度是起点 minT = 20 -- maxT 度是风扇最高速度 maxT = 80 temperature = nil speed = nil duty = 1023 function t() temperature = ds18b20.readNumber(nil,ds18b20.C) if (temperature == nil) then duty = 1023 else if (temperature<=minT) then duty = 0 elseif (temperature>=maxT) then duty = 1023 else duty = (temperature-minT)*1023/(maxT-minT) end end pwm.setduty(7,duty) print("temperature:",temperature," duty:",duty,"/1023") tmr.softwd(-1) tmr.softwd(5) end tmr.alarm(1,1000,tmr.ALARM_AUTO,t) tmr.softwd(5) wifi.setmode(wifi.STATION) wifi.sta.config("XXX", "XXXXXXXXXX", 1) wifi.sta.sethostname("NodeMCU") cfg={} cfg.ip="192.168.101.6" cfg.netmask="255.255.255.0" cfg.gateway="192.168.101.1" wifi.sta.setip(cfg) srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive",function(conn,payload) local html = string.format("HTTP/1.0 200 OK\r\n" .."Content-Type: text/html\r\n" .."Connection: Close\r\n\r\n" .."Temperature:%f<br>duty:%d/1023",temperature,duty) conn:send(html,function(sent) conn:close() end) end) end) |
4线风扇温度调速+WEB显示
发表评论