---
# System prepended metadata

title: Tutorial on How to Build a Thermometer that Saves data

---

# Tutorial on How to Build a Thermometer that Saves data

###### 2022-07-02
###### Julia Jakobsson, jj224eh

[ToC]


### Introduction

By following this tutorial, you will be able to use a temperature sensor to check and save the temperature. This tutorial is based on the pycom fipy and the pysense 2.0X extension board. By following this tutorial, you will be able to use the temperature sensor of the pysense to check and save the temperature from/to a computer or a phone.

It will take approximately 2 hours to complete the tutorial. 

### Objective 
About a year ago I started a search to buy a thermometer where I could check the temperature when I was not at home. I also wanted the thermometer to be able to store data so that I could check what temperature it had been a couple of days ago. 

When I couldn't find something that fit my want, I started to look into creating something simple myself and begun this project. 

The insight of this tutorial is a working thermometer that you can check from your phone/computer as well as an introduction to IoT with pycom. To check the temperature when you are not at home might be useful if you have pets, temperature sensitive medicine or other temperature sensitive objects at home.  


### Material
The materials I used to complete this project were:


|Name | Description| Link      | Price |Picture|
| -------- | -------- | -------- |-------- |-------- |
| [Pycom fipy development board](https://pycom.io/product/fipy/)  | Development board - this is the part you can program. | I bought mine [here](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-fipy-basic-bundle/) in a budle, but you can also buy it directly from the [pycom website](https://pycom.io/product/fipy/).   |€59.40 – €65.40|<img src="https://i.imgur.com/Wye0UNd.png" width="10000" />|
|[Pysense 2.0X expansion board](https://pycom.io/product/pysense-2-0-x/)|Expansion board - needed to connect your development board to your computer. This also contains the temperature sensor used.|I bought mine [here](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-fipy-basic-bundle/) in the bundle, but you, but you can also buy it directly from the [pycom website](https://pycom.io/product/pysense-2-0-x/).|€29.65|<img src="https://i.imgur.com/rw94fqT.png" width="10000" />|
|A USB cable capable of data transfer|Used to connect the device to your computer|I bought mine [here](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-fipy-basic-bundle/) in the bundle. |approximately €4||
|A bread board|Used to connect the development board to the expansion board.|I bought mine [here](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-fipy-basic-bundle/) in the bundle.|approximately €6.5||
|Jumper wire (male-male) I ended up using 9|Used to connect the development board to the expansion board.|I bought mine [here](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-fipy-basic-bundle/) in the bundle.|approximately €3|
|A computer (I used a MacBook)|To be able to program your development board.||||

Pictures from https://pycom.io/


### Computer Setup (MacBook OS)
To complete this project I used the following programs:
- Atom [https://atom.io/](https://atom.io/)
- Firmware Update program to update the development board [Download here](https://software.pycom.io/findupgrade?product=pycom-firmware-updater&type=all&platform=win32&redirect=true)
- Firmware Update program to update the expansion board [Download here](https://docs.pycom.io/updatefirmware/expansionboard/)
- Datacake [https://datacake.co/](https://datacake.co/)

#### Step by Step Computer Setup 
##### 1. Atom IDE
I chose to use the IDE (integrated development environment) Atom, this means that I write my code in Atom that is then uploaded to the board. The reason why I chose Atom was that it was recommended to me and after using it I found it beginner friendly. 

To install Atom I followed [this tutorial](https://hackmd.io/@lnu-iot/SydH7MTcw#Mac-OS). 
1. The first step is to install [Node js](https://nodejs.org/en/). 
2. Then install [Atom](https://atom.io/) and open the program.
3. To import [Pymakr](https://atom.io/packages/pymakr) (which will help the computer/atom to communicate with the board) press Atom, then Preferences... and lastly Install. Under Install Packages search for Pymakr and then press install.

#### 2. Update Firmware on Your Development Board
To update the firmware on my fipy I followed [this](https://hackmd.io/@lnu-iot/SJ91R_jSO) tutorial.

:::danger
**To interrupt the firmware update while it has started may damage your device.** Make sure this don't happen by having a fully loaded computer. Don't disconnect the board, push any buttons on the board or cancel the update when process has started.
:::

1. Connect your development board to your expansion board. If using the fipy and pysense it should look similar to this. The LED on the development board should be above the usb-port. 
<img src="https://i.imgur.com/rDZfcLT.png" width="400" />
Picture from https://pycom.io/product/fipy/

2. Connect the expansion board to your computer by the usb-cable. 
3. Download the Firmware Update [here](https://software.pycom.io/findupgrade?product=pycom-firmware-updater&type=all&platform=win32&redirect=true).
4. Open the file and press "Continue" twice. (I didn't need to press the "reset button".)
5. Under "Communication" choose Type - development. I didn't need to change anything for the port. Then press "Continue".
6. Under "Advance Settings" it should automatically sense your type of board, but make sure this is correct. Select File System: FatFS and check all the boxes (Erase during update, CONFIG partion & NVS Partition). Then press "Continue".
7. After the installation you can press "Done".

#### 3. Update Firmware on Your Expansion Board 
To use the sensors on the pysense 2.0x board I used the class [pycoproc2](https://docs.pycom.io/firmwareapi/pycom/expansionboards/pycoproc2/) which demands the board to be updated to version 16. If you want to use pycoproc2 and don't know the firmware version on your board you can try to run the code and if you need to update, you will get an error that will tell you. If you need to update, follow the directions below. 

I followed [this](https://docs.pycom.io/updatefirmware/expansionboard/) tutorial. 

0. Install Homebrew
My first step to update the firmware was to install a program called [Homebrew](https://brew.sh/). Homebrew is a package manager for macOS which help with the installment. To install Homebrew, I simply copied the installation line from Homebrews website and passed it in the terminal. Then I just pressed continue until the program was installed, which took a while. 

1. Download the DFU by clicking the correct link that match your expansion board. The links are found [here](https://docs.pycom.io/updatefirmware/expansionboard/)

<img src="https://i.imgur.com/fRbBv8J.png" width="200" />

2. To install the DFU write "brew install dfu-util" in the terminal. 
3. When the installation is done make sure the terminal is open in the same folder as where the DFU is. This can be done by closing the terminal, opening finder and mark the foler where the DFU is downloaded (do not enter the folder). Then press Finder --> Preferences --> Open in Terminal. 
4. Unplug your expansion board and remove the development board. 
5. Press the S1/MCLR button and hold while you plug in your board and wait one second before releasing the button. The placement of the S1/MCLR button can be found [here](https://docs.pycom.io/updatefirmware/expansionboard/).
6. After you have released the button you have approximately 7 seconds while the board is in "update mode". During this time type "sudo dfu-util -D pytrack_0.0.8.dfu" into the terminal but replace pytrack_0.0.8.dfu with the name of the dfu you downloaded. 
7. If you see Done! in the terminal, the update was successful!

### Putting everything together
It is possible to use the sensor while the development board directly attached to the expansion board. However, I found that the device got rather warm when it was used and to avoid this affecting the measured temperature I connected the breadboard like this: 
![](https://i.imgur.com/ZnpOTxL.png)
Picture taken from [here](https://hackmd.io/@lnu-iot/By-930cTu).





### Platform - Datacake
To present the data I chose the platform [Datacake](https://datacake.co/). I wanted something fairly simple that didn't need a paid subscription and was easily accessible on both my computer and my phone. Datacake is a cloud based platform and doesn't require you to download anything. 

With Datacake it is possible to easy connect the platform to your device and then display your data in many different forms. I chose to display the current temperature, the temperature of the past 24 hours and the temperature of the last week. 

To connect my device to Datacake I followed [this](https://hackmd.io/@lnu-iot/r1aui0B59) tutorial.


### The code
From the [Datacake tutorial](https://hackmd.io/@lnu-iot/r1aui0B59) I ended up with four code files, `main.py`, `boot.py`, `mqtt.py` and `config.py`. In mqtt.py is a MQTT library and help publish data to datacake. `boot.py` contains the code connecting the device to Wi-Fi. `config.py` contains the Wi-Fi and datacake information. In `main.py` I replaced the dummy data with this:

```python=16
while True:
    py = Pycoproc()
    si = SI7006A20(py)
    temperature = str(si.temperature())
    client.publish(topic=my_topic, msg=str(temperature))
    client.check_msg()
    print("Send data to MQTT broker, sleeping for 15 minutes...")
    time.sleep(900) # Wait 15 minutes (900 seconds)

```
The entire code can be found [here](https://github.com/juliajak/temperature_datacake/tree/main). 

### Transmitting the data / connectivity
Every 15 minutes the sensor measures the temperature and send to datacake. The device uses wifi to connect to datacake and the transport protocol used is MQTT. The choise of these two was that they seamed the easiest to use. The reason the measuring frequency isn't higher is to save power so that the battery will last longer. 


### Presenting the Data
In my dashboard I chose to include the current temperature, a diagram of the temperature of the day and a diagram of the last week. The data for the last 24 hours is saved and updated every 15 minutes. 
![](https://i.imgur.com/2KLBG0H.png)
Computer version

<img src="https://i.imgur.com/Azznr56.png" width="300" />

Phone version

### Finalizing the design

Here is what my project looks like. I am currently waiting for a battery pack which I will connect so that freely move the device as it then no longer needs to be connected to a computer. 
![](https://i.imgur.com/1Ujn4mu.jpg)







