# Remote Desktop Temperature and Fire Monitor
###### tags: `IoT`, `MicroPython` , `Pycom`, `lopy`
by Malek Darwish (md222xq)
### Introduction
This tutorial will demonstrate how to build a temperature and fire monitor that is internet connected. Data gathered from this device is presented on a dashboard for easy monitoring of a remote desktop. Alerts are sent out to the user when conditions are met.
This device will be placed inside of a desktop tower case, this is to observe the inner conditions such as temperature, humididty, and most importantly to detect fire.
This project may take around 5 hours to complete, depending on the amount of bugs faced..
### Objective
The idea for this device came from the fact that I most often have a computer running as a remote desktop that I connect to from other devices when I am away from home. A remote desktop can useful as it gives access to a complete PC from any device and anywhere. This is useful as it means I can carry around a thin and light laptop/iPad but still have access to a full workstation PC, allowing for more power intensive work to be done from anywhere.
However, the fact that I might not have direct control to a remote desktop, or awareness of what is happening around/within it is a little disturbing.
This is where the Temperature & Fire Monitor comes into play!
Using this device I am able to monitor temperatures, humidity and most importantly fire!
Lets move on to the materials used,
### Materials
To build this device we need the following;
- Microcontroller
- Temperature Sensor
- Fire (IR) Sensor
- Power adapter
- Breadboard, jumper-cables
For the microcontroller I decided to go with the Pycom Lopy4. This microcontroller can be programmed by MicroPython and has built-in WiFi, which is perfect for our needs.
For the temperature sensor I will be using the DHT11, which is a basic temperature + humidity sensor. This sensor is very cheap and convenient.
The fire sensor we will be using is a KY-026 flame sensor, this sensor detects infared light emitted by fire which allows us to detect fire.
And lastly as for the power adapter and breadboard I used what I had lying around at home. This ended up being a standard 5V power supply and a generic breadboard with jumper-cables.
- Bill Of Materials
| Compontents | Price | Link |
| ----------------- |:----------------------- |------------|
| Pycom Lopy4 with Expansion board | 949 SEK |https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/|
| Temperature-Humidity Sensor | 49 SEK |https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/|
| Flame Sensor | 36 SEK |https://www.electrokit.com/produkt/eldsensor-ir-760-1100nm/|
| 5V Power Adapter | 149 SEK |https://www.electrokit.com/produkt/batterieliminator-5v-3a/|
| Breadboard, Jumper-cables | 0 SEK ||
## Getting Started
This section will contain information about the IDE and how to get started.
#### Visial Studio Code
We will be using VS Code since I already use this text editor and it supports plenty of plugins which makes our lives much easier, in this case we will be using a plugin call Pymakr which will allow us to interface with the pycom.
Start off by downloading and installing [Visual Studio Code][vsCode] for your operating system.
#### Node.js
Next off we need to install Node.js, this is a JavaScript runtime environment which we will be needing since the Pymakr plugin we will shortly be installing needs it.
Get Node.js [from this link][nodeJs].
#### Pymakr
Pymakr is the plugin which will allow us to communicate with our Pycom.
Pymakr adds a REPL console to the terminal and allows us to upload directly to the pycom.

First click on the search bar to search for plugins, then click on install (in my case it says uninstall because I already installed it)
Then you should be ready to start communicating with your pycom.
#### Pycom
We will be using a Pycom Lopy4 with an extension board, this will give us access to a micro-usb port which we will use to connect to our PC.
Start off by connecting your Pycom expansion board to your PC with a micro-usb cable cable.

It should look like this when connecting.
#### Circuit Diagram
<!-- 
-->

In the diagram above we can see three modules:
(left to right)
-Buzzer
-Temperature-humidity sensor
-Flame sensor
The breadboard is connected to the VIN and Ground pins on the expansion board to supply the other modules with power. The breadboard is connected to a 5V powersupply which is powering both the pycom and modules.
The buzzer sensor was initially used and kept as backup, however it is not currently being used.
The temperature and humidity sensor is connected to pin 13 and uses the 5V rails for power from the breadboard.
The flame sensor is connected to pin 23 and uses the 5v from the breadboard.
Since all modules used are directly compatible and support the volates used(3.3-5v) no resistor or other components are neccessary.
#### Database Platform
For our database we will be using Ubidots.
Ubidots offers a free for Stem membership which is what we will use.
We decided to choose Ubidots as it offers a very simple to use user dashboard which displays all data neatly. Furthermore Ubidots offers simple to use events(triggers) functionality which allows us to setup actions to be performed based on data output from our device.
## The code
This section will discuss about the code we used to make this device.
First lets start off with the file structure:

As can be seen we have a folder called "lib" that contains two python files.
And in the root directory we have two python scripts, boot.py and main.py.
The 'lib' folder stands for library and contains scripts necessary for the temperature-humidity sensor, this is the dht.py file. And the other file urequests.py is used to upload data to Unidots(our database and dashboard)
The boot.py contains code that will be run first upon first startup followed then by the main.py.
- boot.py:
```python
import machine
from machine import UART
import os
uart = UART(0, baudrate=115200)
os.dupterm(uart)
machine.main('main.py')
```
Our boot.py contains code that sets up our baudrate.
The main logic takes places in the main.py, this is where we are doing the uploading of data, and reading data from our modules.
We first start off by connecting to wifi;
```python=3.9.5
wlan = WLAN(mode=WLAN.STA)
wlan.antenna(WLAN.INT_ANT)
# Wifi Credentials
wlan.connect("SSID", auth=(WLAN.WPA2, "PASSWORD"), timeout=5000)
while not wlan.isconnected ():
machine.idle()
```
After that we have two functions which help with preparing and uploading data to our database (Ubidots) this code was provided by Ubidots and then slightly modified to suit our needs better.
```python=
#Flame sensor at pin 13, this pin is set to anolog
adc = ADC(bits=10)
flameSensor = adc.channel(attn=ADC.ATTN_11DB, pin='P13')
#Temperature and Humidity sensor, uses DHT library
tempHumidSensor = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0)
#Buzzer, uses PWM to control buzzer frequency
while True:
pwmBuzzer = PWM(0, frequency=0)
buzzer = pwmBuzzer.channel(0, pin='P12', duty_cycle=0.3)
flameValue = flameSensor()
tempHumidReading = tempHumidSensor.read()
if tempHumidReading.is_valid():
print(tempHumidReading.temperature)
print(tempHumidReading.humidity)
data = build_json( "Fire", flameValue, "Temperature", tempHumidReading.temperature,"Humidity", tempHumidReading.humidity)
post_var(data)
time.sleep(15)
```
So looking at our code we see that we are first initialising our modules at the designated pins.
Then we have a loop that runs forever, inside this loop we are collecting data and preparing it to upload.
Before uploading the data we first prepare a json using the build_json() function then upload the data using post_var().
After all the data has been collected and uploaded we sleep for 15 seconds, then restart the loop over and over again.
#### Uploading the Code
Once the code is ready all we need to do to upload the code to out Pycom is to make sure our usb cable is connected and Pycom is connected successfully.

Then we need to click on the upload button to upload the code to out pycom. There is no need to compile the code as it is automatically compiled on device thanks to MicroPython.
## Data Transmission
Data will be transmitted over Wifi.
The data is first parsed into a json which is then uploaded using HTTP protocol POST. In the HTTP transmission the json along with a header including my Ubidots Token are uploaded.
The Ubidots token is a personal token used to identify the datas origin, this token must be secured so that unwanted parties do not upload data to your database.
We use Ubidots provided libraries along with a slightly modified code to parse the data build_json() and then transmit the data post_var()
```python=
#Unidot provided code
# Builds the json to send the request
def build_json(Data, value, Data2, value2,Data3, value3):
try:
data = {
Data: value,
Data2: value2,
Data3: value3
}
return data
except:
return None
# Sends the request. Please reference the REST API reference https://ubidots.com/docs/api/
def post_var(data):
try:
url = "https://industrial.api.ubidots.com/"
url = url + "api/v1.6/devices/" + "pycom"
headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}
if data is not None:
print(data)
req = requests.post(url=url, headers=headers, json=data)
return req.json()
else:
pass
except:
pass
```
Since the device is stationary and placed inside a desktop PC case there was no need to worry about power draw or wifi range.
## Data Presentation
For our database we will be using Ubidots. Ubidots was chosen due to their simple yet functional platform. Data can be uploaded directly from the pycom using Unidots provided starter code and then displayed on the Unidot dashboard. Events can also be set up which allows for alerts to be setup which is useful for our device.
We will be using Unidots Stem membership which is free and stores data for upto 30 days, more than enough for our usecase.

This is what our dashboard looks like. We are able to monitor the humidity, temperature, and a fire guage.
The fire guage displays data from the flame sensor, it scales on a ranges from 1024 to 0 due to us using 10bit analog input.
When there is a flame within range the the flame sensor outputs a value depending on the range of the flame from the sensor and the intensity.
Combining these data metrics allows us to have a better idea of what is going on inside of our desktop case.
(note: The humidity and temperature charts are a little over the place due to me testing the sensors.)
What is great with Ubidots is their Events functionality which allows us to setup free alerts based on certain rules.

Using data from our modules we can set up rules that are triggered when a modules data reading crosses a certain value.
For example I have a SMS event which sends me a SMS message when fire is detected.

I also have other rules set up when the temperature is too high to alert me.
Not only is there SMS notification but also email, voice call, slack, and many more.
This means I can be alerted at anytime if there is anything that could be going wrong.
## Conculusions

Side view showing the sensors and wiring.

Top down view, (Disclaimer: Be very careful around exposed live AC power, risk of electric shock! This photo was taken only for demonstration of the device outside of my PC as its dark in there.)

This photo demonstrates the Flame detector at work, Once there is a flame within range the sensor detects the fire and outputs a signal. (you can notice that now both leds are lit up, compared to the first photo where only one led(right) was lit.)
This device was super fun to build, throughout the project I had different ideas on how to improve and change the device. This tutorial only demonostrated the mvp product due to several factors. However, there are already some more ideas I have in mind on how to improve it, these include adding a relay which is able to disconnect my PC from mains power. This would add another layer of safety. Other improvements could also include automatic fire extinguishing, but maybe we leave that for when I learn a bit more.
Thank you for reading this tutorial, hope you learned thing something new or got inspired!