Intelligent Farming System Using IoT and Edge AI

Published  November 13, 2024   0
Intelligent Farming System Using IoT and Edge AI

For this contest, I want to build something around edge AI and IoT. I also want this to tackle a real-world problem and be relevant to my interest i.e. IoT. So, I'm relating the theme to my interest in developing an Intelligent Farming System Using IoT and Edge AI, which can help solve the real-world problem of wasteful farming.

My goal is to mainly deploy this in an agricultural field or greenhouse to automate tasks like irrigation by effectively monitoring different parameters such as soil moisture, humidity, temperature, etc. In addition to this, I’ll train the voice recognition model of Sipeed Maixduino to detect the presence of animals in the area (specifically bird and cat for my use case).

By doing so, we collect data for sustainable agriculture and effective resource management. Furthermore, the initiative intends to meet the present agricultural industry's demands:

"Lower the environmental impact of farming while simultaneously producing more food for the increasing global population."

The concept is achievable, but the project's complexity lies in making everything scalable, flexible, and reasonably priced. This will help farmers to boost their production and income by many times while using the same resources!!

Think Up

In my creative approach, I always prefer to record all of my ideas in a notebook based on my online research. After that, I select ideas based on their practicality, time required to implement them, and the cost.

During my research on this topic, I prepared a list of features that can be incorporated into the Intelligent Farming System Using IoT and Edge AI.

Memory Dump

I divided it in two parts, design choices and smart agriculture components. Design Choices refer to the engineering and architectural decisions made to ensure reliability, flexibility, and ease of use in real-world agricultural settings while on the other hand, Smart Agriculture Components refer to the advanced functionalities that leverage AI and IoT to enhance farming efficiency.

Design choices

  • Dedicated data processing node on the cloud or a different computer (say Raspberry Pi) for processing AI.

  • Powering the complete system using a battery.

  • IP-rated enclosure design.

  • Some kind of “Emergency response” feature so that the data will never get lost in case of any failure.

  • SD card for storing the data locally.

  • A real-time IoT dashboard (preferably custom-made and running locally) that can be used to monitor and manually control everything.

  • Anti-theft system.

  • Integrated LCD screen so that we can monitor key parameters without opening the dashboard.

  • Industry-standard protocol used for seamless integration.

  • Flexible input supply requirements.

  • Integrated SMPS so that it can be plugged directly into an AC 230V outlet.

  • Can be easily tailored as a “custom solution” for any farm.

Smart agriculture components

  • NPK monitoring in soil.

  • Soil moisture level monitoring.

  • Voice recognition-based Livestock health monitoring.

  • Voice recognition-based animals and bird detection.

  • Smart irrigation.

  • Agricultural drones. (Similar to ESP32-based drone designed by circuit digest itself!!)

  • AI-based weed detection.

  • AI-based crop health monitoring.

  • Automatic pest detection.

  • Precision Farming.

  • AI disease detection.

  • API-based weather monitoring to predict weather conditions like droughts and storms.

  • Water usage monitoring.

Shortlisted Ideas

Even though most of these ideas are possible, it is quite hard to implement all of them at once due to time and cost constraints. So, I have shortlisted some of them that will go into this project.

Design Choices

  • Powering the complete system using a battery.

  • Some kind of “Emergency response” feature so that the data will never get lost in case of any failure.

  • Flexible input supply requirements.

  • Real-time IoT dashboard (preferably custom-made, running locally) to monitor and manually control everything.

  • Can be easily tailored as a “custom solution” for any farm.

  • Anti-theft system.

Smart Agriculture Components

  • Soil moisture level monitoring.

  • Voice recognition-based animals and bird detection.

  • Smart irrigation.

  • API-based weather monitoring to predict weather conditions like droughts and storms.

  • Water usage monitoring.

In addition to this, I also want to expose some I/O pins for external circuits like relay control boards or motors in my final PCB. These can be used to control greenhouse windows, ventilation systems, lights, and many more to future-proof the system.

Note: I was also planning to use an LCD screen with the Sipeed Maixduino AI+IoT kit. However, the display that comes with the board got out of stock.

Components Required

Below is the list of components:

