Now it's time to use the Blinkt HAT!
Disconnect your Zero from power and attach the Blinkt. It just slips right on top of the gpio headers on the zero (assuming you got the Zero WH with the headers already installed, or you've soldered your own on) - don't be afraid to apply a little force to get it all the way seated. Pay close attention to the Blinkt, as one side has rounded corners to match the Zero. The straight edge corners should be facing into the Zero, and the rounded corners should be on the outside. With the Blinkt properly attached, power the Zero back up and connect via ssh again.
We'll first need to install the python modules. Pimoroni has provided an easy script to get this done, as well as install some useful examples, etc. From the command line enter in:
And then just follow the prompts to get everything done. It'll take a few a minutes, the Zero isn't the fastest computer out there...
Once that's done, let's try one of the example scripts and just make sure that the LEDs are working as expected:
You should see some cool rainbow colors. If not, check out that Pimoroni tutorial for some troubleshooting.
Now we're going to use the Nightscout data that we pulled with python in the last part to drive the color of the Blinkt.
We'll need to install one more python module to help make the magic. To install SciPy, use:
If you have trouble installing with that method (as I did), you can also try:
When you have the SciPy module installed, let's create the complete python script:
and paste in the following (without the line numbers):
Again, like you did in the ns_data.py file, make sure you update the MongoDB URI with the correct info and password!
Lines 25-29 are defining the LED colors that we'll be using. Feel free to change these to whatever you'd like. Use an RGB color selector to get the R, G, and B values. You can also add additional colors by following the same format.
At line 43, we're checking to see if the latest reading is out of bounds low (under 30) and/or if the time between the last two timestamps is more than 700,000 ms (~11.5 minutes). Since Nightscout bridge updates every 5 minutes, this should be a good indicator if we're not getting data. If either of these are true, it will make the LEDs blue.
At line 46, we're checking to see if the latest reading is under 80, and if so turn the LEDs red. We also adjust the brightness based on how low it is - the lower the reading, the brighter the LEDs.
Similarly, at line 49 we're checking to see if the latest reading is over 140. We turn the LEDs purple and also adjust the brightness so that they are brighter the higher the reading is.
And finally, at like 52, we set the LEDs as green if the reading is between 80 and 140.
All of this is built into a loop, so the script will run perpetually. At line 61, we put in a 5 minute delay before it starts over again, since we know Nightscout only gets new data once every 5 minutes.
Go ahead and run this:
And you should get an output saying what the current reading is and what color the LEDs were set to. You should also see the Blinkt lit up accordingly. After 5 minutes, you'll see another line of output with the current reading and color, and should see the Blinkt adjust if applicable.
Congratulations! There's just one more catch. As soon as you disconnect your ssh session, the script will stop running and so will your notification lights. To get around this, I used a cron scheduled for every minute to initially start the script (then it's not tied to your ssh session), and also restart the script if it somehow terminates.
Edit your crontab with:
Then scroll all the way down to the bottom of the file and copy/paste this on to a new line:
And now you should be all set!
Disconnect your Zero from power and attach the Blinkt. It just slips right on top of the gpio headers on the zero (assuming you got the Zero WH with the headers already installed, or you've soldered your own on) - don't be afraid to apply a little force to get it all the way seated. Pay close attention to the Blinkt, as one side has rounded corners to match the Zero. The straight edge corners should be facing into the Zero, and the rounded corners should be on the outside. With the Blinkt properly attached, power the Zero back up and connect via ssh again.
We'll first need to install the python modules. Pimoroni has provided an easy script to get this done, as well as install some useful examples, etc. From the command line enter in:
curl https://get.pimoroni.com/blinkt | bash
And then just follow the prompts to get everything done. It'll take a few a minutes, the Zero isn't the fastest computer out there...
Once that's done, let's try one of the example scripts and just make sure that the LEDs are working as expected:
cd ./Pimoroni/blinkt/examples/ python rainbow.py
You should see some cool rainbow colors. If not, check out that Pimoroni tutorial for some troubleshooting.
Now we're going to use the Nightscout data that we pulled with python in the last part to drive the color of the Blinkt.
We'll need to install one more python module to help make the magic. To install SciPy, use:
python -m pip install --user numpy scipy
If you have trouble installing with that method (as I did), you can also try:
sudo apt-get install python-numpy python-scipy
When you have the SciPy module installed, let's create the complete python script:
cd ~ pico ns_light.py
and paste in the following (without the line numbers):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | from __future__ import division import urllib import pymongo import pprint import time from pymongo import MongoClient import blinkt from scipy import stats def pr(x,min,max,direction): if direction == 1: y = 99.9 - stats.percentileofscore(range(min,max,1),x,kind='rank') else: y = stats.percentileofscore(range(min,max,1),x,kind='rank') y = round(y/100,2) if y < 0.1: y = 0.1 if y > 0.9: y = 0.9 return y pswd = urllib.quote_plus('MongoDB Password goes here') client = MongoClient('mongodb://ns_scrape:'+pswd+'@ds155252.mlab.com:55252/heroku_xxxxxx') db = client.heroku_xxxxxx color_array = {'PURPLE': {'Rval': 128, 'Gval': 0, 'Bval': 128}, 'RED': {'Rval': 255, 'Gval': 0, 'Bval': 0}, 'GREEN': {'Rval': 0, 'Gval': 255, 'Bval': 0}, 'BLUE':{'Rval': 0,'Gval': 128, 'Bval': 255} } while True: 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 if latest_bs < 30 or delta > 700000: color = 'BLUE' bright = 0.1 elif latest_bs < 80: color = 'RED' bright = pr(latest_bs,30,81,1) elif latest_bs > 140: color = 'PURPLE' bright = pr(latest_bs,141,200,0) else: color = 'GREEN' bright = 0.1 print('BS is %d and LEDs are %s' %(latest_bs,color)) blinkt.clear() blinkt.set_all(color_array[color]['Rval'],color_array[color]['Gval'],color_array[color]['Bval'],bright) blinkt.show() time.sleep(300) |
Again, like you did in the ns_data.py file, make sure you update the MongoDB URI with the correct info and password!
Lines 25-29 are defining the LED colors that we'll be using. Feel free to change these to whatever you'd like. Use an RGB color selector to get the R, G, and B values. You can also add additional colors by following the same format.
At line 43, we're checking to see if the latest reading is out of bounds low (under 30) and/or if the time between the last two timestamps is more than 700,000 ms (~11.5 minutes). Since Nightscout bridge updates every 5 minutes, this should be a good indicator if we're not getting data. If either of these are true, it will make the LEDs blue.
At line 46, we're checking to see if the latest reading is under 80, and if so turn the LEDs red. We also adjust the brightness based on how low it is - the lower the reading, the brighter the LEDs.
Similarly, at line 49 we're checking to see if the latest reading is over 140. We turn the LEDs purple and also adjust the brightness so that they are brighter the higher the reading is.
And finally, at like 52, we set the LEDs as green if the reading is between 80 and 140.
All of this is built into a loop, so the script will run perpetually. At line 61, we put in a 5 minute delay before it starts over again, since we know Nightscout only gets new data once every 5 minutes.
Go ahead and run this:
python ns_light.py
And you should get an output saying what the current reading is and what color the LEDs were set to. You should also see the Blinkt lit up accordingly. After 5 minutes, you'll see another line of output with the current reading and color, and should see the Blinkt adjust if applicable.
Congratulations! There's just one more catch. As soon as you disconnect your ssh session, the script will stop running and so will your notification lights. To get around this, I used a cron scheduled for every minute to initially start the script (then it's not tied to your ssh session), and also restart the script if it somehow terminates.
Edit your crontab with:
crontab -e
Then scroll all the way down to the bottom of the file and copy/paste this on to a new line:
* * * * * /usr/bin/flock -w 0 /home/pi/ns_light.lockfile /usr/bin/python /home/pi/ns_light.py
And now you should be all set!
This is really helpful, thanks for sharing!
ReplyDelete