# AI-Based Agricultural Anomaly Detection Using kiwisiot

> Source: <https://www.hackster.io/yuvansris2002/ai-based-agricultural-anomaly-detection-using-kiwisiot-2b82ec>
> Published: 2026-11-16 13:00:49+00:00

Project introduction

AI-Based Agricultural Anomaly Detection is a smart farming project that monitors agricultural conditions using an ESP32 and different sensors.

The system continuously monitors:

- Soil moisture level.
- Temperature.
- Air humidity.
- Light intensity.

AI-based anomaly detection identifies unusual readings, such as extremely dry soil, unusually high temperature, or abnormal humidity.

The ESP32 sends sensor data to the KiwisIoT dashboard, where farmers can monitor agricultural conditions and identify abnormal situations.

Main objective: Detect unusual agricultural conditions early and help farmers protect their crops.

Step 1: Install Arduino IDE

Download: Arduino IDE

Step 2: Install ESP32 board

1. Open Arduino IDE.

2. Go to Boards Manager.

3. Search for ESP32.

4. Install Espressif ESP32.

5. Select your ESP32 Dev Module board.

Step 3: Install libraries

Open Library Manager and install:

- DHT sensor library by Adafruit.
- Adafruit Unified Sensor.
- BH1750 by Christopher Laws.

Install the KiwisIoT library manually:

1. Visit KiwisIoT.

2. In Arduino IDE, select Sketch → Include Library → Add.ZIP Library.

The KiwisIoT SDK supports ESP32, Wi-Fi, MQTT communication and sensor-data publishing.

7. Complete ESP32 program

This code:

- Reads all three sensors.
- Converts soil readings into a percentage.
- Detects abnormal conditions.
- Sends sensor values to KiwisIoT.
- Sends anomaly status to the dashboard.

Code:

#include <WiFi.h>

#include <Wire.h>

#include <KiwisIoT.h>

#include <DHT.h>

#include <BH1750.h>

// WiFi details

const char* ssid = "YOUR_WIFI_NAME";

const char* password = "YOUR_WIFI_PASSWORD";

// KiwisIoT panel topic ID

const char* topic = "YOUR_PANEL_TOPIC_ID";

KiwisIoT kiwisiot(ssid, password, topic);

// Sensor pins

#define SOIL_PIN 34

#define DHT_PIN 4

#define DHTTYPE DHT22

DHT dht(DHT_PIN, DHTTYPE);

BH1750 lightMeter;

// Soil calibration values.

// Replace these after testing your own sensor.

const int DRY_VALUE = 3000;

const int WET_VALUE = 1200;

unsigned long lastSend = 0;

const unsigned long interval = 5000;

void setup() {

Serial.begin(115200);

dht.begin();

Wire.begin(21, 22);

lightMeter.begin();

kiwisiot.begin();

Serial.println("Agricultural Monitoring Started");

}

void loop() {

kiwisiot.run();

if (millis() - lastSend >= interval) {

lastSend = millis();

// Read soil moisture

int soilRaw = analogRead(SOIL_PIN);

int soil = map(

soilRaw,

DRY_VALUE,

WET_VALUE,

0,

100

);

soil = constrain(soil, 0, 100);

// Read temperature and humidity

float temperature = dht.readTemperature();

float humidity = dht.readHumidity();

// Read light intensity

float light = lightMeter.readLightLevel();

// Check sensor validity

if (isnan(temperature) ||

isnan(humidity) ||

light < 0) {

Serial.println("Sensor reading error");

return;

}

// Anomaly detection

bool anomaly = false;

if (soil < 20) {

anomaly = true;

Serial.println("ALERT: Soil is too dry");

}

if (temperature > 40) {

anomaly = true;

Serial.println("ALERT: High temperature");

}

if (humidity < 30) {

anomaly = true;

Serial.println("ALERT: Low humidity");

}

// Send data to KiwisIoT

kiwisiot.send("0", soil);

kiwisiot.send("1", temperature);

kiwisiot.send("2", humidity);

kiwisiot.send("6", light);

kiwisiot.send("7", anomaly ? 1 : 0);

// Serial output

Serial.println("---------------------");

Serial.print("Soil Moisture: ");

Serial.print(soil);

Serial.println("%");

Serial.print("Temperature: ");

Serial.print(temperature);

Serial.println(" C");

Serial.print("Humidity: ");

Serial.print(humidity);

Serial.println("%");

Serial.print("Light: ");

Serial.print(light);

Serial.println(" lux");

Serial.print("Anomaly: ");

Serial.println(anomaly ? "DETECTED" : "NORMAL");

Serial.println("---------------------");

}

}

Important: The code uses the KiwisIoT send(channel, value) pattern documented in its agriculture example. The channel numbers above are the proposed channel mapping for this project. Configure the dashboard widgets to match them.

KiwisIoT+1

8. KiwisIoT dashboard setup

3insys.com

Step 1: Create an account

Open KiwisIoT and sign in.

Step 2: Create a panel

1. Open My Panels.

2. Click Create New Panel.

3. Enter the panel name:

AI Agricultural Anomaly Detection

4. Save the panel.

Step 3: Add dashboard widgets

Create these widgets and connect them to the matching channels.

Widget name Channel Display

Soil Moisture 0 Gauge

Temperature 1 Gauge

Humidity 2 Gauge

Light Intensity 6 Gauge

Anomaly Status 7 Indicator

Also add charts for soil moisture, temperature and humidity.

Step 4: Connect ESP32

1. Copy the dashboard topic ID.

2. Paste it into the YOUR_PANEL_TOPIC_ID section of the code.

3. Enter your Wi-Fi name and password.

4. Connect ESP32 using USB.

5. Select the correct COM port.

6. Upload the program.

Step 5: Monitor readings

Open your KiwisIoT dashboard. You should see live sensor values when the ESP32 successfully connects and publishes data.

Advantages

1. Helps identify dry soil conditions.

2. Detects abnormal environmental readings.

3. Supports remote farm monitoring.

4. Reduces the need for continuous manual checking.

5. Can be extended with automatic irrigation.

6. Can be upgraded using machine learning.

Project Summary:

[Read more](<javascript:void(0)>)