Microcontrollers and Boards

Sensors

Power Components

Actuators and Motors

Control and Connectivity Components

Mechanical and Assembly Components

  • Female connectors *7

  • PCB 2-pin connector *1

  • Enclosure (preferably IP-rated) *1

Navigate to this YouTube Video for Full Demonstration of the Project

Board Selection

As per the rules, we can only use one of three boards: Sipeed Maixduino AI+IoT kitMax78000FTHR Evaluation Kit, and Arduino UNO R4 WiFi

Once I had gone through the resources section of the contest page, for easy understanding, I made a comparison table.

 

Feature

Sipeed Maixduino AI+IoT Kit

Max78000FTHR Evaluation Kit

Arduino UNO R4 WiFi

Microcontroller

K210

MAX78000

ATmega4809

Processing Capability

Dual-core RISC-V

AI Accelerator

8-bit Microcontroller

AI Support

Yes (Neural Network)

Yes (Neural Network)

No

Connectivity

Wi-Fi, Bluetooth

I2C, SPI

Wi-Fi, Bluetooth

Digital Pins

14

6

14

Analog Pins

6

1

6

Operating Voltage

5V

3.3V

5V

Power Consumption

Low

Low

Moderate

Programming Environment

Arduino IDE

Custom (C/C++)

Arduino IDE

Size

Compact

Compact

Standard

Ideal Use Cases

AI applications, IoT projects

AI inference, machine learning

General-purpose projects

Summary

Best for AI and IoT applications with dual-core processing.

Focused on AI inference and machine learning tasks.

Versatile for general-purpose projects with built-in connectivity.

 Overall, the Sipeed Maixduino AI+IoT kit is my best choice due to its higher processing power, the highest number of GPIO pins, onboard components, and low power requirements.

Sensors Used

With a development board chosen, we can now focus on the sensors and actuators I'll utilize. My selection criteria are based on ease of availability, robustness, and interface with Arduino, which means that a library for that specific sensor is readily available. This would help me minimize problems with the supply chain and will be easily scalable thanks to its open-source nature.

Soil Moisture Sensor

This sensor measures soil moisture and will be part of my irrigation system.

Soil Moisture Sensor

 

YFS201 Water Flow Sensor

Both the overall volume of water utilized and the rate of water consumption are measured by this sensor. I'll use this to keep an eye on how much water my system uses.

Water Flow Sensor

 

Ultrasonic Sensor

I was looking for a sensor that’s accurate and can give us our water level in percentage. I selected the ultrasonic sensor as it is reliable, easy to find, and, a waterproof version of this sensor is reasonably priced.

Ultrasonic Sensor

 

DHT11 Sensor

Both temperature and humidity can be measured with this sensor. Although I am aware that there is a superior BME280 sensor out there, I am still going with the DHT11 sensor because BME280 is not readily available where I live. Furthermore, DHT11 provided me with rather accurate results during my testing when I compared it to the information from my phone's weather app.

DHT11 Sensor

 

Water Pump

This pump will be used to maintain the water flow whenever required in my irrigation system. I am using a DC 12V peristaltic water pump.

Water Pump

 

Sensors Purchase Links and Guides

The sensors I used are pretty popular and available in the local market. Additionally, Circuit Digest has an article explaining everything from working principles to a use case project for all the sensors and actuators I used in this project. For easy understanding, I compiled all the data along with an Amazon purchase link (For reference) in a tabular form below:

Sr. No.

Sensor name

Purchase Link

Circuit Digest Tutorial

1

Soil Moisture Sensor

Amazon

Sensor Guide

2

YFS201 Water Flow Sensor

Amazon

Sensor Guide

3

Ultrasonic Sensor

Amazon

Sensor Guide

4

DHT11 Sensor

Amazon

Sensor Guide

5

12V DC Water Pump

Amazon

-

 

IoT Dashboard

