Wemos D1 Mini, Nightscout and e-paper

My 2-year-old has Type 1 Diabetes. This is a condition that requires constant monitoring, so that insulin can be administered at the right time and in the right doses to ensure he doesn’t die.

When he was first diagnosed, we used fingerpricks and a blood glucose meter to assess Blood Glucose (BG) levels and calculate the insulin dose to be given by injection.

I’d just like to note, that none of the below is used for medical decision-making, it is only an indicator.

A few months after diagnosis, we were provided with an insulin pump, and a Continuous Glucose Monitor (CGM) – the pump allows us to deliver much smaller doses of insulin, and the CGM provides a measurement of glucose levels every 5 minutes – the only fingerpricks we need are for calibration about 4-5 times per day.

Xdrip

A home-made Xbridge, with a LiPo battery as a base

The CGM is wireless, and this allows us to read it with a device other than the insulin pump. There’s plenty of documentation online, but there is a device you can build which is known as an xBridge. It reads the Glucose levels wirelessly from the CGM with a wireless microcontroller (specifically a Wixel by Pololu), and pushes that to an Android Phone running a special application via an HM-10 Bluetooth module.

The phone pushes the data up to “The Cloud” – “The Cloud” is a term which normally equates to “Someone else’s computer”, but in this case, it’s mine. The server runs an application called Nightscout, which interfaces with a MongoDB instance to collect the readings.

Nightscout also publishes current levels and some other useful data in a JSON format, which can be used to display on a Pebble smartwatch. My wife has one of these, but I don’t – So I decided to build something else as a “Desktop display” for his BG readings.

The output I need to parse comes in the following format:

{
"status":[{
"now":1519203661659
}],
"bgs":[{
"sgv":"11.8",
"trend":3,
"direction":"FortyFiveUp",
"datetime":1519201987940,
"filtered":225536,
"unfiltered":232480,
"noise":1,
"bgdelta":"0.3",
"battery":"62",
"iob":"0",
"cob":0}],
"cals":[{
"slope":995.2425082371145,
"intercept":20781.66273980129,
"scale":1}]
}

The fields we are interested in are:
• now: This is the current UNIX timestamp (plus milliseconds)
• sgv: The Glucose Level
• datetime: The UNIX timestamp (plus milliseconds) of the last reading
• direction: What direction the Glucose level is moving in (possible values are: Flat, SingleUp, SingleDown, FortyFiveUp, FortyFiveDown, DoubleUp, DoubleDown)
• bgdelta: This is the delta between this reading and the last. I don’t currently use this, but it’s useful for a future enhancement.

Wemos D1 Mini

The Wemos D1 Mini (Might be a clone)

The Hardware:
I needed something small, wifi-enabled, easily programmable, and low-power. The ESP8266 is a low-cost Wifi module with a microcontroller onboard, which is compatible with the Arduino development environment, which I was already familiar. I managed to pick up a Wemos D1 mini for about £3. Perfect.

I also needed a display. I initially went for an HD44780 20×4 LCD display with an I2C interface, though power drain was a little high, as it’s difficult to read in sunlight without the backlight, which obviously draws power all the time. Eventually I found a 2.9” e-paper module with an SPI  interface. Even better, the logic level for this is 3.3V, the same as the ESP8266 (versus a standard Arduino which outputs 5V logic, needing a level shifter) This was connected as follows:
• BUSY -> D2
• RST -> D4
• DC -> D3
• CS -> D8
• CLK -> D5
• DIN -> D7
• GND -> GND
• 3.3V -> 3.3V

As this can run from 3.3V (and works well down to about 2.7V) – I could use a single Li-Ion 3.7V battery. Li-Ion has a maximum voltage of 4.2V, so I needed to use the “5V” input of the Wemos D1, which pushes the power through its internal 3.3V regulator. Looking at the datasheet of that regulator, it has a dropout voltage of 0.4V, so anything below 3.7V will be unregulated. This is just about within spec, so I grabbed a knock-off Samsung battery from ebay, which already has overcurrent and under-voltage protection, so I could use a Li-Ion charging board without this protection.

One other modification I made – I wanted to be able to read the voltage of the battery – I couldn’t do this directly from the power, but the Wemos D1 mini is able to read voltages of up to 3.3V on pin A0. Problem is, my battery can run up to 4.2V. I used 2 resistors in a simple voltage divider – 5V -> 100K -> A0 -> 330K -> GND to give a maximum voltage on pin A0 of 3.22V at 4.2V input.

Voltage Divider

The voltaqe divider between 5V, A0 and GND

As for the Code, we want the ESP8266 to do a few jobs:
• Connect to Wifi
• Download the JSON
• Parse the JSON
• Get the variables into readable format
• Display them on the screen
• Go to sleep

e-Paper display and Wemos D1

Showing that the image remains when power is removed

I can go into more detail later if anyone is interested, but the code is uploaded to https://github.com/robjohnc/nightscout_wemos_epaper, which *should* be fairly well documented.

Share This!

One thought on “Wemos D1 Mini, Nightscout and e-paper

  1. Awsome! I have been thinking about doing the exact thing for a few weeks and started googling. After reading a lot I by chance found you already have done exactly what I had in mind. I will try/adapt it for our setup so our son can monitor his bg level while immersed in online gaming…

Leave a Reply

Your email address will not be published. Required fields are marked *