# Template for how to build a window blinds notifier
Alexander Werthén (aw223mc)
**This tutorial aimes to gather data about temperature and light change on the inside of a window to give recommendation about window blind adjustments. The time needed to build this setup is approximately 1 hour.**
**Objective**
The background to this project was a desire to have window blinds open for as long as possible, but at the same time avoid the temperature increase created by sunlight passing through the window during the hotter part of the year. With the use of a lopy4, connected to a temperature sensor and photoresistor, data can be collected and analyzed. As the measured light is at a max and at the same time the temperature increases, this would indicate a situation where blinds should be closed in order to avoid uncomfortable temperature levels inside the apartment.
**Material**
The material needed for this project is included in the "LNU – 1DT305 Tillämpad IoT – LoPy4 and sensors bundle" provided by Electrokit. This bundle includes more than what is needed for project, following is a list of the material that is needed:
| Device | Purpose | Price |
| -------- | -------- | -------- |
| Pycom - LoPy4 | Compute and internet comunication | Included in elctrokitbundle for 995 SEK |
| Pycom - Expansion board | Connecting hardware | Included in the bundle |
| USB cable | Connecting computer to Expansionboard | Included in the bundle |
| Breadboard | Wiring the sensors | Included in the bundle |
| MCP9700 | Temperature sensor | Included in the bundle |
| LDR | Photoresistor - measuring light value | Included in the bundle |
| Resistor 10K ohm | Photoresitor wiring | Included in the bundle |
| Jumper wires (8pcs) | Connecting sensor and expanssion board | Included in the bundle |
Central for this project is the Pycom LoPy4(See fig 1) which computes and communicates all collected data. The Project could be constructed without the expanssionboard and breadboard as they only serve to simplify the connection between sensors and LoPy4. However, figure 1 visualises how the pins are easier accessible with the expansionboard.

Figure. 1. LoPy4 connected to a expanssionboard. Pycom.io
**Computer setup**
The computer used in this tutorial is a Macbook Pro, macOS Catalin v10.15.4. To program the LoPy4, Atom was downloaded and installed to use as IDE. First time of running Atom the pymakr package has to be installed. This is done through setting/install and then searching for pymakr. Before coding, the latest firmware should be uploaded to the device. This is done by downloading the firmware updater from Pycoms website:
https://docs.pycom.io/gettingstarted/installation/firmwaretool/
At this stage it is also recommended to create a project in Pybytes, to provision a key that is later used to register the device when flashing firmware.
When firmware updater is installed, run the program and connect the device to the computer with the USB. At this stage Atom should be closed as it disturbs the firmware updater. The program then gives instruction for what to do when flashing the latest firmware. In the settings page the configuration should be as in figure 2. The "Force update Pybytes registration" is important in order to simplify the connection to Pybytes. When this tutorial was created firmware 1.20.2.RC10 was used for the LoPy4.

Figure. 2. Pycom firmware updater communication configuration. print-screen(2020.07.09)
After flashing the firmware and registration of the device it should be connected to the project page of pybytes and coding can commence on Atom. To upload the code to device, simply press the "upload project" on the terminal. As the program is uploaded, mark the "main.py" file and press "run file" on the terminal, with this the code will start.
**Putting everything together**
The hardware is connected according to figure 3. The basic principle is that both sensors are connected in a parallel connection to have the same voltage flowing through them. The voltage is 3.3v as that is the electric output pin chosen for this project. There are multiple pins that sensors could be connected to but it is important observe figure 4, as not all pins are inputs that is required for the sensors in this project. The temperature sensor is in the figure 3 configuration connected to P18 and the Photoresistor is connected to P14.

Figure. 3. Circuit diagram.

