CrdewittRyan DenHartighMatthew Branan
Published © GPL3+

IOT Project: Integrated Sprinker System

The Integrated Sprinkler System can tell when the weather is not appropriate, and turn itself on and off as needed.

IntermediateFull instructions provided2 hours251
IOT Project: Integrated Sprinker System

Things used in this project

Hardware components

Argon
Particle Argon
×3
Breadboard (generic)
Breadboard (generic)
×4
Jumper wires (generic)
Jumper wires (generic)
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
RFID reader (generic)
×1
water level sensor
×1
Relay (generic)
×3
SparkFun Breadboard Power Supply 5V/3.3V
SparkFun Breadboard Power Supply 5V/3.3V
×1

Software apps and online services

Google Sheets
Google Sheets
Particle Build Web IDE
Particle Build Web IDE
ThingSpeak API
ThingSpeak API

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Tape, Electrical
Tape, Electrical
Solder Wire, Lead Free
Solder Wire, Lead Free
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Argon Relay

Argon RFID Scanner

Argon Water Level and Temperature Sensor

Code

Access Badge Argon

C/C++
Argon that reads RFID badge swipes to activate sprinkler.
/* FILE:    RC522_RFID_Module_Example
   DATE:    23/03/14
  https://build.particle.io/build/5a37b640e59b62ae52000fb8#flash VERSION: 0.2.1 Spark
REVISIONS:
29/08/16    Version 0.2.1, modified by ScruffR: corrected Software SPI sample
23/03/14    Version 0.2, modified by Paul Kourany to run on Spark Core
            with added support for Software SPI
24/07/13    Created version 0.1
This is an example of how to use the RC522 RFID module. The module allows reading
and writing to various types of RFID devices and can be found in our MFRC-522 
(HCMODU0016) and Ultimate RFID (HCARDU0068) kits. This example Arduino sketch uses
the RFID library written by Miguel Balboa to read the pre-programmed serial number 
from RFID cards and tags supplied with our RFID kits. Snapshots and links to the 
library are available on our support forum.
PINOUT:
RC522 MODULE    SPARK HARD SPI  SPARK SOFT SPI
SS                  A2              ANY
SCK                 A3              ANY
MOSI                A5              ANY
MISO                A4              ANY
IRQ                 N/A             N/A
GND                 GND             GND
RST                 D9              ANY
3.3V                3.3V            3.3V
You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/


/* Include the RFID library */
/* SEE RFID.h for selecting Hardware or Software SPI modes */
#include "RFID.h"

/* Define the pins used for the SS (SDA) and RST (reset) pins for BOTH hardware and software SPI */
/* Change as required */
#define SS_PIN      A2      // Same pin used as hardware SPI (SS)
#define RST_PIN     D2

/* Define the pins used for the DATA OUT (MOSI), DATA IN (MISO) and CLOCK (SCK) pins for SOFTWARE SPI ONLY */
/* Change as required and may be same as hardware SPI as listed in comments */
#define MOSI_PIN    D3      // hardware SPI: A5
#define MISO_PIN    D4      //     "     " : A4
#define SCK_PIN     D5      //     "     " : A3

int Switchstate = 0; //A variable that holds the current state of the sprinkler relay
int Laststate = 0; //A variable that holds the last state of the sprinkler relay
int tempdata = 0; //A variable that holds the last state of the temp sensor
int waterdata = 0; //A variable that holds the last state of the water level sensor
int led1 = D7; //A variable for the LED output

/* Create an instance of the RFID library */
#if defined(_USE_SOFT_SPI_)
    RFID RC522(SS_PIN, RST_PIN, MOSI_PIN, MISO_PIN, SCK_PIN);    // Software SPI
#else
    RFID RC522(SS_PIN, RST_PIN);                                 // Hardware SPI
#endif


void setup()
{ 
  Serial.begin(9600);
  
#if !defined(_USE_SOFT_SPI_)
  /* Enable the HW SPI interface */
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV8);
  SPI.begin();
#endif

  /* Initialise the RFID reader */
    RC522.init();
    
    pinMode(led1, OUTPUT);

    Particle.subscribe("Temp", f1, MY_DEVICES); //When "Temp" is published, run the relayon function
    Particle.subscribe("RawWater", level, MY_DEVICES); //When "Water" is published, run the relayon function
    Particle.subscribe("Relay", Relay, MY_DEVICES); //When "Relay" is published, run the relayon function
  
}

void loop()
{
  /* Temporary loop counter */
  uint8_t i;

  /* Has a card been detected? */
  if (RC522.isCard())
  {
    /* If so then get its serial number */
    RC522.readCardSerial();

    Serial.println("Card detected:");
    Particle.publish("Card detected");

    /* Output the serial number to the UART */
    for(i = 0; i <= 4; i++)
    {
      Serial.print(RC522.serNum[i],HEX);
      Serial.print(" ");
    }
    Serial.println();
  }
  else
      Serial.println("Card NOT detected:");
      
  delay(1000);
}

