Temperature and Humidity Moniter Graph With Raspberry pi LAB4

รายการอุปกรณ์
-ชุด Raspberry pi -SD Card 4G Class4 ขั้นต่ำ-OS Raspbian "2013-09-25-wheezy-raspbian.zip" http://www.raspberrypi.org/downloads-DHT11, DHT22-WIFI USB / LAN Wires-Breadboard+Jumper Wires-Mount+Keyboard
DHT11, DHT22 เป็นอุปกรณ์ที่ใช้วัดอุณหภูมิและวัดความชื้นโดยเราจะเก็บเป็นไฟล์ temp.log จากนั้นให้ web application ดึงข้อมูลเหล่านี้ไปแสดงกราฟโดยเรียกใช้งานผ่านหน้าเว็บบราวเซอร์
การเชื่อมต่อ GPIO ของ Raspberry pi กับ DHT11, DHT22
RPi VCC (pin 1) >> DHT11 pin 1
RPi GPIO4 (pin 7) >> DHT11 pin 2
RPi GND (pin 6) >> DHT11 pin 4

ติดตั้งโปรแกรม git-core build-essential ด้วย
#sudo apt-get install git-core build-essential
ทำการดึง wiringPi C library เพื่อจะ compile
#git clone git://git.drogon.net/wiringPi
เข้าไปยังไดเร็กทอรี่ wiringPi
#cd wiringPi
คอมไฟล์ไฟล์ build ด้วย
#sudo ./build
จากนั้นสร้างไฟล์ dht11.c
#sudo nano dht11.c
จากนั้น coppy code ข้างล่างนี้เข้าไปยังไฟล์ dht11.c
#include <wiringPi.h>#include <stdio.h>#include <stdlib.h>#include <stdint.h>#define MAX_TIME 85#define DHT11PIN 7#define ATTEMPTS 5int dht11_val[5]={0,0,0,0,0};int dht11_read_val(){ uint8_t lststate=HIGH; uint8_t counter=0; uint8_t j=0,i; for(i=0;i<5;i++) dht11_val[i]=0; pinMode(DHT11PIN,OUTPUT); digitalWrite(DHT11PIN,LOW); delay(18); digitalWrite(DHT11PIN,HIGH); delayMicroseconds(40); pinMode(DHT11PIN,INPUT); for(i=0;i<MAX_TIME;i++) { counter=0; while(digitalRead(DHT11PIN)==lststate){ counter++; delayMicroseconds(1); if(counter==255) break; } lststate=digitalRead(DHT11PIN); if(counter==255) break; // top 3 transistions are ignored if((i>=4)&&(i%2==0)){ dht11_val[j/8]<<=1; if(counter>16) dht11_val[j/8]|=1; j++; } } // verify checksum and print the verified data if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF))) { printf("%d.%d,%d.%d\n",dht11_val[0],dht11_val[1],dht11_val[2],dht11_val[3]); return 1; } else return 0;}int main(void){ int attempts=ATTEMPTS; if(wiringPiSetup()==-1) exit(1); while(attempts) { int success = dht11_read_val(); if (success) { break; } attempts--; delay(500); } return 0;}
จากนั้น save ไฟล์ dht11.c ด้วย ctrl+x กด y กด enter
แล้วทำการ Compile and execute source code จาก dht11.c ไปเป็น dht11
#sudo gcc -o dht11 dht11.c -L/usr/local/lib -lwiringPi
#sudo ./dht11
จากนั้นทำการกำหนดค่าเพื่อตั้งเวลาในการเก็บค่าอุณหภูมิและความชื้นไว้ในไฟล์ temp.log
#sudo crontab -e
แล้วเพิ่มไว้ในส่วนล่างสุดของ crontab
* * * * * echo `date +\%Y\%m\%d\%H\%M\%S`,`/home/pi/wiringPi/dht11` >> /home/pi/temp.log

จากนั้นทำการ start service เพื่อเรื่มทำงาน
#sudo service cron start
#sudo update-rc.d cron defaults
จากนั้นอ่านค่า วันที่ อุณหภูมิ และความชื้น เพื่อนำข้อมมูลเหล่านั้นมาแสดงเป็น graphing โดยใช้ library Javascript โดยจำเป็นต้องสร้างเว็บเพจ HTML
ติดตั้ง apache2 แล้วสร้างไฟล์ temp.html
#sudo apt-get install apache2
#sudo apt-get update
#sudo ln -s /home/pi/temp.log /var/www/temp.log
#sudo wget -P /var/www http://dygraphs.com/dygraph-combined.js
#cd /var/www/
#sudo nano temp.html
แล้วนำ code ด้านล่างนี้เพิ่มเข้ามาในไฟล์ temp.html
<html><head><script type="text/javascript" src="dygraph-combined.js"></script></head><body><div id="graphdiv" style="width:750px; height:400px;"></div><script type="text/javascript"> function addZero(num) { var s=num+""; if (s.length < 2) s="0"+s; return s; } function dateFormat(indate) { var hh = addZero(indate.getHours()); var MM = addZero(indate.getMinutes()); //var ss = addZero(indate.getSeconds()); var dd = addZero(indate.getDate()); var mm = addZero(indate.getMonth()+1); var yyyy = addZero(indate.getFullYear()); return dd+'/'+mm+' '+hh+':'+MM; } g = new Dygraph( document.getElementById("graphdiv"), "temp.log", { xValueParser: function(x) { var date = new Date(x.replace( /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/, '$4:$5:$6 $2/$3/$1' )); return date.getTime(); }, axes: { x: { ticker: Dygraph.dateTicker, axisLabelFormatter: function(x) { return dateFormat(new Date(x)); }, valueFormatter: function(x) { return dateFormat(new Date(x)); } } }, labelsDivWidth: 310, rollPeriod: 30, strokeWidth: 2.0, labels: ['Date','Humidity (%)','Temp (°C)'] } );</script></body></html> |
จากนั้น save ไฟล์ temp.html ด้วย ctrl+x กด y กด enter
จากนั้นเปิดเว็บบราวเซอร์เพื่อตรวจสอบกราฟ ด้วย ip ของ Raspberry pi เช่น
http://10.0.0.105/temp.html ดังรูป

**การหยุดไฟลืที่รันอยู่โดยกด ctrl+c หรือ ctrl+z**
Temperature and Humidity Moniter Graph With Raspberry pi LAB4