Figure. 4. Pin connections for LoPy4. pycom.io
**Platform**
For this project Pybytes was found to be suitable as the device is for home use where there is usually Wi-Fi and the collected data is relatively simple to analysis. With Pybytes the configuration of the system is simple and gives the user the possibility to visualies the collected data though graphs.
As the system uses Wi-Fi the possibilities to connect to a smart home hub is something to consider, but would require additional programming. This would also enable the use of the sensor data for other applications in the home as well as complimenting with more data from other sources to give a more accurate analysis for how to set up the window blinds.
For further development there are more usage of this kind of device. The use of this kind of device configuration and data collection could also be appropriate in other areas where you want to monitor how weather affects the indoor climate.
**The code**
Create a folder called "Smart window", in this folder you should also create a "main.py". With the setup configured on pybytes a file called "pybytes_config.json" will be generated to this folder. When creating the file it is important that it ends with ".py" for Atom to understand that it is a program.
```
# @title: Smart window
#
# This code is used for a Pycom LoPy4 device and serves to collect
# temperature and light data, compute it and send it to pybytes.
#
# @author: Alexander Werthén
# @date: 2020/07/09
# @ver: 1.0
#Import necessary libraries
import time
import pycom
import machine
from machine import Pin
import _thread
# Photoresistor configuration
adc = machine.ADC(bits=10) # create an ADC object, number of bits defines the range gives
apin = adc.channel(pin='P13', attn = machine.ADC.ATTN_11DB) # create an analog pin on P13 where the data come in
# Temperature configuration
adc_2 = machine.ADC() # create an ADC object without bit count as it is not needed
apin_2 = adc_2.channel(pin='P18') # create an analog pin on P18
while True: # creates a iteration of data collection based on if there is a signal recived from pins
# Read and print temperature and light
millivolts_1 = apin_2.voltage() # collects the first value of millivolts
degC_1 = ((millivolts_1 - 500.0) / 10.0) # converts millivolts to temperature(Celcius)
print(str(degC_1)+"C value 1") # print the first temperature
light_1 = apin() # collects the first light value
print(str(light_1)+ " Light value 1") # prints the first light value
time.sleep(300) # the iteration is paused for 5 minutes
millivolts_2 = apin_2.voltage() # collects the second value of millivolts
degC_2 = ((millivolts_2 - 500.0) / 10.0)
diffC = degC_2 - degC_1 # to see the change in temperature the second value is subratcted with the first
print(str(degC_2)+"C value 2")
print(str(diffC)+" temp diff")
if diffC >= 0: # if statement presenting if the temperature is increaseing or decreasing
print("temperature increses")
else:
print("Temperaturen decreases")
light_2 = apin()
print(str(light_2)+ " Light value 2")
print("................... ")
pybytes.send_signal(1, degC_2) # sends temperature signal to pybytes on signal 1
pybytes.send_signal(2, light_2) # sends light signal to pybytes on signal 2
pybytes.send_signal(3, diffC) # sends temperature difference signal to pybytes on signal 3
time.sleep(300) # system sleeps for 5 minutes before the code is run again
```
The code is pretty straight forward. Starting of with importing the libraries needed to run the code. Then the signal inputs are configured. From the configured inputs the data is collected iteratively in a while loop and then computed.
The computations in this code firstly serves to convert the milivolts to temperature. As the code collects data each 5 minutes the difference between each temperature collection is measured. This servers to see if temperature is increasing och decreasing. Would there be be an increase in temperature it could be analysed that the window blinds should be closed.
The photoresistor only sends the value of the last measured light value to be able to see if sunlight is affecting the temperature difference or not. If the light value is not at a max, then it can not be assumed that the temperature increase due to sunlight.
The data is lastly sent every 10 minutes to Pybytes for presentation and analysis.
**Transmitting the data / connectivity**
As the device is for home use Wi-Fi seemed to be the obvious choice do to it's convenience and the relative stability with a Wi-Fi network. Since a drastic increase in temperature was looked for, it was assumed to be possible to measure a difference in temperature with 5 minute intervals. as well as the increase in temperature being acceptable before action was required. The data would then be transmitted to Pybytes every 10 minutes. Another major benefit with Pybytes is the integration possibilities provided.
**Presenting the data**
In Pybytes the data is visualized in 3 graphs on the dashboard, temperature, light and temperature differens. From this page it is possible to analysis if there is a dramatic increase in temperature due to sunlight passing through the window. Figure 5 and 6 shows the visual presentation on the dashboard of the Pybytes platform with this configuration. **IMPORTANT:** Figure 5 and 6 are from the configuration stage of the project and the data do not represent the current code.

Figure. 5. Dashboard for device part 1. pycom.io

Figure. 6. Dashboard for device part 2. pycom.io
Pybytes is free of charge for 5 mb the first 6 months, which is enough in this project and time span.
**Finalizing the design**
Summarizing this tutorial and giving some thought about the course it's been really interesting. Going in to this I had no previous knowledge about programming or electrical gadgets. Starting of with just understanding the linguistics within the programming world and understanding the structure behind the coding was quite a steep hill to climb. But looking to the final result it is interesting to see how it was possible to learn how to build a IoT device during a 5 week time. The final setup, see figure 7, of my project is perhaps not so impressive when it comes to design but still hold all the functionalities it needs.

Figure. 7. light and temperature sensoring device. pycom.io
Further devloping this project there are alot more improvement that could be done. for instance Grafana and Node-RED could be added in the analysis and create more automated responses. But due to the complexety in those systems in comparison with my knowledge it was not possible to integrate them during set out time. Furthermore it would be interesting to use a engine of some sort to turn the blinds as a automated response to the increased temperature.
All in all a real interesting subject that i might further develop as this courses ends.