Skip to content

CGM Loop with openxshareble scratchpad

Jason Calabrese edited this page Sep 10, 2016 · 22 revisions

Overview

There are easier ways to use openxshareble, with this loop I'm trying to get the timing and reliability as good as possible.

This CGM Loop is designed to run outside your normal openaps loop, it should be in a different directory next to your main openaps loop. A cron will be used to have it run continuously even if the pump isn't in range or network isn't available. In your main loop you'll copy the monitor/glucose-raw.json instead of going and getting it again.

Get openxshareble installed

Follow instructions here: https://github.com/TC2013/aps-scripts/blob/master/openxshareble_instructions.md

Clone and add opendex-tools scripts

later these scripts or something better should be added to oref0

cd
git clone https://github.com/jasoncalabrese/opendex-tools.git
echo export PATH=$PATH:~/opendex-tools >> .profile
sudo apt-get install bc jq

Create CGM Loop and add devices

cd
mkdir cgm-loop
openaps init cgm-loop
cd cgm-loop
mkdir monitor
mkdir nightscout
openaps vendor add openxshareble
openaps device add cgm openxshareble
openaps vendor add openapscontrib.timezones
openaps device add tz timezones
openaps device add oref0 process oref0
openaps device add raw process --require "glucose cal maxraw" oref0 raw

Find the mac address for the receiver

from a location without only 1 receiver in range

openaps use cgm list_dexcom

Look for your DEXCOMRX device and find the mac address in the form A1:B2:C3:D4:E5:F6

openaps use cgm configure --serial <SM12345678> --mac <A1:B2:C3:D4:E5:F6>

Make sure the time on your Edison is correct and sync Dexcom clock

date # compare to phone

openaps use cgm UpdateTime --to now

Test that you can get data with:

openaps use cgm oref0_glucose --threshold 100 --hours 1

Configure Nightscout

nightscout autoconfigure-device-crud <https://your-host.com> <yourapisecret>

Setup reports and aliases

openaps report add monitor/cal.json JSON cgm nightscout_calibrations 1
openaps report add monitor/cal-zoned.json JSON tz rezone monitor/cal.json --date "display_time system_time dateString" --adjust missing
openaps report add monitor/glucose-oref0.json JSON cgm oref0_glucose --threshold 100 --hours 4.5
openaps report add monitor/glucose-zoned.json JSON tz rezone monitor/glucose-oref0.json --date "display_time system_time dateString" --adjust missing
openaps report add monitor/glucose-raw.json JSON raw shell monitor/glucose-zoned.json monitor/cal.json 150
openaps report add nightscout/recent-missing-entries.json JSON ns shell format-recent-type tz entries monitor/glucose-zoned.json
openaps report add nightscout/uploaded-entries.json JSON ns shell upload entries.json nightscout/recent-missing-entries.json
openaps report add nightscout/uploaded-cals.json JSON ns shell upload entries.json monitor/cal-zoned.json

openaps alias add wait-till-expected '! bash -c "wait-till-expected.sh monitor/glucose-zoned.json 5.1"'
openaps alias add glucose 'report invoke monitor/glucose-oref0.json monitor/glucose-zoned.json monitor/glucose-raw.json'
openaps alias add echo-glucose '! bash -c "echo \">>>>RESULTS<<<<\" && json -f monitor/glucose-raw.json -a sgv raw dateString | head -n 3"'
openaps alias add extras 'report invoke monitor/cal.json monitor/cal-zoned.json nightscout/uploaded-cals.json'
openaps alias add maybe-extras '! bash -c "if (is-fresh.sh monitor/glucose-zoned.json 3); then openaps extras; else echo \"Glucose isnt fresh, not pulling extra data\"; fi"'
openaps alias add upload 'report invoke nightscout/recent-missing-entries.json nightscout/uploaded-entries.json'
openaps alias add push-cgm '! bash -c "(git push && echo pushed to github) || echo unable to push to github"'
openaps alias add monitor-cgm '! bash -c "openaps wait-till-expected && time openaps glucose && openaps echo-glucose && (openaps upload || echo cgm upload failed) && (openaps maybe-extras || echo cgm extras failed)"'

