Pages

Tuesday, 29 December 2015

Nightscout on the 640g: On The Go Details

As promised, here are a few details on how to get my current mobile solution to getting the 640g and Nightscout fully mobile (i.e. talking across networks).

The easiest and most reliable 'mobile' solution is to use a mobile phone and VirtualHere to talk to the pump AND have a CareLink-connected PC on the same local network (e.g. your home or school WiFi network or a MiFi network setup on the phone or a separate MiFi device). If that's all you need, you don't need to follow these instructions, just this post [Edit: updated post here] and the "USB IP" bit of this one!

For the basics, the post introducing the mobile part of the solution is here. Core details on getting the 640g on Nightscout at the moment (pre-Minimed Connect release) are here. I'd suggest reading both of these posts before scrolling down, otherwise this won't make much sense at all...

 
Need to be mobile? Take one CWD, one 640g, one mobile and one Bayer Contour Next Link 2.4

I'm still developing and testing this implementation. This is far from a robust and perfect solution. Indeed, I've been having problems reliably transferring data across some of the WiFi and 4G spots I've tested, which I think is due to excessive latency in the connection.

The same level limitation of use and caution apply as per the previous posts:
All information, thought, and code described here is intended for informational and educational purposes only. Use of code from Github is without warranty or support of any kind.
Each element of the system can fail at any time rendering the system unusable. There is no password protected privacy or security provided by these tools; all data you upload can be available for anyone on the Internet to read if they have your specific URL to view your data. 

Do not use any of this information or code to make medical decisions.

Connecting and using the Bayer meter via your mobile phone may damage your phone and/or the Bayer meter!

I've tried to outline the core steps below. Please keep in mind that I've only tested this on the wireless networks I have access to and single mobile phone (a Sony Z3C Android phone running Android Kitkat and on the UK's 4G Vodafone network). I can't guarantee any of this will work with your local setup, but I do know others who have managed to get this up and running, which is great to hear :-)

Dynamic Domain Name Service
We need to give the home network - and the PC running CareLink - a consistent look up address. If you're ISP gives you a fixed (external) IP address you don't need to take any further action. You can use the IP address directly (e.g. 5.1.2.3 - note, this isn't the IP address that you use to look up your router. That probably starts 192.something or 10.something and is for internal use only).

So assuming you have a dynamic external IP address assigned, you'll need another way to identify your network on the internet so that it can be found each and every day. The solution is to use a Dynamic Domain Name Service (DDNS). Several options are available. There are several options available, but I used Change IP. Please follow the instructions to setup your account and then use a suitable client on a machine on your local network to keep the entry up-to-date. I use  DDclient on a Raspberry Pi that's on ours. Downloads for Change IP's clients are here if you need them. Don't forget to make sure they're running to keep your domain name and IP synced :-)

USB IP
The key protocol used is USB IP. The application I've used that implements the protocol is VirtualHere. We run the VirtualHere server on an Android phone or Raspberry Pi that stays with / near Janki (below left). The client runs on the Windows PC that's running CareLink etc as per the original solution (below right).



The very first thing to do is to check the Bayer Contour Next Link 2.4 meter is working correctly over the same intranet (i.e. with both your PC and mobile / Pi connected to your local WiFi). For use with Android, you'll need a phone with USB on-the-go (OTG) capability and a suitable cable to connect the micro USB connection on the phone to the Bayer meter. If you hit any problems with OTG functionality on your mobile, please Google along with your exact Android version. I had to roll back my Sony Z3C to KitKat to restore bug-free OTG functionality...

In order to get USB IP working across networks - i.e. via the internet - there appear to be two possible routes: one is to use the "reverse clients" option baked into VirtualHere; the other is to combine the conventional setup with a Virtual Private (VPN) which will allow the mobile / Pi to appear to be on the same internal network.

VirtualHere: Reverse Clients (Option 1)
This is the simplest route to getting access over the Internet. However, if it fails to work reliably, go for Option 2 below.

The VirtualHere app has a Reverse Clients option, which (as it says on the tin) allows connections to be pushed from the server (on the mobile / Pi) to the client. This has the advantage that if the phone is sat behind another firewall (e.g. at school) it should (usually) be able to contact 'out' across the internet to your home network. Initiating a connection the other way (i.e. the conventional client initiated connection) would require the external network hosting the VirtualHere Server to expressly allow the connection.

Instructions on how to set up the reverse client options are here. It appears to be necessary to initially setup reverse client option with both devices on the same local network (i.e. your local WiFi). Once you've done that initial setup, the server (phone,Pi) can be placed used on a different network.

The default port for the server initiated connection to the client is 7573. Depending on your local (usually home) router, you may need to open this port and will need to forward requests on this port to your CareLink device.  As all routers are different, I suggest looking up Port Forwarding in your internet router manual for instructions.


VirtualHere: VPN Access (Option 2)
If Option 1 doesn't work reliably, try routing your VirtualHere data through a Virtual Private Network (VPN). Essentially, a VPN pushes all network data securely across the internet to your home internet network. This can allow a remote device to appear to be part of your local network.

There are a  number of options out there. The one I'll briefly run through here is OpenVPN. We run with OpenVPN running on a Raspberry Pi. There are a number of installation and setup guides on the Internet ready to guide you through setup (e.g. here or here). This isn't a five minute job so please put some time aside to work through each bit carefully.
Trade Mark (tm) of OpenVPN Technologies, Inc

There are some key bits you'll need to setup (plus some optional stuff I've thrown in below be make life easier):
  • Give the Pi a static IP address on your local, internal network
  • Use a wired (cable) connection rather than WiFi [optional]
  • Add SSH and use Putty on another PC on the network to control / check on your Pi for maximum flexibility [optional]
  • Make sure your Pi is up-to-date using apt-get update and apt-get upgrade [optional]
  • Install OpenVPN on the Pi
  • Generate a RSA key so that you don't leave your network 'door' wide-open...
  • Tweak the OpenVPN server.conf file
  • Set for TCP connection with "No Delays" - i.e. forward on any packets as soon as they're ready (not network efficient but minimises latency which is important for our application)
    • socket-flags TCP_NODELAY
    • push "socket-flags TCP_NODELAY"
  • Set up Pi to forward on IPv4 traffic in /etc/sysctl.conf
  • Tweak /etc/firewall-openvpn-rules.sh to and /etc/network/interfaces to allow OpenVPN traffic to pass through the Pi's firewall
This may look like a lot of steps but there's plenty of detail in the guides available online and so I haven't reproduced it line by line here. If you hit an error, a quick Google with the error message or the troublesome file in the search string will usually quickly direct you to the solution.

You'll need to get your OpenVPN key onto the device you want to run remotely - depending on your setup, you can easily do this via Windows Explorer, SFTP (e.g. WinSCP) plugging in the SD card from your phone into the PC or connecting it via USB.

If you aim to run remotely on a phone, as we are, you'll need to use a VPN client on the phone. I use the OpenVPN Connect app for our Android solution. Follow your chosen client's instructions to get your connection working, including importing the required key. Please test the VPN link by trying to connect to a website via VPN on your phone.

Depending on your setup you may well need to make some changes on your local network to make all this work. Common tweaks include:

  • Port-forwarding of port 1194 to your Raspberry Pi's local IP address
  • You may need to make a persistent route change on your CareLink PC so it can see the VPN-side of the Pi (e.g. as administrator on your PC, type something along the lines of:
    • route -p ADD 10.8.0.0 MASK 255.255.255.0 192.168.1.129
(where 10.8.0.0 is your VPN network, 192.168.1.129 is your local IP address for the CareLink PC)

Turn off Reverse Client in VirtualHere (right click on USB Hubs, left click, Specify Hubs, Advanced, turn off Enable Reverse Connections, click Close)

Common Issues and Fixes
Bayer not plugged in error:
Occurs occasionally with our setup. Leave it for one cycle (10 minutes). If that doesn't 'solve' the error, close the VirtualHere client on the PC, including stopping any VirtualHere processes via Task Manager if needed. Then restart the client. We rarely have to restart anything on the phone unless the cable has come loose.

Pump communication error:
When running on the phone, the Bayer range appears to be limited (this is probably due to the mA supply from the OTG USB port being less than that supplied by a PC USB port, but I've not looked into this): get the pump and Bayer Contour system close to the pump and remember the human body is great at absorbing radio-frequency transmissions - we get better range by putting the phone and Bayer meter under Janki's bed (even downstairs, under her bed) or by hanging the meter above the bed.

CareLink doesn't seem to deal with (any) latency between the pump and the Bayer. Latency over 3G or 4G is likely to be higher than over WiFi and so communication can drop. Please keep this in mind and use WiFi whenever possible.

2 comments:

  1. Hi
    as I can not find a way other to communicate with you I will post here :)
    I came across this video
    https://www.youtube.com/watch?v=Jr_iVKfOzz8
    there is some info about the gaps in measurements around 17min saying that they intentionally do not show data if a big dip occurs .. I guess they mean when the sensor gets older and its getting more and more sensitive on pressing it
    Regards

    ReplyDelete
  2. Hi Leo, thanks for getting in touch. Sorry for the delay getting back to you :/
    That's a great find - I'll put a short post up on on the FB page to let others know, who may not stumble across it in the comments.
    Thanks again! Matt
    PS Feel free to direct message via www.facebook.com/littlet1d or https://twitter.com/LittleDMatt

    ReplyDelete