จากครั้งที่แล้วเราได้ลองทำการเขียนโปรแกรมเพื่อรับค่าน้ำหนักจากเครื่องชั่งน้ำหนักกันไปแล้ว คราวนี้เราจะมาลองเขียนโปรแกรมเพื่อรับค่าน้ำตาลในเลือดจากเครื่องวัดน้ำตาลในเลือดของ Yuwell กันบ้าง โดยในข้อมูลจะถูกส่งออกมาเป็นสัญญาน Bluetooth เช่นกัน แต่เราต้องทำการรับค่าด้วยการรอรับ Notification ที่เครื่องจะส่งเข้ามา

ข้อควรระวัง ระหว่างคอมพิวเตอร์ที่รันโปรแกรมและตัวเครื่องตรวจน้ำตาล Yuwell ต้องอยู่ห่างกันไม่เกิน 5 เมตร หรือควรลองในระยะไกล้ ๆ ก่อน แล้วค่อยขยับให้ไกลขึ้นได้ เผื่อเวลาใช้จริงจะวางเครื่องคอมพิวเตอร์และเครื่องตรวจน้ำตาลไว้คนละห้องกัน

0. Install library

เริ่มต้นด้วยการติดตั้งโปรแกรมและไลบรารีที่จำเป็นกันก่อน

# apt-get update
# apt-get install -y bluez* pkg-config libbluetooth-dev libglib2.0-dev
# apt-get install -y libboost-thread-dev libboost-python-dev python3-capstone

# pip3 install requests
# pip3 install bleak
# pip3 install asyncio

1. discover.py

จากนั้นทำการเขียนโปรแกรมสำหรับค้นหา MAC Address ของเครื่องตรวจน้ำตาลในเลือด Yuwell

# nano discover.py
import sys
import asyncio
import time

from typing import Sequence
from bleak import BleakScanner
from bleak.backends.device import BLEDevice

async def findBluetoothDevice():
    devices: Sequence[BLEDevice] = await BleakScanner.discover(timeout=1)

    print("** Date Time: ", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "**\n")

    for device in devices:
        print(device)

    print("--------------------------------------------------\n")

print("If you want to exit, you can press Ctrl + C.\n")

while True:
    try:
        loop = asyncio.get_event_loop()
        loop.run_until_complete(findBluetoothDevice())
    except KeyboardInterrupt:
        print("\nExit\n")
        sys.exit(0)
    except Exception as e:
        print("Exception Message:", str(e))
        time.sleep(1)

2. Find device address

ทำการรันคำสั่ง discover.py เพื่อหา MAC Address ของเครื่องตรวจน้ำตาลในเลือด Yuwell เมื่อรันคำสั่งแล้วให้กดเปิดเครื่อง โดยชื่อเครื่องจะใช้ชื่อว่า Yuwell Glucose

# python3 discover.py
Image

3. blood-glucose-yuwell.py

จากนั้นทำการเขียนโปรแกรมสำหรับรับค่าจากเครื่องตรวจน้ำตาลในเลือด อย่าลืมแก้ไขค่าในตัวแปร GLUCOSE_DEVICE_ADDRESS เป็นค่า MAC Address ที่ได้จากขั้นตอนที่แล้ว

# nano blood-glucose-yuwell.py
import sys
import asyncio
import struct
import requests
import time
import platform

from typing import Sequence
from bleak import BleakClient, BleakScanner
from bleak.backends.device import BLEDevice
from datetime import datetime
from requests.structures import CaseInsensitiveDict

GLUCOSE_DEVICE_ADDRESS = "D0:03:XX:XX:1A:A9" # Change to your device address
GLUCOSE_MEASUREMENT_UUID = "00002a18-0000-1000-8000-00805f9b34fb" # Glucose measurement UUID

def glucoseMeasurement(handle, data):
    unpackData = [dt[0] for dt in struct.iter_unpack("<B", data)]

    hexData = [hex(dt) for dt in unpackData]
    print("hexData : ", hexData)

    deviceDateTime = "{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}".format(int(((unpackData[4] & 0xFF) << 8) | unpackData[3]), int(unpackData[5]), int(unpackData[6]), int(unpackData[7]), int(unpackData[8]), int(unpackData[9]))
    currentDateTime = datetime.strptime(deviceDateTime, "%Y-%m-%d %H:%M:%S")

    glucoseMm = (unpackData[10] / 10.0)
    glucoseMg = "{:.1f}".format((unpackData[10] / 10.0) * 18)

    print("Date Time: ", str(currentDateTime.strftime("%Y-%m-%d %H:%M:%S")), "\t\tGlucose: ", glucoseMm, "mmol/L , ", glucoseMg, "mg/dL")

async def receiveNotification():
    print("Connect To: ", GLUCOSE_DEVICE_ADDRESS, "\t\t", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

    async with BleakClient(GLUCOSE_DEVICE_ADDRESS) as client:
        await client.start_notify(GLUCOSE_MEASUREMENT_UUID, glucoseMeasurement)
        await asyncio.sleep(5.0)
        await client.stop_notify(GLUCOSE_MEASUREMENT_UUID)

async def findBluetoothDevice():
    devices: Sequence[BLEDevice] = await BleakScanner.discover(timeout=1)

    for device in devices:
        if device.address == GLUCOSE_DEVICE_ADDRESS:
            await receiveNotification()

print("If you want to exit, you can press Ctrl + C.\n")

while True:
    try:
        loop = asyncio.get_event_loop()
        loop.run_until_complete(findBluetoothDevice())
    except KeyboardInterrupt:
        print("\nExit\n")
        sys.exit(0)
    except Exception as e:
        print("Exception Message:", str(e))
        time.sleep(1)

4. Run

สั่งรันคำสั่งเพื่อรอรับค่าจากเครื่องตรวจน้ำตาลในเลือด เมื่อรันโปรแกรมแล้ว ก็ลองเจาะเลือดเพื่อตรวจน้ำตาลในเลือดและดูการแสดงผล

# python3 blood-glucose-yuwell.py
Image
Image

5. การนำไปประยุกต์ใช้

เช่นเคย ฉันใช้เก็บข้อมูลเอาไว้ดูทีหลัง และก็ส่งข้อมูลเข้า LINE

6. ดูโค้ดทั้งหมดได้ที่ GitHub

GitHub - LookHin/blood-glucose-meter-yuwell
Contribute to LookHin/blood-glucose-meter-yuwell development by creating an account on GitHub.