# Monitoring humidity, temperature and light to have healthy condition for your plant #### by Mohammad zandkarimi (mz222ih) ### Overview ------------------------------ This project easily describes how you can set up your own IOT device to monitor the environment of your plant at home by monitoring humidity, temperature and light. It also involves few simple lines of code. By following this tutorial if you are compeltely new to this area it should take about 2-3 hours to set up everything but, if you are experienced with programming or IOT it could be done in less than 50 minutes. ### Objective ----------------------- I chose this project because when I moved to Sweden and bought my first plan it died in just 3 weeks! And of course it was due to climate which I didn't use to! With this project you can monitor the humidity, temperature and the most important factor in Sweden the light! The purpose of this project is to collect data and visualize them so that I can figure out if the plants are in a suitable place or not! By collecting these data we can figure out which plants we can have in Sweden in our apartmants based on the humidity, temperature and light during different seasons. ### Material --------------------- The list of needed materials is provided in the table below this text, but of course we also need a laptop or pc to configure Lopy and code. | Component | Description | price | link | | --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Lopy4 | The LoPy4 is Micropython-programmable quadruple bearer board. It works with LoRa, Sigfox, WiFi and Bluetooth, making it an exceptional enterprise-grade IoT platform. | 388 kr | [link](https://www.digikey.se/products/en?keywords=lopy4) | | Photoresistor | light sensitive devices most often used to indicate the presence or absence of light | 39 kr | [link](https://www.conrad.se/p/iduino-1485310-ljusmotstand-laemplig-foer-enkorstdator-arduino-1-st-1485310?gclid=CjwKCAjwr56IBhAvEiwA1fuqGqx-fmeWmoly5pqeKjKKmhK9SI3aZLZbXXocXkzgGnkqW_hr1BPlTBoCoj0QAvD_BwE&gclsrc=aw.ds&utm_campaign=shopping-feed&utm_content=free-google-shopping-clicks&utm_medium=surfaces&utm_source=google&utm_term=1485310&vat=true) | | DHT 11 | Digital humidity and temperature sensor | 49 kr | [link](https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/) | | Bread board | For connecting sensors and lopy | 59 kr | [link](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/) | | Jumper Wires | To connect sensors and lopy | 33 kr | [link](https://www.electrokit.com/en/product/test-wires-100mm-m-m-30pcs/) | |Micro USB cable|To connect Lopy to Computer |39 kr|[link](https://www.electrokit.com/produkt/usb-kabel-a-hane-micro-b-5p-hane-1-8m/)| | Expansion board | A circuit board that can be inserted in a computer to give extra facilities or memory. | 176 kr | [link](https://www.digikey.se/product-detail/en/pycom-ltd/UNIVERSAL-EXPANSION-BOARD/1871-1004-ND/7721842) | ### Computer Setup ----------------------- To start using our lopy and sensors we need to install some softwares: First of all we need to update the firmware of the expansion board. You can download the tool to update the firmware [here](https://docs.pycom.io/updatefirmware/device/). After downloading the proper tool based on your OS you can connect the lopy to your computer by the micro USB cable and then open the pycom firmware update tool. you should click on the continue till you get to the window you see below ![](https://i.imgur.com/sOmYv93.png) Make sure to checked two boxes indicated in the figure. Then continue the wizard window to the end to update your firmware. After updating the firmware we download the IDE which I used VSCode, to do so you can download the VSCode [here](https://code.visualstudio.com/docs/setup/windows). After downloading and installing the VSCode we need to download NodeJS you can download it [here](https://nodejs.org/en/). Next we have to use a plugin in the VSCode for our project to do so open your VSCode and then follow the steps [here](https://docs.pycom.io/gettingstarted/software/vscode/). We have to make sure we have Python installed on your computer and if you don't have it you can download it on the Python [website](https://www.python.org/downloads/). Now we have to do two steps to connect our LoPy to Pybytes. 1. Go to Pybytes [website](https://pybytes.pycom.io/) and make an account then log in. 2. We have to add our device to Pybytes we can follow [this short tutorial](https://docs.pycom.io/pybytes/connect/) to do it. Now we can upload our first code on the LoPy! to do so I suggest useing [this sample code](https://docs.pycom.io/gettingstarted/) from step 3. After this step to upload the code from VSCosde to our lopy, First we have to make sure our device is connected to the computer the after saving the code in the VSCode by pressing Ctrl+S we click on the Upload as indicated in the figure below. ![](https://i.imgur.com/P44JWC4.png) ### Put everything together ----------------------------- ![](https://i.imgur.com/MvkgsM9.png) you can connect sensors according to the figure. there is no need to use a resistor, especially for this photoresistor. if you are going to use the different photoresistor sensor as indicated below, ![](https://i.imgur.com/Jtdcn1c.jpg) then you have to use 10kΩ resistance before conecting it to the GND as the figure ![](https://i.imgur.com/bDberAB.png) After connecting sensors we need to connect the expansion board to the computer or using battery if we need to send data for a longer time. I use a powerbank which can handle 2 weeks. If you want to use different pins make sure to read about all pins [here](https://docs.pycom.io/datasheets/development/lopy4/). ### Platform ------------------- There are many different platforms to choose for instance Ubidots, pybytes, datacake, adafruit IO, grafana and... First I used the pybytes platform but to be honest it's not that much appealing I changed the connection from WiFi to Sigfox but the sigfox backend was not satisfying as well there are not enough tools to visualizing the data so at the end I used ubidots which completely fulfilled my purpose for this project. We can easily get the data and also by just looking at different diagrams and gauges we can figure out if the condition is ok or not. Also ubidots is free up to 3 devices and we can have up to 20 variables for free! more than 3 devices or more than 20 variables will cost you! ### Code ---------------------------- For this project we need Libraries which the first one is DHT library you can download it [through this link ](https://github.com/iot-lnu/applied-iot/tree/master/sensor-examples/DHT11%20%26%20DHT22%20-%20Humidity%20%26%20Temperature%20Sensor/lib) the second library is used for MQTT which you can [get from this link](https://github.com/iot-lnu/applied-iot/blob/master/network-examples/mqtt_ubidots/mqtt.py) We need to make an extra file in the VSCode for our project and name it as constants and copy [this code ](https://github.com/iot-lnu/applied-iot/blob/master/network-examples/mqtt_ubidots/constants.py) into it. In the constant file we have to change the WiFi_ssid and WiFi_pass based on our network as indicated below ![](https://i.imgur.com/DcdmPEv.png) then we have to import from our libraries ```python= import time # Allows use of time.sleep() for delays from machine import Pin # Pin object to configure pins from machine import ADC # ADC object to configure reading values from dht import DHT from lib.mqtt import MQTTClient import ujson import constants as CONST ``` After whis we have to declare the pin we want to use ``` python= LightSensorPin = 'P20' lightPin = Pin(LightSensorPin, mode=Pin.IN) adc = ADC(bits=10) apin = adc.channel(attn=ADC.ATTN_11DB, pin=LightSensorPin) ``` The most important part is to setup MQTT and publishing our data from sensors. we print the data in the console and sending that to the ubidots through MQTT. If you want to know more about MQTT please [check this link](https://mqtt.org/) ```python= client = MQTTClient(client_id, CONST.UBIDOTS_MQTT_URL, user=CONST.UBIDOTS_TOKEN, password='', port=CONST.UBIDOTS_MQTT_PORT) client.set_callback(sub_cb) client.connect() print('connected') my_topic = CONST.UBIDOTS_MQTT_TOPIC + "My_Device" payload = {} while True: time.sleep(2) result = th.read() while not result.is_valid(): time.sleep(.5) result = th.read() val = apin() # read an analog value print("Value", val ,result.temperature, result.humidity ) payload['temperature'] = result.temperature payload['humidity'] = result.humidity payload['light'] = val json_payload = ujson.dumps(payload) print(json_payload) client.publish(topic=my_topic, msg=json_payload) client.check_msg() ``` You can download all the code from [this repository](https://) ### Connectivity ---------------------- As we are using LoPy4 we have different ways to send our data. We can use Lora, Sigfox, Bluetooth or WiFi. As I use this IOT device at my apartment and I have access to WiFi 24/7 so I prefer to use WiFi over the other option. Of course if you want to make it outdoor project then you can use lora or sigfox as they don't need that much power and also we don't need data every single minute. In this project as I just want to monitor my plant environment so power consumption is not the problem as I use powerbank as source of power. In this code we send data every 2 seconds which if we have limitaion on our platform we will reach it very very soon. In ubidots we are allowed to send 4000 dots per day. So if you have any limitations please change the code at line 40. ```python= while True: time.sleep(2) # change this line result = th.read() ``` #### Transport protocol we are using MQTT protocol as it is a safe protocol and easy to use. In the code we publish our data through MQTT and in the Ubidots we subscribe the same topic which can involve several variables (data). Also MQTT is a really simple protocol and it's one of the most common ones for IOT. ### Presenting the data -------------------------- There are several options in the ubidots website to make our dashboard. For each signal we can make different graphs and indicators. ![](https://i.imgur.com/vqJj6tg.png) ![](https://i.imgur.com/rvmk6Sf.png) In ubidots website we can visualize data in different ways. here we are using gauges and line charts to indicates data. Also we can have one chart for all of our data as the figure indicates below ![](https://i.imgur.com/EwdfryX.png) The data retention in ubidots STEM is 1 month and as soon as LoPy publish the data and transfer it through MQTT we have the data in ubidots website. ### Finalizing the design ---------------------------- What is indicated below is not the last design, as I am waiting for 3D printer to arrive and then make suitable box which doesn't cover the humidity sensor and doesn't block the light sensor while preventing dust and water splash to cause damage to the device. ![](https://i.imgur.com/JQjCBNv.jpg) ![](https://i.imgur.com/GSP8Bxr.jpg) ![](https://i.imgur.com/9StuFL0.jpg) I wanted to have moisture sensors as well to have more data about the plant. I got 2 moisture sensors as indicated below but they both had problems and didn't work properly. I ordered new sensors and as they arrive I will add them to the device to gather more data. ![](https://i.imgur.com/qGPNwOT.jpg) ### Resources ------------------------- [David github](https://github.com/iot-lnu/applied-iot/tree/master/network-examples/mqtt_ubidots)