Try โ€‚โ€‰HackMD

Tutorial on how to connect a soil moisture sensor

Course: Introduction to Applied Internet of Things (1DT305)

Student: Sara Enander (se223hv)

The aim of this project is to connect a soil moisture sensor to Ubidots using a lopy4 and pycom expansion board, and to notify user when moisture is below 30% of maximum moisture value. Depending on skill level and previous experience this project might take a few hours up to a couple of days to finalise

Objective

I chose to do this project because I love having lots of plants in my apartment, but I find it hard to keep track of when I have watered the plants and if I have watered enough - or maybe too much! That is why I chose to connect a sensor to measure the soil moisture and send the information to a platform and alert user when the soil is dry. This will also give insight to how often the plants need to be watered in order to stay on a relative constant moisture level.

Material

The material used for this project is listed below:

| Material | Short specification |
| โ€“โ€“โ€“โ€“ | โ€“โ€“โ€“โ€“ | โ€“โ€“โ€“โ€“ |
|Pycom lopy4 with headers | Processing data recieved from sensor and sending to platform
| Pycom expansion board | To mount the lopy4 on and organise outlets and inputs |
|Soil moisture sensor | Consists of two parts: an electronic board and a probe. Probe is put in soil to collect data by measuring the resistance between the two legs on the probe. |
|Breadboard | Makes it easy to connect the sensor in a neat way |
| Jumper wires | Connecting the sensor to the expansion board |

All of the material was purchased from electrokit.com and cost in total about 1000 SEK.

Computer setup

To handle the data from the device it was connected to the Integrated Development Environment Atom. This IDE was chosen because it seemed easy learn and to use, and well suited for this project. First a new device was set up on the Pybytes webside, and then the pybytes package was installed in Atom. The device was then connected using an activation string from the Pybytes website. The code was then uploaded to the device, and the data from the sensor was sent to Ubidots for display. No extra installations had to be done.

Putting everything together

The sensor was connected to the hardware as shown in Figure 1:

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 โ†’

Figure 1: Circuit diagram
Picture sources: https://docs.pycom.io/gettingstarted/connection/lopy4/
https://www.electrokit.com/uploads/productfile/41015/41015738_-_Soil_Moisture_Sensor.pdf

How the setup looks in real life, with the breadboard, is shown in Figure 2:

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 โ†’

Figure 2: The setup

Platform

The platform used to present and visualise the data was Ubidots. The free version was used. Ubidots was chosen because it had a user-friendly interface and many ways to present the data in a clear and simple way. It also contained the function to notfiy the user when the moisture level was too low. In the first tests the platform pybytes was used, and it was also easy to use, and to present data in a neat way. Ubdots were chosen over Pybytes because Ubidots contains more functions that suited the project.

Code

The code in the main program is presented below:

from time import sleep from machine import Pin,ADC from network import WLAN import urequests as requests import pycom pycom.heartbeat(False) #Stops the blue light from blinking #UBIDOTS TOKEN = "your Ubidots token" wlan = WLAN(mode=WLAN.STA) wlan.antenna(WLAN.INT_ANT) # Assigning Wi-Fi credentials wlan.connect("your wifi SSID", auth=(WLAN.WPA2, "your wifi password"), timeout=5000) while not wlan.isconnected (): machine.idle() print("Connected to Wifi\n") #Assigning pins on expansion board adc = ADC() apin = adc.channel(pin='P14',attn=ADC.ATTN_11DB) p_out = Pin('P9', mode = Pin.OUT, pull = Pin.PULL_DOWN) #Calibration: maxValue = 1106 #measured from very wet soil minValue = 4095 #measured in very dry soil def moist_sensor(): p_out.value(1) #powers sensor sleep(5) rawValue = apin.value() #Reads value p_out.value(0) finalValue= round((((rawValue - minValue)/ (maxValue-minValue))*100), 1) #Calculates value percentage if finalValue <= 0: #Makes sure no negative values are sent finalValue=0 if finalValue > 100: #Makes sure value is never >100% finalValue=100 return finalValue def build_json(variable1, value1): #Creates json object try: data = {variable1: {"value": value1},} return data except: return None def post_var(device, value): #Sends the data to Ubidots 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("moisture", value) 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: number=int(moist_sensor()) #Reads value from sensor post_var("soil-moisture-sensor", number) #posts it to ubidots sleep(3595) #Waits one hour before new measurement

The inital code was copied from the following guide: https://help.ubidots.com/en/articles/961994-connect-any-pycom-board-to-ubidots-using-wi-fi-over-http. The code was then altered to read data from the sensor connected and to suit the needs for this project.

Transmitting the data / connectivity

The data was transmitted from the device over wi-fi, once every hour. The transport protocol MQTT was used. All this was implemented using the guide linked to above.

Presenting the data

The Ubidots dashboard contains of two widgets, showed in Figure 3:


Figure 3: The Ubidots dashboard

The line chart is showing the last 100 measurements of the moisture level, which is equivalent to about 4 days back in time. The other widget is showing the current moisture level. The user is notified by e-mail when the moisture level is below 30%, an example is shown in Figure 4. This is easily added under the tag "Data" and then "Events". The trigger moisture level can easily be adjusted depending on the plant's water needs.


Figure 4: The email received by user when moisture level is too low

Finalizing the design

The final setup can be seen in Figure below:

hej
Figure: The final setup

To make this project more usable and neater a box for the Pycom unit and breadboard could be used. A waterproof box would also be needed if the soil moisture of outdoor plants would be measured. Multiple sensors could also be connected to measure the moisture in different pot plants.