---
# System prepended metadata

title: Creating a not so smart home

---

---
title: 'Creating a not so smart home'
disqus: hackmd
---

Measuring temperature and starting a fan
===
[TOC]

**Tutorial by Hans-Ove Åkesson ha223mr
 Estimated time is about an hour.**

The tutorial explains how to start from scratch and develope an internet of things device that is sending data with WiFi to a cloud known as Pybytes. The sensor that sends data is a temperature sensor which is built into the FiPy-device.

Objective
---
The objective is to develope a basic understanding of IoT which also includes coding and integrating hardware with software. The temperature data collected would be an indicator for purchasing a better airconditioner or a fan. I decided upon doing this project out of interest in IoT, coding and I wanted to design something to measure temperature because it was really hot in my apartment.

Material
---
All the material was from a [kit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-fipy-and-sensors-bundle/) which costs 1499.00 SEK. (Rougly €139.11).

These are the most important pieces of the kit and will be used in this tutorial.

| Name | Five sensor shield | Five-network |
| -------- | -------- | -------- |
| Link   | [Pysense 2.0X](https://www.arrow.com/en/products/pysense/pycom-ltd?utm_currency=EUR&utm_source=google&utm_campaign=16213278696&utm_medium=cpc&utm_term=pysense&gclid=Cj0KCQjw5ZSWBhCVARIsALERCvyFI98Q2vh1vu_iUj1Ez-lq75rUNo_Tpj5_6G1NjW8Ma-7E_rY-DN4aAm6PEALw_wcB&gclsrc=aw.ds) | [FiPy 1.0 with Headers](https://www.amazon.co.uk/Pycom-FiPy-Headers-networks-IoT/dp/B078Y48JF1) |
| Price | €24.72 | €116.98 |

The FiPy is the programmable device and the main component in this tutorial and the Pysense acts as a breadboard and sensor component to measure temperature.
The sensor name is SI7006A20 which is interegrated in pysense 2.0X.

Computer Setup
---
Start by installing a program that can run micropython code. This tutorial will use Atom as it is easy to understand and a lot better than most alternatives.
When Atom is installed on the right hand side choose Install a Package. Search for pymakr and install the addon. See figure 2.
![](https://i.imgur.com/8CDguIz.png) *Figure 1, Welcome Guide Atom.*
If for some reason you can't find the welcome guide, use [this](https://atom.io/packages/pymakr) link to download pymakr.
Once connected to a sensor it should look something like this.
![](https://i.imgur.com/ekThw4S.png)
*Figure 2, connected.*
Code is tested when clicking the play button and uploaded when clicking the button beneth it. 

Putting everything together
---
1. Attach the FiPy onto the pysense 2.0X board, make sure the pins allign with the holes and the LED-light should be on the same side as the micro-usb cable input and cover all the pin holes of the pysense 2.0X board.
3. Attach the micro usb at the "USB"-port on the pysense 2.0X board. is attached. See figure 3.
4. Connect the other part of the USB-cable into a device that has atom installed.
![](https://i.imgur.com/DbDdWVd.jpg)
*Figure 3, connected FiPy and pysense.*

Platform
---
This tutorial uses pybytes as it's platform which has the best and easiest functionally. It helps your FiPy connect towards this cloud platform with it's inbuilt function. It's also very easy to integrate with the software mainly because it requires some basic code. Pybytes also has some good widgets and graphs that are easily to implement.
This is only a basic setup and if more sensors are used this could prove ineffective and would require a external breadboard and wires.

The code
---
Now the bulk of the tutorial, the software part. Micropython code.
First of you need to import two different libraries which are not created by me but are located in [this github](https://github.com/pycom/pycom-libraries/tree/master/shields/lib). Download pycoproc_2.py and SI7006A20.py. Both are needed for the sensor to work with the code.
First line of interesting code is
```
if 'pybytes' in globals():
    if(pybytes.isconnected()):
        print('Uppkopplad till pybytes.')
        pybytes_kopplad = True
```
It searches the global function to see if the device is connected to pybytes.
The rest of the main function is pretty straight forward, it has a conditional statement that takes in a floatnumber from the sensor and return either a function that only prints the temperature or prints out a different statement that tells you to adjust the temperature externally through manual work. Both cases sends data to pybytes. Adjust the `if si.temperature() <25:` statement if you want to have a different temperature for the threshold.

```
while True:
    if si.temperature() <25:
        print("Temperaturen är: " + str(si.temperature())+ " grader.")
        pycom.rgbled(0xFF0000)
    else:
        print("Temperaturen är för varm. Det är " + str(si.temperature()) + " grader.")
        print("Bör sätta igång AC:n")
        pycom.rgbled(0x00FF00)
        time.sleep(40)

    if(pybytes_kopplad):
        pybytes.send_signal(1, str(si.temperature()))
    else:
        print("Inte uppkopplad till Pybytes, försöker igen")
        if 'pybytes' in globals():
            if(pybytes.isconnected()):
                pybytes_kopplad = True
                pybytes.send_signal(1, str(si.temperature()))
    time.sleep(5)
```
Transmitting the data / connectivity
---
This tutorial only uses WiFi to transmit data and this is done with the help of Pybytes. First you need to create a login account on pybytes. This is done [here.](https://sso.pycom.io/register?client_id=pycom&redirect_uri=https%3A%2F%2Fpyauth.pybytes.pycom.io%2Fauth_code%2Fcallback&scope=profile&response_type=code&state=pybytes-browser)

Secondly you need to go to settings tab and press the add new network button. Input your SSID name (WiFi-name) and password. See figure 4.
![](https://i.imgur.com/foNcYiU.png)
*Figure 4, connecting to the SSID.*

Then you connect your sensor through the devices tab. Now you can send data with pycom.

The data is sent through the `pybytes.send_signal(1, str(si.temperature()))` line, it sends the data each 5th second if colder than threshold and each 40th second if over the threshold. It's sent on the first pin towards Pybytes and that is decided through the first piece of code inside the parenthesis. Also changes the LED on the FiPy to match if colder or warmer.

The transport protocol is MQTT.

Presenting the data
---

Data is presented in a graphical way by using a simple inbuilt function of Pybytes.
Pybytes was chosen because of it's inbuilt function to connect to WiFi networks while not using code and has easy graphic solutions.
This is the finished dashboard for all data. There is the temperature, past hour data and the integerdata.
![](https://i.imgur.com/QF41aod.png)
*Figure 5, finished dashboard.*

Figure 6 displays temperature which is updated with every instance of sent data.
![](https://i.imgur.com/9uYWsKR.png)
*Figure 6, temperature.*

Figure 7 describes data recieved per instance of sent data.
![](https://i.imgur.com/OW2VtRZ.png)
*Figure 7, data recieved.*

Data is saved for a month in the free version of pybytes and also as described above the data is saved or sent with different intervalls depending on the threshold.

Finalizing the design
---
The product looks something like this and is easily modified to include other type of sensors with a breadboard and some cables.
![](https://i.imgur.com/K9tCNRU.jpg)
*Figure 8, finished product, over threshold.*

The project was difficult at first but with time it became easier. However what I initially wanted to accomplish was a sensor that measured temperature and initiated a fan or AC but I got the sensors kind of late and I had a lot of trouble at first when I was using VSC instead of Atom but after switching it started working fluently.

Hopefully this tutorial gave you an insight and interest in IoT.

