Archive for 11月, 2016

4线风扇温度调速+WEB显示

 

-- 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
duty:%d/1023",temperature,duty) conn:send(html,function(sent) conn:close() end) end) end)

No comment »