There are several choices when it comes to cloud platforms. However, it becomes expensive when we attempt to scale up. Due to this, I will use a platform that can be managed effortlessly without considering cost and other issues. I will be using Home Assistant as my IoT platform. I choose Home Assistant over other similar cloud platforms for the following reasons:

  • Home Assistant offers local control and data ownership, giving users complete control over their data and enhancing privacy, which is crucial in agriculture.

  • It provides extensive customization options, making it flexible, cost-effective, and adaptable to specific needs without recurring fees.

  • Running locally enhances reliability and performance with lower latency, essential for real-time applications like irrigation monitoring.

  • The platform’s community-driven development continuously adds new integrations and features, and its scalability allows users to expand without cloud service limitations.

  • Home Assistant avoids vendor lock-in, allowing users the freedom to choose the hardware and software that best suit their needs.

  • Home assistant has a great mobile app for both Android and iOS with features like real-time integration, seamless user experience, native device integration, secure remote access, offline access and many more.

Home Assistant Installation

The Home Assistant installation is quite straightforward using the Home Assistant installation documentation on its website. However, if you want more information, I highly recommend checking out the home assistant projects on Circuit Digest.

If you want to replicate my exact functionality, you can download the final ESP home file from the linked GitHub page. I’ll discuss more about it later in the code section of the article.

Note: I initially set up Home Assistant on a Raspberry Pi. Unfortunately, I encountered a critical issue when the microSD card socket on the Raspberry Pi failed, resulting in the loss of all my data due to microSD card corruption. Fortunately, I had a backup, which allowed me to successfully migrate my Home Assistant instance to a VirtualBox environment on my laptop.

Home Assistant Instance running in Virtual Box

Oracle VirtualBox Setup

 

Setup the virtual machine to run Home Assistant

Permitting Access to Oracle VirtualBox

 

Create an Account in the Home Assistant

Account Creation Home Assistant

 

I installed the ESP home file

ESPHome Installation

 

I discovered that it was operating as planned when everything was set up, and I was presented with the default dashboard (overview), which offered a simple arrangement for every entity.

Home Assistant Overview UI

 

It was functional but was not visually appealing and I found it challenging to keep track of different data categories efficiently in this setup.

To resolve this issue and for more clarity, I designed a custom dashboard with dedicated sections for Sensor Data, Water Usage, and data from the internet & security.

I added the reference images about designing it on my GitHub repository.

Home Assistant My Garden UI

 

Guest User

I added a guest user so that others can also experience my setup. You can click here more details on how I did it for this demonstration. However, I am I won't be running this server for too long, as it's only for demo purposes and I am running it in my laptop instead of a Raspberry Pi as I planned initially.

 

Home Assistant Settings UI

 

Bringing It All Together!

Once I had all the components with me, I tested the circuit on the breadboard, made necessary changes, and then put a circuit together.

Circuit

Final Circuit Diagram

 

Power Supply Connections

  • DC Jack: This connects to the 12V battery. It powers the entire circuit, including the pump, microcontrollers, and sensors. I used a DC jack for easy connect and disconnect.

  • The 12V input from the DC jack connects to two voltage regulators.

  • The 5V regulator provides 5V output, which powers both ESP8266 Node Mcu and Maixduino.

  • The 3V regulator supplies 3.3V to all sensors as both boards can only work with a 3.3 voltage level.

