基于 openWRT、DS18B02、digitemp、rrdtool的开源多点温度监控系统

基于 openWRT、DS18B02、digitemp、rrdtool的开源多点温度监控系统

一、设备型号 

主设备型号为贝尔RG-100A-AA刷为OpenWRT系统

基于 openWRT、DS18B02、digitemp、rrdtool的开源多点温度监控系统_第1张图片
 

温度芯片为DS18b20防水温度感应芯片,后端做成了音频线接口,便于下面的转接

基于 openWRT、DS18B02、digitemp、rrdtool的开源多点温度监控系统_第2张图片

转换接头为,音频转USB接口

 

 

硬件连接完之后如下

 

   

 二、安装系统

1.设置OpenWRT

a)         设置源

由于设备的自带内存不足,会导致安装相关工具失败,需要更改opkg的使用位置基于 openWRT、DS18B02、digitemp、rrdtool的开源多点温度监控系统_第3张图片

/dev/sda1设备是接上的U盘,虽然可写,但是不能将软件安装到上面

更改/etc/opkg.conf文件如下


b)        获取digitemp程序

 

c)         安装rrdtool


d)        安装USB驱动相关

opkg install kmod-usb-core kmod-usb-ohci kmod-usb-serial

opkg install libusb kmod-usb-acm chat comgt usb-modeswitch

opkg install kmod-usb-serial-ch341 usbutils cgilib rrdcgi

e)         查看端口,因为我们使用的是USB模拟串口设备,所以应当是ttyUSB0设备号 基于 openWRT、DS18B02、digitemp、rrdtool的开源多点温度监控系统_第4张图片f)         从端口读取数据

 

 

 

 

2.Rrdtool操作

创建数据库(此处的脚本与下面的不对应,此处创建的数据库列为4个,下面为9个,请读者自行阅读修改)

rrdtool create home.rrd --step 60 DS:temp0:GAUGE:120:-10:100 DS:temp1:GAUGE:120:-10:100 DS:temp2:GAUGE:120:-10:100 RRA:AVERAGE:0.5:1:3000 RRA:AVERAGE:0.5:4:3500 RRA:AVERAGE:0.5:24:3900 RRA:AVERAGE:0.5:288:4000 RRA:MAX:0.5:1:3000 RRA:MAX:0.5:4:3500 RRA:MAX:0.5:24:3900 RRA:MAX:0.5:288:4000 RRA:MIN:0.5:1:3000 RRA:MIN:0.5:4:3500 RRA:MIN:0.5:24:3900 RRA:MIN:0.5:288:4000

 

3.读取,转换,更新数据库

上面从端口获取芯片数据的时候,显示有一个.digitemprc的文件,将该文件复制为配置文件

root@OpenWrt:~# cp .digitemprc /etc/digi.cfg 

4.获取数据并存入数据库

脚本如下

cat /root/digi.sh

#!/bin/sh

# The rutinue

output1=`/usr/sbin/digitemp -s /dev/ttyUSB0 -c /etc/digi.cfg -t0 -q -o"%4.1C"`

output2=`/usr/sbin/digitemp -s /dev/ttyUSB0 -c /etc/digi.cfg -t1 -q -o"%4.1C"`

output3=`/usr/sbin/digitemp -s /dev/ttyUSB0 -c /etc/digi.cfg -t2 -q -o"%4.1C"`

output4=`/usr/sbin/digitemp -s /dev/ttyUSB0 -c /etc/digi.cfg -t3 -q -o"%4.1C"`

output5=`/usr/sbin/digitemp -s /dev/ttyUSB0 -c /etc/digi.cfg -t4 -q -o"%4.1C"`

output6=`/usr/sbin/digitemp -s /dev/ttyUSB0 -c /etc/digi.cfg -t5 -q -o"%4.1C"`

output7=`/usr/sbin/digitemp -s /dev/ttyUSB0 -c /etc/digi.cfg -t6 -q -o"%4.1C"`

output8=`/usr/sbin/digitemp -s /dev/ttyUSB0 -c /etc/digi.cfg -t7 -q -o"%4.1C"`

output9=`/usr/sbin/digitemp -s /dev/ttyUSB0 -c /etc/digi.cfg -t8 -q -o"%4.1C"`

/usr/bin/rrdtool update /tmp/A1.rrd N:$output1:$output2:$output3:$output4:$output5:$output6:$output7:$output8:$output9 

5.读取数据

root@OpenWrt:~# rrdtool fetch /tmp/home.rrd AVERAGE

6.绘制图形

使用cgi脚本,将数据填入到png图形里面

#!/usr/bin/rrdcgi

<html>

<head><title>RRDCGI</title></head>

<body>

<h1>    <p> Last temperature update at: <RRD::TIME::LAST /tmp/A1.rrd "%A %e. %B %Y on %H:%M"> </p> </h1>

