# **Balcony-monitor**
By André du Rietz
project part of applied IoT-course
Linneuniversity Sweden
Studentid: ad222ja
---
**Overview:**
The project consists of two sensors that are able to measure temperature, humidity and light intensity that are intended to monitor the climate on my balcony for better understanding my plants located there.
**Time required:**
For someone with some experience with electronics this project would take less then 2-3 hours total.
**Objective:**
Since I have a balcony facing southeast I can grow plants there and are currently growing tomatoes, cucumber, chilies, strawberries etc...
But some of the more warmth and light-loving plants (looking at you tomatoes) are doing less then ideal. Therefore my project idea came to be a sensor setup to monitor the temperatue, humidty and light on my balcony to better understand my plants and their needs.
**Bill of materials:**
* Lopy4 (microcontroller)
* Expansionboard 3.1 (connect lopy4 to)
* Mini breadboard (for connecting sensor to)
* TSL2591 (light sensor)
* DHT11 (temp/humid sensor)
* Some wires
Cost: ~900 kr (~88€)
All of the materials were bought at [electrokit](https://www.electrokit.com/)
**Wiring the hardware:**
Since both sensors run on 3.3V and they all need to have common ground, both ground and 3.3V is provided from the microcontroller to the breadboard. Sensors were then connected to the breadboard for power and ground.
The DHT11 temperature and humidity sensor sends it data digitaly so it can be hooked up to any GPIO pin, I have chosen P23.
TSL2591 communicates via I2C which have 2 specified pins P9 (SDA) and P10 (SCL). SDA is for the data and SCL is for the synchronised clock. You also have some additional pins that you don't have to worry about that is;
3Vo, that is if you supply the sensor with 5V you can get 3.3V out to supply other things up to 100 mA.
INT, this is to set up interrupt features with the sensor, this is not used in this project.
Make sure you connect SDA<->SDA and SCL<->SCL on your microcontroller and sensor, not like RX-TX if you have previous experience with UART serial communication.
Schematic:

**Cloud-Platform**
For my cloud platform I chose Pybytes as it's free and easy to use and shows the data in a simple and compact format. To connect to pybytes you need to download pycom firmware updater and provision the board so you link the board with your device on pybytes, follow this [guide](https://docs.pycom.io/pybytes/gettingstarted/) for a full walkthrough how to get started with pybytes.
I connect to Pybytes via Wifi and the messages are sent using MQTT.
Since you get a free trial year with SigFox* when you register an account with a pycom device. I also tried connecting the device with SigFox which was rather simple using this [tutorial](https://hackmd.io/@lnu-iot/SyUxJU7pu). You have to make sure your device is in an area with [coverage](https://www.sigfox.com/en/coverage) before using SigFox.
Pybytes will stay free for however many devices you choose to connect, however is the amount of data that can be saved on the cloud pretty limited but can you pay for more. You can also connect pybytes via API to many other services to send the data elsewhere.
**To connect to SigFox you might need an antenna to amplify the signal*
**Software and code:**
I used Visual studio code for programming the microcontroller.
For a complete tutorial how to install VSCode with Node.js and pymakr extension that you need to connect it with your lopy4, follow this [tutorial](https://hackmd.io/a1Nq_9kqR0CZBrYL1xNJDg).
The code is quiet simple, its devided in 3 steps.
1. Initialize sensors as objects.
2. Read sensors and calculate light intensity from the readings from TSL2591. (This is done by a simple function from the library)
3. Print and send the readings from the sensors
Keep looping step 2 and 3 every 1800 sec (30min) or more often if you want, it's up to you as the user. However, make sure you don't excede the daily message limit if you're using SigFox (every 11 min)
```python=
import time
import tsl2591
from machine import Pin
from dht import DHT
#1 Initialization of sensors
dht_sensor = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0) # Temp/Humid sensor
tsl_sensor = tsl2591.Tsl2591(0) # Lux sensor
#2 Reading sensor values
while True:
dht_value = dht_sensor.read() # Read dht values
full, ir = tsl_sensor.get_full_luminosity() # Read raw values (full spectrum and ir spectrum)
lux = tsl_sensor.calculate_lux(full, ir) # Convert raw values to lux
#3 Print data and send to pybytes
print('Temp:', dht_value.temperature)
print('Humidity:', dht_value.humidity)
print('Lux:', lux)
pybytes.send_signal(1,dht_value.temperature)
pybytes.send_signal(2,dht_value.humidity)
pybytes.send_signal(3,lux)
print('sensor reading finished')
time.sleep(1800) #Make measurement every half hour (or however often you want to read sensors)
```
*Important to know when working in VSCode is to make sure that you always save changes before running them on the board so you don't run the same thing over and over and wonder why nothing is changing. Also if you make changes to any other files then main you need to upload them to the board again to make sure that the changes are active on the board.*
The library for TSL2591 is a modified version of this [library](https://github.com/jfischer/micropython-tsl2591), I had to make some changes in to make it work. First of we had to put a 0 in the () when initilizing the object from the class. This we had to do because we need to give a value to sensor_id in the TSL2591 class, which was not mentioned in the library readme file.

We also had to change line 52->57 in the library, to make sure it connects properly via I2C. In the picture is the modified code that worked in the end.

(Thanks to TA David M for helping me edit the library)
For code and library downloads go to my github repository.
https://github.com/Bonebite/Balcony-monitor
**Dashboard:**
In Pybytes I have made a simple display of all the measured quantities in line charts of the latest 96 values. That will display the readings 2 days back in time.
Temperature:

Humidity:

Illuminance:

If you are wondering why illuminance is around 0. This is because while running tests and setting everything up, I have the sensors in a drawer to stop my cat from eating the wires.
**Summary:**
Now if you have followed the whole tutorial you will most likely have climate sensor setup for whatever room you like. I'm gonna place mine on the balcony, I hope you managed to follow along the tutorial and you learned something.
Here is a picture of my sensors on the balcony
