# 開南大學健康產業管理學系 智慧醫療 Python範例 以下範例為旗標科技出版的「Python x AI 生醫感測健康大應用」套件。這裡僅提供學生練習用,請勿移作它用。 完整產品介紹:https://www.flag.com.tw/maker/FM636A 所有範例程式:https://www.flag.com.tw/DL.asp?FM636A ## LAB01.py LED閃爍 ```python= # 從 machine 模組匯入 Pin 物件 from machine import Pin # 匯入時間相關的time模組 import time #建立 5 號腳位的 Pin 物件, 設定為腳位輸出, 命名為 led led = Pin(5, Pin.OUT) while True: led.value(1) # 熄滅LED燈 time.sleep(0.5) # 暫停0.5秒 led.value(0) # 點亮LED燈 time.sleep(0.5) # 暫停0.5秒 ``` ## LAB02.py 膚電反應量測器 ```python= import time from machine import Pin, ADC adc_pin = Pin(36) # 36是ESP32的VP腳位 adc = ADC(adc_pin) # 設定36為輸入腳位 adc.width(ADC.WIDTH_12BIT) # 設定分辨率位元數(解析度) adc.atten(ADC.ATTN_11DB) # 設定最大電壓 while True: gsr = adc.read() print(gsr) time.sleep(0.1) ``` ## LAB03.py 無線介面測謊器 ```python= # 匯入 utime 模組用以計時 from utime import ticks_ms, ticks_diff from machine import Pin, ADC import network, ESPWebServer adc_pin = Pin(36) # 36是ESP32的VP腳位 adc = ADC(adc_pin) # 設定36為輸入腳位 adc.width(ADC.WIDTH_9BIT) # 設定分辨率位元數(解析度) adc.atten(ADC.ATTN_11DB) # 設定最大電壓 angle = 180 # 膚電反應轉換後角度 def SendAngle(socket, args): # 處理 /lie 指令的函式 ESPWebServer.ok(socket, "200", str(angle)) # 將膚電反應對應到180~360的函式 def gsr_to_angle(raw_val, min_val, max_val): raw_val *= -1 new_val = ((raw_val + max_val) /(max_val - min_val)*(360 - 180) + 180) return new_val print("連接中...") sta = network.WLAN(network.STA_IF) sta.active(True) sta.connect("WIFI SSID", "PASSWORD") # 請自行輸入無線網路帳號/密碼 while not sta.isconnected(): pass print("已連接, ip為:", sta.ifconfig()[0]) ESPWebServer.begin(80) # 啟用網站 ESPWebServer.onPath("/lie", SendAngle) # 指定處理指令的函式 time_mark = ticks_ms() # 取得當前時間 while True: # 持續檢查是否收到新指令 ESPWebServer.handleClient() # 當計時器變數與現在的時間差小於 100 則執行任務 if ticks_diff(ticks_ms(), time_mark) > 100: gsr = adc.read() angle = gsr_to_angle(gsr, 400, 511) time_mark = ticks_ms() # 重置計時器 ``` ## LAB04.py 血氧量測範例(直接顯示) ```python= # 導入函式庫 from machine import SoftI2C, Pin from max30102 import MAX30102 from pulse_oximeter import Pulse_oximeter my_SCL_pin = 25 # I2C SCL 腳位 my_SDA_pin = 26 # I2C SDA 腳位 i2c = SoftI2C(sda=Pin(my_SDA_pin), scl=Pin(my_SCL_pin)) # 建立I2C通訊埠 sensor = MAX30102(i2c=i2c) # 使用MAX30102類別 sensor.setup_sensor() pox = Pulse_oximeter(sensor) # 使用血氧濃度計算類別 count = 0 while True: pox.update() spo2 = pox.get_spo2() count = count+1 if spo2 > 0: print("SpO2:", spo2, "%") # else: # print(count) ``` ## LAB04.py 血氧量測範例(網頁顯示) ```python= # 匯入 utime 模組用以計時 from utime import ticks_ms, ticks_diff from machine import SoftI2C, Pin import network, ESPWebServer from max30102 import MAX30102 from pulse_oximeter import Pulse_oximeter my_SCL_pin = 25 # I2C SCL 腳位 my_SDA_pin = 26 # I2C SDA 腳位 i2c = SoftI2C(sda=Pin(my_SDA_pin), scl=Pin(my_SCL_pin)) sensor = MAX30102(i2c=i2c) sensor.setup_sensor() pox = Pulse_oximeter(sensor) # 使用血氧濃度計算類別 spo2 = 0 def SendSpo2(socket, args): # 處理 /handleCmd 指令的函式 ESPWebServer.ok(socket, "200", str(spo2)) print("連接中...") sta = network.WLAN(network.STA_IF) sta.active(True) sta.connect("無線網路名稱", "無線網路密碼") # 請手動修改 while not sta.isconnected(): pass print("已連接, ip為:", sta.ifconfig()[0]) ESPWebServer.begin(80) # 啟用網站 ESPWebServer.onPath("/measure", SendSpo2) time_mark = ticks_ms() while True: ESPWebServer.handleClient() pox.update() spo2_tmp = pox.get_spo2() spo2_tmp = round(spo2_tmp, 1) if spo2_tmp > 0: time_mark = ticks_ms() spo2 = spo2_tmp print("SpO2:", spo2, "%") if ticks_diff(ticks_ms(), time_mark) > 5000: spo2 = 0 ``` ###### tags: `KNU`