Announcement

Collapse
No announcement yet.

Sprinkler Control C# source and setup

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Sprinkler Control C# source and setup

    I researched this for a long time and never found what I wanted, so I went out and just made this up. IMO very simple setup and control, run as event from homeseer. I probably won't have much time to provide other details, so don't be mad if I am not quick to respond. But I thought I would share because I searched for a low cost solution for a long time to no avail.

    First, I purchased a Cai networks webcontrol board (which provides more functionality than just this function). I won't post a link, just google it (mentioned several times on this site, although the price has gone up quite a bit since original posts).
    looks like this....(green thing on left). This will allow intranet control of the 5v on/off signal that operates and completes the relay switch.

    http://s17.postimage.org/5lua9awcr/photo_3.jpg
    Then a relay board.

    http://s14.postimage.org/txrxsfhkt/photo_4.jpg

    For those who don't know, a relay is just an electronic switch. When the (in this case i think 5 volt) signal is sent from the weeder to the relay board it closes the "switch" and completes the circuit to the solenoid valves allowing the water to flow in the designated zone. Connect up the weeder i/o board to the relay board and now you have ability to control the sprinklers via the web.

    Now to integrate with homeseer....I made a program in C# (console app to keep it simple). My coding is probably not up to par for some people, if you don't like it, fix it and post it. But I basically settled on a webcommand to turn the sprinkler on or off. The error coding could be a lot better (I know this) but I thought I would post it anyway along with the source so that people could do what they want with it. I wish someone would have done this for me, but then again I probably wouldn't have learned all i needed to about it, part of the fun!

    Enjoy. ... I will try to answer a couple of questions if they are posed, but can make no promises.
    Last edited by pikeaggie; December 22, 2012, 11:09 PM. Reason: Wrong board

    #2
    Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Mail;
    using System.Data;
    using System.IO;
    using System.Reflection;
    namespace SprinklerControl
    {
    class Program
    {
    static void Main(string[] args)
    {
    double minutes=0;
    bool broken = true;
    string successOn="", successOff="";
    // This is the full directory and exe name
    String fullAppName = Assembly.GetExecutingAssembly().Location;

    // This strips off the exe name
    String fullAppPath = Path.GetDirectoryName(fullAppName);
    TimeSpan timeToPause = new TimeSpan();
    DataSet sprinklerZone = new DataSet();
    sprinklerZone.ReadXml(fullAppPath + @"\Xml\ZoneTimers.xml");
    int rowCount=sprinklerZone.Tables[0].Rows.Count;
    WebClient downloader = new WebClient();

    for (int i = 1; i <= rowCount; i++)
    {
    //find the zone assciated with specific zone
    foreach (DataRow dr in sprinklerZone.Tables[0].Rows)
    {
    if (dr[0].ToString() == i.ToString())
    {
    minutes = Convert.ToDouble(dr[1]);
    }
    }
    timeToPause = TimeSpan.FromMinutes(minutes);
    while (successOn != "success")
    {
    // you should put your proper IP address in this line and run test to make sure this url will work.

    string urlOn = "http://192.16x.x.xxx:xx/api/setttloutput.cgi?output=" + i + "&state=1";
    successOn = downloader.DownloadString(urlOn);
    successOn = htmlRemover.StripTagsRegex(successOn);
    successOn = successOn.Replace("\r\n", string.Empty);
    if (successOn != "success" && broken)
    {
    sendMessage(i, successOn);
    broken = false;
    }
    }
    successOn = ""; broken = true;
    Console.WriteLine("Zone " + i + " is on for " + timeToPause + " minutes!");
    System.Threading.Thread.Sleep(timeToPause);
    while (successOff != "success")
    {
    // you should put your proper IP address in this line and run test to make sure this url will work.
    string urlOff = "http://192.16x.x.xxx:xx/api/setttloutput.cgi?output=" + i + "&state=0";
    successOff = downloader.DownloadString(urlOff);
    successOff = htmlRemover.StripTagsRegex(successOff);
    successOff = successOff.Replace("\r\n", string.Empty);
    }
    if (successOff != "success" && broken)
    {
    sendMessage(i, successOff);
    broken = false;
    }
    successOff = ""; broken = true;
    Console.WriteLine("Zone " + i + " is off now.");
    }
    }
    public static void sendMessage(int i, string whatsWrong)
    {
    // make sure you put your email in this line as well as password, so that you can send yourself something if it goes wrong...this will keep this error from flooding your yard...but it not all inclusive and should be tested prior to use.
    var fromAddress = new MailAddress("email@gmail.com", "Jack Attack");
    var toAddress = new MailAddress("email@yahoo.com", "Jack Attack");
    const string fromPassword = "Pa$$word";
    const string subject = "Sprinkler Problem";
    string body = "there is a sprinkler problem in zone "+ i + ". " + "The error reads " + whatsWrong;
    var smtp = new SmtpClient
    {
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
    };
    using (var message = new MailMessage(fromAddress, toAddress)
    {
    Subject = subject,
    Body = body
    })
    {
    smtp.Send(message);
    }

    }
    }

    }

    Comment


      #3
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Text.RegularExpressions;
      namespace SprinklerControl
      {
      public static class htmlRemover
      {

      /// <summary>
      /// Methods to remove HTML from strings.
      /// </summary>

      /// <summary>
      /// Remove HTML from string with Regex.
      /// </summary>
      public static string StripTagsRegex(string source)
      {
      return Regex.Replace(source, "<.*?>", string.Empty);
      }
      /// <summary>
      /// Compiled regular expression for performance.
      /// </summary>
      static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
      /// <summary>
      /// Remove HTML from string with compiled Regex.
      /// </summary>
      public static string StripTagsRegexCompiled(string source)
      {
      return _htmlRegex.Replace(source, string.Empty);
      }
      /// <summary>
      /// Remove HTML tags from string using char array.
      /// </summary>
      public static string StripTagsCharArray(string source)
      {
      char[] array = new char[source.Length];
      int arrayIndex = 0;
      bool inside = false;
      for (int i = 0; i < source.Length; i++)
      {
      char let = source[i];
      if (let == '<')
      {
      inside = true;
      continue;
      }
      if (let == '>')
      {
      inside = false;
      continue;
      }
      if (!inside)
      {
      array[arrayIndex] = let;
      arrayIndex++;
      }
      }
      return new string(array, 0, arrayIndex);
      }
      }
      }

      Comment


        #4
        htmlremover.xml ----this one is adjustable for the zones you have at your house

        <?xml version="1.0" encoding="utf-8" ?>
        <Sprinklers>

        <zone>
        <id>1</id>
        <OpTime>10</OpTime>
        <description>Front Flower Beds</description>
        </zone>
        <zone>
        <id>2</id>
        <OpTime>10</OpTime>
        <description>Garden</description>
        </zone>
        <zone>
        <id>3</id>
        <OpTime>10</OpTime>
        <description>Side Yard Front</description>
        </zone>
        <zone>
        <id>4</id>
        <OpTime>10</OpTime>
        <description>Front Curb Rotors</description>
        </zone>
        <zone>
        <id>5</id>
        <OpTime>10</OpTime>
        <description>Left Side Back</description>
        </zone>
        <zone>
        <id>6</id>
        <OpTime>10</OpTime>
        <description>Back Fence</description>
        </zone>
        <zone>
        <id>7</id>
        <OpTime>10</OpTime>
        <description>Front Left</description>
        </zone>
        <zone>
        <id>8</id>
        <OpTime>10</OpTime>
        <description>Front Left of Driveway</description>
        </zone>

        </Sprinklers>

        Comment


          #5
          neat, hardware wise, looks like you are using the same RELAY board that I am using for my PAWS device.

          --Dan
          Tasker, to a person who does Homeautomation...is like walking up to a Crack Treatment facility with a truck full of 3lb bags of crack. Then for each person that walks in and out smack them in the face with an open bag.

          Comment

          Working...
          X