Pump and Pump Control Connections

  • Pump: The positive terminal of the pump connects to the 12V power line, while the negative terminal is connected to the drain pin of the IRFZ44N MOSFET.

  • Diode: This protects the circuit from voltage spikes generated by pump during it's operation.

  • IRFZ44N MOSFET

  1. The source pin is connected to the ground.

  2. Drain pin connects to the negative terminal of the pump.

  3. The gate pin is connected to a BC547 transistor through a resistor, which acts as a switch to control the MOSFET.

  • BC547 Transistor

  1. Collector pin connects to the gate of the IRFZ44N MOSFET.

  2. The base pin is connected to a 10kΩ resistor (R1), which then connects to a control pin on the NodeMCU.

  3. Emitter pin connects to ground.

  • Resistors

  1. R1 (10kΩ) connects between the NodeMCU control pin and the base of the BC547 transistor to limit the current.

  2. R2 (1kΩ) connects between the NodeMCU control pin and the base of the BC547 transistor for stable switching.

  • NodeMCU Connections

  • Power Pins: The NodeMCU is powered from the 5V voltage regulator, with GND connected to the common ground.

  • Pump Control Pin: The GPIO10 pin from the NodeMCU connects through resistors R1 and R2 to the base of the BC547 transistor, allowing the NodeMCU to control the pump through the MOSFET.

  • Sensor Pins

  1. Soil Moisture Sensors (SM1, SM2, SM3): Connected to GPIO14, GPIO12, and GPIO13 pins on the NodeMCU to check the soil moisture level.

  2. Water Level Sensor (LEVEL): Connects to Tx and RX pins, monitoring water levels in the tank.

  3. Flow Sensor (FLOW): Connects to GPIO4 pin to measure the water flow rate.

  4. DHT11 Sensor: Connects to GPIO2 pin on the NodeMCU.

  • Communication Pins: The NodeMCU’s GPIO16 and GPIO9 are connected to the Maixduino’s GPIO10 and GPIO11 to read data.

  • Maixduino K210 Connections

  • Power Pins: The Maixduino is powered by a 5V voltage regulator from the main power input.

  • Additional Sensors and Peripherals (if applicable): Depending on project requirements, other peripherals could be added to Maixduino's available GPIO pins. I left them exposed purposefully so that we can use them if required for added functionality.

  • Sensor Connections

  • All sensors are powered by the 3.3V voltage regulator.

  • The data pin connects to a GPIO pin on the NodeMCU to send data.

 

PCB design

I used EasyEDA to design both the circuit and PCB.

2D View

PCB Design 2D View

 

3D View

PCB Design 3D View


 

PCB Design 3D View

 

 

Circuit Assembly

I got the Sipeed Maixduino Board from Digikey and the rest of the components are sourced from the local market.

Once I had all the parts, the assembly is quite straightforward. However, after consulting with a PCB manufacturer, I found that the cost and delivery timelines were higher than anticipated which will result in missing the project timeline.

So, I assembled the circuit on a perf-board instead. This solution has worked reliably and allowed me to maintain functionality while meeting the project timeline.

Circuit Assembly

 

Circuit Testing

I performed two types of testing:

Bench Testing

While testing the system on the breadboard overnight, I observed that the Maixduino board was overheating significantly, posing a risk to the hardware and system stability. After checking everything, I concluded that running multiple functions, especially real-time sensor monitoring, data transmission, and voice processing, placed high demands on the board, causing it to overheat. In addition to this, the whole system will be installed outside with an ambient temperature hovering about 30 degrees Celsius right now. There is no chance, the board will survive for long.

To solve this issue, I restructured the system to split the tasks between two boards as a master-slave configuration. I used a separate ESP8266 NodeMCU to manage all sensor readings, direct actions, and communication with the IoT dashboard. The Maixduino on the other hand will only run the voice recognition model. The data is then relayed to the ESP8266, which will be sent to the IoT dashboard.

Once I created an updated test setup and ran it overnight, the system operated efficiently without overheating. In addition to this, I also used a bigger enclosure for better airflow. I also added a suitable 3D printable design for enclosure on my GitHub repository if you have access to a 3D printer.

This also demonstrates the benefits of distributing tasks across specialized boards in complex and demanding IoT systems like these.

Circuit Bench Testing

 

 

Field Testing

I tested the circuit on my balcony with my plants. I kept them running for 2 Days straight. It worked great!!

Circuit Field Testing

 

I powered the complete system using a 12V battery I put together using three Li-ion cells I had laying around. To ensure safety with the DIY version, I am using a BMS with safety features like over current protection, over charge protection, cell balancing, short circuit protection and many more.

12V Batteries with Lithium Ion Cells and PCB

 

Code

I am using Arduino IDE to write code for the Maixduino board and my ESP8266 is running the ESP home. The features that I am implementing are:

Sipeed Maixduino Board

  • Get the training data from the voice samples.

  • Use the data to update the “voice_model.h” file initially.

  • The Maixduino board interprets audio inputs and activates corresponding LEDs for cat or bird detection after deploying.

