วันเสาร์ที่ 8 มีนาคม พ.ศ. 2557

Home heating automation with a Raspberry Pi and a smartphone

Home heating automation with a Raspberry Pi and a smartphone

Introduction

In this article we explore the possibilities of using a Raspberry Pi for the remote control and monitoring of a home heating system using servo control, analogue to digital conversion, C programming and web development.

Servo control

We decided to use a servo device to control the on/off switch of the boiler. We could have used relay switches for the same purpose, but tried to stick to the principle of making no modifications to the boiler (as it’s owned by the landlord :D). The following image shows how the servo is mounted and the various states of control.
armlowres
Our mini servo has reduced power requirements, so we could power it directly from the 5V pin of the Raspberry Pi. To create the needed pulse for servo control, we utilized wiringPi library’s softPWM implementation. Small glitches exist, but the accuracy is sufficient for this kind of control. The code below uses GPIO pin 0 as a PWM signal output to control the servo. We assume that wiringPi library is already installed.
pinMode(0, OUTPUT);
digitalWrite(0, LOW);
softPwmCreate(0,0,200);
softPwmWrite(0,control);
The “control” variable accepts integer values in the range of 180 to 194. The input to that variable will control the three positions of the switch. This will eventually be controlled from a web based user interface.

Sensors and ADC

