Try   HackMD

Device that checks if plant needs water

By Hugo Dahl - hd222gt

This tutorial will take you through how to make a an IoT device that checks a plants soil moisture level and visualise this on the Pybytes platform and in a smartphone app. The focus on this tutorial will be on the IoT device which in this case is the Lopy4 and how to transfer the data to the Pybytes platform.

Approximated time to complete this project:

~5h to create the device and send the data to the Pybytes platform, depending on previous experiences.

~7-12h to also create an app and redirect the data from the Pybytes servers.

Table of Contents

Objective

The idea for this project came to mind because I have trouble remembering to water my plants. With the help of this device I can now check if my plant needs water, even when I am not home. By visualizing this data I might be able to see a good watering schedule for my plant.

Material

To build this device you need the following material:

  • Lopy4 (329.90 Kr) - A micropython programmable development board with wifi and other network capabilities.
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • Expansion Board V3.0 (170.26 Kr) - An expansion board for the Lopy4 which gives it more inputs.
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • Soil Moisture Sensor (29.00 Kr) - A sensor that measures the moisture in soil.
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • Breadboard and Jumper cables (299.90 Kr) - Used for connnecting the sensor to the Lopy4. I got mine from an electronics kit that contained a lot more than just the breadboard and cables. Can be bought seperately pretty cheap.
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Computer setup

The Lopy4 is programmed in micropython and to do this I used Visual Studio Code where i wrote my code and the Pymakr Plugin for Visual Studio Code which I used to easily run and upload my code to the Lopy4.

Pymakr plugin bar to easily run and upload code to the Lopy4:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Step by step computer setup

  1. Install necessary drivers (only if you are using Windows 7)

  2. Create new device on the Pybytes platform If you don't have an account you need to create that

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →




  3. Update Firmware on Lopy4 by installing and then running the firmware upgrade tool




  1. Install Node.JS in order to run the VS Code Pymakr plugin
  2. Install VS Code if you do not already have it
  3. Install the Pymakr VS Code plugin

Putting everything togheter

Following the picture below should be pretty straight forward. Ground on the Lopy4 goes to ground on the sensor. The 3v3 pin on the Lopy4 goes to VCC pin on the sensor. Lastly the A0 pin on the sensor goes to any pin on the Lopy4 that is capable of analog input. In my case i chose P16. The Lopy4 is powered by its USB input, the power comes from a wall outlet or a PC when coding and during development.

If i where to continue to work on this project i would create a case for the device and solder the cables to create a neat device with a small footprint.

Platform

The platform that is used to store and visualize the data is Pybytes and it's an easy to use cloud platform from the creators of the Lopy4. Sending data is easy because of the Pybytes library that comes already on the Lopy4 unit after flashing it. Once you get data to the platform you set up simple graphs to visualize the data. Though you dont really have full control over the database and the data. In order to get the data to my mobile app I had to use a platform called Pipedream which got the data from Pybytes via a webhook. I could then consume the data in my app from pipedream via REST API calls. This way I could use the data I got from Pipedream in my app. Probably better solutions out there but this is how i solved the problem.

Link to a video on how to setup a webhook on Pybytes

The code

This code sends the value from the sensor to the pybytes server. This code is located in the main.py file on the Lopy4.

from machine import ADC
from machine import Pin
import time

moistureSensorPin = 'P16'

moisturePin = Pin(moistureSensorPin, mode=Pin.IN)  # set up pin mode to input

adc = ADC(bits=10) # create an ADC object bits=10 means range 0-1024 the lower value the less moisture detected

apinMoisture = adc.channel(attn=ADC.ATTN_11DB, pin=moistureSensorPin)   # create an analog pin on P16;  attn=ADC.ATTN_11DB measures voltage from 0.1 to 3.3v

while True:
    value = apinMoisture() # read analog value from sensor
    pybytes.send_signal(1, value) # send value to pybytes server
    time.sleep(43200)  # wait 12 h, before sending next time

Transmitting the data / connectivity

The data is sent to Pybytes servers from the Lopy4 via WiFi using the Pybytes library(which uses MQTT). A webhook then sends the data to Pipedream via a HTTP request. I can then get the data from pipedream to use in my mobile application.

Presenting the data

The data can be seen on the pybytes dashboard. The data is saved as often as you send it, and the data that is stored one month on the Pybytes servers before it gets removed. Pybytes has an easy way to display the signals it gets. To create a graph, you click on the signal you want to create a graph for(Device > Signals > Specific Signal) then you click on the "create new display" button. Now you can choose from a few different type of graphs. Once the graph is created you have the option to add that graph to the dashboard after clicking the "Edit" button on the graph.

Note: This screenshot were taken when the correct interval(1 signal each 12th hour) was only on the latest value

Finalizing the design

The project resulted in a simple app that tells you if the plant needs water or not and a working device that sends sensor data to a server.

So all in all a decent protoype for checking if a plant needs water. If I were to do things differently in this project I might have taken a different route when it comes to what platform to store the sent data on. Choosing a different platform or storing the data locally might have made the process of getting the data on the mobile application easier. Things you learn after doing these type of projects. And to make the device less like a prototype I would have soldered all the cables together.

This is how the data is shown on the mobile app. It shows the 5 latest signals: