Linux簡介(1)-Raspberry Pi-ok
-
以Raspberry Pi 為例
樹莓派(RaspberryPI)是由英國營利組織的樹莓派基金會(Raspberry Pi Foundation)所開發的一片信用卡般大的電路板,搭載處理器(ARM CPU)和Linux系統,以 SD 卡為硬碟,目的是以低價硬體及自由軟體刺激學校的電腦科學教育。樹莓派有開放許多的GPIO(General Purpose Input Output)腳位可以連接許多週邊硬體電子零件。
外接LED燈
Lesson1
【實作一:亮燈】
#!/usr/bin/python #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #|R|a|s|p|b|e|r|r|y|P|i|.|c|o|m|.|t|w| #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # Copyright (c) 2016, raspberrypi.com.tw # All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # # led_on.py # Turn on a led # # Author : sosorry # Date : 06/22/2014 import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) LED_PIN = 12 GPIO.setup(LED_PIN, GPIO.OUT) print("LED is on") GPIO.output(LED_PIN, GPIO.HIGH) time.sleep(3) GPIO.cleanup()
Lesson
【實作二:閃燈】
#!/usr/bin/python #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #|R|a|s|p|b|e|r|r|y|P|i|.|c|o|m|.|t|w| #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # Copyright (c) 2016, raspberrypi.com.tw # All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # # led_blink.py # Blinking led without warning (Add try/except) # # Author : sosorry # Date : 06/22/2014 import RPi.GPIO as GPIO import time LED_PIN = 12 GPIO.setmode(GPIO.BOARD) GPIO.setup(LED_PIN, GPIO.OUT) try: while True: print("LED is on") GPIO.output(LED_PIN, GPIO.HIGH) time.sleep(1) print("LED is off") GPIO.output(LED_PIN, GPIO.LOW) time.sleep(1) except KeyboardInterrupt: print("Exception: KeyboardInterrupt") finally: GPIO.cleanup()
【課後練習:紅綠燈】
溫度感測器
Lesson3
【實作三:熱敷袋】
sudo pip3 install Adafruit_DHT
import time import Adafruit_DHT GPIO_PIN = 4 try: print("--------------") while True: h, t = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11,GPIO_PIN) if h is not None and t is not None: print("temp={0:0.1f}".format(t)) else: print("fail,please retry") time.sleep(10) except KeyboarInterrupt: print('close')
空氣盒子(PMS5003)
Lesson4
【實作四:空氣盒子】
Raspberry Pi 安裝套件
sudo apt-get install -y python3-libgpiod
空氣盒子程式碼
#!/usr/bin/python3 # # source: https://github.com/raspberrypi-tw/PiM25/ # # 使用方法 # python3 pigpiod_read_g5.py 23 import sys import time import pigpio import subprocess try: G5T_GPIO = int(sys.argv[1]) except Exception as e: print(e) print("Usage: python3 pigpiod_read_g5t.py <PIN>") exit() ret = subprocess.Popen("sudo pidof pigpiod", shell=True, stdout=subprocess.PIPE).stdout.read() if ret: pi = pigpio.pi() else: print("pigpiod was not running") ret = subprocess.call("sudo pigpiod", shell=True) time.sleep(0.5) pi = pigpio.pi() def read_g5t(pin=G5T_GPIO): try: pi.bb_serial_read_open(G5T_GPIO, 9600) time.sleep(1) size, data = pi.bb_serial_read(G5T_GPIO) try: import struct except ImportError: import ustruct as struct buffer = [] data = list(data) buffer += data while buffer and buffer[0] != 0x42: buffer.pop(0) if len(buffer) > 200: buffer = [] # avoid an overrun if all bad data if buffer[1] != 0x4d: buffer.pop(0) check = sum(buffer[0:30]) chksum = buffer[30]*256 + buffer[31] if check == chksum: print("==========", time.ctime(),"==========") PM1 = buffer[10]*256 + buffer[11] PM25 = buffer[12]*256 + buffer[13] PM10 = buffer[14]*256 + buffer[15] #print(PM1, PM25, PM10) print("PM1: {} ug/m3".format(PM1)) print("PM2.5: {} ug/m3".format(PM25)) print("PM10: {} ug/m3".format(PM10)) Temp = (buffer[24]*256 + buffer[25])/10 Humd = (buffer[26]*256 + buffer[27])/10 print("Temp: {} ℃".format(Temp)) print("Humd: {} %".format(Humd)) #print(buffer[28]) except Exception as e: print("No data received!") print(e) finally: pi.bb_serial_read_close(G5T_GPIO) if __name__ == '__main__': while True: read_g5t() time.sleep(1)
樹莓派擷圖指令
scrot
檔案存放位置
pi