『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 13153|回复: 21

[经验分享] DIY净化器二——只接攀藤设备(监测PM和温湿度)

[复制链接]

26

主题

94

帖子

1943

积分

金牌会员

Rank: 6Rank: 6

积分
1943
金钱
1814
HASS币
60

教程狂人论坛风云人物突出贡献

发表于 2017-6-26 15:43:50 | 显示全部楼层 |阅读模式
本帖最后由 FrankLv 于 2017-8-13 22:02 编辑

后面的内容我就不更新了(不过代码部分还是更新为最新的,方便新人使用),直接下载我的附件即可,去除了无效的二氧化碳显示,其实上次就是懒,增加了pm10的显示。
更新内容:修正了攀藤G5取数假死问题!




前面发布过《打造属于自己的空气检测器》,有人询问能否只检测PM和温湿度,书记也是这么想的,所以就抽时间改了下代码,由于本人较懒,屏幕显示就没有改,好在将CO2的数值一直设置位0,所以大家就凑合看吧!
其实你要是觉得屏幕碍事,直接不接屏幕也是可以的,攀藤的接发详见我的第一个帖子:https://bbs.hassbian.com/thread-195-1-3.html


 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()


老规矩,还是放源代码,中级会员下载,如果积分不够多在论坛活动下,或是直接按照上面代码加入init.lua,面包和自行车都会有的。修改其中的WIFI和MQTT部分即可代码:
init.rar (2.11 KB, 下载次数: 8)



评分

参与人数 2金钱 +12 收起 理由
antsunzj + 7 谢谢大神分享!
eric + 5 &amp;lt;font&amp;gt;&amp;lt;font&amp;gt;很给力!&amp;lt;/font&amp;gt

查看全部评分

回复

使用道具 举报

0

主题

162

帖子

693

积分

高级会员

Rank: 4

积分
693
金钱
531
HASS币
0
发表于 2017-6-26 15:47:29 来自手机 | 显示全部楼层
谢谢分享
回复

使用道具 举报

30

主题

999

帖子

4119

积分

论坛元老

Rank: 8Rank: 8

积分
4119
金钱
3115
HASS币
0

活跃会员

发表于 2017-6-26 16:11:00 | 显示全部楼层
收藏了,谢谢分享!
回复

使用道具 举报

1

主题

159

帖子

555

积分

高级会员

Rank: 4

积分
555
金钱
396
HASS币
0
发表于 2017-6-27 07:58:10 来自手机 | 显示全部楼层
感谢,太多!
回复

使用道具 举报

16

主题

81

帖子

354

积分

中级会员

Rank: 3Rank: 3

积分
354
金钱
273
HASS币
0
发表于 2017-6-27 13:57:01 | 显示全部楼层
顶一个,正需要
回复

使用道具 举报

24

主题

605

帖子

3412

积分

元老级技术达人

积分
3412
金钱
2802
HASS币
0

卓越贡献

发表于 2017-6-28 09:15:30 | 显示全部楼层
我是书记,我做主。。哦不,是F大做主,多谢F大,辛苦了!感激!十分感激!
回复

使用道具 举报

39

主题

750

帖子

4717

积分

论坛元老

Freddy.

Rank: 8Rank: 8

积分
4717
金钱
3942
HASS币
0

最佳新人卓越贡献

发表于 2017-6-28 21:32:55 | 显示全部楼层
感谢分享。。CO2检测个人觉得没啥用。。
回复

使用道具 举报

26

主题

1200

帖子

5424

积分

元老级技术达人

积分
5424
金钱
4199
HASS币
100
发表于 2017-7-9 17:43:35 | 显示全部楼层
请问如何调试,我按照这个方法成功刷进去了,没接co2传感器。
hass上看到日志,wifi和mqtt连接应该都对,数据看不出来,该怎么调呢?
2017-07-09 17:37:53 INFO (MainThread) [homeassistant.components.http] Serving /api/history/period to 192.168.2.36 (auth: True)
2017-07-09 17:38:03 INFO (MainThread) [homeassistant.components.http] Serving /api/history/period to 192.168.2.36 (auth: True)
2017-07-09 17:38:07 INFO (MainThread) [homeassistant.components.http] Serving /api/history/period to 192.168.2.36 (auth: True)

回复

使用道具 举报

26

主题

1200

帖子

5424

积分

元老级技术达人

积分
5424
金钱
4199
HASS币
100
发表于 2017-7-9 17:44:19 | 显示全部楼层
25989406 发表于 2017-7-9 17:43
请问如何调试,我按照这个方法成功刷进去了,没接co2传感器。
hass上看到日志,wifi和mqtt连接应该都对,数 ...

另外显示屏还在路上。。。没到,没法用显示屏看数据
回复

使用道具 举报

1

主题

162

帖子

1029

积分

金牌会员

Rank: 6Rank: 6

积分
1029
金钱
867
HASS币
0
发表于 2017-7-9 19:55:21 | 显示全部楼层
支持!!!!!!!!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-5-19 16:18 , Processed in 0.066444 second(s), 36 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表