OpenSprinkler Forums Hardware Questions OpenSprinkler Pi (OSPi) Google Calendar code – my plan and a request

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #22411

    jonmc56
    Member

    I’ve taken the Google calendar python demo and split it into two pieces. The first piece queries my sprinkler calendar for ‘today’, and gets all the schedules for today. It writes the schedules to an XML file on the Pi. The second piece will read the schedule XML file periodically, and control the valves. My thinking is that I can schedule the first piece to run just once daily (just after midnight) to get schedules, rather than hitting the calendar all day (and dealing with potential network issues). I will schedule the second piece to run more frequently because it isn’t using the network, just reading the local XML file.

    I’ve also changed the code to use Google’s ‘magic cookie’ to access the calendar, so that I don’t have to make it public.

    Part 1 is done, a sample XML output is below. I’m starting on part 2. I haven’t done any real coding in over 10 years, and am brand new to Python. 2 questions:
    – anyone see a problem with this design? Only problem I can think of is that you can’t add a new schedule for today ‘on the fly’, unless you schedule part1 code to run more often. I figure I’ll have a separate interface for manual control of stations anyway, so am not concerned about this.

    – any python experts willing to look over my code to see if I’m doing anything stupid?

    I’ll be glad to share the code when it’s finished.

    Sample XML for today’s schedules:



    Daily schedule for OpenSprinkler
    2013-04-30
    4


    zone20
    2013-04-30T00:30:00.000-07:00
    2013-04-30T01:30:00.000-07:00


    zone19
    2013-04-30T12:30:00.000-07:00
    2013-04-30T13:30:00.000-07:00


    zone16
    2013-04-30T16:00:00.000-07:00
    2013-04-30T17:00:00.000-07:00


    zone12
    2013-04-30T08:00:00.000-07:00
    2013-04-30T09:00:00.000-07:00


    Jon

    #23572

    Anonymous
    Inactive

    Great job. I’m excited to see the code. I’m not very good at Python but I’m happy to check it out once you have it ready.

    #23573

    danico
    Member

    Jon,

    Your approach to using Google calendar for the scheduling via two code pieces, is a similar way to which I was looking to approaching this. Have you been able to progress it any further?
    I am very keen to see how you got to this point – my few attempts to use a private calendar have not been very successful!
    I will chip away at this, and hope to get it running, but would be great to see how you achieved the success in the first half of the method.

    #23574

    jonmc56
    Member

    For those who asked for the code:
    https://github.com/jonmc56/Opensprinkler

    get_schedule.py connects to Google Calendar to get all schedules for all zones for ‘today’, and writes a schedule xml file. Please note that this script expect 3 command line parameters as documented in the code.

    read_schedule.py reads the schedule xml file, and the intent is for it to set up a cron job for each schedule item. This part isn’t working, I got stuck on finding a way to add a schedule item in cron for each schedule item. I will keep after it when I have more time, would welcome any ideas.

    Jon

    #23575

    danico
    Member

    Jon,
    I went and wrote two programs, one to harvest the event info for the next seven days from Google Calendar and save to a XML file, the other to use that XML file to run the events. Will be good to compare your method to gather the events, I’m sure it is more elegant!
    Also expanded the idea, so not just sprinklers (or Reticulation as we call it here), but to also include garden lights and maybe house lights when on holidays. Can be expanded further to include blinds and windows or whatever we need done on a periodic basis.
    None of this is original work, it is all taken from others, and just tweaked, massaged and stretched a bit…
    Works like this – the calendar and access is set up as per Ray’s instruction, it is a specific calendar used just for house tasking. On this calendar, we create our events, anything to do with sprinklers, the event starts with ‘Retic’, so to water my Citrus trees, the event title is ‘Retic:Citrus’, then we put in the start and stop time. For the garden lights the events are ‘Lights:Letterbox’ etc.,
    I think the code is quite readbleto where you can add or remove these event types, and how to change the prefix wording.
    So once every 15 minutes, GetCal reads the Calendar for the next seven days. If any of the events are newer or changed since the last read of the calendar, then the file is updated, else it is ignored. Also if the stored file is older than 24 hours, it will force a re-write of the event file.
    At the moment the events are written to the current directory, eventually they will be written to my NAS, just not got there yet.
    So that is all this does, over and over. Will likely be put on to my DNSPi – an extra job for it to be doing….

    So now the other side of things.
    SprinklerPi gets hold of the saved ‘CalRetic.txt’ file and pulls out the relevant zone data. I was planning on writing an elegant program that ordered the events, put all the of the starts and stop in sequence, set various clock alarms to take care of the event, but that just got too difficult. Looking at how Ray had already done the calendar program, it was just the most straight forward way to go. So in a very similar method as that program, every say 15 seconds we look to see if any events are currently in play, if they are then the zones are set. It is really just Rays original program, but data from a file instead of from Calendar.
    It does check to see if the file has been updated, and if so re-reads the data, else it just runs on checking and setting zones.
    When it came to the Master valve, this is set in the software (as I have one), or it could be a calendar event that encompasses all of the sprinkler events.

    LightPi is very similar, it will sit in a box outside, but instead of an OpenSprinklerPi attached, it just has a SainSmart relay board, basically the same code but using the ‘Light’ XML data file.
    In both of these programs, the tricky part is keeping track of how the time is being stored. The Atom feed from Google is in rfc3339 format, we want to save to file in a nice readable string, but to do comparisons and deltas, we need to be in datetime format. Can get very confusing, but I think it is right in the end.
    Both programs are not yet complete, but are operational, and still contain many prints of current status as part of the debug process. These will be removed soon, and then the programs will run as a service.

    This is my first outing in Python, so I’m sure that there is much better and elegant ways of doing things, and really the hard work was initially done by Devon and Ray, but just wanted to get these out there as they may assist others in their home reticulation designs!

    #23576

    barikatec
    Member

    Dear all,

    Many thanks for your work and explanation. I am really interested to work with your code.

    I already use an opensprinkler with my google calendar and it works like a charm.

    But, now, i want to use it in my new house where my internet connection is not reliable.

    I tried to use your RetiCal solution Danico. But when i launch

    python GetCal.py

    i got the following error:

    Traceback (most recent call last):
    File "GetCal.py", line 24, in
    from feed.date.rfc3339 import tf_from_timestamp #also for the comparator
    ImportError: No module named feed.date.rfc3339

    I am quite anoob in code… I begn to be able to understand some functions but easily lost… If anyone can help i will be very thanksfull !!

    Sorry for my english as i don’t use it so often…

    #23577

    barikatec
    Member

    Dear all,

    I found by myself the solution. Some modules were in need of being installed on my pi.

    Thanks

    #34612

    eastrader
    Participant

    Jon, do you have an updated link? the one posted herein fails. Thanks, Ed

    #34614

    Samer
    Keymaster

    The site is fine but the link was broken. I’ve fixed the above link from Jon. Please let me know if this works now.

    Thanks!

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.

OpenSprinkler Forums Hardware Questions OpenSprinkler Pi (OSPi) Google Calendar code – my plan and a request