function initOLED(sda, scl) --Set up the u8glib lib
sla = 0x3c
i2c.setup(0, sda, scl, i2c.SLOW)
disp = u8g.sh1106_128x64_i2c(sla)
disp:setFont(u8g.font_6x10)
disp:setFontRefHeightExtendedText()
disp:setDefaultForegroundColor()
disp:setFontPosTop()
print("Finished initial OELD")
end --初始化OLED
function initSCREEN()
SDA = 5 --D5
SCL = 6 --D6
initOLED(SDA, SCL) --需要换成自己的串口
disp:firstPage()
repeat
disp:drawFrame(15,15,100,25)
disp:drawStr(25,25,"PM2.5 Detector")
disp:drawStr(30,50,"Frank's Home")
until disp:nextPage() == false
print("Finished initial Screen")
end --初始化屏幕
function dispOLED(d1, d2, d3, d4, d5) --dispOLED(pm25, pm10, aqi1, temp, hum)
disp:firstPage()
repeat
disp:drawFrame(25,0,80,12)
disp:drawStr(30, 2, "Frank's Home")
disp:drawStr(0, 15, "PM2.5")
disp:drawStr(0, 23, "ug/m3")
disp:drawStr(98, 15, "pm10")
disp:drawStr(98, 23, "ug/m3")
disp:drawStr(60, 15, "AQI")
if (d1 == nil) then
disp:drawStr(0, 37, "")
else
disp:drawStr(0, 37, d1)
end
if (d2 == nil) then
disp:drawStr(98, 37, "")
else
disp:drawStr(98, 37, d2)
end
if (d3 == nil) then
disp:drawStr(60, 35, "")
else
disp:drawStr(60, 35, d3)
end
if (d4 == nil) then
disp:drawStr(0, 52, "Temp ".."".."C")
else
disp:drawStr(0, 52, "Temp "..d4.."C")
end
if (d5 == nil) then
disp:drawStr(66, 52, "Humi ".."".."%")
else
disp:drawStr(66, 52, "Humi "..d5.."%")
end
until disp:nextPage() == false
end --OLED显示
function initWIFI()
print("Setting up WIFI...")
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID", "password") --改为自己的wifi信息
wifi.sta.connect()
tmr.alarm(1, 500, 1,
function()
j =j + 1
if wifi.sta.getip()== nil then
disp:firstPage()
repeat
disp:drawFrame(15,5,100,25)
disp:drawStr(25,15,"PM2.5 Detector")
disp:drawStr(30,35,"Frank's Home")
disp:drawStr(33,50,"IP Getting")
until disp:nextPage() == false
print("IP unavailable, Waiting...")
else
j = 0
tmr.stop(1)
disp:firstPage()
repeat
disp:drawFrame(15,5,100,25)
disp:drawStr(25,15,"PM2.5 Detector")
disp:drawStr(30,35,"Frank's Home")
disp:drawStr(20,50,"IP:"..wifi.sta.getip())
until disp:nextPage() == false
print("Config done, IP is "..wifi.sta.getip())
end
if wifi.sta.getip()== nil and j >= 20 then
j = 0
tmr.stop(1)
disp:firstPage()
repeat
disp:drawFrame(15,5,100,25)
disp:drawStr(25,15,"PM2.5 Detector")
disp:drawStr(30,35,"Frank's Home")
disp:drawStr(25,50,"IP unavailable")
until disp:nextPage() == false
print("IP unavailable, Please check up")
end
end) -- function
end -- initWIFI
function sendData(t1, t2, t3, t4, t5, t6) --sendData(aqi1,pm25,pm10,pm01,temp,hum,co2)
print("Setting up mqtt.Client...")
m = mqtt.Client("franklv-3", 10, "pi", "password") --改为自己想要的客户端信息
print("Attempting client connect...")
--改为自己的MQTT地址和端口和订阅的主题信息
m:connect("192.168.1.22", 1883, 0, 0,
function(conn)
print("Connected to MQTT")
if t1 ~= nil then
m:publish("/aqi3", t1, 0, 0,
function(client)
end)
end
if t2 ~= nil then
m:publish("/pm253", t2, 0, 0,
function(client)
end)
end
if t3 ~= nil then
m:publish("/pm103", t3, 0, 0,
function(client)
end)
end
if t4 ~= nil then
m:publish("/pm013", t4, 0, 0,
function(client)
end)
end
if t5 ~= nil then
m:publish("/temp3", t5, 0, 0,
function(client)
end)
end
if t6 ~= nil then
m:publish("/hum3", t6, 0, 0,
function(client)
end)
end
end,
function(client, reason)
print("Connection failed, reason: " .. reason)
end)
end -- 连接到MQTT服务器,192.168.1.22为服务器IP,1883为服务器端口
function parse(data)
local bs = {}
for i = 1, #data do
bs = string.byte(data, i)
end
if (bs[1] ~= 0x42) or (bs[2] ~= 0x4d) then
return nil
end
local d = {}
d['pm1_0-CF1-ST'] = bs[5] * 256 + bs[6]
d['pm2_5-CF1-ST'] = bs[7] * 256 + bs[8]
d['pm10-CF1-ST'] = bs[9] * 256 + bs[10]
d['pm1_0-AT'] = bs[11] * 256 + bs[12]
d['pm2_5-AT'] = bs[13] * 256 + bs[14]
d['pm10-AT'] = bs[15] * 256 + bs[16]
d['0_3um-count'] = bs[17] * 256 + bs[18]
d['0_5um-count'] = bs[19] * 256 + bs[20]
d['1_0um-count'] = bs[21] * 256 + bs[22]
d['2_5um-count'] = bs[23] * 256 + bs[24]
d['temperature'] = bs[25] * 256 + bs[26]
d['humidity'] = bs[27] * 256 + bs[28]
return d
end -- parse
function aqipm25(t)
if (t <= 12) then return t * 50 / 12
elseif (t <= 35) then return 50 + (t - 12) * 50 / 23
elseif (t <= 55) then return 100 + (t - 35) * 5 / 2
elseif (t <= 150) then return 150 + (t - 55) * 2
elseif (t <= 350) then return 50 + t
else return 400 + (t - 350) * 2 / 3
end
end --aqipm25
function aqipm10(t)
if (t <= 55) then return t * 50 / 55
elseif (t <= 355) then return 50 + (t - 55) / 2
elseif (t <= 425) then return 200 + (t - 355) * 10 / 7
elseif (t <= 505) then return 300 + (t - 425) * 10 / 8
else return t - 105
end
end --aqipm10
function aqi(t25,t10)
if (t25 > t10) then return math.ceil(t10)
else return math.ceil(t25)
end
end --aqi
function getaqi()
uart.alt(0)
data = nil
uart.setup(0, 9600, 8, 0, 1, 0)
uart.on("data", 32,function(data)
pms5 = parse(data)
tmr.delay(5000000)
if pms5 ~= nil then
pm01 = pms5['pm1_0-AT']
pm25 = pms5['pm2_5-AT']
pm10 = pms5['pm10-AT']
temp = pms5['temperature'] / 10
hum = pms5['humidity'] / 10
aqi25 = aqipm25(pm25)
aqi10 = aqipm10(pm10)
aqi1 = aqi(aqi25,aqi10)
end
getsend()
end,0)
end --getaqi
function getsend()
uart.alt(1)
data = nil
uart.setup(0, 9600, 8, 0, 1, 0)
tmr.alarm(3, 5000, 1, function()
tmr.stop(3)
k = k + 1
dispOLED(pm25, pm10, aqi1, temp, hum)
if wifi.sta.getip()~= nil and k >= 2 then
k = 0
sendData(aqi1,pm25,pm10,pm01,temp,hum)
end
getaqi()
end)
end --getsend
j = 0
k = 0
pms5 = nil
aqi1 = nil
pm25 = nil
pm10 = nil
pm01 = nil
temp = nil
hum = nil
disp = nil
initSCREEN()
initWIFI()
getaqi()