# Plantwatch with temperature and hall sensor, in progress.
Name: Zara Gyllebring
StudentId: zg222ad
This project was meant to mixes a very old technology for watering plants - clay pots, togheter with some simple sensors of today - a temperatur sensor and a hall effect sensor. The temperature sensor simply gives you the temperature near the plant, while the hall sensor lets you know when its time to fill up the water in the clay pot.
For more information on the ancient technology of using clay pots as a watering system and their benefits check out this wonderful link: https://www.permaculturenews.org/2018/12/25/how-to-make-your-garden-drought-proof-using-unglazed-clay-pots/?fbclid=IwAR2I9E3WQuLHVXOnnUVsYnBsplyP_ejft_Na-l_aHZrst4xsbqbbPR2FU74
However, the hall sensor setup in the claypot is not yet done. Due to 3d printing errors. However the hall effect sensor works and is sending data to the platform.
### Time
Approximate 2hours from start to finish + possible 3d printing time.
## Objective
### why this project
Honestly, I was going for a higher grade with a temperature sensor and a capacative moisture soil sensor, but my privatelife, time and a broken soil moisture sensor, got inbetween. During this I was also taking care of our pumpkin plants and had some fun trying out diffrent wateringsystems (clay pots, pet flasks with holes, normal watering etc) and what i came to realize was that the clay pots was more efficent than anything else. Less water was needed and the pumkins trive, no matter the weather condition. The only thing i need to do is make sure the clay pots have water in them.
### Purpose it serves
Since the clay pots always keeps the soil moiste but never wet, there is no need for a soil moisture sensor. However, since the water in the claypots last for days it can be hard to remember to check the waterlevels in the clay pots.
After going through the few options of sensors i had and how it could be solved i realized that if we placed a "boat" in the claypot with a mast, and instead of a sail we placed a magnet high up on the mast we would have a magnetic feild that followed the boat, and the boat follows the waterlevel. With a hall effect sensor at the top of the claypot we could get a magnetic reading when its time to fill upp the clay pot.

