3回答

0收藏

為Raspberry PI設一個關機開關

Raspberry Pi Raspberry Pi 6082 人阅读 | 3 人回复 | 2015-02-12

本帖最后由 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

1.png (93.8 KB, 下载次数: 49)

Raspberry PI接腳圖

Raspberry PI接腳圖
分享到:
回复

使用道具 举报

回答|共 3 个

倒序浏览

沙发

kittka

发表于 2015-2-13 04:09:50 | 只看该作者

PI上在microUSB旁边有2个引脚,把这2个引脚焊上排线,短接接可以关机
板凳

tjserver

发表于 2015-2-13 11:46:09 | 只看该作者

原來還有這個
學習了
謝謝
地板

tjserver

发表于 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 (102.34 KB, 下载次数: 59)

Abbildung-1_large.png

Abbildung-3_large.png (125.48 KB, 下载次数: 66)

Abbildung-3_large.png

Abbildung-4_large.png (103.12 KB, 下载次数: 64)

Abbildung-4_large.png
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

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