查看: 2547|回复: 0

[教程] 【一网打尽】LEARN RASPBERRY PI (13 of 13)

[复制链接]
  • TA的每日心情
    开心
    2016-8-4 10:56
  • 签到天数: 242 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2015-3-30 22:13:12 | 显示全部楼层 |阅读模式
    分享到:
    Overview SIMON MONK

    The Raspberry Pi can make a great home automation controller. But to be really useful, it needs to be able to turn electrical appliances and lights on and off. That means it needs to be able to control 110V safely.

    learn_raspberry_pi_overview.jpg
    In this lesson, you will combine the PIR sensor from Lesson 12 with the Powerswitch Tail 2 module from Adafruit, to automatically switch something on when movement is detected.

    Parts SIMON MONK

    To build the project described in this lesson, you will need the following parts.

    learn_raspberry_pi_pirsensor_MED.jpg
    PIR Sensor
    learn_raspberry_pi_powerswitchtail2_MED (1).jpg
    Power Switch Tail 2
    learn_raspberry_pi_cobbler.jpg
    Pi Cobbler
    learn_raspberry_pi_breadboard_half_web.jpg
    Half-size Breadboard
    learn_raspberry_pi_pi.jpg
    Raspberry Pi
    learn_raspberry_pi_jumpers_web.jpg
    Jumper wire pack

    Hardware SIMON MONK

    The Powerswitch Tail 2 from Adafruit, looks like a 110V electrical lead with a box in the middle.

    If you have 220V mains, this tutorial wont work for you - If we knew of an equivalent 220V controller like the PST we would have a link to it here

    learn_raspberry_pi_powerswitchtail2_MED.jpg
    The box is actually an opto-isolated solid-state relay. The opto-ilsolation bit means that there is no actual electrical connection between the low voltage switching circuit and the 100V mains. This makes it extremely safe and removes any chance of accidentally frying your Pi.

    What is more the control input consumes just 3 mA of current at 3.3V, which means we can control it directly from one of the Pi's output pins.
    The PIR sensor is connected to a different pin from lesson 12, so be sure to move the yellow lead.


    learn_raspberry_pi_breadboard.png
    The Powerswitch Tail comes with a little indicator LED that shows when it is on, so you do not need to connect anything high voltage to it yet.

    Software SIMON MONK

    This project is possible the most over-engineered automatic light switch ever created. You really do not need a Raspberry Pi to turn on the Powerswitch, but the example can easily be adapted for other purposes. For example, you could use a combination of temperature, humidity, light, and perhaps some internet data on weather forecasts to turn on or off a heater, fan or humidifier.

    Copy Code


    • import time
    • import RPi.GPIO as io
    • io.setmode(io.BCM)
    • pir_pin = 24
    • power_pin = 23
    • io.setup(pir_pin, io.IN)
    • io.setup(power_pin, io.OUT)
    • io.output(power_pin, False)
    • while True:
    •     if io.input(pir_pin):
    •         print("POWER ON")
    •         io.output(power_pin, True)
    •         time.sleep(20);
    •         print("POWER OFF")
    •         io.output(power_pin, False)
    •         time.sleep(5)
    •     time.sleep(1)

    The program first sets up the two GPIO pins that are used, one as an input to the PIR sensor and one as an output for the Powerswitch Tail.
    The main loop then just waits for the PIR sensor to sense movement and then prints a message and turns on the Powerswitch tail, waits 20 seconds then turns it off again.
    It the output was turned on, it waits for 5 seconds to prevent immediate retriggering.
    There is also always a 1 seconds delay each time around the loop.

    Configure and Test SIMON MONK

    There are lots of ways to get the sketch from the listing below onto your Raspberry Pi. Perhaps the easiest is to connect to your Pi using SSH (See Lesson 6) opening an editor using the command below:

    Copy Code


    • nano powerswitch.py

    and then pasting in the code , before saving the files using CTRL-x.

    learn_raspberry_pi_nano.png
    To start with, cover the PIR sensor with something, so that it is not activated until you are ready.

    Run the program as superuser using the command:


    Copy Code


    • sudo python powerswitch.py

    Uncover the PIR sensor and you should see a message saying 'POWER ON' and the little LED on the Powerswitch Tail should light, then turn itself off after 20 seconds.
    Now this is all working, you can try attaching a lamp or some electrical appliance to the Powerswitch Tail.

    Do not write code that turns the power on and off very rapidly. Many kinds of appliance, including lamps will be damaged by high frequency switching. This type of setup should turn things on for a few seconds minimum.











    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-27 12:30 , Processed in 0.111852 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.