---
# System prepended metadata

title: Untitled

---

## Temperature sensor! 

Felix Långström Nygård (fl222wg)

This tutorial will guide you through the steps on how to build and connect a temperature (and humidity) sensor to a breadboard, read the data and upload it on a chosen platform for IoT projects. 

I am using windows 10, 64 bit.
Approximate time from start to finish is between 5-8 h.

### Objective

For me, this project has been about learning what IoT is, getting hands-on experience and understand what I can use it for, and learn for future projects. 
I had several ideas in the beginning of the course but due to some troubles with getting the hardware and my own schedule I came to the conclusion that I just wanted to get one (1) sensor working to understand the process of collecting data and sending it via a protocol to a web page. So in this project I am collecting temperature and data and sending it via MQTT to Datacake were it is visualized. 
In the future I hope to make different and bigger projects with the insights from this course. 

### Material

| Hardware (with link to where I got them) | Description | Price |
| -------- | -------- | -------- |
| [NodeMCU ESP32 Heltec](https://www.amazon.se/gp/product/B08243JHMW/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1)| The MCU I used for this project | 369 sek   |
| [Jumper wires and breadboard](https://www.amazon.se/gp/product/B01N4VCYUK/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1) | For connecting the sensor to the MCU | 110 sek| 
| [DHT11 sensor](https://www.kjell.com/se/produkter/el-verktyg/arduino/arduino-tillbehor/temperatur-och-luftfuktighetssensor-for-arduino-p87086) | Sensor that measures temperature and humidity, I will only send the temperature data to the platform! | 100 sek |
| [USB micro cable](https://https://www.webhallen.com/se/product/260293-iiglo-USB-2-0-Typ-A-till-mini-USB-Kabel-2m-Svart) | You need a cable that can transfer data! | 100 sek | 

:::success
:bulb:  **Regarding the material!**
You can use a different board for the project, but I will go through how to flash the heltec board to use microPython.
Note that it is important that your USB cable can transmit data, otherwise you will definately run into some errors.
:::

### Computer setup

#### Download IDE and node.js

The first step is to download the current (!) version of node.js which is needed for a plugin, the link is [here](https://nodejs.org/en/). 

Next, you need to decide what IDE to use. I used Atom since that was used in the tutorials and it was really easy to follow. You can get Atom [here](https://https://atom.io/)!
Now, open Atom, go to "settings" in the right lower corner, then in the menu, go to 'install' and search for 'pymakr' and install that. 


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

#### Update firmware

Before we can upload anything to the board we need to uptade the firmware and flash it so that we can use microPython.
I will link the guide I followed, provided by Linnéuniversitetet, in the course 1DT305 - Introduction to Applied IoT, 2022. The first one is for windows - this is the guide you should follow.
Note that I needed to keep the PRG button pushed through the whole flashing process to get it to work, even though there is no comment about that in the guide. If you follow the guide and it doesn't work, try doing that. [Here is the link](https://hackmd.io/@lnu-iot/By5ZUqvOq)!

### Putting everything together

Here is how the board should be connected! Before you connect to anything make sure the wires are in their right places!! 

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

You need to connect the sensors data output to a GIOP pin on the heltec board, for example nr 32, which you will see in the code, is 'P19'.
The colors describe what goes where. 
The power cable is connected to a 3 voltage output. 

### The code

Make a folder somewhere in your computer that you can easily find, and name it, for example dht11.
The code and libraries can be most often be found online for different sensors. Since I used the DHT11 sensor I got code for that. When in Atom, open your created folder and right click on it and then 'create file'. Name it main.py -> this is where your main loop will be!  

[Here is a link](https://github.com/iot-lnu/applied-iot) to a github repository where you can find code to loads of different sensors. I used the one for dht11 and modified it a bit o suit the project. You can either just open that folder and use the code, or write your own in the 'main' file. 

When you are ready to upload the program to the board, plug it in to the computer, and press this upload button.
![](https://i.imgur.com/mCg6Anm.jpg)

In Atom, you sometimes have to chose the project again, you will notice if you in the terminal get the error 'No files to upload'. 

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


### Transmitting the data / connectivity
Since I'm measuring data inside my appartment, I thought is best suited for this project to use Wifi. It is also a realatively simply to get working. I'm using MQTT protocol to transfer the data.

To be able to connect to your WiFi, create another file in the same way and name it boot.py, and paste this code;

```python=0
import network
import time
# setup as a station
wlan = network.WLAN(mode=network.WLAN.STA)
wlan.connect('Your WiFi', auth=(network.WLAN.WPA2, 'the password to your Wifi'))
while not wlan.isconnected():
time.sleep_ms(50)
print(wlan.ifconfig())

# now use socket as usual

```
In main you need this code to connect to Datacake and send data via MQTT:

```python=0
from mqtt import MQTTClient
import time
import ujson
import machine
from machine import Pin
from dht import DHT # https://github.com/JurassicPork/DHT_PyCom
#import ssd1306
#from machine import I2C

def sub_cb(topic, msg):
   print(msg)

# MQTT Setup
client = MQTTClient('Banan123',   
                    'mqtt.datacake.co',
         user='df19ad2c3134104f6814c9545feaa43e9c1f0988', #datacake IP token
                    password='df19ad2c3134104f6814c9545feaa43e9c1f0988', #go to profile, edit API
                    port=1883)

client.set_callback(sub_cb)
client.connect()
print('connected')
```

The MQTT client is you serial number for your device. The user and password is under your profile name, then 'Edit profile' and unmark the stars from the popup window, past the code under user and password.

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


Since I use the program for free I will only send data every 15 min, if you aren't paying for Datacake, you can only get 500 data points/24 h. 

### Platform and presenting the data

I chose the platform Datacake to visualize my data. It was pretty simply to use, again, so I stuck with it! You have a dashboard which you can easily manage and design how you want to present the data. Press the marked button both to enable editing and saving.

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

There is no locally saved data with this platform, it is all in the cloud. 
You can send a message via mail (free) to notify when it is a certain degree! Go to 'Rules' in the menu field. 

Datacake uses an MQTT broker which allows us to send and publish data, it is good for real-time data. 

 ```python=56
payload = temperature
client.publish(topic=my_topic, msg=str(payload)) #message is value
```
So here we are sending a string with the temperature. To get the data correct at Datacake you need to go in under 'Configuration' 
First you have to make an account, [here](https://datacake.co/)! 
Then you can add up to 2 devices for free. 
To scale this project I would need to subscribe, pay, to do so, but that would mean that I could connect several more sensors and collect more data, for example if I wanted a sensor in every room, or program so that my ac turns on at a certain temperature!

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

The data is saved from last time you connected and sent data. I haven't tried making a graph for example, but it could be possible that the data is saved so that you can see from a set day what the temperature has been. 

### Finalizing the design

So, despite the little time I had at the end I am very pleased to have been able to send data to the intehttps://datacake.co/rnet and visualize it. 
In my future projects I will probably build some kind of cage to the IoT device - I'm not a fan of the rustic and raw look... 
I would also like to add that I read both temperature and humidity but due to my poor time management and working at the same time I didn't send that data to datacake.
But all in all, it was a fun experience! 

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