查看: 5151|回复: 3

為Raspberry PI設一個關機開關

[复制链接]
  • TA的每日心情
    开心
    2018-8-6 10:16
  • 签到天数: 128 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2015-2-12 17:21:31 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 tjserver 于 2015-2-12 17:29 编辑

    為Raspberry PI設一個關機開關
    因為Raspberry PI本身就是一個作業系統,如果不去作正常的關機動作那就容易讓這Raspberry PIOS以後容易損毀而開不了機,所以一般我們還是要正常的去作關機的動作。
    但是Raspberry PI本身硬體上是沒有關機的開關只有電源的接頭,如果在Raspberry PI的作業系統運作的狀況下直接把電源拔掉,這樣就容易讓Raspberry PI的作業系統損毀。
    而要正常的關機時則必在命令模式下去下指令,讓Raspberry PI來作關機的動作,指令為:
    #shutdown –h now
    #poweroff
    如果能利用Raspberry PI本身的GPIO的觸發控制來透過程式進行關機指令的下達,這樣就不用再透過putty sshRaspberry PI上來下指令關機了。
    程式
    package ycan.com.tw;                                 

    import java.io.IOException;                                 

    import com.pi4j.io.gpio.GpioController;                                 
    import com.pi4j.io.gpio.GpioFactory;                                 
    import com.pi4j.io.gpio.GpioPinDigitalInput;                                 
    import com.pi4j.io.gpio.GpioPinDigitalOutput;                                 
    import com.pi4j.io.gpio.PinPullResistance;                                 
    import com.pi4j.io.gpio.PinState;                                 
    import com.pi4j.io.gpio.RaspiPin;                                 
    import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;                                 
    import com.pi4j.io.gpio.event.GpioPinListenerDigital;                                 

    public class ClosePi {                                 
       public static void main(String[] args) throws IOException {                                 

          final GpioController gpio = GpioFactory.getInstance();                                 
          //設定GPIO_07Button                                 
          final GpioPinDigitalInput closeButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_07, PinPullResistance.PULL_DOWN);                                 
            Thread th = new Thread(new Runnable() {                                 

                @Override                                 
                public void run() {                                 

                    while (true) {                                 

    //                    if (closeButton.isLow()) { // D1 on ground?                                 
    //                        System.out.println("in Low----");                                 
    //                                 
    //                    }                                 
                                                         
                        if (closeButton.isHigh()) { // D1 on ground?                                 
                            System.out.println("in Hi----");                                 
                            //關機                                 
                         Runtime runtime = Runtime.getRuntime();                                 
                         try {                                 
                          Process proc = runtime.exec("shutdown -h now");                                 
                       } catch (IOException e) {                                 
                          e.printStackTrace();                                 
                       }                                 
                         System.exit(0);                                 
                        }                                                     

                    }                                 

                }                                 
            });                                 
            th.setPriority(Thread.MAX_PRIORITY);                                 
            th.start();                                 
            System.out.println("Thread start");                                 

            for (;;) {                                 
                try {                                 
                    Thread.sleep(500);                                 
                } catch (InterruptedException e) {                                 
                    // TODO Auto-generated catch block                                 
                    e.printStackTrace();                                 
                }                                 
            }                                 

        }                                 
    }                                 

    這樣就可以透過GPIO第7腳位開關來觸發程式執行系統的關機。
    不過有沒有發現一個問題,這程式並沒有被註冊成系統服務,所以變成要系統開機完後要手動去將這程式執行起來,這樣勢必不是我們想要的結果,所以要將這程式便成在Raspberry PI開機完後同時將這程式執行起來,這樣這個程式才有意義。這個在Raspberry PI裡要將Java程式註冊成系統的服務程式,再找時間把這作法寫上來。

    我的Blog   http://www.gonet.idv.tw/TjBlog

    Raspberry PI接腳圖

    Raspberry PI接腳圖
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2015-7-7 05:53
  • 签到天数: 550 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2015-2-13 04:09:50 | 显示全部楼层
    PI上在microUSB旁边有2个引脚,把这2个引脚焊上排线,短接接可以关机
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2018-8-6 10:16
  • 签到天数: 128 天

    连续签到: 1 天

    [LV.7]常住居民III

     楼主| 发表于 2015-2-13 11:46:09 | 显示全部楼层
    原來還有這個
    學習了
    謝謝
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2018-8-6 10:16
  • 签到天数: 128 天

    连续签到: 1 天

    [LV.7]常住居民III

     楼主| 发表于 2015-2-13 16:52:31 | 显示全部楼层
    找了一下kittka講的,有個網頁有這資料:
    http://www.raspberry-pi-geek.com/Archive/2013/01/Adding-an-On-Off-switch-to-your-Raspberry-Pi

    下面這幾張圖是從這網頁轉貼過來的,供大家參考,不過從這網頁上看,這個作用應該是對照我們PC的Reset的按鈕作用。

    文章裡還是建議用指令的方式來正常關機:
    The reset switch is a very useful feature of the Rev 2 board; however, it does not allow for a safe shutdown of the Rasp Pi. A safe shutdown is very important to avoid causing SD card errors. Fortunately, it is possible to create a GPIO-connected switch, which, at the press of a button, allows you to ensure that the Rasp Pi has fully and correctly shut down before you pull the power plug. Tapping a safe shutdown switch is a far easier and cooler way to turn off a computer than opening a terminal window and typing:
    sudo shutdown -h now
    以上不知道我的理解是否正確?
    Abbildung-1_large.png
    Abbildung-3_large.png
    Abbildung-4_large.png
    回复 支持 反对

    使用道具 举报

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

    本版积分规则

    关闭

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



    手机版|小黑屋|与非网

    GMT+8, 2024-5-6 09:50 , Processed in 0.154192 second(s), 22 queries , MemCache On.

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

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.