# Tutorial - Easy IOT Temperature monitor.
## :memo: Tutorial on how to build a room temperature monitor.
Room temperature monitor.
Kai Levin (kl223hu)
In this project, the goal is to create a simple room temperature monitor with Heltec Lora32 chip and MCP9700 sensor.
The project takes about 2 hours to do.
:rocket:
### Objective
The room temperature monitor is a simple concept which allows users to check the temperature of the room or any enviroment from mobile phone or PC with a WIFI connection.
I choose to do this project because of it is something useful in our everyday life.
### Material
1x Heltec Lora32 chip with Wifi connection
1x MCP9700 sensor
1x Breadboard
Jumper wire
1x Micro-USB to USB Cable
1x PC(Windows)
Registered account at Datacake.
WiFi connection
Everything can be bought at Electrokit.
## Putting everything together
1. Connect the Heltec chip to the breadboard. Shown in the picture.
2. Connect the positive voltage leg to 3V3.
3. Connect the ground leg to GND.
4. Connect the output leg to 39 (pin 16).

5. Connect the chip to the computer with a USB Cable.
6. Follow this https://hackmd.io/@lnu-iot/By5ZUqvOq To flush the heltec chip and make it ready to be programmed.
## Platform
When this project started, choosing the right platform was tough because there are so many awesome solutions out there. The most important factors were
ease of use
available documentation
price/subscription costs
After considering these factors, MQTT using (https://datacake.co/pricing) seemed like a perfect solution. Some of the functionality available on this platform are
Inbuilt easy to use dashboards
Straightforward and easy to use/understand documentation
Advanced services like weather, zapier
## The Code
The project contains:
main.py
config.py
boot.py
mqtt.py
lib
main.py:
```
from mqtt import MQTTClient
import time
import ujson
import machine
import config
def sub_cb(topic, msg):
print(msg)
client = MQTTClient(config.SERIAL_NUMBER,
config.MQTT_BROKER,
user=config.TOKEN,
password=config.TOKEN,
port=config.PORT)
client.set_callback(sub_cb)
client.connect()
print('connected to MQTT broker')
my_topic = config.TOPIC
while True:
adc = machine.ADC()
apin = adc.channel(pin='P16')
millivolts = apin.voltage()
celsius = (millivolts - 500.0) / 10.0
temperature = celsius
time.sleep(1)
client.publish(topic=my_topic, msg=str(temperature))
client.check_msg()
print("Send data to MQTT broker, sleeping for 2 minutes...")
time.sleep(3)
```
config.py:
```
WIFI_SSID = 'xxx'
WIFI_PASS = 'xxx'
SERIAL_NUMBER = 'DEVICE_SERIAL_NUMBER'
MQTT_BROKER = 'mqtt.datacake.co'
TOKEN = 'ACCOUNT_TOKEN'
PORT = 1883
TOPIC = 'TOPIC_TO_PUBLISH_TO'
```
boot.py:
```
import network
import time
import config
wlan = network.WLAN(mode=network.WLAN.STA)
wlan.connect(config.WIFI_SSID, auth=(network.WLAN.WPA2, config.WIFI_PASS))
while not wlan.isconnected():
time.sleep_ms(50)
print(wlan.ifconfig())
```
main.py includes the code to catch the data from the sensor and send it to Datacake.
boot.py and config.py includes the code for the Wifi connection. The SSID and Password need to be set manually.
mqtt.py is the source code for mqtt modul. https://raw.githubusercontent.com/pycom/pycom-libraries/master/lib/mqtt/mqtt.py
## Transmitting the data / connectivity
WiFi is used to transfer data. Data collected by the sensor is encapsulated within the IPv4 packet and send to the Datacake server by transport protocol MQTT.
This provides a reliable way to transfer the data. The home WiFi network provides enough bandwidth for the monitor.
The data is sent every 3 secs.
## Presenting the data
After registeration at Datacake.
Now that we can send data to Datacake, let’s build a simple dashboard. To do that, navigate to your device and select Dashboard.
Click on the toggle button to the far right and you will see some additional options.
Click on Add Widget and select Value.
Give it a name and select Data in the middle menu. Select the Field from which you want your Widget to fetch data from, in our case it is the Temperature field.
Finally, Click on Save and make sure to click on the toggle button to the far right again to save your dashboard.

## Finalizing the design
Great work! Now the project is finished!
Enjoy monitoring the temperature! :)
