# Small IoT Project Tutorial
*by David Råsberg, dr222hv*
This tutorial guides you through the process of completing my very first IoT project, a soil moisture monitor complemented with a temperature and air humidity sensor. The project consists of connecting, at least, two sensors and three LEDs to a LoPy4 and displaying data from the sensors on PyComs platform PyBytes.
The time required to complete the project varies with experience, from less than an hour for an expert to a couple of hours for a beginner.
**Objective**
I chose this project mainly because of what equipment I had at hand and the projects relative simplicity. The device serves the purpose of telling me when I need to water my plant(-s). After using the device for a while, you can gain some insight into how often you should water your plants.
**Material**
I bought an IoT bundle from [electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) containing every component of the procect except for the sensors. The bundle also contained additional components that I did not use. I purchased the bundle to a cost of **949 SEK**.
| Device | Link | Desciption | Price, vendor |
| ---------------- | ----------------------------------------------------- | ------------------------------------------------------------------- | ------ |
| LoPy4 | [Link](https://pycom.io/product/lopy4/) | Development platform with 4 network bands. Micropython-progammable. | Part of bundle |
| Expansionboard 3 | [Link](https://pycom.io/product/expansion-board-3-0/) | For interaction with the LoPy. | Part of bundle |
|DHT11|[Link](https://www.electrokit.com/produkt/temp-fuktsensor-dht11/)|Temperature and humidity sensor-|39 SEK @electrokit|
|FC-28|[Link](https://www.electrokit.com/produkt/jordfuktighetssensor/)|Soil moisture sensor. Measures resistance in the soil between the sensor legs. |29 SEK @electrokit|
|Breadboard|[Link](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/)|Simplifies wiring.|Part of bundle|
|LEDs|[Link](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/)|Light Emitting Diodes used as actuators (for fun).|Part of bundle|
|Varies resistors|[Link](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/)|Used as needed as part of circuits.|Part of bundle|
|Varies wires (M/M)|[Link](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/)|Used to connect everything to the LoPy.|Part of bundle|
|Micro-USB cable|[Link](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/)|Used to connect the LoPy4 to a computer.|Part of bundle|
||||**Total Cost=** 1017 SEK|
**Computer Setup**
First of all, you might need to update the firmware of the expansionboard. This is done by following the simple procedure outlined by Pycom [here](https://docs.pycom.io/updatefirmware/expansionboard/). You should also update the LoPys firmware. Pycom has developed intuitive software for this purpose. Simply download the [Pycom Firmware Updater](https://docs.pycom.io/updatefirmware/device/) from pycoms website, connect the board to your computer and complete the update.
To write code and upload it to the LoPy you will need an IDE, I chose VSCode, with the PyMakr plugin. If you are coding on a windows machine you need to install Node.js before using the PyMakr plugin. The folder of this kind of project should always contain a subfolder called "lib" where you store modules for your sensors. The main folder should also contain a file called "boot.py" which executes when the LoPy boots. At last, there should also be a file called "main.py". This is the file that should contain your program and it should be kept relativly short for readability.
The PyMakr plugin will add a toolbar at the bottom of the VSCode window. From this toolbar you can upload to and download files from the LoPy. You also have the option to execute files on the LoPy without uploading them. In addition to this, the PyMakr plugin adds a terminal at the bottom of the VSCode window. This terminal automatically connects to the LoPy and here you can run python interactively which executes on the LoPy.
**Putting everything together**
The inserted, badly drawn, image illustrates how to connect everything. I have excluded the micro-usb cable I used to power everything. Simply connect a micro-usb cable to the expansion board and plug it into your computer.
Worth noting is the LoPy4s orientation relative the expansion board, i.e., which way you stick the LoPy into the expansion board. You should line up the LoPys male connections to the middle two rows of female connections on the expansion board, **and make sure the LED on the LoPy4 is located directly above the micro-usb socket on the expansion board**.

*Note!! The color bands on the resistors do not reflect the resistors I used.*
Make sure nothing is connected to power before connecting the sensors to the LoPy. The breadboard can be confusing of you have not seen one before. The two outmost columns to the left and right are connected vertically, while columns a through e and f through j are connected horizontally.
Connect everything as I have illustrated above, but bear with you a couple of notes:
* The resistors I used in the LED circuit were 560 Ω.
* The majority of wires going from the bread board to sensors or resistors can be ignored. I connected all of the components directly to the breadboard and have illustrated it in the given way for clarity.
* The LEDs are connected in a different order in my circuit, and therefore in my project code. This does not really matter. Just make sure you actually use the pins stated in your code.
This type of circuit would only suit a development setup. The bread board is large and clumbsy and the wires to the FC-28 are only 20 centimeters long.
**Platform**
I chose to use Pycoms platform PyBytes to visualize data sent from the device. PyBytes is easy and free to use. Simply create an account and follow the instructions to add a device to a project, it will not take long. How data is fed to the platform will become apparrent in the project code. You can send sensor values on 255, 0-254, different channels to PyBytes. Therefore, you could use up to 255 sensors per device connected to PyBytes. This is technically possible but would be tedious since there are far from 255 different pins on the LoPy4.
**The code**
The code I wrote and used for this project can be found on my [github account](https://github.com/dr222hv/IoT).
The boot.py file connects the LoPy to a wifi network. Substitute <SSID> with your WiFi and <Password> with the password for your WiFI.

The lib folder only contains one file, "dht.py", containing functions for the DHT11. I did not have to write any of that code myself, and neither do you! Use google or my github link to find the file.
The main.py file is the actual project code, with the projects main() function.

This is whole main function. What it does is create a list object called res, calls functions that add sensor data to that list, and lastly sends every instance in the list as different signals on PyBytes. The function "send_signal(counter, i)" is the only interaction required with PyBytes.
Apart from this, the main file also declares which pins I have connected the sensors to.

This is how the soil moisture pins are declared. Later we will create ADC objects on the ao_pins to enable analog readings.
The main file additionally contains functions that fetch values from the sensors.

This is most of the function that loops through every soil moisture sensor and calls a function that reads data from each of them. The function fetches data from every sensor ten times before calculating the mean of those readings. The mean is later used as the actual reading from the sensor.

This is a later part of the same function and illustretes the only usage of the LEDs. If the LoPy4 is powered on or is woken from sleep by the push of a button, the LEDs will light up depending on the soil moisture.

This function does the following:
* creates an ADC object on a pin connected to a FC-28
* turns on power to the sensor
* waits two seconds before reading the resistance in the soil
* turns off power to the sensor
* lastly it returns the measured value
The remainder of the code is pretty self explainatory. If you are interested, the code in its entirety is uploaded to my [github](https://github.com/dr222hv/IoT).
**Transmitting the data/ connectivety**
Since this project is only intended to be run in my apartment, I chose to transmit the data over WiFi. This means that I do not have to worry about large data amounts. Since the LoPy is not battery powered I did not do any calculations regarding, or consider in general, power consumtion. If I were to take this project outdoors, I would transmit the data over LoRa. Since the device would most likely be battery powered, I would try to lower the amount of data sent and be concious the LoPys power consumtion.
The LoPy4 is woken from deep sleep once every 20 minutes. It collects the sensor values and publishes them to the PyBytes platform over WiFi and then goes back to deep sleep.
Publishing data on the PyBytes platform, you do not follow any particular transport protocol. You simply use a function from the PyBytes API to send a value on a specified channel (signal) on PyBytes.
**Presenting the data**
A project at PyBtes contains a dashboard and on that dashboard, you can add several kinds of graphs for your sensor data, or signals as they are called on the platform.

This is my dashboard. The three charts located at the bottom are displaying my sensor data. The three upper charts/windows are default. The upper leftmost window displays your devices locaton while the two upper rightmost windows display how much data PyBytes has received from the device the past hour and day.

You can configure every signal you have received data on, and see time, value and size of packages received in the past.
To my knowledge, data is saved on the PyBytes server for 30 days after being uploaded.
**Finalizing the design**

The project result was on par with what I expected. I chose a simple project since it was my first IoT experience and I learned a lot. I succesfully connected all of the sensors and published the collected data, but of course things could have gone smoother. In my opinion, pycoms documentation was sometimes difficult to navigate and I often resorted to google my problem only to end up in pycoms documentation again.