---
# System prepended metadata

title: Tutorial on how to make a sensor connected via WiFi

---

# Tutorial on how to make a sensor connected via WiFi

> This tutorial is made by Julia Ek (je224bw).

- Table of Content
[ToC]

### Introduction


Living in an attic apartment in the summer can sometimes get very hot. When you also have a pet it can be stressful to leave them alone at home during a hot summer day which is why I wanted to develop this sensing unit. This way I can have knowledge about the temperature at home and relax when it is not so bad, or go home to take care of the pet if it reaches critical temperatures. This is however only one example or what you can use this device for, maybe you want to know the temperature because of another reason or you might just simply be interested to know.

This tutorial aims to show how you can build a sensor unit at home for measuring the temperature. The data is then sent through a network and displayed on a dashboard so that you can have knowledge about what is going on at home. The data is also stored so you can not only see the temperature in real time but also look back in time. Another feature that I am going to make is a notification to your phone when the temperature reaches a certain value.

Following the tutorial you can have your device ready in just a couple of days.

Lets get started!


### Material
Some material is needed to build this device, so I am going to do a list of the items needed and what they cost roughly. A computer is also needed to program the microcontroller. It is worth mentioning that the microcontroller used in my project (FiPy) can be replaced by another model, same for the sensors. It does not have to be the exact same as I bought, but just make sure the items you buy fulfill your needs.

**List of materials**
- Fipy, 730 SEK
- PySense, 360 SEK
- micro-USB cable that can transfer data, 30 SEK
- male to male jumper wires 30 SEK
- breadboard 30 SEK
- Possibly batteries if you are going to place it where you will not access to a wall plug

These items are available at electrokit.com but they are also sold in other places.

**Fipy:**
This device is a microcontroller which is basically like a small computer. It has a processor, a memory and inputs/outputs that you can connect to other units, like sensors.

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


**PySense:**
The Pysense is a sensor shield, which is a board that contains extra pins so you can easier connect more sensors to your microcontroller. It also contains some sensors itself namely temperature, humidity, ambient light, pressure and accelerometer. I want to use it for the temperature sensor and that can operate between -40°C to 85°C which is in the range for my application.

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


### The setup

**Software**

First you need to install [Node js](https://nodejs.org/en/) and [Atom](https://atom.io/). Node js needed for the pymakr plugin and Atom is the IDE that we will use. When Atom is downloaded, open the program and click on File >> Settings >> Install. Search for pymakr in the search bar and install that. Finally we need to install a PyCom Firmware Update application to be able to update the device, and it is found [here](https://software.pycom.io/findupgrade?product=pycom-firmware-updater&type=all&platform=win32&redirect=true). Instructions for updating the device is written down below.
- Connect the device to the computer (For now the FiPy is placed on top of the PySense, see the picture down below for how to place them. Then plug in the device to the computer using the micro-USB cable.)

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

- Open the file that was downloaded from the link
- Press continue twice
- Choose the right port and under Type choose 'developement'
- Click the 'Erase during update' box
![](https://i.imgur.com/PGHRWtr.png)


Now create a folder with your project and open it in Atom. You should be able to see it in the left box. Next, check if the device is able to connect. It should say connecting to COMX... And then you should get three arrows like >>>. For me this did not work the first time and I had to do the update again from step one, and this time check all three reset boxes.

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

Make sure you have selected your project folder to the upper left (see picture below). When you later on have written some code you upload it to the device by clicking on the upload button to the left of the terminal (see picture below). Do not forget to save first or the old code will run.

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



**Hardware**

The FiPy gets hot when it is running and since we are measuring temperature with the PySense it can cause problems when the FiPy and the PySense is placed on top of each other. I solved this problem by simply seperating them and connecting them in a different way using a breadboard shown below.

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


### Connectivity

Now it is time to connect the device to a network. I chose WiFi because my device is always going to be at home and it is therefore a good option since WiFi is always available there.

Create four files inside your project folder called boot.py, main.py, config.py and mqtt.py. In boot.py we will write the code for connecting to the network and in config.py we will provide the WiFi credentials. When typing your credentials make sure it is a 2.4GHz network, or the device will not be able to connect.

Some libraries are also needed for the code to work. Create a folder inside the project folder named lib and place the files necessary inside it. The project folder should look like this at the end.
![](https://i.imgur.com/U0oavEn.png)

### Platform

The plaform chosen for this project was Datacake because it is simple and had all the functionalities I needed, like being able to notify me when the temperature reaches a certain value. Furthermore, in the 'History' tab you can look at data that has been saved in the database. You can choose to look at data up to one year before which means the data is stored for one year. You can also choose a resolution of 1 min so that is how often the data is stored. This is good to know in case you need to store data for a longer time or you want a high resolution. In my case, this is more than I need so this solution works good for me. 

To set up the platform you go to the website of Datacake and create an account. Once you are in go to Devices and follow these steps:
- Click API >> Next
- Select New product and give it a name >> Next
- Fill in a serial number of your chosing and give your device a name >> Next
- Choose a plan (I chose the free plan for now) >> Add 1 device

Now you get directed to the page of your device. To configure your device go to configuration and scroll down until you see the title 'Fields' and click 'Add Field'. Put the information:
- Type: Float
- Name: Temperature
- Unit: °C

If the device is going to send data to the platform we need to provide the topic. Under 'Fields' you will find Integrations. Click on configure and copy the string according to the picture down below and paste it in config.py where it says TOPIC and replace [FIELD_NAME] with TEMPERATURE. Type your serial number and token in the file as well.

```javascript=
SERIAL_NUMBER = 'your serial number'
MQTT_BROKER = 'mqtt.datacake.co'
TOKEN = 'your token'
PORT = 1883
TOPIC = 'your topic'

```

The token can be found by clicking on your profile >> Edit profile >> API >> Show. Save all files in Atom and then upload the code to your device.

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


To create some cool visuals for your temperature data, go back to Datacake and go to dashboard, then click on the toggle button to the right and then 'Add Widget'. Select how you want to display your data. There is no right way of doing it but I am going to describe what I did to make my dashboard look like this:

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


Current temperature: This shows the current temperature

- Select 'Value'
- Basics >> Title: Current temperature
- Data >> Field: Temperature

Maximum temperature today: This shows the highest temperature measured today, so from midnight until current time.

- Select 'Value'
- Basics >> Title: Maximum temperature today
- Data >> Field: Temperature
- Timeframe >> Timerange Operation. Operation: Maximum. From: 00:00. Until: now. Timezone: W European Standard Time.

Temperature: This shows a chart over the temperature during last 24 hours.

- Select 'Chart'
- Basics >> Title: Temperature
- Data >> Field: Temperature
- Axis >> Domain From: Auto. Domain To: Auto
- Timeframe >> Day

Click on the toggle button again to save. Then I went into 'Rules' and added a new rule. I selected that if the temperature is equal to 30°C with a hysteresis of 1°C, send an email to me. This way it will alert me if the temperature reaches 30°C when I am not at home.

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


I chose to transmit the code every 20 minutes because there is no significant temperature change in that amount of time but still fairly often so that I can get notified quickly.


### The code


```javascript=16
py = Pycoproc()
si = SI7006A20(py)

while True:

    # Here, we publish the data to the broker, under topic
    temperature = si.temperature()
    print(str(temperature))
    client.publish(topic=my_topic, msg=str(temperature))
    client.check_msg()
    print("Send data to MQTT broker, sleeping for 20 minutes...")
    time.sleep(1200) # Wait 20 minutes (1200 seconds)
```

If the microcontroller is going to be able to talk to the temperature sensor we need to install a library for that. That is the 'SI7006A20'. From that we can make an object called "si" that we can get the temperature readings from by calling the method temperature(). In the while-loop we call the method, print it and send it to the MQTT broker that is going to transfer it to Datacake. Then the device goes to sleep for 20 minutes and the while loop is repeated again.

### The finished product
The end product worked as intended. Further improvements would be to make it look better by containing it in some box for example but here is how it looks for now.

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

The hardware setup

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

The hardware setup

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

The dashboard
