# Simple soil moisture sensor
Name: Axel Canneroth
Student credentials: ac222pq@student.lnu.se
This is a simple project with a sensor that collects data on soil moisture in a plant. The device is collecting the data once every hour, then goes into deep sleep to save battery. The data is uploaded to pybytes.
For someone with no or very little prior knowledge within this area, a project like this will take 3-4 hours.
# Objective
The reason I chose this project is because I have a tendency to not being able to keep plants alive.
Collecting data regarding the moisture levels in a plant will help to prevent it from dying from under and over watering. For now, data is only uploaded to pybytes database via wifi, but the next step would be to move away from pybytes and set up a notification system for when it's time to water the plant.
# Material
| Materials | Purpose |
| -------- | -------- |
| Lopy 4 | Microcontroller programmable with MicroPython. |
| Expansion Board 3.0 | Adds functionality to the Microcontroller such as USB connection and the ability to connect other electronic devices. |
| Breadboard | Not really necessary |
| Soil moisture sensor (specification unknown). | This sensor will measure the humidity in the soil. It works by measuring the resistance across the two legs and convert it to a voltage that can be read by a microcontroller. |
| Battery Holder | Holds 3x AAA batteries to power my device. |
Lopy 4: https://pycom.io/product/lopy4/ ~ 350kr
Expansion board 3.0: https://pycom.io/product/expansion-board-3-0/ ~ 160kr
Moisture sensor: https://www.electrokit.com/produkt/jordfuktighetssensor/ ~ 29kr
Battery holder : https://www.m.nu/batterier/3-x-aaa-battery-holder-with-on-off-switch-and-2-pin-jst ~ 40kr
Total cost: 579 kr
# Computer Setup
IDE used for this is Atom. In Atom I use the plugin called Pymakr. Pymakr is both used in the Atom IDE and it is also uploaded to my Lopy 4 device for it to be able to interact with the Pybytes platform. In order to use Pymakr, you need node.js installed on your computer aswell.
I upload MicroPython code directly to my Lopy via Atom and a USB cable. Pymakr plugin helps to ease this process and Pybytes is used to monitor my device and its data.
Installing everything is not difficult, just download Node.js, Atom and pymakr plugin. You also need to the Pymakr update firmware software to add Pymakr to your device (otherwise it cant connect to Pybytes).
# Electronics connection

Sensor requires yellow cable for ground, green for 3V power supply and Pin 13 for analog data transmission. A 3xAAA battery pack to power the device.
# Platform
The platform I used to manage my data is Pybytes. It is a cloud based platform offered for free by Pybytes (the company from who I bought my MicroController and Expansion board). Pybytes offers an easy way to set up a connection and dashboard with your device. Sending data from the device to Pybytes is really easy with only 1 line of code. It is also free.
Furthermore Pybytes offers status overview of my devices, visualization tools for my data (dashboards) and a terminal to push code and commands to my device via Wifi.
# Code
```
import machine
import _thread
import time
adc = machine.ADC()
adc.init(bits=12) # create an ADC object
while True:
time.sleep(5)
apin = adc.channel(pin='P13', attn =machine.ADC.ATTN_11DB)
val = apin()
result = val
moisture_percentage = ( 100 - ( (result/4095.00) * 100 ) );
pybytes.send_signal(1, round(moisture_percentage, 2))
print('Fukt:', round(moisture_percentage, 2))
time.sleep(2)
print('sleep')
time.sleep(2)
machine.deepsleep(6000)
```
apin = adc.channel(pin='P13', attn =machine.ADC.ATTN_11DB) = create an analog pin on P13, range 0..3.3V
val = apin() = This will be a value between 0-4095. 0 meaning 0 restistance (very dry soil) and 4095 meaning a lot of resistance (very moist soil).
moisture_percentage = ( 100 - ( (result/4095.00) * 100 ) ); = Instead of giving me a value between 0-4095 this line of code changes it to 0-100.
pybytes.send_signal(1, round(moisture_percentage, 2)) = Send the data to pybytes
print('Fukt:', round(moisture_percentage, 2)) = Prints the data in the terminal
machine.deepsleep(6000) = Maskinen sover i 1 timme
# Connectivity
Data
The data is sent from my device to Pybytes once every hour.
WIFI
Since I could not get any connection to a LoRa antenna (I am probably to far away) I instead used regular Wi-fi connection to transmit my data from the LoPy device.
The data package sent from my device to Pybytes is a 8 byte file presenting the value from my sensor in a format of 0-100% of soil moisture percentage.
MQTT
I think Pybytes uses MQTT protocol. However, its built in to the Pybytes platform so me myself did not have to to any of the coding. Therefore, there is no code to present here.
# Data Presentation
As stated earlier I am using the very simple platform Pybytes. Data is presented like this:

The dashboard saves the 10 latest data singals, then they are ereased. In the picture I receive data every 1 minute, this is not the case for my final work. This is an old picture.
# Final design
Luckily this is not a beauty competition. This is my final design of my very simple IoT project. My next step in the project is to move away from Pybytes and use my own transmission protocoll, build my own database and dashboard (Ubidots for example).
Overall, I think the project went well. Unfortunately I have not had the time to put the amount of effort I would have wanted to in it. In its current state its very simple and have much room for improvement.