ESP8266 Node-MCU Board

  • It runs the ESP home firmware that allows us to connect it to the Home Assistant Platform.

  • Get soil moisture status using 3 soil moisture sensors.

  • Get ambient humidity and temperature values using a DHT11 sensor.

  • Get the water level in the tank using an ultrasonic sensor.

  • Get water usage data from the YFS201 sensor.

  • Control pump.

Home Assistant

  • It gets the environment data from the internet.

  • Uses “automation” to control the pump based on the inputs from the soil moisture sensors.

  • It makes back-ups so that the data never gets lost.

  • Notify me if something requires my attention.

  • Allow me to control the pump using the dashboard (if required).

 

 

Maixduino Board Code Explanation

Setup() Function

  • Serial Communication Initialization: Initializes serial communication at a baud rate of 115200 for viewing messages on the Serial Monitor.

  • Speech Recognition Setup: Prepares Maixduino’s speech recognition by loading specific cat and bird detection sound models.

  • LED Setup: Configures two LEDs: 

  1. Red LED for cat detection

  2. Blue LED for bird detection.

  • Sound Model Loading: Loads four variations each for cat and bird sounds into memory, allowing the Maixduino to detect and differentiate between these sounds.

Loop() Function

  • Listening for Sounds: Continuously waits for the detection of a sound. When a sound is detected, it tries to match it with the preloaded models for either a cat or bird sound.

  • Sound Detection Response: 

  1. Cat Sound: If detected, the red LED turns on, and "Cat Detected" is printed to the Serial Monitor.

  2. Bird Sound: If detected, the blue LED lights up, and "Bird Detected" is printed to the Serial Monitor.

  • Delay Between Detections: A 10-second delay is added to allow some time between detections and prevent repetitive detections from triggering continuously.

Helper Functions

  • recordModel() (only in the recording mode): Allows you to add new sounds by recording and storing them as part of the Maixduino’s model. This function is not running in regular detection mode.

Core Functionalities

  • Sound Recognition: The Maixduino recognizes sounds based on specific preloaded models for cat and bird calls.

  • Notification via LEDs and Serial Monitor: Based on the sound detected, the Maixduino turns on the appropriate LED and sends a message to the Serial Monitor.

  • Model Loading: The Maixduino loads multiple variations of each sound (cat and bird) into memory for accurate detection from voice_model.h file.

Voice_model.h:

  • Data for Voice Recognition Patterns: Contains arrays with pre-processed data which are key features for recognizing cat and bird sounds.

  • Keyword Models: Defines constants like fram_num_cat_0, representing the number of frames for a particular sound model. Each sound model is associated with arrays that contain values to match specific patterns.

  • Integration with Speech Recognition Library: This integration works with Maixduino’s Maix_Speech_Recognition library by providing the necessary data for the system to identify keywords accurately.

  • Overall, it provides the Maixduino with reference data for recognizing specific sound patterns allowing the device to recognize these sounds in real-time.

     

Generating Data for the voice_model.h File

Set the record mode to “1” to start recording the audio.

Voice Model CodeLaptop Display

 

Note: Initially I was thinking of training the model to detect the health of cows in the barn based on the calls they make (livestock health monitoring). I also gathered some audio of cow calls and asked a farmer to verify the health of the cow based on these calls so that I could train my model based on a real-life scenario. I was also going to install the system on his farm. However, due to some unavoidable circumstances, I had to drop that plan and use my balcony instead for a demonstration. Now the cow’s use case was far from practical on a balcony. Due to this, I decided to go with cat and bird calls and detect them as they cause chaos on my balcony. I think it is a common occurrence for most people with completely open balconies. Still, I added the files in the livestock health monitoring folder in my GitHub repository so that you can use them.

Compiled Voice Recognition Program for Maixduino

Compiled Voice Recognition Program for Maixduino

 

ESP8266 Code Explanation

The ESP8266 runs the ESP home firmware and most of the things are taken care of by the ESP home itself in the code. We only have to add the functionality we desire. It is a “.yaml” file and the explanation of the code is given below:

Basic ESPHome Setup:

  • The ESPHome section initializes the project with a device name (esp) and a friendly display name (ESP).

  • The esp8266 board type is defined as esp01_1m, specifying the memory size and model of the ESP8266.

