Arduino Irrgation System

I have been slowly accumulating Arduino parts to build more smart devices around my house. A couple of weeks ago I decided it was time to start a Sprinkler system and I wanted to make it “smart”. The idea is to use Arduino parts so Home Assistant could control the water valves to turn on and off depending on the weather, soil moister levels, how much sun we get in a day, and even delay the sprinklers if there is motion in the backyard.

This article is a work in progress. Follow it to keep up to date.

My parts list that I’m using for this build:

Some code

I decided to use MQTT for the communications. This makes it easy to add to Home Assistant and easy to add to my phone if I want to manually turn on the sprinklers.

#include <UIPEthernet.h> // Used for Ethernet
#include <PubSubClient.h> // Used for MQTT

const char* device_name = "watersystem-1";
const char* mqtt_server = "mqtt.mydomain.com";
const char* mqtt_user = "homeassistant";
const char* mqtt_password = "password";
const char* mqtt_subscribe_topic = "sensor/water/set";

EthernetClient ethClient;
PubSubClient client;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };                                      

int pinArray[] = {2,3,4,5,6,7,8,9}; // define the pins that will connect to the relays

long previousMillis = 0;

void mqttCallbackHandler(char* topic, byte* payload, unsigned int length) {
  payload[length] = '\0';
  
  String strPayload = String((char*)payload);

  Serial.println("Callback Called... ");

  for (int i = 0; i < (sizeof(pinArray)/sizeof(int)); i++){
    if(strPayload == String("V")+i+":ON") {
      digitalWrite(pinArray[0], LOW);    // turn on this relay
      Serial.println(String("Relay ")+i+" ON");
    } else if(strPayload == String("V")+i+":OFF") {
      digitalWrite(pinArray[0], HIGH);    // turn off this relay
      Serial.println(String("Relay ")+i+" OFF");
    }
  }
}

void setup() {
  Serial.begin(9600);

  for (int i = 0; i < (sizeof(pinArray)/sizeof(int)); i++){
    pinMode(pinArray[i], OUTPUT);
    digitalWrite(pinArray[i], HIGH);
  }
  
  Ethernet.begin(mac);

  Serial.print("IP Address: ");
  Serial.println(Ethernet.localIP());
  
  // setup mqtt client
  client.setClient(ethClient);
  client.setServer(mqtt_server, 1883);
  client.setCallback(mqttCallbackHandler);
}

void reconnect() {
  Serial.print("Attempting MQTT connection...");
  Serial.print("Connecting to ");Serial.print(mqtt_server);Serial.print(" as ");Serial.println(device_name);
      
  while (!client.connected()) {
      if (client.connect(device_name, mqtt_user, mqtt_password)) {
        Serial.println("Connected!");
        client.subscribe(mqtt_subscribe_topic);
      } else {
        Serial.print("Failed, rc=");Serial.print(client.state());Serial.println(" try again in 5 seconds.");
        delay(5000);
      }
    }
}

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

  client.loop();
}
Follow
( 0 Followers )
X

Follow

E-mail : *

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.