Tech Gyan Set
Published © MIT

Smart Agriculture Motor Protection + Auto ON/OFF System

IoT based smart system jo motor ko dry run aur voltage fault se bachata hai aur irrigation ko automatic banata hai. 🚜⚑

BeginnerFull instructions provided8 hours16
Smart Agriculture Motor Protection + Auto ON/OFF System

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

1️⃣ Libraries

C/C++
πŸ”Ή Purpose:
These libraries enable WiFi connectivity, MQTT communication, and voltage/current monitoring using the PZEM module.

πŸ”Ή Role in System:
They allow ESP32 to connect to the internet, communicate with the cloud, and monitor motor electrical parameters.
/*
====================================================
1️⃣ LIBRARIES
Required libraries for WiFi, MQTT and PZEM
====================================================
*/

#include <WiFi.h>              
#include <PubSubClient.h>      
#include <PZEM004Tv30.h>       

πŸ”Ÿ Setup Function

C/C++
πŸ”Ή Purpose:
Initializes hardware and network configuration.

πŸ”Ή Role in System:
Prepares the system for real-time operation.
void setup() {

  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, HIGH);

  Serial.begin(115200);

  setup_wifi();

  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
}

1️⃣1️⃣ Main Loop Logic

C/C++
πŸ”Ή Purpose:
Implements the core logic of voltage protection, dry run detection, and irrigation automation.

πŸ”Ή Role in System:
Acts as the intelligent control engine of the entire motor protection system.
void loop() {

  if (!client.connected()) {
    reconnect();
  }

  client.loop();

  float voltage = pzem.voltage();
  float current = pzem.current();

  int soilValue = analogRead(SOIL_PIN);
  int moisturePercent = map(soilValue, 4095, 1500, 0, 100);

  client.publish(topic_voltage, String(voltage).c_str());
  client.publish(topic_current, String(current).c_str());
  client.publish(topic_moisture, String(moisturePercent).c_str());

  if (voltage > overVoltage || voltage < underVoltage) {
    digitalWrite(RELAY_PIN, HIGH);
    motorState = false;
    faultState = true;
    client.publish(topic_fault, "Voltage Fault");
  }

  if (current < dryRunCurrent && motorState) {
    digitalWrite(RELAY_PIN, HIGH);
    motorState = false;
    faultState = true;
    client.publish(topic_fault, "Dry Run Detected");
  }

  if (!faultState) {

    if (moisturePercent < moistureLow) {
      digitalWrite(RELAY_PIN, LOW);
      motorState = true;
    }

    if (moisturePercent > moistureHigh) {
      digitalWrite(RELAY_PIN, HIGH);
      motorState = false;
    }
  }

  client.publish(topic_status, motorState ? "ON" : "OFF");

  delay(2000);
}

2️⃣ WiFi Configuration

C/C++
πŸ”Ή Purpose:
Defines the WiFi credentials required for internet connectivity.

πŸ”Ή Role in System:
Allows ESP32 to connect to the local network for remote monitoring and control.
/*
====================================================
2️⃣ WIFI SETTINGS
Enter your WiFi credentials
====================================================
*/

const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

3️⃣ MQTT Configuration

C/C++
πŸ”Ή Purpose:
Defines the MQTT broker address and communication topics.

πŸ”Ή Role in System:
Enables real-time cloud communication between the motor controller and mobile/web dashboard.
/*
====================================================
3️⃣ MQTT SETTINGS
Broker details and topics
====================================================
*/

const char* mqtt_server = "broker.hivemq.com";
const int mqtt_port = 1883;

const char* topic_control = "agri/motor/control";
const char* topic_status  = "agri/motor/status";
const char* topic_voltage = "agri/motor/voltage";
const char* topic_current = "agri/motor/current";
const char* topic_moisture = "agri/motor/moisture";
const char* topic_fault   = "agri/motor/fault";

4️⃣ Pin Configuration

C/C++
πŸ”Ή Purpose:
Assigns GPIO pins for relay control, soil sensor input, and PZEM communication.

πŸ”Ή Role in System:
Ensures proper hardware communication and motor switching.
/*
====================================================
4️⃣ PIN DEFINITIONS
Relay and sensor pin setup
====================================================
*/

#define RELAY_PIN 4
#define SOIL_PIN 34

#define PZEM_RX 16
#define PZEM_TX 17

5️⃣ Object Initialization

C/C++
πŸ”Ή Purpose:
Creates communication objects for network and sensor handling.

πŸ”Ή Role in System:
Handles data transmission and real-time energy monitoring.
/*
====================================================
5️⃣ OBJECT INITIALIZATION
WiFi, MQTT and PZEM objects
====================================================
*/

WiFiClient espClient;
PubSubClient client(espClient);
HardwareSerial pzemSerial(2);
PZEM004Tv30 pzem(pzemSerial, PZEM_RX, PZEM_TX);

6️⃣ Protection Parameters

C/C++
πŸ”Ή Purpose:
Defines safety limits for voltage, current, and soil moisture.

πŸ”Ή Role in System:
Acts as decision-making parameters for motor protection and automation.
/*
====================================================
6️⃣ PROTECTION LIMITS
Voltage and dry run thresholds
====================================================
*/

float overVoltage = 260.0;
float underVoltage = 180.0;
float dryRunCurrent = 2.0;

int moistureLow = 30;
int moistureHigh = 70;

bool motorState = false;
bool faultState = false;

7️⃣ WiFi Connection Function

C/C++
πŸ”Ή Purpose:
Establishes connection between ESP32 and WiFi router.

πŸ”Ή Role in System:
Enables remote monitoring and control capabilities.
void setup_wifi() {
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

8️⃣ MQTT Callback Function

C/C++
πŸ”Ή Purpose:
Receives ON/OFF commands from the MQTT server.

πŸ”Ή Role in System:
Allows farmers to remotely control the motor through a mobile app.
void callback(char* topic, byte* message, unsigned int length) {

  String msg;

  for (int i = 0; i < length; i++) {
    msg += (char)message[i];
  }

  if (String(topic) == topic_control) {

    if (msg == "ON" && !faultState) {
      digitalWrite(RELAY_PIN, LOW);
      motorState = true;
    }

    if (msg == "OFF") {
      digitalWrite(RELAY_PIN, HIGH);
      motorState = false;
    }
  }
}

9️⃣ MQTT Reconnection Function

C/C++
πŸ”Ή Purpose:
Maintains a stable MQTT connection.

πŸ”Ή Role in System:
Ensures continuous cloud communication without interruption.
void reconnect() {

  while (!client.connected()) {

    if (client.connect("AgriMotorClient")) {
      client.subscribe(topic_control);
    } else {
      delay(2000);
    }
  }
}

Credits

Tech Gyan Set
13 projects β€’ 5 followers
Thanks to Tech Gyan Set .

Comments