Test aliases

openaps glucose

openaps monitor-cgm

Setup cron

Add something like this to your cron with crontab -e

* * * * * cd /home/edison/cgm-loop && ( ps aux | grep -v grep | grep -q 'openaps monitor-cgm' && echo monitor-cgm already running || openaps monitor-cgm ) 2>& 1 | logger -t openaps-monitor-cgm

* * * * * killall -g --older-than 7m openaps

Integrate with your main loop

cd ~/my-loop # or similar
openaps report add monitor/glucose.json text oref0 shell copy-fresher /home/edison/cgm-loop/monitor/glucose-raw.json

Optimizing loop speed with merge

Only fetch 30 minutes, instead of the 4.5 hours above

cd /home/edison/cgm-loop
openaps report add monitor/glucose-oref0.json JSON cgm oref0_glucose --threshold 100 --minutes 30

The merge device

openaps device add merge process --require 'short long' 'jq -s ".[0] + .[1]|unique|sort_by(.dateString)|reverse|.[0:288]"'

Some new reports and aliases (openaps reset-glucose-merge is expected to be run manually if needed)

openaps report add monitor/glucose-oref0-long.json JSON cgm oref0_glucose --threshold 100 --hours 4.5
openaps report add monitor/glucose-zoned-long.json JSON tz rezone monitor/glucose-oref0-long.json --date "display_time system_time dateString" --adjust missing
openaps report add monitor/glucose-zoned-merge.json JSON merge shell monitor/glucose-zoned.json monitor/glucose-zoned-merge.json
openaps report add monitor/glucose-raw-merge.json JSON raw shell monitor/glucose-zoned-merge.json monitor/cal.json 150

openaps alias add glucose-long 'report invoke monitor/glucose-oref0-long.json monitor/glucose-zoned-long.json'
openaps alias add reset-glucose-merge '! bash -c "openaps glucose-long && rm -f monitor/glucose-zoned-merge.json && cp monitor/glucose-zoned-long.json monitor/glucose-zoned-merge.json && openaps report invoke monitor/glucose-raw-merge.json"'

Updated aliases

openaps alias add glucose 'report invoke monitor/glucose-oref0.json monitor/glucose-zoned.json monitor/glucose-zoned-merge.json monitor/glucose-raw-merge.json'

openaps alias add echo-glucose '! bash -c "echo \">>>>RESULTS<<<<\" && json -f monitor/glucose-raw-merge.json -a sgv raw dateString | head -n 3"'

Updated reports

openaps report add nightscout/recent-missing-entries.json JSON ns shell format-recent-type tz entries monitor/glucose-zoned-merge.json

Don't forget to update your main loop to use glucose-raw-merge.json instead of glucose-raw.json

Optimizing loop upload speed by uploaded first, before gap sync

New upload first device, reports, and alias

openaps device add first process --require 'file' 'jq .[0:2]'
openaps report add monitor/glucose-zoned-first.json JSON first shell monitor/glucose-zoned-merge.json
openaps report add nightscout/uploaded-first.json JSON ns shell upload entries.json monitor/glucose-zoned-first.json
openaps alias add upload-first 'report invoke monitor/glucose-zoned-first.json nightscout/uploaded-first.json'

Only do the full upload and extras when data is fresh enough that we won't miss the next point

openaps alias add maybe-upload '! bash -c "if (is-fresh.sh monitor/glucose-zoned.json 3); then openaps upload; else echo \"Glucose isnt fresh, not doing full upload\"; fi"'

openaps alias add monitor-cgm '! bash -c "openaps wait-till-expected && time openaps glucose && openaps echo-glucose && (openaps upload-first || echo upload first failed) && (openaps maybe-upload || echo cgm upload failed) && (openaps maybe-extras || echo cgm extras failed)"'