Given the choice between a) changing my lifestyle to be boring and b) hacking my metabolism, I chose the easier option. I built an artificial pancreas using OpenAPS! And it's changing my life.
What's an artificial pancreas?
- When your pancreas is not munted like mine, it produces insulin
- But more importantly, it knows when and how much insulin to produce - in response to the amount of glucose in your blood
- Everything affects this:
- Food. Diabetics have to give themselves insulin for carbohydrates, using personal ratios we guess and refine. Different foods have different amounts of carbohydrates, which release at different speeds (glycaemic index). Companies are legally allowed to include an error margin of up to 10% on their nutritional information.
- When you wake up (dawn phenoemenon / residual insulin effects - you need more insulin)
- Illness and stress (you need more insulin)
- Your current levels. Your body is more insulin resistant when your levels are higher than they should be.
- Alcohol (occupies the liver, but only after more than one beer - you need less insulin)
- Exercise (adrenaline makes it temporarily go up, and stabilises in the long run)
- Sleep quality (makes metabolism more or less volatile)
- For diabetics, we are running mathematical optimizations of complex control systems, 24-7-365. Pulling levers, making guesses, trying to reduce our margins of error.
- An artificial pancreas is just this - but 80% is automated.
This post is a little blog of my experience building such a system. I've outlined the tech and costs of buying it, the research involved and assembly. And most of all - the actual human impact it is making on my life. Let's begin!
The Tech Stack
- FreeStyle Libre CGM - continuous glucose monitoring
- Miaomiao transmitter - transmits the Libre (which is NFC-based) readings via Bluetooth to my phone
- Nightscout deployment on Heroku - an open-source frontend/backend for T1 Diabetes data hosting and visualisation
- self-signed xDrip app - an open-source app which receives readings via Bluetooth, and sends them to Nightscout
- Medtronic Insulin Pump - to inject insulin
- Intel Edison + Explorer HAT - a Linux electronic board with WiFi and support for 900MhZ radio comms
- 4400mA Lithium Ion battery - to power the "rig" on-the-go
- OpenAPS - the 'operating system' of the artificial pancreas rig. Downloads readings from Nightscout, dynamically predicts and adjusts background insulin delivery on the insulin pump via radio, uploads all of this data to Nightscout for continuous monitoring.
Costs are in AUD and EUR.
Insulin pump - I already had this from years ago, but usually around €3100/$5000.
FreeStyle Libre CGM - €70/$112 for the reader (one time cost), and €70/$112 per sensor (which lasts 14 days, so €140/$225/month)
Miaomiao - this was €200/$322
Hosting Nightscout on Heroku. Free.
xDrip app. The app is open-source, but since Apple doesn't allow it in App Stores, I had to purchase a 1 year developer license to sign and install it on my phone. €93/$150
Intel Edison. Even though these boards are no longer sold by Intel, it was still rather cheap. €57/$92 (including shipping of €12/$20).
Explorer HAT. This piece is a bespoke product made by a company called Enhanced Radio in the US. €68/$109, although I had to pay Dutch import tax of like €20/$32 because I forgot this was outside the US.
Lithium Ion battery. Opted for a battery twice the capacity as the docs, just to err on the side of caution. €50/$81 (including shipping of €22/$36, and taxes €8/$13).
OpenAPS. Open-source ;)
Total fixed costs (excluding pump):
Total monthly costs (excluding insulin):
Stage 1: Research
There was quite a lot of research involved, and definitely more to come. Some things I had to understand:
- which insulin pump I had, and whether it could be hacked to run this system
- comparing the different open-source systems. There's another called Loop, which is an iOS app, that uses a custom board called RileyLink. There's also an Android port, called AndroidAPS.
- whether I could get this working with FreeStyle Libre - which is the cheapest and most easily-purchasable CGM (you can't just buy these things off Amazon) but definitely not the most ubiquitous.
- after choosing to build on OpenAPS, having to decide which of the Intel Edison or Raspberry Pi I would choose. I ended up selecting the Edison, since it's got much better power efficiency than the Pi.
Estimate: 7 hours
Stage 2: Ordering
All of these parts came from different places:
- The Edison has been out-of-production for 2 years now. I had to do a bit of sleuthing on Amazon and various other sites to find one. Eventually I found a seller on eBay who shipped from the US.
- The Explorer HAT was ordered from the US, easily through the one supplier.
- The Miaomiao took the longest out of all (2-3 weeks), coming from some dodgy place in China (despite being a Brazilian company).
- The battery (and some extra assembly bits) was ordered from Adafruit (a very reputable online electronics company), again coming from the US, but different shipping company.
Total time waiting: 3 weeks
I spent my $1000, sent them to my business address, and then went to Sweden and Finland while I was waiting.
This also misses the part where I had to go to the Dutch post office to pickup one of the packages, because apparently I owed import taxes on it.
Stage 3: Assembly
I was so excited to get this thing setup.
Estimated time: 9 hours
This stage took the longest. Why?
- 1 hour in total. I had to read the OpenAPS documentation, which is long. Very long.
- 2 hours. Downloading Jubilinux and properly flashing the Edison, over a microUSB cable. Fun fact - there are microUSBs cables that support data, and ones that don't!
- 2.5 hours. Downloading packages/dependencies using APT and NPM on a microcomputer. Then downloading them again, because OpenAPS is just a collection of rudimentary Bash scripts.
- 1 hour. I had to buy an Apple Developer License and learn how sign and install this xDrip app.
- 1 hour. Getting Nightscout setup on Heroku, with proper authentication and specific plugins for the OpenAPS system. Then adding these credentials to xDrip on my phone, and to OpenAPS
- 2 hours. Debugging why my loop was not running properly. For some reason, it couldn't get the levels from Nightscout. I spent some time,
cat'ing various logs, before I jumped into the nightscout/intend-to-bolus channel on Gitter. Within an hour, the very friendly folks were able to help me - we found the source of the issue was with the Tomato uploader, and I swapped to using Xdrip.
OpenAPS is remarkably simple for a large collection of Bash, Python and JS. Running
l will tail the system log.
This is the point at which I noticed a familiarly dumb stack trace -
myopenaps/monitor/glucose.json: Unexpected end of JSON input
I swapped from the app I was using, to signing and installing xDrip. You can clearly see that it's open-source software by the design flair. But it works - and that's the point.
It was 2am before I figured this out, so I had to break and come back to it. The next day I got in touch over the Gitter channel, and within an hour, a mate called Jon had helped me solve it!
Miraculously - my loop was running!
Stage 4: Usage
Below is a picture of Nightgraph, the visual dashboard of my artificial pancreas.
This is overwhelming, and again, typical of open-source projects to not have the resources for good visual design. I've annotated the main parts so you can better understand what you're looking at -
This is the brains of the artificial pancreas. You can see how it sees my levels, how it understands how much insulin I have on board, what it predicts, and how it's going to dynamically adjust to make sure it stays in range.
The way OpenAPS works is that it is continually readjusting a temporary basal - a background level of insulin - every 5 minutes. If you're not a diabetic but you are a nerd, then take a read of How OpenAPS Makes Decisions for a nice overview of the different maths involved. The core parameters it calculates are:
- the BG delta - the difference between current BG and an average of BG's over the past 5mins
- the short/long average BG deltas - the average rate of change of the BG over the timespans of the past 15 and 45 minutes respectively
- your insulin-on-board (IOB) - how much insulin is still on board. This is important because insulin doesn't get consumed like you would intuit - it stays in the body for X amount of time, rather than disappearing after X amount of carbohydrates have been processed
- insulin sensitivity ratio (more on this below)
This is basically real-world maths! The BG deltas are calculated as the derivative of BG levels. The insulin on board is calculated by modelling your type of insulin (there are different types) as an exponential curve. Here's a little picture I've pulled from the OpenAPS docs:
Why is this important? Because the community is as much as part of this experience as the technology itself. Even with faster acting insulin being introduced, the community stays up-to-date.
The other thing to note is that OpenAPS takes care of you when you're sick. Up to 24h before I have any symptoms of illness, my levels usually start getting much higher. This is an interesting example of insulin resistance - due to my body releasing some stress hormones when fighting off illness, my levels go up, which in turn increases my resistance to insulin, which leads them to further highs and so on...Now where OpenAPS comes in - every 24h, it is estimating whether your insulin sensitivity has changed. And using this information, it automatically adjusts to give you more insulin.
There's a slew of other things I haven't yet checked out (this is only my 3rd day in), such as integrating with IFTT, and some really funky reports you can generate in Nighscout, like the one below:
An OpenAPS Loop is not a catch-all solution. You still need to remember to give yourself insulin for food. And this won't help you with alcohol, probably. But it's a hell of a lot better — an order of magnitude better - than what was happening before.
Philosophy and struggle
In many ways, Diabetes is a confrontation with the absurd. It's an affliction that just happens to you one day (no, you don't have to be born with it). It's not your fault that you have it, and it's also sometimes not your fault when things go wrong - there are some things we do not know about metabolism, and especially about the natural feedback loops of the body. But, it's your responsibility to deal with - you either make something of it, or you don't.
Sometimes I would get so angry at myself, so upset with what was happening to me, because it is my own body. And not feeling in control of basic sensations, it does fuck you up a bit, mentally. Annoyed that medical practition is more than ever, a matter of skin in the game - made up of people that don't live with the disease, and don't have any large stakes either, and in Western medicine, prefer to prescribe rather than analyse. I started reading research from doctors who actually had T1 diabetes - the problem is not because you're failing to remember to give insulin - but the type of food you eat creates these volatile swings. One doctor went so far as to eliminate carbohydrates from his diet. These are not flat earthers - they are certified doctors, but unlike your typical Diabetes clinic, they have skin in the game.
To conclude: building this system was the most self-actualizing thing I've done in a while. All of this planning — investment of money ($1000), thought (3 weeks waiting) and time (20hrs) – led up to a moment. When I finally got that rig running, I felt ecstasy. Not that the burden was entirely lifted - but I really felt like I was back in control. And that, that was empowering.
Vladimir asked: what do you actually wear everyday? So here are the pictures of the gear: