# IoT Project - Combining an aeroponic system with pH-level and temperature measurements
![Mig](https://i.imgur.com/6koOt7Y.jpg =100x)
Name: Rickard Larsson
Student credendtials: re222km
The aim of my project is to monitor pH-levels (measurement of the hydrogen ion concentration in the solution) and temperature for a water solution that will be nutrienting plants in my aeroponic system.
If you only want to do the things that are included in my project, the IoT-part, you would be able to set up everything in a day. If you would like to learn the each steps a little more i would suggest reading spending up to 5 days.
# Objective
So the thing is that I have always loved cooking food. I love to experiment with different herbs and spices but often tend to use dried ones. When I buy fresh herbs they usually end up being used for that particular evening, and then slowly decaying since i can't seem to keep them alive. So for this project I will initially build an aeroponic water system to grow my beloved herbs. Then I will use sensors to measure pH-levels and temperatures in the water that are being used to water the plants.
The sensors will play an important part in the success of my ambition to become the master chef I feel I am born to be :). The right pH-level is crucial because it affects nutrient availability for the plants growing. In addition for the plants to thrive the water solvent must keep a proper temperature to create an environment for optimal nutrient absorption. So my hope is that I will not be needing to check these things manually every now and then, and instead get a notification when these levels will fall out of a specific range I will be setting.
# Material
The materials being used for this project is presented in table 1 and 2 below. I have included pictures for parts that I think you as a reader find interesting.
In this project I have chosen to work with the PyCom LoPy4 device as seen in table 1. The LoPy4 is a quadrupole network MicroPython enabled development board (LoRa, Sigfox, WiFi, Bluetooth). The device has many digital and analog inputs and outputs and is well suited for an IoT project. Since the module needs some way to be programmable we will be using the Pycom Expansion Board v3.1. For this project I have used one digital temperature sensor (DS18B20) with a range of -55 to 125$^\circ$C and a $\pm 0.5^\circ$C and one pH sensor (SEN0161) from DFRobot. SEN0161 can measure 0-14pH with $\pm 0.1^\circ$pH at 25$^\circ$.
*Note: I will not go in depth explaining the parts for the aeroponic system or how I did connect them.*
**Table 1: IoT materials:**
| Part |Description | Shop | Price | Image |
| ---------|-------------------------|-----------|-----------|------------|
| DS18B20 | Digital waterproof temperature sensor |CDON.se |3.79 EUR |![](https://i.imgur.com/iqchNu9.jpg =80x )
| SEN0161 | Analog pH sensor that measure pH-levels in water |[Arduino](https://store.arduino.cc/gravity-analog-ph-sensor-meter-kit-for-arduino?___store=default) |29.90 EUR| ![](https://i.imgur.com/aJtaxvN.jpg =250x)
| LoPy4 Bundle | A bundle containing LoPy4 with headers, expansion board, antenna and some sensors, resistors and wires. Top image is the LoPy4 and the Expansion Board v3.1 beneath. |[Elektrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) |94.63 EUR | ![LoPy4](https://i.imgur.com/RswyY8I.jpg) ![](https://i.imgur.com/MDnxrOX.jpg)
| Junction box | A simple plastic box with the purpose of a house and safely protecting the structure´s electrical connections. |[Biltema](https://www.biltema.se/bygg/elinstallationer/eldosor/kopplingsdosa-2000034324?gclid=Cj0KCQjwirz3BRD_ARIsAImf7LMubwct1vUZGi4ymHkrd3kle3Z7hdq4ln-8Mq6MuvsXjWm71IgTHZcaAoMgEALw_wcB) |2.99 EUR | ![](https://i.imgur.com/za6xRHQ.jpg)
| Total | | |131,31 EUR |
**Table 2: Aeroponic system materials:**
| Part | Amount |Description | Shop | Price | Image |
| ---------| ---------|-------------------------|-----------|-----------|---------|
| Plastic barrel | 1 | A grey plastic barrel that was previously used in the food industry |Private seller found on [Blocket](https://www.blocket.se) |38.00 EUR
| Micro spray mister | 20 | Water spreader (18 L/h) |[Hydrogarden](https://www.hydrogarden.se/odlingssystemkrukor/bevattning-pumpar/sprinklers-sprayers-misters/micro-spray-mister-18-ltim.html) |15.22 EUR | ![](https://i.imgur.com/VAH6UD3.jpg)
| Mesh basket 2" | 30 | A Mesh basket to for holding the plants |[Hydrogarden](https://www.hydrogarden.se/odlingssystemkrukor/krukor-natkorgar/natkorgar/natkorg-2.html) |5.71 EUR |![](https://i.imgur.com/o1yZumk.jpg)
| Rockwool cubes | 77 | Used as a medium for seeds or cuttlings |[Hydrogarden](https://www.hydrogarden.se/sticklingar/medium-for-sticklingar/grodan-rockwool-kuber-sbs-large-35-x-35-x-40mm.html) |12.27 EUR |![](https://i.imgur.com/uVs98tn.jpg)
Water Pump | 1 | A basic water pump, 1500L/h |[Hydrogarden](https://www.hydrogarden.se/odlingssystemkrukor/bevattning-pumpar/cirkulationspumpar/boyu-fp-1500-adjustable-pump-1500lhr.html) |28.44 EUR| ![](https://i.imgur.com/5rXUPyD.jpg)
Neoprene rings | 30 | Neoprene rings used to cover the mesh baskets |[Hydrogarden](https://www.hydrogarden.se/odlingssystemkrukor/krukor-natkorgar/natkorgar/neoprenringar-for-2-natkorgar.html) |22.83 EUR | ![](https://i.imgur.com/r2FDf9q.jpg)
| Starterkit | 1 | A solution for plant nutrition and pH regulation |[Hydrogarden](https://www.hydrogarden.se/naring/ghe/ghe-floraseries-tripack-starter-set-soft-water.html) |23.68 EUR |![](https://i.imgur.com/uUTC8c3.jpg)
| Filter | 1 | 13mm filter to filtrate rootparts and nutrition solution from the water running back to the tank |[Hydrogarden](https://www.hydrogarden.se/odlingssystemkrukor/bevattning-pumpar/13-25mm-kopplingar/13mm-filter.html) |6.54 EUR |![](https://i.imgur.com/haGnhQo.jpg)
| Other connections | 40 | Connections and hoses |Bought privat from a friend |28,44 EUR |
| Plastic tank | 1 | A simple plastic container with a lid | [IKEA](https://www.ikea.com/se/sv/p/trofast-back-svart-10252573/) |2.84 EUR | ![](https://i.imgur.com/KvkbVXu.jpg)
| LED tubes| 10 | 1500mm led tube, 2900lm, T8 | [Lampornu](https://www.lampornu.se/noxion-avant-led-t8-tube-extreme-ho-em-150cm-20-6w-830-varm-vit-led-glimtaendare-inkl-ersaettare-58w) |89.72 EUR |![](https://i.imgur.com/OR4gkyB.jpg)
| LED tube lamp holder | 5 | A lamp holder for 2 LED tubes, 1500mm | [Lampornu](https://www.lampornu.se/noxion-led-t8-allmaenbruksarmatur-click-reflektor-2x58-1500) |90.39 EUR |![](https://i.imgur.com/UKX1gwT.jpg)
| Total | | | |364.08 EUR |
# Computer setup
Updates and programming for this project were made on my stationary desktop, a 64bit Win 10 Pro OS running on a i7-6700K CPU.
1. **Update Expansion Board v3.1**
Before connecting my module to the Expansion Board V3.1 I did use PyCom´s guide to update the firmware. You can find it [here](https://docs.pycom.io/pytrackpysense/installation/firmware/). The Direct Firmware Update being used were [expansion31_0.0.11.dfu](https://software.pycom.io/findupgrade?key=expansion31.dfu&type=all&redirect=true)
2. **Update LoPy4**
After i updated the board, i moved on to update my LoPy4 module with the help from PyCom firmware update tool found [here](https://docs.pycom.io/gettingstarted/installation/firmwaretool/). I chose version 1.20.2.rc9.
3. **Install a source-code editor**
I did try out both the VS code and Atom before choosing my preferable IDE. I found out that both of them were easy to work with. I would recommend any of them. Since I had to pick one I had to go with my personal preferences regarding a controversial category, the design.
Installed Visual studio code (V1.45.1) that can be found [here](https://code.visualstudio.com/).
4. **Install NodeJS**
I installed the open source server environment [NodeJS](https://nodejs.org/en/download/) (V12.18.0)
5. **Install extension on VS Code**
Used Pycoms visual studio code guide found [here](https://docs.pycom.io/pymakr/installation/vscode/) to install and [Pymakr VSCode Extension](https://marketplace.visualstudio.com/items?itemName=pycom.Pymakr) (V1.1.6).
Since I am a beginner and have not been programming much, I used a library already created for DS18B20. The code that i write in main.py is uploaded by the upload button at the bottom of VS code, that will upload the current code to the board.
# Putting everything together
Since the usage will be inside my garage, I will be using the micro USB connector for power. The two sensors is connected in accordance with the **circuit diagram** below:
![](https://i.imgur.com/FhtnZnz.jpg =600x)
The VIN pin supplies 3.5V-5.5V and is used for the ph meter since the datasheets state that it would preferably be connected to 5V. For the DS18B20 Pin 3V3 is enough. Worth mentioning is that I am using a 4.7k$\Omega$ pullup resistor for DS18B20. DS18B20 is a one-wire sensor. One-wire devices need a pull-up resistor connected to their signal line to be properly read by your board.
Since I am initially only going to use WiFi for communication I use the on-board WiFi antenna. The external antenna is connected as seen below:
![](https://i.imgur.com/AMn7IRg.jpg =300x).
Even though this is only made for personal use with the development board. It could be produced in small quantities since it has a "case" that can withstand outer environments. But i need to point out that for scaling of this project, i would recommend custom boards. For reducing the price and only pack it with things that are actually needed. But also to be able to control all sourcing of components and the quality of the design. This would maybe be a thing to do before moving on to crowdfunding.
How is all the electronics connected? Describe all the wiring, good if you can show a circuit diagram. Be specific on how to connect everything, and what to think of in terms of resistors, current and voltage. Is this only for a development setup or could it be used in production?
Like mentioned earlier this project is for usage at my home, and therefore have no limitation regarding power consumption. However I might think about moving the project out to a greenhouse that is located 500m from my house. In this case I would use batteries.
To estimate what kind of battery I need and how long it will be lasting I need to estimate the power consumption. The power consumption is based on many variables like network availability, HF noise, clock skews, temperature etc. We need to estimate how much power is being drawn both when the system is awake and also when it is sleeping. We would also be unable to know the exact capacity of the battery. The value printed on the battery is typical $\pm %$ a certain percentage, also varies with temperature and how my charging pattern has been historically.
*Note: I would probably change my code and use deep sleep to save battery. Maybe change wireless protocols as well depending on what is available. There won't be a single correct number, but instead a probability distribution. This is why estimates values would work just fine for this project.*
I would assume that the largest amount of power consumption comes from my LoPy4. I have been using a multimeter in current mode. My device has, according to my readings, a average power consumption of 90mA for the device.
*Note: During WiFi connection the current consumption spikes to around 200mA.*
I have not yet tested deep sleep. But if all goes according to the manufacturer, the device should have a power consumption of 10-20$\mu$A during Deep Sleep.
If i take my device, have sensor readings every 60s, use WiFi and combine this with Deep Sleep, the process would be something like this:
Deep Sleep → start WiFi→ get readings from sensors→send → back to Deep Sleep.
$(5s*90A*10^{-6}+55s*20A*10^{-9})*\frac{1}{60s} ≈ 7.5mAs$
If i would use a 2000mAh battery this would be equal to 11,1 days.
However there might not be necessary to measure every 60 seconds. If i would measure every 30 min instead the battery would last close to 1 year.
*Note: I have calulated with an awake time of 5 sec per cycle, this could possibly be higher, up to 10 sec in worst case and affect the calculations.*
# Platform
There are several cloud platforms to choose from. I have tried [Pybytes](https://pybytes.pycom.io/) and [Ubidots (STEM edition)](https://ubidots.com/stem/). Pybytes is a free cloud-based platform that is available for all the development boards from Pycom. It is really easy to set up. However i found their dashboard lack a lot of features i did want to experiment with. In this project i did choose to work with Ubidots STEM which is the free version. Ubidots is a two platform company, were the non-commercial plan is called STEM. It has some limitation, but no one that would be of any issue for me in this project. With the Ubidots STEM platform, users collect, enhance, and deliver sensor data to the dashboard. The dashboard has a lot of functionality for presenting your data. You can also download an app for monitor your data with your phone. You can create triggers for the sensor data that could be sent in a numerous kind of ways. This was the main reason I chose to continue working with Ubidots. Ubidots is based on cloud. If there would be any kind of scaling with this project, i could easily just move over to the paid subscription at Ubidots. This would increase the number of devices, numbers of data ingestions and extraction, and number of end users.
*Note. There are also the big ones, [AWS IoT Things Graph](https://aws.amazon.com/iot-things-graph/), [Azure IoT](https://azure.microsoft.com/en-us/overview/iot/) and [Google Cloud IoT](https://cloud.google.com/solutions/iot) that you could try out.*
# The code
Since I am not a programmer, and had little knowledge of programming, I did use existing codes. The existing codes had to be modified to fit my project however. The code i am using for this project in main.py is seen below:
```python=
from network import WLAN
import urequests as requests
import machine
import time
from machine import ADC
from machine import Pin
from onewire import DS18X20
from onewire import OneWire
Pin('P12', mode=Pin.OUT)(True) #Setting P12 as output pin for external antenna usage
# SEN0161 data line connected to pin P20
adc = machine.ADC() # create an ADC object
apin = adc.channel(pin='P20', attn=ADC.ATTN_11DB) # create an analog pin on P20
ow = OneWire(Pin('P22')) # DS18B20 data line connected to pin P22
temp = DS18X20(ow)
TOKEN = "X" #My Ubidot TOKEN
DELAY = 1 # Delay in seconds
wlan = WLAN(mode=WLAN.STA)
wlan.antenna(WLAN.INT_ANT)
wlan.connect("Ssid", auth=(WLAN.WPA2, "Password"), timeout=5000) # My Wi-Fi credentials
while not wlan.isconnected ():
machine.idle()
print("Connected to Wifi\n")
def build_json(variable1, value1, variable2, value2): # Builds the json to send the request
try:
data = {variable1: {"value": value1},
variable2: {"value": value2}}
return data
except:
return None
def post_var(device, value1, value2): # Send the request.
try:
url = "https://industrial.api.ubidots.com/"
url = url + "api/v1.6/devices/" + device
headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}
data = build_json("Temp", value1, "ph", value2) # Since im using two sensors i will need two values
if data is not None:
print(data)
req = requests.post(url=url, headers=headers, json=data)
return req.json()
else:
pass
except:
pass
while True:
mvolt= apin.voltage() # Read voltage
ph = -2.5440366428206*10**(-12)*mvolt**4+1.0717620652870*10**(-8)*mvolt**3-0.0000163809*mvolt**2+0.0136789*mvolt-0.332595 #Newton's interpolation
temp.start_conversion()
time.sleep(1)
Temp = int(temp.read_temp_async()) # Data values
post_var("pycom", Temp, ph)
time.sleep(60)
```
The OneWire lib can be found [here](https://docs.pycom.io/tutorials/all/owd/).
The Urequest lib can be found [here](https://github.com/jotathebest/micropython-lib/blob/master/urequests/urequests.py).
As seen in the code I am using a polynomial to retrieve the ph values from the solution. When i had connected the ph meter i used 5 different solutions with known ph values. I did take readings at 5 different occasions and then used the average value. To estimate the values between my readings I use interpolation (Newtons). The function that were estimated and used for my ph meter is seen in equation (1) below:
$f(x)=-2.544...*10^{-12}x^{4}+1.071...*10^{-8}x^{3}-0.001...x^{2}+0.013...x-0.332... \space\space\space (1)$
$x$ will be equal to the electric potential difference in mV gathered from the SEN0161 sensor.
I took only readings from ph of value 7 and below, since the range at which this sensor will be monitoring is somewhere between 4-7. Outside the range, the determined ph value is likely to be incorrect. It is likely that $y=mx+c$ would have been sufficient for my purpose. As seen in the graph below, where the y-axis is ph values and x-axis mV, between the monitoring range the equation is somewhat linear.
![](https://i.imgur.com/FtFnbLX.jpg =400x)
# Transmitting the data / connectivity
How is the data transmitted to the internet? Describe the package format. All the different steps that are needed in getting the data to your end point. Explain both the code and choice of wireless protocols
I used a guide that you can find [here](https://help.ubidots.com/en/articles/961994-connect-any-pycom-board-to-ubidots-using-wi-fi-over-http), to setup and send data to Ubidots.
Since I am using the free version I am limited to a certain amount of dots to send. Dots is a data point containing a value and a timestamp. The total amount of dots that I can send 4000 each day, that corresponds to roughly 1 every 22 second.
As this will be a project located in my garage within the distance of my router I use WiFi.
*Note: I do not have a LoRa gateway in a close proximity anyway.*
For Ubisofts i am using HTTP. If i were to be working with a resource constrained device that also needed help to save the battery i would probably choose MQTT that could be more suitable when it is referred to IoT development.
Since both the temperature and ph value do not change very often i send data every minute. I could probably use sleep modes and reduce the frequency of data sent a lot more, since both ph and temperature does not change quickly in my aeroponic system. But since i will be using a cord for power supply i am not affected by any battery consumption.
# Presenting the data
Describe the presentation part. How is the dashboard built? How long is the data preserved in the database?
![](https://i.imgur.com/6VxSRYp.jpg =400x)
The data is being saved immediately after the dots are received at Ubidots, in their database. Since i will be sending the data every minute, the data will be saved every minute as well. With Ubidots STEM version there will be one month of data retention.
*Note: There is however an option to export the data and save it, if needed.*
I am using database storage for my data via Ubidots. Ubidots do not offer information regarding this database. The database is from IBM located in Toronto. Information regarding security can be found [here](https://help.ubidots.com/en/articles/889691-security-is-ubidots-secure).
I have also stated automation/triggers of the data. If i have not received any data from any of the two sensors (i.e. being inactive) a notification is being sent to my email. The pH level needs to stay above 5.0 and below 7.0 (a pH of 6.0 is ideal). The temperature needs to be above 15.0$^\circ$C and under 29.0$^\circ$C (ideal temperature is 18.0-26.0$^\circ$C). If either the pH level or temperature level ger readings outside of this threshold a notification will also be sent to my email.
# Finalizing the design
Here is some pictures i have taken for the final result of the project. They are taking inside my garage were the growing will take place later on.
| | |
| -------- | -------- |
|[1] ![](https://i.imgur.com/6PatQP2.jpg =400x) | [2]![](https://i.imgur.com/FUSRxKJ.jpg =400x) |
|[3] ![](https://i.imgur.com/MScbqQQ.jpg =400x) | [4]![](https://i.imgur.com/EvrSFbN.jpg =400x) |
Picture [1] is showing the backside of the system, with the backside of the reflector and the fluorescent lamps can barely be visible in the top and lower part of the reflector.
Picture [2] shows the actual barrel with the 45 degree pipes sticking out. These will be filled with plants that hopefully will be thriving. On the side the pipe connections is being visible. Middle part is a filter that will be filtering root particles and so forth. The pipe enter the barrel from the top lid, and inside the the pipe is filled with mist sprayers creating a full humid environment for the roots of the plants. In [3] the fluorescent lamps better. In the last picture, [4], the IoT device is being presented. There are 3 cords going out from the box, power cord, temperature sensor and ph sensor. Both of the sensors are now located inside the water tank were they, as mentioned before, takes sample data every 60 sec.
Here you can see how the inside of the device is looking.
![](https://i.imgur.com/VPfA3gS.jpg =400x)
Overall I am happy with the results of the project. This is such a nice and easy way to make proof of concepts for ideas people have. I will most likely do other upcoming projects, since I have already begun processing ideas.
If i could change something, i would have liked to control the on and off power of the growing lights and the Water Pump with a relay. Instead of relying on external timers. This might as well be something I will adjust to in the future.