树莓派3B和攀藤PMS5003ST

0.数据仅供学习参考,一切以官方数据为准。

1.需要的设备:攀藤PMS5003ST,树莓派3B。如果直接连接电脑也可以,需要的软件http://on8vei0g9.bkt.clouddn.com/PM2.5Collector.zip。下面所有代码下载http://on8vei0g9.bkt.clouddn.com/PM25.rar

2.使用RS232USB转串口线连接攀藤,参照文档,接VCC,GND和TXD即可,PIN3可设置休眠。

3.使用python读取数据,在命令行直接显示数据,网页的话不需要这个文件

#encoding=utf-8
import os
import serial
import time
from struct import *

ser = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=2.0)

def read_pm_line(_port):
    rv = b''
    while True:
        ch1 = _port.read()
        if ch1 == b'\x42':
            ch2 = _port.read()
            if ch2 == b'\x4d':
                rv += ch1 + ch2
                rv += _port.read(38)
                return rv

def main(): 
    # conn = sqlite3.connect('pm25.db')
    # c = conn.cursor() 
    recv = read_pm_line(ser)

    tmp = recv[4:36]
    datas = unpack('>hhhhhhhhhhhhhhhh', tmp)
    print('Plantower PMS5003ST,Updated:',time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    print('PM1.0(CF=1): {}ug/m3\n'
              'PM2.5(CF=1): {}ug/m3\n'
              'PM10 (CF=1): {}ug/m3\n'
              'PM1.0 (STD): {}ug/m3\n'
              'PM2.5 (STD): {}ug/m3\n'
              'PM10  (STD): {}ug/m3\n'
              '>0.3um     : {}/0.1L\n'
              '>0.5um     : {}/0.1L\n'
              '>1.0um     : {}/0.1L\n'
              '>2.5um     : {}/0.1L\n'
              '>5.0um     : {}/0.1L\n'
              '>10um      : {}/0.1L\n'
              'HCHO       : {}mg/m3\n'
              'Temperature: {}C\n'
              'Humidity   : {}%'.format(datas[0], datas[1], datas[2],
                                       datas[3], datas[4], datas[5],
                                       datas[6], datas[7], datas[8],
                                       datas[9], datas[10], datas[11],
                                       datas[12]/1000.0, datas[13]/10.0, datas[14]/10.0))
    ser.flushInput()
    time.sleep(0.1)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        if ser != None:
            ser.close()

4.网站主页,单击当前数值显示完整数据




空气质量




5.getData.php,上面的index.html需要

$line[0],"date"=>$line[1]);
echo json_encode($data);
mysql_free_result($result);
mysql_close($con);
?>

6.sendPM2.5.py,7的sh文件需要

#encoding=utf-8
import os
import serial
import time
from struct import *

ser = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=2.0)
  
def read_pm_line(_port):
    rv = b''
    while True:
        ch1 = _port.read()
        if ch1 == b'\x42':
            ch2 = _port.read()
            if ch2 == b'\x4d':
                rv += ch1 + ch2
                rv += _port.read(38)
                return rv

def main():
    recv = read_pm_line(ser)

    tmp = recv[4:36]
    datas = unpack('>hhhhhhhhhhhhhhhh', tmp)
       sendData = datas[0],datas[1],datas[2],datas[3],datas[4],datas[5],datas[6],datas[7],datas[8],datas[9],datas[10],datas[11],datas[12]/1000.0,datas[13]/10.0,datas[14]/10.0
    sendData = str(tuple(sendData))
    tmp = sendData.split(' ')
    sendData = ''.join(tmp)
    print(sendData)
    ser.flushInput()
    time.sleep(0.1)


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        if ser != None:
            ser.close()

7.发送数据sendPM2.5.sh,要把这个文件加入crontab -e里,我是每10分钟获取一次数据

#!/bin/bash
newdata=$(python sendPM2.5.py)
info=$(curl http://127.0.0.1/pm25/receivePM2.5.php?pm=$newdata -s)

8.sql表结构

CREATE TABLE `pm` (
  `data` varchar(60) NOT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) 

9.网页用ajax,php,mysql做的,预览http://andi.dynu.com:8888/pm25/

转载于:https://my.oschina.net/wangandi/blog/901648

你可能感兴趣的:(树莓派3B和攀藤PMS5003ST)