However, sadly, pieces of our 3d printer broke. So the boat and the mast isnt done. But the rest works.
### insights it will give
The temperature sensor mesures the temperature near the plant.
The led lamp on the pycom lights red when the temperature is over 25, and green when its below. That way we can get a fast indication by looking at the LoPy wather its hot or not (weather more water is possibly going to be used or not). The temperature data gives us a longer timeline on how warm it has been around the plant.
The hall effect sensor gives us a value of 1 if no magnetic energy fields are near, and a value of 0 if a magnet is near (0 - 1 cm in front of the sensor). This can give us an indication wether the pots need refilling or not.
Later on, when data over a range of weeks have been collected it will be possible to get a hint on how fast the pot needs refilling in diffrent temparatures. This could later bee calculated and added to the code, for example later on its possible to fix a mail to be sent when the pot has been empty for 1 day in warm temperature or 2 days in colder temperatures, or whatever would fit well with the plants.
## Material
Here are the materials used.
| Object | Description | link |
| ----------- | ---------- | ---- |
|Pycom LoPy4 |34,95 Euro |https://pycom.io/product/lopy4/?gclid=Cj0KCQjwgJv4BRCrARIsAB17JI5uATWFQXMSrI0jO9fWWfGypRbLSxZvBsY7luaeaGGHdF7LlkOIggwaAmfNEALw_wcB
|Pycom Expansion board |16 Euro |https://pycom.io/product/expansion-board-3-0/|
|Micro USB cable | | Had one lying around at home.
|Jumper wire | 36 SEK | https://www.electrokit.com/produkt/kopplingstrad-byglar-for-kopplingsdack-mjuka-65st/
|Breadboard | 59 SEK | https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/
|Temperature sensor MCP9700 | 11 SEK | https://www.electrokit.com/produkt/mcp9700-e-to-to-92-temperaturgivare/
|Hall-effect sensor TLV49645|18 SEK | https://www.electrokit.com/produkt/tlv49645-sip-3-hall-effektsensor-digital/
|Magnet |11 SEK | https://www.electrokit.com/produkt/magnet-neo35-o5mm-x-5mm/
The brain for this project is the Lopy 4, as recommended in the course, it has great connections (both wifi and lora, but also bluetooth and sigfox) and a lot of pins. Its also ready to use with Micropython
[<img src="https://pycom.io/wp-content/uploads/2018/08/lopy4NoHeadersSideN.png">](https://pycom.io/product/lopy4/?gclid=Cj0KCQjwgJv4BRCrARIsAB17JI5Fijjy-SORpa72is03CMcEv9ZdgacqOE8ldNPXg9WuWr9ofTfIbQwaAi3SEALw_wcB)
Its right hand is the Expansonboard. It makes it easier to connect to the pins and it also enables the use of a cable so we can power it through our computer and also communicate through the computer when needed.
[<img src="https://pycom.io/wp-content/uploads/2020/03/Webshop-Product-Box-Exp-Board.png">](https://pycom.io/product/expansion-board-3-0/)
Any micro USB cable will do. I had one lying around at home.
With the jump wires we can connect everything we need to connect to eachother. I have used 3 black, 3 red, 1 yellow and 1 orange. But any color works.
[<img src= "https://www.electrokit.com/uploads/productimage/41003/41003181-600x450.jpg">](https://www.electrokit.com/produkt/kopplingstrad-byglar-for-kopplingsdack-mjuka-65st/)
The breadboard is like a helping hand, it makes it easie to to test and try out the sensors and makes it easy to connect to Lopy
[<img src="https://www.electrokit.com/uploads/productimage/41012/41012199-600x479.jpg">](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/)
It is with the temperature sensor we measure the temperature.
[<img src="https://www.electrokit.com/uploads/productimage/41011/41011628.jpg">](https://www.electrokit.com/produkt/mcp9700-e-to-to-92-temperaturgivare/)
With the hall effect sensor we can detect magnetic fields that are close.
[<img src="https://www.electrokit.com/uploads/productimage/41015/41015964-600x450.jpg">](https://www.electrokit.com/produkt/tlv49645-sip-3-hall-effektsensor-digital/)
Finally we have the magnet that we use togheter with the hall effect sensor.
[<img src="https://www.electrokit.com/uploads/productimage/41011/41011480.jpg">](https://www.electrokit.com/produkt/magnet-neo35-o5mm-x-5mm/)
## Computer setup
Firstly, I have windows 10 on my computer. If you use any other OS you might have to do other steps, the pycom documentation provides more information on this. https://docs.pycom.io
Here are the steps I took:
### Step 1.
Connect the Expansion board to my computer via the usb micro cable. Then I uppgraded the firmware of the Expansion board, as described here: https://docs.pycom.io/pytrackpysense/installation/firmware/.
Note: If this step is hard to do, you should be able to skip it.
### Step 2.
I removed the usb micro cable from the computer, and then mounted the Lopy 4 ontop of the Expansion board as described here, under Basic Connections: https://docs.pycom.io/gettingstarted/connection/lopy4/
Note: The first step (dot) is allredy done in Step 1.
### Step 3
I downloaded and installed Visual Studio Code, nodeJs and Pymakr plugin by following this guide: https://docs.pycom.io/pymakr/installation/vscode/
### Step 4
Made an account on https://pybytes.pycom.io/.
In the menu I choosed "Devices" -> "Add via Usb" -> "Lopy 4" -> "Enable Networks - Wifi" -> Gave my device a name, and "manage WiFi" in order to fill in my wifi name and password.
### Step 5
Then back to Devices, click the name i gave my Device and then click "provisioning", then i followed "ACTIVATE YOUR DEVICE WITH FIRMWARE UPDATER" in order to update my Lopy 4.
### Step 6
Lastly i followed "ACTIVATE YOUR DEVICE WITH PYMAKR PLUGIN" thats on the page.
## Putting everything together
In order to understand how a breadbord works you can check out this link: https://www.sciencebuddies.org/science-fair-projects/references/how-to-use-a-breadboard
How everything is connected
| Component | Location | Notes|
| ----------- | ----------- | --- |
|Hall sensor | f30, f29, f28| The long flat surfaces should be facing away from the cables|
|Red cable| h30, (+)bus| Gives the hall sensor power|
|Black cable| i29, (-)bus| Ground to hall sensor|
|Yellow cable| h28, P19 on Lopy | Data communication for hall sensor|
|Temperature sensor| f3,f2,f1| The long flat surface should be facing the cables|
|Black cable|h3, (-)bus| Ground to temperature sensor|
|Orange cable| i2, P16 on Lopy| Data communication for temperatur sensor|
|Red cable| h1, (+) bus| Gives the temperature sensor power|
|Black cable|(-)bus, GND on Lopy| Ground between breadboard and Lopy|
|Red cable|(+) bus, 3v3 |Power between breadboard and Lopy|

However do remember any colors on the wires work. I have choosed to use black cables for Ground, Red for power and Orange and Yellow as Data transmittor, in order to make it visual easy to spot what is doing what.
The breadboard should probably not be used in the finished product, but replaced with proper cables (male/female vires). However, since this product isnt finished and this was what I had home, this is what I used as a prototype.
## Platform
My initial thougth was to use the TIG stack with Docker compose and use my Raspberry Pi 4 as a server. However, as stated before, things got inbetween. I had alredy fiddled with Pybytes a bit and acctually enjoyed the simplicity of it, so in the end I choosed Pybytes as my platform.
Even though Pybytes is quite new and a few problems might arise according to other peers, i acctually had no problems with it. The possibility to show data is slim, but for this project, in the state it is in, it works. Its also very simply to transform your data from your LoPy4 to the Pybytes platform which is a plus. The data can be stored for 30 days, which is enough at the moment.
In the future however, I plan to use the TIG stack and use my Raspberry Pi as the hub that collects and stores data. In order to tailor my needs as the project grows.
## The code
Since the sensors are of a simple sort, they doesnt really need their own library. The temperature sensor need an acd channel while the hall sensor uses a pin. All the code is written in main.py
Ive used pin 19 on the lopy for the hall sensor, and therefore used nr 19 as signal to Pybytes. When you look at your devises signals on the Pybytes platform "signal 19" is hall sensor. For the same reason i choosed number 16 for the temperature sensor.
```python=
#skagulteas Plantwatch Project version 0.2
import pycom
import machine
import time
from machine import Pin
pycom.heartbeat(False)
high = 25.0 #define high temperature
# defining the Pin for the temperature sensor
adc = machine.ADC()
tempPin = adc.channel(pin='P16')
#defining the Pin for the hall effect sensor
WaterPin = Pin('P19', mode=Pin.IN)
while True: #loop
millivolts = tempPin.voltage() #temp is taken in as millivolt..
celsius = (millivolts - 500.0) / 10.0 #..and calculated to celsius.
water = WaterPin() #gives a value of 1 or 0. 0 = magnet is close.
if celsius > high: #if temp is high
print("The temperature is high, its " + str(round(celsius)) + " C") #shows up in VS Code
pycom.rgbled(0x7f0000) # Lopys ledlight glows red
else:
print ("The temperature is low, its " + str(round(celsius)) + " C") #shows up in VS Code
pycom.rgbled(0x007f00) # lopys ledlight glows green
print("Waterlevel: " + str(water)) #shows up in VS Code
pybytes.send_signal(16, round(celsius)) #send info about temp to Pybytes platform
pybytes.send_signal(19, water) #send info about hall sensor to Pybytes platform
time.sleep(1800) # sleep for 30min
```
## Transmitting the data / connectivity
Lopy 4 records the sensor data. Through code it is sent to Pybytes using wifi and mqtt protocol, every 30 minutes. Pybytes then store the data for up to 30 days.
### How often is data sent?
When i did the test runs i choosed 10 to 30 seconds. This is what will be shown in the pictures later in this tutorial. However the code that i will provide is set to send information every 30 minutes. If it was gonna be over LoRa i might have choosen every hour instead of half hour in order to minimize data transfers.
### Wireless protocol used
I was curious about LoRa and inteended to use it, but unfortunality I seem to live in a dead LoRa spot, therfore i used Wifi instead.
### Transport protocol
In order to transmitt the data from Lopy 4 to Pybytes i use Pybytes own integrated code pybytes.send_signal(x, xxx). Pybytes uses the mqtt protocol.
## Presenting the data

The dashboard is built in Pybytes. When you are viewing your device in pybtes you can have a menu option "signals". There you can click on a signal and create a chart for your signal. Click "create new Display". Choose "line chart". Fill in a name, I used Pull size 30 for this example. Time scales is in seconds in the example but will be changed to days later in the production. Draw on grid is checked, aswell as Disaply points. Save the chart. Then click on "Edit" on your new chart. Check the box "Display on dashboard" and save.
Both signals need to be filled in. After that you can go back to your devises dashboard and rearange the charts as you like.
Describe the presentation part. How is the dashboard built? How long is the data preserved in the database?


The charts shows signals that have been transmitted every 30 seconds. This is to show that it works. The code is set to 30 minutes.
If you want to change the names and unit on your signals simply go to "signals". To the right on the side you have all your signals, click on the pen to the signal you want to change name and/or unit for and save the changes. If you change unit, it doesnt really do anything except show the right unit in the signals list and in the charts.
## Finalizing the design
Here is the masterpiece.

This was fun. Even though this is not a final product yet. I will continue to fix this project, and hopefully its up and running in the pots soon.
This was supposed to be a soil moisture project, and even though it took a turn that i didnt expect, i defently like the outcome of this more. To be able to use old technology with new one feels awsume. The hall sensor + magnet is much cheaper and easier to configure then the soil moisture sensor wich is another plus. This way its cheaper to scale up.
I cant wait to chew on it some more, get the repair parts for the 3d printer and put this to the test. And then scaling it up... togheter with a better tutorial! One thing i now for sure i will add, that i did forgett!, is a blue light when the pot is empty (if hall sensor signal is 0)