Nijat Hasanov
Published © GPL3+

Automatic Watering System

Automate watering of your garden with the help of Cayenne!

IntermediateFull instructions provided2 hours6,652

Things used in this project

Hardware components

WeMos D1 R1
I have an old version of WeMos D1. You can use new version.
×2
SparkFun Soil Moisture Sensor (with Screw Terminals)
SparkFun Soil Moisture Sensor (with Screw Terminals)
Mine is from Keyestudio
×1
3/4" DC12V N/C Solenoid Valve
×1
SparkFun Wall Adapter Power Supply - 9VDC 650mA
It is used to power WeMos. You can use anything that powers it.
×1
SparkFun Wall Adapter Power Supply - 12VDC 600mA
It is used to power both one of WeMos boards and solenoid valve(requires 12V).
×1
SparkFun Resistor 1K Ohm 1/4th Watt
×1
STMicroelectronics TIP121 - NPN Power Transistor
×1
IN4007 Diode
×1
SparkFun Jumper Wires
Just a few jumper wires...
×1

Software apps and online services

Cayenne
myDevices Cayenne
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Sensor Controller

Circuit of sensor controller. I tighten it on simple piece of wood. But it should have casing in case of continuous use.

Valve Controller

Circuit of Valve Controller. Board should be powered with 12V.

Code

Valve Controller

Arduino
This code should be uploaded to WeMos which is connected to solenoid valve. Do not forget to change WiFi and Cayenne details.
// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling. 

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define VALVE D10 //digital pin 10 is connected to transistor which controls the valve
#define DHT_PIN D8 //DHT temperature and humidity sensor is connected to digital pin 8
#define DHTTYPE DHT11
#include <CayenneMQTTESP8266.h>
#include <DHT.h>

DHT dht(DHT_PIN, DHTTYPE, 11); //define DHT sensor

// WiFi network info.
char ssid[] = "your wifi ssid";
char wifiPassword[] = "your wifi password";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "your Cayenne username";
char password[] = "your Cayenne password";
char clientID[] = "your device client ID";

unsigned long lastMillis = 0;

void setup() 
{
	  Serial.begin(9600);
    pinMode(VALVE,OUTPUT);
	  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
    dht.begin();
}

void loop()
{
	  Cayenne.loop();
    if (millis() - lastMillis > 1000) //read and send data every second
        {
            lastMillis = millis();
            float t = dht.readTemperature(); //read DHT sensor
            float h = dht.readHumidity();

            if(isnan(t) || isnan(h))
            {
                Serial.println("FAIL Reading DHT11!"); //if there is fail in   reading, start loop again
                return;
            }
            Cayenne.virtualWrite(V2,t); //send temperature to channel 2 on     Cayenne
            Cayenne.virtualWrite(V3,h); //send humidity to channel 3 on Cayenne

            Serial.println(t);
            Serial.println(h);
        }
}


CAYENNE_IN(1) //data from Cayenne channel 1(VALVE widget)
{
   int value = getValue.asInt(); //accept and convert value to Int
   Serial.println(value);
   digitalWrite(VALVE,value); //change valve state
}

Sensor Controller

Arduino
This code should be uploaded to WeMos which is connected to soil moisture sensor. Do not forget to change WiFi and Cayenne details.
// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling. 

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid[] = "your wifi ssid";
char wifiPassword[] = "your wifi password";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "your MQTT username";
char password[] = "your MQTT password";
char clientID[] = "your clientID";

unsigned long lastMillis = 0;
int soilData; //it will store sensor reading after calculations
int THRES=40; //default sensor reading threshold which will switch valve state

void setup() 
{
    Serial.begin(9600);
    Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() 
{
    Cayenne.loop();

    
    if (millis() - lastMillis > 500) 
    {
    lastMillis = millis();

    int sum=0; //stores sum of 16 readings from sensor
    for(int i=0;i<16;i++) //to increase consistency and avoid wild fluctuations, we take 16 sensor readings and average them
    {
        int val=map(analogRead(A0),0,1024,0,100); //map the sensor reading to between 0 and 100 to show it in percentage
        sum += val;
    }
    soilData=sum/16; //store final average value in soilData
    Cayenne.virtualWrite(V0,soilData); //send value to Cayenne
    Serial.print("Sent: ");
    Serial.println(soilData);
    if(soilData>THRES) //decide valve state according to sensor reading and send result to Cayenne(IfUp widget)
    {
        Cayenne.virtualWrite(V5,0);
    }
    else
    {
        Cayenne.virtualWrite(V5,1);    
    }
   
    }
}

CAYENNE_IN(2) //accept data from Threshold widget in Cayenne
{
    THRES = getValue.asInt();
    Serial.print("Threshold changed to: ");
    Serial.println(THRES);
}

DHT sensor library

If you use my code, you will need to add this library to your Arduino IDE.

Adafruit Unified Sensor Driver

DHT library will not work without this one.

Credits

Nijat Hasanov

Nijat Hasanov

0 projects • 3 followers
Student,hardware enthusiast

Comments