Skip to content

Commit 1fd7c72

Browse files
committed
added RTC date/time stamp
1 parent c1c2851 commit 1fd7c72

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

Pulse_Oximeter_Logger/pulse_oximeter_logger.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import busio
1919
import digitalio
2020
import storage
21+
import adafruit_pcf8523
2122
import _bleio
2223
import adafruit_ble
2324
from adafruit_ble.advertising.standard import Advertisement
@@ -32,13 +33,32 @@
3233
vfs = storage.VfsFat(sd_card)
3334
storage.mount(vfs, "/sd_card")
3435

36+
log_interval = 2 # you can adjust this to log at a different rate
37+
38+
# RTC setup
39+
I2C = busio.I2C(board.SCL, board.SDA)
40+
rtc = adafruit_pcf8523.PCF8523(I2C)
41+
42+
days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
43+
44+
set_time = False
45+
if set_time: # change to True if you want to write the time!
46+
# year, mon, date, hour, min, sec, wday, yday, isdst
47+
t = time.struct_time((2020, 4, 21, 16, 36, 0, 2, -1, -1))
48+
# you must set year, mon, date, hour, min, sec and weekday
49+
# yearday not supported, isdst can be set but we don't use it at this time
50+
print("Setting time to:", t) # uncomment for debugging
51+
rtc.datetime = t
52+
print()
53+
3554
# PyLint can't find BLERadio for some reason so special case it here.
3655
ble = adafruit_ble.BLERadio() # pylint: disable=no-member
3756

3857
pulse_ox_connection = None
3958
initial_time = time.monotonic()
4059

4160
while True:
61+
t = rtc.datetime
4262
print("Scanning for Pulse Oximeter...")
4363
for adv in ble.start_scan(Advertisement, timeout=5):
4464
name = adv.complete_name
@@ -80,7 +100,10 @@
80100
valid, spo2, pulse_rate, pleth, finger = values
81101
if not valid:
82102
continue
83-
103+
if (
104+
pulse_rate == 255
105+
): # device sends 255 as pulse until it has a valid read
106+
continue
84107
print(
85108
"SpO2: {}% | ".format(spo2),
86109
"Pulse Rate: {} BPM | ".format(pulse_rate),
@@ -96,13 +119,24 @@
96119
"Seconds since current data log started:",
97120
int(time_stamp),
98121
)
122+
sdc.write(
123+
"{} {}/{}/{} {}:{}:{} ".format(
124+
days[t.tm_wday],
125+
t.tm_mday,
126+
t.tm_mon,
127+
t.tm_year,
128+
t.tm_hour,
129+
t.tm_min,
130+
t.tm_sec,
131+
)
132+
)
99133
sdc.write(
100134
"{}, {}, {}, {:.2f}\n".format(
101135
int(time_stamp), spo2, pulse_rate, pleth
102136
)
103137
)
104138

105-
time.sleep(2)
139+
time.sleep(log_interval)
106140
except OSError:
107141
pass
108142
except RuntimeError:

0 commit comments

Comments
 (0)