Danh mục tài liệu

Smart Home Automation with Linux- Part 4

Số trang: 30      Loại file: pdf      Dung lượng: 714.25 KB      Lượt xem: 16      Lượt tải: 0    
Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Smart Home Automation with Linux- P4:For every word I’ve written, five have been discarded. Such is the nature of writing. For every tenprograms I’ve downloaded, tried, and tested, nine have been discarded. Such is the nature of software.Finding a perspicuous overlap has been a long and arduous tasks, and one that I’d wish for no one tosuffer in solitude.
Nội dung trích xuất từ tài liệu:
Smart Home Automation with Linux- Part 4 CHAPTER 2 ■ APPLIANCE HACKING There are many Arduino shields on the market, most with freely available specifications and circuitdiagrams. Each shield has a specific task and includes the following problem domains.Ethernet NetworkingThere is the Arduino Ethernet Shield that supports four concurrent connections, working in either clientor server mode using TCP or UDP packets. It is based on the Wiznet W5100 chipset and uses digital pins10–13 to communicate.Wireless ControlThe main contender here is the Xbee shield, which uses the ZigBee wireless protocol, meaning it is notdirectly compatible with existing WiFi connections but can act as a radio transmitter and receiver forbasic scenarios and has an indoor range of about 30 meters.SoundThe LadyAda Wave shield provides playback support for .wav files, up to 16-bit mono 22KHz samples,which is a marked improvement over the PCM examples we saw earlier. To handle the problems ofmemory, this shield also supports SD cards (provided they’re formatted to FAT16 and have all their filesin 8.3 format in the root directory). It is still quite a heavy library, however, occupying 10KB of flashmemory, but it is still the best audio solution. It also provides a small power amplifier, able to drive 1/8W8 ohm speakers. This could be used for a talking clock, a kitchen-based stopwatch, or a virtual pet.MotorsAlso from LadyAda, the motor shield supports medium power control for DC, servo, and stepper motors.The total number of supported motors and the total power drain are governed by the specific motorsthemselves, but the quoted specs permit you two DC servos (on 5V) and up to four DC motors, twostepper motors, or one stepper and up to two DC motors. This shield does utilize a lot pins for control,and a lot of power, but can be used to lock cat flaps or build a robot.Example: The Arduino Welcome MatWith this knowledge, you can build a simple circuit, write some Arduino software, and add a Linux-sidescript to trigger a piece of speech whenever someone enters or leaves the house. I’ll show how to use the Arduino to monitor the state of a pressure mat (using a normally openswitch) placed under a rug and transmit messages to the PC. The Arduino will also remember thecurrent state, so once the switch inside the pressure mat has been stepped on, the house state isassumed to be “vacant” since people have left the house, and when the switch is closed again, the statechanges to “occupied.” The circuit is a simple switch, as shown in Figure 2-2. 73 CHAPTER 2 ■ APPLIANCE HACKING ■ Note You can also use a pressure mat to determine whether you’ve gotten out of bed after your alarm has gone off, and you can use the act of leaving the bedroom as a means to stop the alarm from sounding. The Arduino software is slightly more complex since you are looking for the case when the switch goes from the closed state to the open, since people might stand on the mat for a minute or more while they put on their coat. I have also included a timer here so that the house state doesn’t change if a second rising edge (caused by someone else stepping on the mat) is detected within two seconds of the first. This is to allow several people to leave the house at once, without the state getting confused. Naturally, this doesn’t solve the problem of only some occupants leaving the house, but it’s a start! int inputSwitchPin = 2; int lastState; long timeLastPressed; long debouncePeriod = 100; int houseState; long doormatUnblockAt; long doormatDelayPeriod = 2000; int blockSteps; void setup() { Serial.begin(9600); pinMode(inputSwitchPin, INPUT); // declare pushbutton as input lastState = digitalRead(inputSwitchPin); timeLastPressed = millis(); blockSteps = 0; houseState = 0; } void loop() { int pinState = digitalRead(inputSwitchPin); if (pinState != lastState && millis() - timeLastPressed > debouncePeriod) { if (pinState == 0) { // i.e., pressed if (!blockSteps) { houseState = 1-houseState; blockSteps = 1; Serial.print(houseState?1:0); } doormatUnblockAt = millis() + doormatDelayPeriod; } timeLastPressed = millis(); lastState = pinState; }74 CHAPTER 2 ■ APPLIANCE HACKING if (millis() > doormatUnblockAt) { blockSteps = 0; }} Finally, the USB script trigger code shown previously is adapted to watch for the serial messages of 0and 1: if (v == 1) { system(enter_house.sh); } else if (v == 0) { system(leave_house.sh); ... as before ...which runs either the enter_house.sh script for entering:say default welcome homex10control default on lounge_lightor the leave_house.sh script for leaving as appropriate:say default GoodbyeRAIN=`weatherstatus | head -n 1 | grep -i [rain|shower]`if [ $? -eq 0 ]; then say default Remember your umbrella it might rain today. say default $RAINfi In these code samples, I have used simplified commands without paths to demonstrate theprocess. The commands themselves are the abstractions that appear in the Minerva system, coveredin Chapter 7. This “house state” information could be extended to switch on security lights or redirect personal e-mails to a work account, for example. To connect the Arduino output to the rest of the system, you willeither need to use a networking shie ...