<P>

<RRD::GRAPH /www/A1.png --title="A1-9F Temperatures"

--start now-48h

-w 1000

-h 500

--slope-mode

--color CANVAS#000000

--color BACK#363636

--color FONT#FFFFFF

DEF:ce1=/tmp/A1.rrd:temp0:AVERAGE

DEF:ce2=/tmp/A1.rrd:temp1:AVERAGE

DEF:ce3=/tmp/A1.rrd:temp2:AVERAGE

DEF:ce4=/tmp/A1.rrd:temp3:AVERAGE

DEF:ce5=/tmp/A1.rrd:temp4:AVERAGE

DEF:ce6=/tmp/A1.rrd:temp5:AVERAGE

DEF:ce7=/tmp/A1.rrd:temp6:AVERAGE

DEF:ce8=/tmp/A1.rrd:temp7:AVERAGE

DEF:ce9=/tmp/A1.rrd:temp8:AVERAGE

LINE1:ce1#006400:"Cabinet S3528       "

GPRINT:ce1:MIN:"Min %5.2lf C"

GPRINT:ce1:MAX:"Max %5.2lf C"

GPRINT:ce1:AVERAGE:"Avg %5.2lf C"

GPRINT:ce1:LAST:"Last %5.2lf C\n"

LINE1:ce4#00fa9a:"Cabinet S8512       "

GPRINT:ce4:MIN:"Min %5.2lf C"

GPRINT:ce4:MAX:"Max %5.2lf C"

GPRINT:ce4:AVERAGE:"Avg %5.2lf C"

GPRINT:ce4:LAST:"Last %5.2lf C\n"

LINE1:ce6#6b8e23:"Cabinet Servers     "

GPRINT:ce6:MIN:"Min %5.2lf C"

GPRINT:ce6:MAX:"Max %5.2lf C"

GPRINT:ce6:AVERAGE:"Avg %5.2lf C"

GPRINT:ce6:LAST:"Last %5.2lf C\n"

LINE1:ce9#6b5200:"Cabinet UNICOM      "

GPRINT:ce9:MIN:"Min %5.2lf C"

GPRINT:ce9:MAX:"Max %5.2lf C"

GPRINT:ce9:AVERAGE:"Avg %5.2lf C"

GPRINT:ce9:LAST:"Last %5.2lf C\n"

LINE1:ce7#1e90ff:"Battery Left        "

GPRINT:ce7:MIN:"Min %5.2lf C"

GPRINT:ce7:MAX:"Max %5.2lf C"

GPRINT:ce7:AVERAGE:"Avg %5.2lf C"

GPRINT:ce7:LAST:"Last %5.2lf C\n"

LINE1:ce2#0000ff:"Battery Right       "

GPRINT:ce2:MIN:"Min %5.2lf C"

GPRINT:ce2:MAX:"Max %5.2lf C"

GPRINT:ce2:AVERAGE:"Avg %5.2lf C"

GPRINT:ce2:LAST:"Last %5.2lf C\n"

LINE1:ce5#ffff00:"Air-Condition-Left  "

GPRINT:ce5:MIN:"Min %5.2lf C"

GPRINT:ce5:MAX:"Max %5.2lf C"

GPRINT:ce5:AVERAGE:"Avg %5.2lf C"

GPRINT:ce5:LAST:"Last %5.2lf C\n"

LINE1:ce8#b8860b:"Air-Condition-Right "

GPRINT:ce8:MIN:"Min %5.2lf C"

GPRINT:ce8:MAX:"Max %5.2lf C"

GPRINT:ce8:AVERAGE:"Avg %5.2lf C"

GPRINT:ce8:LAST:"Last %5.2lf C\n"

LINE1:ce3#ff0000:"OutDoor             "

GPRINT:ce3:MIN:"Min %5.2lf C"

GPRINT:ce3:MAX:"Max %5.2lf C"

GPRINT:ce3:AVERAGE:"Avg %5.2lf C"

GPRINT:ce3:LAST:"Last %5.2lf C\n"

</P>

</body>

</html>

运行一下该文件,将会有数据填充到图形里面

/usr/bin/wget -q http://10.254.252.186/cgi-bin/3s.cgi > /dev/null 2>&1 

7.设定定时机制,不然不会自动存储显示数据的

root@OpenWrt:/www/cgi-bin# crontab -l

*/1 * * * * /etc/digi/digi.sh

*/5 * * * * /usr/bin/wget -q http://10.254.252.186/cgi-bin/3s.cgi > /dev/null 2>&1 

8.使用浏览器访问,一段时间之后,将绘制出连续的图形 

 

你可能感兴趣的:(基于 openWRT、DS18B02、digitemp、rrdtool的开源多点温度监控系统)