Core Features:

  • Logging: Enabled using the logger component to track sensor and device logs for easy troubleshooting.

  • Home Assistant API: This API enables integration with Home Assistant, using encryption for secure communication.

  • Over-The-Air Updates (OTA): Allows remote firmware updates with a password for secure access.

Wi-Fi Configuration:

  • Wi-Fi Credentials: Using !secret for secure storage, the ssid and password are stored separately.

  • Fallback Hotspot: If Wi-Fi fails, a fallback hotspot activates for easy troubleshooting and reconfiguration. It creates an access point with a defined ssid and password.

Captive Portal:

  • This component creates a captive portal, making it easier to configure Wi-Fi by automatically displaying a configuration page if the ESP8266 fails to connect.

Time Component:

  • Home Assistant Time: This component fetches time from Home Assistant, which is used to calculate daily water consumption for the water flow sensor.

Sensors:

Humidity and Temperature:

  • Configured with a DHT11 sensor on GPIO2, it provides temperature and humidity readings.

  • Updated every 60 seconds, ideal for monitoring ambient conditions.

Water Tank Level:

  • Uses an ultrasonic sensor connected to trigger (GPIO1) and echo (GPIO3) pins.

  • Provides a percentage reading for the tank level, calculated using a lambda filter to map distance into a 0-100% scale.

  • Updates every 30 seconds to track changes in water levels.

Water Flow Rate:

  • The YFS201 water flow sensor is attached to GPIO4, measuring flow rate in Liters per minute.

  • A filter with a calibration factor (0.1) adjusts the pulse count for accurate flow measurement.

Total Water Usage:

  • The total daily energy sensor calculates cumulative water usage in Liters, based on the water flow rate sensor.

Digital Soil Moisture Sensors:

  • Each sensor is set up as a binary sensor on GPIO14, GPIO12, and GPIO13.

  • device_class: moisture sets the sensor as a moisture sensor in Home Assistant.

  • delayed_on and delayed_off filters add a 2-second delay to stabilize the readings.

Cat and Bird Detection:

  • Monitor GPIO9 (Cat Detection) and GPIO16 (Bird Detection) of Maixduino board.

  • Both are configured as motion sensors, triggering based on sound signals from the external Maixduino setup.

Pump Control:

  • A switch component on GPIO10 controls a pump.

Home Assistant Automation

In Home Assistant, automation refers to the setup of rules that trigger specific actions based on certain conditions, enabling smart, hands-free control of connected devices.

In my case, I’ll use these to control the pump based on the status of the soil (wet or dry).

I also added an automation that will track my device's location in the zone. This will also act as an “anti-theft system” and notify me if the device leaves the zone.

Home Assistant Automation

 

Working Setup

The system can be used in both indoor and outdoor conditions. For my setup, I set it up on my balcony.

I installed the three soil moisture sensors in my pot at three defined positions left, right, and Centre.

The ultrasonic sensor (facing downward), along with the pump and water flow sensor, is installed on a piece of wooden board I had lying around. I will keep this on the water bucket I will be using as my water tank to check the water level.

To charge the battery, I am using a 12V DC adapter.

Working Setup


 

Working Setup

 

Finally, we can watch and control all this in real-time using the Home Assistant dashboard on any device.

Controlling Access Via Home Assistant

 

Resources

https://circuitdigest.com/microcontroller-projects/interfacing-soil-moisture-sensor-with-arduino-uno

https://circuitdigest.com/microcontroller-projects/arduino-based-water-flow-sensor

https://circuitdigest.com/microcontroller-projects/interface-arduino-with-ultrasonic-sensor

https://circuitdigest.com/microcontroller-projects/interfacing-dht11-sensor-with-arduino

https://maixduino.sipeed.com/en/

https://www.home-assistant.io

https://www.home-assistant.io/installation/

https://circuitdigest.com/tags/home-assistant

https://youtu.be/jriRW4rGQp4?si=X4wQOh2qzPY-FV_t

https://github.com/andriyadi/Maix-SpeechRecognizer

https://everythingsmarthome.co.uk/free-remote-access-for-home-assistant-with-cloudlfare/

Have any question realated to this Article?

Ask Our Community Members