added RTC date/time stamp
@@ -18,6 +18,7 @@ import board
import busio
import digitalio
import storage
+import adafruit_pcf8523
import _bleio
import adafruit_ble
from adafruit_ble.advertising.standard import Advertisement
@@ -32,6 +33,24 @@ sd_card = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sd_card)
storage.mount(vfs, "/sd_card")
+log_interval = 2 # you can adjust this to log at a different rate
+
+# RTC setup
+I2C = busio.I2C(board.SCL, board.SDA)
+rtc = adafruit_pcf8523.PCF8523(I2C)
+
+days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
+
+set_time = False
+if set_time: # change to True if you want to write the time!
+ # year, mon, date, hour, min, sec, wday, yday, isdst
+ t = time.struct_time((2020, 4, 21, 16, 36, 0, 2, -1, -1))
+ # you must set year, mon, date, hour, min, sec and weekday
+ # yearday not supported, isdst can be set but we don't use it at this time
+ print("Setting time to:", t) # uncomment for debugging
+ rtc.datetime = t
+ print()
+
# PyLint can't find BLERadio for some reason so special case it here.
ble = adafruit_ble.BLERadio() # pylint: disable=no-member
@@ -39,6 +58,7 @@ pulse_ox_connection = None
initial_time = time.monotonic()
while True:
+ t = rtc.datetime
print("Scanning for Pulse Oximeter...")
for adv in ble.start_scan(Advertisement, timeout=5):
name = adv.complete_name
@@ -80,7 +100,10 @@ while True:
valid, spo2, pulse_rate, pleth, finger = values
if not valid:
continue
-
+ if (
+ pulse_rate == 255
+ ): # device sends 255 as pulse until it has a valid read
+ continue
print(
"SpO2: {}% | ".format(spo2),
"Pulse Rate: {} BPM | ".format(pulse_rate),
@@ -96,13 +119,24 @@ while True:
"Seconds since current data log started:",
int(time_stamp),
)
+ sdc.write(
+ "{} {}/{}/{} {}:{}:{} ".format(
+ days[t.tm_wday],
+ t.tm_mday,
+ t.tm_mon,
+ t.tm_year,
+ t.tm_hour,
+ t.tm_min,
+ t.tm_sec,
+ )
+ )
sdc.write(
"{}, {}, {}, {:.2f}\n".format(
int(time_stamp), spo2, pulse_rate, pleth
)
)
- time.sleep(2)
+ time.sleep(log_interval)
except OSError:
pass
except RuntimeError:
