---
# System prepended metadata

title: IoT for eldery care (toilet sensor)

---

# IoT for eldery care (toilet sensor)
By Sebastian Berg, sb224bx
Course: Applied IoT (1DT305) at Linnaeus University.

**Summary:**
This is a tutorial for a "connected toilet" that gathers and communicates temperature data so the person and elderly care admins can spot above normal body temperature and thus spot sickness symptoms easier and faster.

This tutorial is using Pycom LoPy 4, an expansion board, and a TMP36 temperature sensor for hardware. The coding is done in MicroPyhton using Atom as IDE. Lastly, the data is transmitted over WiFi to Pybytes (a data management platform).

This is suitable for beginners and takes approximately 10 hours to complete. 

##  Objective
In light of COVID-19, it has become clear that more innovation is needed in elderly care to ensure safe and efficient care. In Sweden, as of the 28th of May, 90% of the total death toll of 4,000 were above 70 years old. Moreover, 49% of the total were from elderly care homes and 25% from home care (Socialstyrelsen, 2020). 

As a believer in technology for good, this project serves the purpose of exploring if IoT can assist in symptom detection for elderly people and patient monitoring for HCPs. Lastly, as a beginner in this field, the purpose is also self-motivated to increase the understanding of IoT and how to apply it in real life. 

This project (or prototype) is by all means very easy. But I hope it can spur some imagination for other enthusiasts to apply software and hardware in this space.

### Overview
![](https://i.imgur.com/tJUQ8rd.png)

Image above is a simple overview of the setups included in this project. Where:  
1. (the pycom device) concerns the hardware setup.
2. is about software / computer setup
3.  the code itself
4.  Connecting to a network (so we can call it an IoT project :-))

A step-by-step guide will follow in the text below and can also be studied [here](https://docs.pycom.io/gettingstarted/).

## Material


The material used for this prototype is the following:

| IoT thing                  | Purpose                                                                                                       |
| -------------------------- | ------------------------------------------------------------------------------------------------------------- |
| LoPy4 with headers         | The LoPy4 is Micropython-programmable quadruple bearer board. It works with LoRa, Sigfox, WiFi and Bluetooth. |
| Expansion board (v.3.1)           | Connect LoPy with USB, breadboard and others.                                                                 |
| Antennae                   | Connect with wireless networks (like WiFi)                                                                    |
| Breadboard                 | make electrical connections between components- like resistors, LEDs, capacitors etc                          |
| Temperature sensor MCP9700 | To obtain temperature data                                                                                    |
| LEDs (1x red, 1x green)    | To indicate temprature with LEDs                                                                              |
| Micro USB cable            | Connect with PC                                                                                               |

And here is an image from my desk:
![](https://i.imgur.com/oCH7CyD.jpg)




All material was bought at https://www.electrokit.com/. The LoPy4 and Expansion board (with specs) can also be found at Pycom's official [website](https://pycom.io/product/lopy4/). Total cost approximetaly around 1,000 SEK (depending on how many sensors you want).

Worth mentioning, this is my very first IoT prototype ever made. I am sure there are many other brands/products you could go for but I found the Pycom bundle (LoPy 4 and expansion board) to be very good with great documentation allowing you to get up and running fairly fast and easy. Full documentation of the expansion board and LoPy 4 (which are used for this project) can be found [here](https://docs.pycom.io/gitbook/assets/expansion3-specsheet-1.pdf).

The primary use of the expansion board, in this case, is to use the USB port to transfer code from the IDE to the device (described below), the battery connector (to have it wireless), and connect jumper wires.

![](https://i.imgur.com/RfzF4yc.png)

As mentioned earlier, LoPy 4 is used. LoPy allows you to connect over Wifi, Lora, Sigfox, LTE, and Bluetooth. For this project, Wifi connection is used primarily and the antenna is connected as seen in the image below.

![](https://i.imgur.com/J1vKwsz.png)


The expansion board and LoPy is then mounted together as seen in image below. 

![](https://i.imgur.com/syoKQAN.jpg)





## Computer setup

Before connecting the module (the LoPy) to an Expansion Board 3.0, its recommended to update the firmware on the Expansion Board. However, since I just got the expansion board, it was already updated to the latest version. But if needed, documentation can be found [here](https://docs.pycom.io/pytrackpysense/installation/firmware/).

### Chosen IDE - Atom
The programming is done in the "hackable text editor for the 21st Century", aka., [Atom.io](https://atom.io/). For this project, I found Atom to be very easy to get started with, i.e., very beginner-friendly. Clean and smart interface for the code and easy to plug in the packages needed. Atom also comes with a built-in Git and GitHub integration, to allow for collaboration on coding projects in real-time via the Teletype package. 

Other IDE's include MS VSCode among others.

### Setting up Atom and Pymakr plugin


To make it easy, Pycom has developed a plugin for two popular text editors, called Pymakr. This plugin is available in Atom.

After installing Atom you simply Navigate to the Install page, via **Atom > Preferences > Install.**

In the install tab, search for Pymakr and select the official Pycom Pymakr Plugin, see the image below. 

![](https://i.imgur.com/ZeFZQ2a.png)

That’s it! You’ve installed the Pymakr Plugin for Atom.

After installing the Pymakr Plugin, I had to configure it. The configuration was done with the following steps:

1. Connect your Pycom device to your computer via USB.
2. Pymakr has auto-connection enabled by default. In case your device hasn’t connected right away, click on Connect device and then on your device. Three arrows >>>, indicating that you are connected! 

This is how it looks like for me: 

![](https://i.imgur.com/7jvxuLV.png)

You are now ready to code and connect wires with sensors!


# Circuit wiring

The main sensor here is a TMP36 temperature sensor. The TMP36 has three leads. One is power, one is ground, and one is the analog signal. With the flat side facing you, power is on the left, the signal is in the middle, and the ground is on the right. See the image below for illustration.

![](https://i.imgur.com/xMm0yul.png)

Pycom provides a [wiring diagram](https://docs.pycom.io/datasheets/development/lopy4/) for the LoPy4, which can help you identify which pins provide analog inputs.

With the flat side of the TMP36 facing you, make the following connections:

1. Connect G3 (P16) on the expansion board to the middle pin on the TMP36.
2. Connect 3V3 on the expansion board to the left pin on the TMP36.
3. Connect GND on the expansion board to the right pin on the TMP36.

Looking like this: 
![](https://i.imgur.com/Rm3uzk9.png)


Here’s a summary of the most relevant specs of the temperature sensor:

1. Communicates over one-wire bus
2. Power supply range: 3.0V to 5.5V
3. Operating temperature range: -55ºC to +125ºC
4. Accuracy +/-0.5 ºC (between the range -10ºC to 85ºC)


# The code
The program we'll flash to the LoPy4 will simply read the sensor's temperature value at a certain time interval and print it to the console (Atom). Secondly, a target (or threshold) will be set where a temperature above this target will indicate that the person sitting on the toilet has an abnormal body temperature. After some testing (on myself) it was found that normal body temperature in this setting was 29°C. Thus if the temperature is above 29°C the person on the toilet will see a red light and the administrator monitoring the temperature can see in its dashboard (in this case Pybytes) the temperature spike.

Below is the code.



```

print("hello world")

## import packages ## 
import pycom
import time
import machine
import pybytes
pycom.heartbeat(False)

## insert pybyte provision snippet to connect with WiFi and dashboard##
pybytes.activate("eyJhIjoiYzIxMjAzNDEtYjM2OC00MTVhLTljNzMtNDFmMmQxNmY1OTc3IiwicyI6IkNPTUhFTV81ZWRhYzUiLCJwIjoiemdpYnRuM3UifQ==")

import _thread
from time import sleep

## enable temp. sensor ## 
adc = machine.ADC()
apin = adc.channel(pin='P16')

target = 29.00

while True:
  millivolts = apin.voltage()
  degC = (millivolts - 500.0) / 10.0
  print(millivolts)
  print(degC)
  pybytes.send_signal(1,degC) #sends temp.data to pybyte
  if degC > target:
      print("to high")
      pycom.rgbled(0xFF0000)  # Red led
  else:
    print ("all good")
    pycom.rgbled(0x00FF00)  # Green led
  time.sleep(3)

```

The first two lines are configuring the [analog input](https://docs.pycom.io/firmwareapi/pycom/machine/adc/) on P16. We then have a loop that repeatedly reads the analog input and prints its value. According to the TMP36's [datasheet](https://cdn-learn.adafruit.com/assets/assets/000/010/131/original/TMP35_36_37.pdf), the voltage it reports can be converted to Celsius using the formula (millivolts - 500) / 10.


As seen in the code, if degC (the measured temperature) is above the threshold two action happens - 1) it prints "to high" in the console and 2) the red LED light is turned on. 

The threshold is easy to configure depending on the person's normal temperature. 

When this is runned on the LoPy4, this is the following output in your console.

![](https://i.imgur.com/LOG8Dkp.png)



# Transmitting the data
## Setup to data management dashboard

For simplicity, the data is transmitted to Pycom's own data management platform - [Pybytes](https://pycom.io/products/software/pybytes-3/). Other alternatives includes [Losant](https://www.losant.com/) among others. 

Pybytes is a free cloud-based device management platform available for all Pycom development boards and modules (used in this tutorial for example). It works both on phone and  desktop and its fairly easy to do network selection, secure Over The Air (OTA) firmware updates and more.


My experience is that Pybytes is fairly easy to use. Following [this](https://docs.pycom.io/pybytes/connect/) documenation a device (the sensor we have just set-up) is added in Pybyte's dashboard. For this project my home wifi is set as primary network and LoRa as secondary (if wifi signal is lost).

The activation/provisioning snippet provided at the end of the set-up is copied and pasted into the Pymakr window. As seen in the code above. The second part of the code is essentially a way for the pymakr and sensor to talk with each other. Thirdly, the pybyte.send_signal() communicates the values to pybyte. "1" indicates that the signal's channel in Pybyte is 1. I will explain how that is interpreted in Pybyte.

In the end, if the configuration is successful, your pymakr window should look like this:

![](https://i.imgur.com/VSRZ1r9.png)

What we can see is that the MQTT protocol is used and a WiFi connection is established. MQTT is a lightweight messaging protocol that is ideal for sending small packets of data to and from IoT devices via WiFi.

Over in the Pybytes dashboard, we have a nice overview of the data and location. Location is provided by the wifi's address. This can be seen as a very good out-of-the-box feature in the case of monitoring elderly people's health as in the event of abnormal body temperature an HCP could be sent to the person's location for fast service.

![](https://i.imgur.com/StZpzqV.png)


Over in Pybyte, we have to press "signal". Here we will define a channel, its name, and its units. As seen in the image below. This is very suitable to do especially when we have multiple sensors with data in different units. In this case, however, we have only temperature data measured in Celcius.

![](https://i.imgur.com/zHEFjXm.png)

We can then (as healthcare admins) monitor the persons' body temperature and initiate an alert if the temperature should reach above a certain threshold. 

![](https://i.imgur.com/P7ies7w.png)


![](https://i.imgur.com/dPk8LyS.png)


# Wrap up

Click [here](https://photos.app.goo.gl/HZzgAixW74HKAJzC9) for full tutorial video and how all the parts looks together.

![](https://i.imgur.com/aJoEvJ6.png)

Worth emphasizing that this is just a prototype. For this to work in real life, and provide accurate temperature data, the sensor would have to be mounted more internally with the toilet ring. Nevertheless, it's a way to show that with fairly simple means even a beginner can try out micropython, IoT and explore how it could be used in healthcare. 

Moreover, for the monitoring part, it would have been beneficial if the data would be distributed to a cloud account (like Azure or AWS) to increase the accessibility for the organization monitoring. I found Pybyte to be easy to use but not that robust. Many times you have to reboot for it to work. This would not be sufficient for elderly care. 

Nonetheless, this was a fun intro project to IoT, micropython and Pycom. I highly recommend other beginners to try out. As a graduate in business and management interested in the field of technology, 5G, IoT, and what it means for businesses it was highly interesting to see how it's done in real terms.

## Future ideas
Looking at elderly care, it's a common problem that old people fall on the slippery bathroom floor and severely hurt themselves and even are there on the floor for longtime helpless. This happened to my grandpa and he was unconscious on the floor a whole night. My thinking here is that the toilet has a quite good hight. Meaning, if you are below the height of the toilet for too long, something is not right and an alarm could be triggered. Like a sensor that recognizes if the person is on the floor for too long.For this to work the person would need to carry like an arm wrist sensor that communicates its position, or relative height, with the sensor on the toilet ring. This sensor could maybe be some type of air pressure in combination with some accelerometer that can determine if the sensor accelerates quickly when one falls for example.