Our servo device can now control the state of the boiler from our Raspberry Pi. However, when we remotely control the system, we cannot be sure that the servo worked as it should and that the boiler is in the state we want. For this reason we need a way to get feedback from the Raspberry Pi regarding the boiler’s state. The most straight-forward way to achieve this is to check the status of the LED indicator on the boiler. When the LED is on the boiler is turned on and when the LED is off the boiler is off. A simple LDR (Light Dependent Resistor) light sensor, in a dark box, mounted on the boiler’s two 7 segment LED displays should do the job. A LDR sensor is a variable resistor whose resistance depends on the amount of light arriving on it.
lightsensor
We also use two LM35 temperature sensors with analogue output; one for air temperature (mounted on the breadboard) and one for water temperature (mounted on a radiator).
You’ll need an analogue-to-digital converter (ADC) when using analogue sensors. We chose a BV4205 (a 10 channel I²C enabled IC) though others like the MCP3008 would also work. The analogue output from the three sensors gets converted to digital and is fed to the Raspberry Pi through the I²C bus. However, due to known issues with the I²C bus on the Raspberry Pi (see goo.gl/IQvN2O), a “patch” to slow down the clock of the I²C bus had to be applied before using the BV4205.
Below, we use the most basic parts of the code needed for communications over the I²C bus to read the analogue input of a sensor connected to channel 7 (use the same code for other channels). Similar code can be used for interaction with any I²C compatible device.
//Initialize I2C bus
int fd;
char *fileName = "/dev/i2c-1";
//Note: When using a Rev 1 Pi, use "/dev/i2c-0" 
int  address = 0x31;     
unsigned char buf[10]; 
fd = open(fileName, O_RDWR);
ioctl(fd, I2C_SLAVE, address);
//Select ADC channel 7
buf[0] = 1;    
buf[1] = 7;
write(fd, buf, 2);
//Start conversion
buf[0] = 2;
write(fd, buf, 1);
//Fetch result of conversion
buf[0] = 4;
write(fd, buf, 1);
read(fd, buf, 2);
unsigned short packedword;
packedword = (buf[0] <<8 buf="" pre="">

User interface

The following schematic shows the setup and how the devices connect and interact. A simple web interface provides easy access from web enabled devices. The web interface is mainly based on jQuery, PHP and MySQL.
system
Using PHP’s exec command, we get access to our two main executables that control the servo’s position and read sensor output. Ajax has been used extensively in order to achieve real-time monitoring of the boiler’s state and sensor readings. Furthermore, we calculate how long the system was on each day (in minutes) by analysing the light sensor’s output. The user is also presented with an SVG (d3.js library) graph of the air temperature for the last 24 hours with time resolution of one minute.
diagram
By exploiting data from the sensors, we could further extend the system and make it more intelligent by fully automating on and off events based on sensor output and specific usage profiles. For more information (code, schematics, pictures) about the project 
ref: http://www.themagpi.com/issue/issue-11/article/home-heating-automation-with-a-raspberry-pi-and-a-smartphone/

วันเสาร์ที่ 11 มกราคม พ.ศ. 2557

การติดตั้ง WiringPi เพื่อใช้งาน GPIO และเรียกใช้ผ่าน PHP บน Raspberry Pi

WiringPi เป็นไลบรารีที่ใช้ติดต่อกับพอร์ต GPIO (General Purpose Input/Output) ของ Raspberry Pi ถูกเขียนขึ้นด้วยภาษาซี พอร์ต GPIO เป็นพอร์ตอินพุตเอาต์พุตอเนกประสงค์ เราสามารถรับและส่งข้อมูลต่างๆได้ผ่านทางพอร์ตนี้ โดยในตัวอย่างวันนี้เราจะส่งค่าออกไปแสดงผลที่ LED และทดสอบรับค่ากลับจากการกดสวิตช์ จริงๆแล้วตัว WiringPi เป็นไลบรารีที่เราต้องเขียนโปรแกรมด้วยภาษาซีเพื่อเรียกใช้งาน แต่มันก็มีคำสั่งที่เป็นคอมมานไลน์ชื่อ gpio มาให้เราใช้งานด้วย คำสั่ง gpio นี้มีข้อดีตรงที่ว่ายูเซอร์ทุกคนในระบบสามารถเรียกใช้งานได้ ไม่จำกัดแค่ว่าต้องมีสิทธิเป็น root เหมือนกับการเรียกใช้ผ่าน shell ซึ่งข้อดีของคำสั่ง gpio ตรงนี้ทำให้เราสามารถเขียนคำสั่งด้วย PHP และเรียกใช้งานผ่านเว็บได้
อย่างแรกเริ่มจากการติดตั้ง GIT ลงไปก่อน
1
sudo apt-get install git-core
ถ้าหากว่าเกิดมี Error ขึ้นมาให้ลองทำการอัพเดทแพคเกจ และลองใหม่อีกครั้ง
1
2
sudo apt-get update
sudo apt-get upgrade
จากนั้นสั่งโคลน
1
git clone git://git.drogon.net/wiringPi
ต่อไปสั่งติดตั้งแพคเกจ
1
2
3
cd wiringPi
git pull origin
./build
เมื่อติดตั้งเรียบร้อย ลองทดสอบเรียกคำสั่ง gpio ขึ้นมาดูก่อนว่าใช้งานได้ไหม ถ้าไม่ได้ก็ตัวใครตัวมันครับ
1
gpio -v
ก่อนต่อวงจรเรามาดูตำแหน่งขาของ GPIO กันก่อนครับว่าอยู่ตรงไหนบ้าง จะเห็นว่ามีพอร์ต GPIO ให้เราใช้งานอยู่ทั้งหมด 17 ขา แต่วันนี้เราจะทดลองใช้ GPIO 7 สำหรับการทดสอบเรื่องอินพุต และใช้ GPIO 8 สำหรับการทดสอบเอาต์พุต
เริ่มต่อวงจรกันเลยครับ ในตัวอย่างเราจะใช้ LED เป็นเอาต์พุต และใช้สวิตกดติดปล่อยดับเป็นอินพุต
ต่อแล้วได้ออกมาหน้าตาประมาณนี้ครับ
เมื่อต่อวงจรเรียบร้อยแล้วมาเริ่มทดสอบกันเลย เริ่มจากทดสอบเอาต์พุตก่อน โดยใช้คำสั่งต่อไปนี้ (สังเกตที่ LED จะติดและดับตามการส่งสัญญานที่เราส่งไป)
gpio -g mode 8 out //กำหนดให้ GPIO 8 เป็นขาเอาต์พุต
gpio -g write 8 1 //ส่งค่า Height ไปที่ขา GPIO 8
gpio -g write 8 0 //ส่งค่า Low ไปที่ขา GPIO 8
1
2
3
gpio -g mode 8 out
gpio -g write 8 1
gpio -g write 8 0
ทำการทดสอบอินพุตกันต่อ (ทดลองกดสวิตและอ่านค่าจาก GPIO 7)
gpio -g mode 7 in //กำหนดให้ GPIO 7 เป็นขาอินพุต
gpio -g read 7 //อ่านค่าจาก GPIO 7 โดยจะมีค่าเป็น 0 กับ 1
1
2
gpio -g mode 7 in
gpio -g read 7
ก่อนออกไปเที่ยว เดี๋ยวเราจะลองเรียกใช้งาน gpio โดยใช้ PHP กันดูบ้าง ซึ่งหลักการก็ง่ายๆ คือเราจะใช้ฟังชั่น exec() ของ PHP เรียกไปที่ /usr/local/bin/gpio ตรงๆเลยง่ายดี
สร้างไฟล์ gpio.php ที่ /var/www/gpio.php
1
sudo nano /var/www/gpio.php
พิมพ์คำสั่งต่อไปนี้ลงไป
1
2
3
4
5
6
7
8
9
10
11
12
13
14

 
exec("/usr/local/bin/gpio -g mode 8 out");
exec("/usr/local/bin/gpio -g mode 7 in");
 
if($_GET['gpio8'] == 1){
 exec("/usr/local/bin/gpio -g write 8 1");
}else{
 exec("/usr/local/bin/gpio -g write 8 0");
}
 
echo "GPIO  7 = ".exec("/usr/local/bin/gpio -g read 7");
 
?>
เพียงเท่านี้เราก็สามารถควบคุม GPIO ของ Raspberry Pi ผ่านเว็บได้แล้ว ลองเปิดเว็บดูได้เลยครับ
http://192.168.8.102/gpio.php
http://192.168.8.102/gpio.php?gpio8=1
http://192.168.8.102/gpio.php?gpio8=0
นำมาจาก http://www.unzeen.com/article/2179/