Now it's time to pull in your Nightscout data. We'll be using python for the coding, and as I mentioned before, I've got Nightscout deployed on Heroku so we'll be looking at that flow.
Start by pulling up your Heroku app. The screen should look something like this:
Go ahead and click on that mLab MongoDB link. This will take you to the mLab database page. We'll need to add a new user. Click on the Users tab, and then the Add database user button on the right.
For the new username, use "ns_scrape". Pick a password of your choosing, and check off read-only.
Towards the top of the mLab page, you'll see your MongoDB URI. You'll need this, copy and paste it somewhere easily accessible for later.
Now back to the Zero. From the command line, logged in as the pi user, we first need to install the MongoDB python module, and then we'll create a new python script:
Once you have the pico editor open, paste in the following:
You'll need to refer back to the original MongoDB URI that you copied and make sure that you adjust the script as necessary (e.g. the ds155252 part is probably different in your URI, and the heroku_xxxxxx will need to be updated to your actual app. Also make sure you update the password!
Now hit ctrl-x to exit and save the script, and run it with
You should see two lines of output, the first being the current glucose reading, and the second being the number of milliseconds between the first and second entries.
So let's step through this a bit. We're querying the "entries" collection to get the most recent 2 entries, sorted with the most recent first. Then we're assigning the sgv (sensor glucose value) value for that first (most recent entry) to the latest_bs. We're also pulling in the date stamps from the 2 entries and then comparing them to see how much time has elapsed since. We'll use this to change the alert color if there hasn't been a new entry within 10 minutes of the last.
If you're able to successfully use python to pull in your most recent Nightscout data, then it's time to move on to Part 3 and get the lights working!
Start by pulling up your Heroku app. The screen should look something like this:
Go ahead and click on that mLab MongoDB link. This will take you to the mLab database page. We'll need to add a new user. Click on the Users tab, and then the Add database user button on the right.
For the new username, use "ns_scrape". Pick a password of your choosing, and check off read-only.
Towards the top of the mLab page, you'll see your MongoDB URI. You'll need this, copy and paste it somewhere easily accessible for later.
Now back to the Zero. From the command line, logged in as the pi user, we first need to install the MongoDB python module, and then we'll create a new python script:
python -m pip install pymongo
pico ns_data.py
Once you have the pico editor open, paste in the following:
import urllib import pymongo import time from pymongo import MongoClient pswd = urllib.quote_plus('MongoDB Password Here') client = MongoClient('mongodb://ns_scrape:'+pswd+'@ds155252.mlab.com:55252/heroku_xxxxxx') db = client.heroku_xxxxxx result = db.entries.find().limit(2).sort('$natural',-1) array = [] for i in result: array.append(i) latest_bs = array[0]['sgv'] latest_date = array[0]['date'] prev_date = array[1]['date'] delta = latest_date - prev_date print(latest_bs) print(delta)
You'll need to refer back to the original MongoDB URI that you copied and make sure that you adjust the script as necessary (e.g. the ds155252 part is probably different in your URI, and the heroku_xxxxxx will need to be updated to your actual app. Also make sure you update the password!
Now hit ctrl-x to exit and save the script, and run it with
python ns_data.py
You should see two lines of output, the first being the current glucose reading, and the second being the number of milliseconds between the first and second entries.
So let's step through this a bit. We're querying the "entries" collection to get the most recent 2 entries, sorted with the most recent first. Then we're assigning the sgv (sensor glucose value) value for that first (most recent entry) to the latest_bs. We're also pulling in the date stamps from the 2 entries and then comparing them to see how much time has elapsed since. We'll use this to change the alert color if there hasn't been a new entry within 10 minutes of the last.
If you're able to successfully use python to pull in your most recent Nightscout data, then it's time to move on to Part 3 and get the lights working!
Comments
Post a Comment