void f1(const char *topic, const char *data) //subscribe data retreival for temperature from Argon 2
{ 
    
    tempdata = String(data).toInt(); //convert subscribe data to int
    Serial.println(tempdata);
    
}
    
void level(const char *topic, const char *data) //subscribe data retreival for water level from Argon 2
{ 
    
    waterdata = String(data).toInt(); //convert subscribe data to int
    Serial.println(waterdata);
    
}

void Relay(const char *topic, const char *data) //subscribe data retreival for Sprinkler Relay State from Argon 3
{ 
   Switchstate = String(data).toInt(); //convert subscribe data to int
    if (Switchstate == 1 && Laststate == 0)
    {
        digitalWrite(led1, HIGH);
        Laststate = Switchstate;
        Serial.println(Switchstate);
        Serial.println(Laststate);
        Serial.println("Its on!");
    }
    else if (Switchstate == 0 && Laststate == 1)
    {
        digitalWrite(led1, LOW);
        Laststate = Switchstate;
        Serial.println(Switchstate);
        Serial.println(Laststate);
        Serial.println("Its off!");
    }
}

Sensor Array Argon

C/C++
Argon that reads all of the sensor data and determines if it is ok to turn on the sprinkler.
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>

// This example assumes the sensor to be plugged into CONN2
#define DHTPIN D6     // what pin we're connected to

// Here we define the type of sensor used
#define DHTTYPE DHT11        // DHT 11 

DHT dht(DHTPIN, DHTTYPE);

bool humid = false;

int waterSens = A0; //define water sensor analog input
int waterVal; //define the water sensor raw input value
float waterInch; //define the water sensor output value in inches
float rawlevel; //define the water sensor raw output value
int Switchstate = 0; //A variable that holds the current state of the sprinkler relay
int Laststate = 0; //A variable that holds the last state of the sprinkler relay
int led1 = D7; //A variable for the LED output

TCPClient client;

unsigned long myChannelNumber =2031349;
const char * myWriteAPIKey ="IHTJSGLV24XX4MEL";
const char * myReadAPIKey ="XZP2TVGFZSTAJO1Z";

void setup() {
    
    Serial.begin(19200); //Start Serial output at 19200 baudrate
    dht.begin(); //Start DHT11 Sensor
    pinMode(waterSens, INPUT);//set water sensor as an input
    ThingSpeak.begin(client); //Start Thinkgspeak client
    
    pinMode(led1, OUTPUT);

    Particle.subscribe("Card detected", Activation, MY_DEVICES); //When "Card detected" is published, run the relayon function. Triggered from Argon 1
    Particle.subscribe("Relay", Relay, MY_DEVICES); //When "Relay" is published, run the relayon function. Data from Argon 3

}

void loop() {
    
    // Reading temperature or humidity takes about 250 milliseconds
    // Sensor readings may also be up to 2 seconds 
    // Read humidity
    float h1 = dht.getHumidity();
    // Read temperature as Farenheit
    float f1 = dht.getTempFarenheit();

    Serial.print(dht.getHumidity());
    Serial.println("h1");
    Serial.print(dht.getTempFarenheit());
    Serial.println("f1");
    
     if (isnan(h1) || isnan(f1)) {
         Serial.println("Failed to read from DHT sensor!");
        return;
    }

    Particle.publish("Temp", String(f1), ALL_DEVICES);
    Particle.publish("Humidity",String(h1), ALL_DEVICES);

    waterVal = analogRead(waterSens); //read the water sensor

    waterInch = 0.00039139*waterVal; //Calculate water level in inches from analog value

    Serial.println("waterInch");
    String level = String(waterInch);
    String rawlevel = String(waterVal);
    Particle.publish("Water", level, ALL_DEVICES);
    Particle.publish("RawWater", rawlevel, ALL_DEVICES);

    ThingSpeak.setField(1,f1); //Primes Temperature data for ThingSpeak
    ThingSpeak.setField(2,h1); //Primes Humidity data for ThingSpeak
    ThingSpeak.setField(3,waterInch); //Primes Water Level data for ThingSpeak
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); //sends my data to thingspeak
    
    delay(1800000);

}

//Declare functions here

void Activation(const char *topic, const char *data) //subscribe data retreival for Card detected from Argon 1
{
    Serial.println("Activation Requested");
    Particle.publish("Activation Requested");
}

