In today’s era of innovative technology, monitoring your garden’s moisture levels can be automated efficiently using a Raspberry Pi, a moisture sensor, and AWS IoT services. This step-by-step guide will walk you through creating a system that sends email alerts when soil moisture levels drop below a certain threshold, ensuring your plants get the care they need.

Introduction: AWS IoT Moisture Sensor Project Overview

This project combines the power of Raspberry Pi, AWS IoT Core, and Amazon SNS to create an innovative soil moisture monitoring system. You’ll configure the Raspberry Pi to read moisture levels from a sensor and use AWS IoT Core to trigger email notifications when specific thresholds are reached.

Step 1: Creating the AWS IoT Policy for Secure Communication

  1. Log in to the AWS Management Console and navigate to AWS IoT Core.
  2. Create a policy under the Security tab by specifying the actions and resources your Raspberry Pi will require (e.g., IoT:Connect, IoT:Publish, IoT:Subscribe).
  3. Define the policy as follows:

    {

    “Version”: “2012-10-17”,

    “Statement”: [

        {

            “Effect”: “Allow”,

            “Action”: [

                “iot:Connect”,

                “iot:Publish”,

                “iot:Subscribe”,

                “iot:Receive”

            ],

            “Resource”: “arn:aws:iot:<region>:<account-id>:*”

        }

    ]

}

  1. Attach the policy to a new or existing IoT certificate.

Step 2: Registering Your Raspberry Pi as an AWS IoT Thing

  1. Go to the Manage tab in AWS IoT Core and select Things.
  2. Create a new IoT Thing and download the generated certificates.
  3. Attach the policy created in Step 1 to the certificates.
  4. Configure your Raspberry Pi with these certificates to securely communicate with AWS IoT Core.

Step 3: Setting Up Amazon SNS for Email Alerts

  1. Navigate to the Amazon SNS console and create a new topic (e.g., moisture-alert).
  2. Add an email subscription to the topic to receive notifications.
  3. Confirm the subscription by clicking the link sent to your email.

Step 4: Creating an AWS IoT Rule for Triggering Email Notifications

  1. Under AWS IoT Core, create a new rule in the Act section.
  2. Define the SQL query to filter messages based on the moisture level:
    SELECT * FROM ‘moisture/readings’ WHERE moisture_level < 30
  3. Add an action to send the notification via the SNS topic created in Step 3.
  4. Save and activate the rule.

Step 5: Hardware Setup: Connecting Raspberry Pi and the Moisture Sensor

  1. Connect the moisture sensor to the Raspberry Pi GPIO pins:
    • VCC to the 3.3V pin.
    • GND to the ground pin.
    • Signal to a GPIO pin (e.g., GPIO18).
  2. Test the sensor using Python to ensure it reads moisture levels correctly.

Step 6: Running the Script and Monitoring Moisture Levels

  1. Install the required Python libraries on your Raspberry Pi:
    pip install AWSIoTPythonSDK
  2. Create a Python script to:
  • Read moisture levels.
  • Publish the data to AWS IoT Core.


from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient

import RPi.GPIO as GPIO

import time

# MQTT Client setup

client = AWSIoTMQTTClient(“RaspberryPi”)

client.configureEndpoint(“your-endpoint.amazonaws.com”, 8883)

client.configureCredentials(“root-CA.crt”, “private.pem.key”, “certificate.pem.crt”)

# GPIO setup

sensor_pin = 18

GPIO.setmode(GPIO.BCM)

GPIO.setup(sensor_pin, GPIO.IN)

def read_moisture():

    return GPIO.input(sensor_pin)

client.connect()

while True:

    moisture_level = read_moisture()

    message = {“moisture_level”: moisture_level}

    client.publish(“moisture/readings”, str(message), 0)

    time.sleep(60)

  1. Run the script and monitor moisture levels in the AWS IoT console or via email notifications.

Conclusion

With this setup, you’ve created a robust IoT-based moisture monitoring system. You can use AWS IoT Core and Raspberry Pi to ensure your garden thrives without constant manual intervention. This project’s versatility opens doors to scaling and integrating more sensors or automating irrigation systems.

References

Tutorial: Monitoring soil moisture with AWS IoT and Raspberry Pi

Setting up your Raspberry Pi and moisture sensor