вторник, 22 августа 2017 г.

Мониторинг температуры ds18b20

Enable support 1-wire
vim /boot/config.txt
Add the line dtoverlay=w1-gpio
reboot
Next
sudo apt-get install libwww-perl rrdtool apache2 php5
Read more about rrdtool here
Create database for rrdtool
vim create_db.sh
#!/bin/bash
rrdtool create hometemp.rrd --start N --step 300 \
DS:temp:GAUGE:600:U:U \
DS:outtemp:GAUGE:600:U:U \
RRA:AVERAGE:0.5:1:12 \
RRA:AVERAGE:0.5:1:288 \
RRA:AVERAGE:0.5:12:168 \
RRA:AVERAGE:0.5:12:720 \
RRA:AVERAGE:0.5:288:365
Create images from rrdtool db
vim create_graphs.sh
#!/bin/bash
DIR="/home/pi/" 
DIR2="/var/www" 
#set to C if using Celsius
TEMP_SCALE="C" 

#define the desired colors for the graphs
INTEMP_COLOR="#CC0000" 
OUTTEMP_COLOR="#0000FF" 

#hourly
rrdtool graph $DIR2/temp_hourly.png --start -4h \
DEF:temp=$DIR/hometemp.rrd:temp:AVERAGE \
LINE2:temp$INTEMP_COLOR:"Temp [deg $TEMP_SCALE]" \
GPRINT:temp:MAX:'Макс\:%2.2lf' \
GPRINT:temp:AVERAGE:'Ср\:%2.2lf' \
GPRINT:temp:MIN:'Мин\:%2.2lf' \
GPRINT:temp:LAST:'Посл\:%2.2lf' \
#DEF:outtemp=$DIR/hometemp.rrd:outtemp:AVERAGE \
#LINE1:outtemp$OUTTEMP_COLOR:"Outside Temperature [deg $TEMP_SCALE]" 

#daily
rrdtool graph $DIR2/temp_daily.png --start -1d \
DEF:temp=$DIR/hometemp.rrd:temp:AVERAGE \
LINE2:temp$INTEMP_COLOR:"Temperature [deg $TEMP_SCALE]" \
GPRINT:temp:MAX:'Макс\:%2.2lf' \
GPRINT:temp:AVERAGE:'Сред\:%2.2lf' \
GPRINT:temp:MIN:'Мин\:%2.2lf' \

#weekly
rrdtool graph $DIR2/temp_weekly.png --start -1w --alt-autoscale \
DEF:temp=$DIR/hometemp.rrd:temp:AVERAGE \
LINE2:temp$INTEMP_COLOR:"Temperature [deg $TEMP_SCALE]" \
GPRINT:temp:MAX:'Макс\:%2.2lf' \
GPRINT:temp:AVERAGE:'Сред\:%2.2lf' \
GPRINT:temp:MIN:'Мин\:%2.2lf' \

#monthly
rrdtool graph $DIR2/temp_monthly.png --start -1m \
DEF:temp=$DIR/hometemp.rrd:temp:AVERAGE \
LINE2:temp$INTEMP_COLOR:"Temperature [deg $TEMP_SCALE]" \
GPRINT:temp:MAX:'Макс\:%2.2lf' \
GPRINT:temp:AVERAGE:'Сред\:%2.2lf' \
GPRINT:temp:MIN:'Мин\:%2.2lf' \

#yearly
rrdtool graph $DIR2/temp_yearly.png --start -1y \
DEF:temp=$DIR/hometemp.rrd:temp:AVERAGE \
LINE2:temp$INTEMP_COLOR:"Temperature [deg $TEMP_SCALE]" \
GPRINT:temp:MAX:'Макс\:%2.2lf' \
GPRINT:temp:AVERAGE:'Сред\:%2.2lf' \
GPRINT:temp:MIN:'Мин\:%2.2lf' \
Writing values into the rrdtool db 
vim get_temp.pl
#!/usr/bin/perl
use LWP::UserAgent;

my $dir = '/home/pi';
#my $metar_url = 'http://export.yandex.ru/weather-ng/forecasts/27037.xml';
my $metar_url = 'http://api.openweathermap.org/data/2.5/weather?q=Vologda,ru&units=metric';
my $is_celsius = 1; #set to 1 if using Celsius

my $ua = new LWP::UserAgent;
$ua->timeout(120);
my $request = new HTTP::Request('GET', $metar_url);
my $response = $ua->request($request);
my $metar= $response->content();

$metar =~/temp":(\d{2}.\d{3})/;
$outtemp = $1;

$modules = `cat /proc/modules`;
if ($modules =~ /w1_therm/ && $modules =~ /w1_gpio/)
{
        #modules installed
}
else
{
        $gpio = `sudo modprobe w1-gpio`;
        $therm = `sudo modprobe w1-therm`;
}

$output = "";
$attempts = 0;
while ($output !~ /YES/g && $attempts < 5)
{
        $output = `sudo cat /sys/bus/w1/devices/28-*/w1_slave 2>&1`;
        if($output =~ /No such file or directory/)
        {
                print "Could not find DS18B20\n";
                last;
        }
        elsif($output !~ /NO/g)
        {
                $output =~ /t=(\d+)/i;
                $temp = ($is_celsius) ? ($1 / 1000) : ($1 / 1000) * 9/5 + 32;
                $rrd = `/usr/bin/rrdtool update $dir/hometemp.rrd N:$temp:$outtemp`;
        }

        $attempts++;
}

#print "Inside temp: $temp\n";
#print "Outside temp: $outtemp\n";
#print "Inside temp: $output\n";
#print $metar;
Web interface
vim /var/www/index.php
RPi Temperature

RPi Temperature Inside & Outside

Last Modified:

Hourly

Daily

Weekly

Monthly

Yearly

run as root
crontab -e
and add
*/5 * * * *     /home/pi/get_temp.pl && /home/pi/create_graphs.sh
Файлы https://drive.google.com/file/d/0B8SzfYc4x7mOQUtMOXI4VEFfNnM/view?usp=sharing

Комментариев нет:

Отправить комментарий