void Relay(const char *topic, const char *data) //subscribe data retreival for Sprinkler Relay State from Argon 3
{ 
   Switchstate = String(data).toInt(); //convert subscribe data to int
    if (Switchstate == 1 && Laststate == 0)
    {
        digitalWrite(led1, HIGH);
        Laststate = Switchstate;
        Serial.println(Switchstate);
        Serial.println(Laststate);
        Serial.println("Its on!");
    }
    else if (Switchstate == 0 && Laststate == 1)
    {
        digitalWrite(led1, LOW);
        Laststate = Switchstate;
        Serial.println(Switchstate);
        Serial.println(Laststate);
        Serial.println("Its off!");
    }
}

Sprinkler Relay Argon

C/C++
Argon that triggers the sprinkler relay.
// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>

//Enable code to start running before connecting to the web
SYSTEM_THREAD(ENABLED);

//Declare variables here
int led1 = D7;
int Relay = D0; //Valve trigger
int Relay2 = D1; //Valve reset
int Relay3 = D2; //Power supply
int Switchstate = 0; //a variable that will hold the current switch state
int tempdata = 0;
int waterdata = 0;

TCPClient client;

unsigned long myChannelNumber =2031349;
const char * myWriteAPIKey ="IHTJSGLV24XX4MEL";
const char * myReadAPIKey ="XZP2TVGFZSTAJO1Z";

void setup ()
{
//Start Serial Coms
Serial.begin(19200);

//Start Thinkgspeak client
ThingSpeak.begin(client); 

//Declare variables inputs, outputs
pinMode(led1, OUTPUT);
pinMode(Relay, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);

//Declare subscription and function
Particle.subscribe("Card detected", Relayon, MY_DEVICES); //When "Card detected" is published, run the relayon function. Triggered from Argon 1
Particle.subscribe("Temp", f1, MY_DEVICES); //When "Temp" is published, run the relayon function. Data sent from Argon 2.
Particle.subscribe("RawWater", level, MY_DEVICES); //When "Water" is published, run the relayon function. Data sent from Argon 2.


}

void loop()
{

    Serial.println(tempdata);
    Particle.publish("tempdata", String(tempdata), ALL_DEVICES);
    Particle.publish("waterdata", String(waterdata), ALL_DEVICES);
    
    delay(600000);
    
}

//Declare functions here
void Relayon(const char *topic, const char *data)
{
   if (tempdata >= 20 && waterdata <= 2400)
   {
    Particle.publish("Relay On");
    Switchstate = 1;
    Particle.publish("Relay", String(Switchstate), ALL_DEVICES);
    ThingSpeak.setField(4,Switchstate); //Primes Sprinkler data for ThingSpeak
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); //sends my data to thingspeak
    digitalWrite(led1, HIGH); //Turns on LED to indicate the sprinkler output is triggered
    digitalWrite(Relay3, HIGH); //Turn on Power for the sprinkler output
    delay(5000); //Pause for 5 second
    digitalWrite(Relay3, LOW); //Power relay off
    digitalWrite(Relay, HIGH); //Flip solenoid wire polarity
    digitalWrite(Relay2, HIGH); //Flip solenoid wire polarity
    delay(1000); //Pause for 1 second
    digitalWrite(Relay3, HIGH); //Turn on Power for the sprinkler output
    delay(1000); //Pause for 1 second
    digitalWrite(Relay3, LOW); //Power relay off
    digitalWrite(Relay, LOW); //Flip solenoid wire polarity
    digitalWrite(Relay2, LOW); //Flip solenoid wire polarity
    Particle.publish("Relay Off");
    Switchstate = 0;
    Particle.publish("Relay", String(Switchstate), ALL_DEVICES);
    ThingSpeak.setField(4,Switchstate); //Primes Sprinkler data for ThingSpeak
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); //sends my data to thingspeak
    
    return;
   }
   else if (waterdata > 2400)
   {
     Particle.publish("Water level too high for sprinkler!");  
   }
   else
   {
     Particle.publish("Too cold for sprinkler!");  
   }
   
   

}

void f1(const char *topic, const char *data) //subscribe data retreival for temperature from Argon 2
{ 
    
    tempdata = String(data).toInt(); //convert subscribe data to int
    Serial.println(tempdata);
    
    }
    
void level(const char *topic, const char *data) //subscribe data retreival for water level from Argon 2
{ 
    
    waterdata = String(data).toInt(); //convert subscribe data to int
    Serial.println(waterdata);
    
    }

Credits

Crdewitt

Crdewitt

1 project • 0 followers
Ryan DenHartigh

Ryan DenHartigh

1 project • 0 followers
Matthew Branan

Matthew Branan

0 projects • 0 followers
Thanks to rob7, Richard, amontanes, and dotpointer.

Comments

Add projectSign up / Login