# IoT - Plant Watering Reminder through Email
**Robin Hellmers**
**LNU credentials**: *rh222sb*
Nothing has been as irregular as me watering my plants. That is why I wanted to create some sort of system for me to keep track of when I last watered my plants. Also I wanted some sort of notification system to remind me to water my plants, which would remind me when it is time and keep reminding me with a certain interval until I have done it.
Approximate time to do, with access to everything: **2 hours**
## Objective
Create an electronic circuit based upon the Pycom LyPo4 (as I have it available) with a sensor. This will keep track of when I last did water my plants and remind me through Email when it is time to water it again. This is dependent on when the sensor last recognised that the plant was watered. The device should use WiFi as the server communication method.
The main reason for creating this project is because of my habit of killing my plants by not watering them regulary. By doing this I will probably learn a lot about IoT and I will probably cut on by costs regarding plants for my household as I hope to keep them alive.
This project will learn me how to use wifi connection with electronics, how to connect it to MQTT brokers and how to make it email on events.
## Material
- **Pycom LoPy4 Microprocessor** [About £35](https://pycom.io/product/lopy4/)
- Could be replaced with a Pycom WiPy as only WiFi will be the wireless protocol used. The brain of the circuit.

- **Pycom Expansion Board 3.0** [About £16](https://pycom.io/product/expansion-board-3-0/)
- Here only used to program the device through usb

- **Soil moisture sensor, capacative, analog** [About 89 SEK](https://www.electrokit.com/produkt/jordfuktighetssensor-kapacitiv-analog/)
- Recommended to be replaced with an with integrated serial protocol such as I2C. Meant to detect me watering my plants

- **Jumper wires male-male for breadboard** [About 29 SEK](https://www.electrokit.com/produkt/labbsladd-20-pin-15cm-hane-hane/)
- To easily hook everything up
- **Breadboard** [About 59 SEK](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/)
- To easily hook everything up
- **Battery 9 V** [About 19 SEK](https://www.electrokit.com/produkt/batteri-9v-gp-extra-heavy-duty-6f22/)
- To easily hook everything up. Easy and portable
- **Connector 9 V battery** [About 10 SEK](https://www.electrokit.com/produkt/batterikontakt-9v-rigid-krage-150mm/)
- To easily hook everything up. Easy and portable

- **5 V regulator** [About 4 SEK](https://www.electrokit.com/produkt/l7805cv-to-220-spanningsregulator-5v-1a/)
- To power the LoPy4 and sensor

- **2 x 4.7 kOhm through hole resistors** [About 1 SEK/part](https://www.electrokit.com/produkt/motstand-kolfilm-0-25w-4-7ohm-4r7/)
- Probably have to be changed to other values for your specific sensor

## Computer Setup
IDE used in this project: **Visual Studio Code**
- Chosen because of its lightweight and previously good experience with it.
Extensions to VSCode: **Pymakr**
- In order to program the LoPy4 and have access to REPL.
Pycom have a guide for this:
https://docs.pycom.io/gettingstarted/installation/pymakr/
Far down, under the terminal in VSCode, there will be an *Upload* button which uploads the files. But first, one must have chosen the right folder through *File->Open Folder...*
Extra: **Node.js**
- In order to use the above.
Flash the Pycom hardware according to their guide:
https://docs.pycom.io/gettingstarted/installation/firmwaretool/
## Putting everything together
Schematic of setup:

For something more permanent, one should not use a breadboard. Rather solder something together or order a PCB.
## Platform
The cloud platformed used here is Ubidots. It was really easy to use and also free.
Set up events:
> *If* variable **state == 2**
> *then* mail "You need to water your plants"
> *then* set variable **state = 0**
> *If* variable **state == 1**
> *then* mail "You just watered your plants, good job!"
> *then* set variable **state = 0**
This will further send email to any given address. The states are set from the LoPy4 through MQTT protocol.
## Code
Here is the code provided in a Github repository:
https://github.com/robinhellmers/iot-summer-2020
Some variables in the code have to be changed by the user.
For the connection:
- **WIFI_SSID** The SSID of your wifi
- **WIFI_PASSWORD** The password of your wifi
- **UBIDOTS_TOKEN** The token of your Ubidots account
Set how often you want to water your plants with the variables given below. Each time you water your plants, a counter will be set to zero and count up to this and then notify you through Ubidots and email. The time is both **days** and **hours** together.
- **WATER_INTERVAL_DAYS** With the number of days the watering interval should occur. Integer between 0 and 31
- **WATER_INTERVAL_HOURS** With the number of hours the watering interval should occur. Integer between 0 and 23
Calibration variables:
- **ADC_VOLTAGE_CALIBRATION** Number representing millivolts from when outputting the internal voltage reference within the LoPy4. Measured by using external voltage measurement equipment. Use the following code:
`from machine import ADC`
`adc = ADC()`
`adc.vref_to_pin('P22')`
Then measure the voltage on pin P22 and set the variable to this integer value in millivolts.
The code will make a decision wether the plants have been watered or not by saving a reference voltage, then measuring once every given interval a given number of times before setting a new reference voltage. This is in order to prevent from triggering the device into thinking that the plants have been watered, when really the voltage have been changed a little bit over a longer time. This could be because of reasons such as the battery and voltage regulator not giving the same voltage output all the time, but actually slowly given a lower and lower value output.
- **TIME_DELAY_MEASUREMENT** Is the time in seconds between each and every measurement.
- **NUM_MEASUREMENTS** Is the number of measurements done before a new reference voltage is set.
- **SENSOR_THRESHOLD** Is the percentage of the reference voltage that the measured voltage must have dropped from the reference voltage, in order to aknowledge it like the plants were watered.
- Value = 0.1 means that a measured voltage of `(1 - 0.1)*reference_voltage` or less will trigger as if the plants were watered.
## Transmitting the data / connectivity
The data is sent each time the plant is watered and also each time it is time for a notification about watering the plant, which is dependet on the user settings and how often this person water the plants.
The wifi wireless protocol is used to connect to a wifi router and the internet. Through this, the MQTT transport protocol is used to communicate with Ubidots broker.
## Presenting the data
Data is saved each and every time the plant is watered, also each and every time the alert for watering the plants is activated. This is dependent on what the user chooses and how often the user actually water their plants.
The email service through Ubidots.

The diagram/graph possibilities through Ubidots:

## Finalizing the design
The final setup with battery taped to the back of the breadboard:


The ADC of the LoPy4 is, as standard, able to have an input of **0 to 1.1 V** but when using the internal voltage dividers it is able to have an as high of an input of **3.3 V**. Thereby if my analog soil humidity sensor is powered with **3.3 V** and thereby have an output of maximum **3.3 V**, this setup should not need any external voltage dividers.
When buying the analog soil humidity sensor, the picture showed a TL555I which can be powered with **2 to 12 V** and the description of the product said that it could be powered with **3.3 to 5 V**. This was not true as when I recieved the product, the timer on it was a **NE555** which should be powered with **4.5 to 16 V**. I did either way try to power it with **3.3 V**, but with no success. When I instead try to power it with **5 V**, it gives some output.
Having the sensor in the air, without any voltage divider on the output, the voltage is about **4.0 V**. This means that I need some voltage divider in order for the ADC to be able to measure the whole output interval of the sensor and also in order to avoid damaging the LoPy4 and its ADC.
Assuming the maximum voltage output of the sensor to be **5 V**, using the internal divider for it to accept up to **3.3 V**, the resistance have to be divided with at least **1.6**. Thereby using a voltage divider that divides the voltage with **2** as the same resistor values then can be used.
Using 2 x 1 kOhm for the voltage divider should bring the voltage output, having the sensor in the air, down from **4 V** to **2 V** as seen in the calculations below.
$$
V_1 = V_2\frac{R_1}{R_1 + R_2} = 4\ V \cdot \frac{1\ k\Omega}{1\ k\Omega + 1\ k\Omega} = 2\ V
$$
But the result is not **2.0 V**, it is **0.5 V**.
When instead using a voltage divider with 2 x 100 kOhm, the result is **1.65 V** instead of **2.0 V**.
When instead using a voltage divider with 2 x 1 MOhm, the result is **1.85 V** instead of **2.0 V**.

After doing these measurements, I realise that the soil moisture sensor probably is not able to put out enough current and thereby the voltage drops instead. Also after some research on some forums, there is one common problem with the sensor as it is shipped with a break between the resistor **R4** and **GND** on the board. This problem was apparent on my board as well, thereby I soldered these together as seen in the figure below.

This did not fix this problem though. From all of this I would not recommend this sensor as it is of low quality. The safest bet would be to use a sensor with integrated serial protocol such as I2C.
### Conclusion
The device does the work, but the sensor had to be calibrated with very fine detail of values in order to get it working. If another sensor is chosen, everything will work much better.
The software works as intended and also does rest of the circuit. Thereby this was mainly a success.