Coop Automation

How's the door working for you? I plan on making a mock door to test in my living room. I'm probably gonna use a pi zero w though, and use the pi 3 for inside projects. I've started making some random functions that I plan on using with my coop pi. You already know I'm not familiar with Python, so I'm probably doing this a dumb way haha, as long as it works.
Here's two messy functions I made this morning to gather some info for me
Code:
from urllib2 import urlopen
import xml.etree.ElementTree as ET

def LocalTemp(StationID):
  Nodes = ["temperature_string","temp_c","temp_f","relative_humidity","wind_string"]
  results = [""] * len(Nodes)
  location = [""]*2
  sunrise = ""
  sunset = ""
  time = ""

  _l = urlopen("http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=" + StationID)
  _xml = _l.read()
  _l.close()
  content = ET.fromstring(_xml)

  for x in range(len(content.tag)):
    if content[x].tag == "location":
      location[0] = content[x][5].text
      location[1] = content[x][6].text

    for y in range(len(Nodes)):
      if content[x].tag == Nodes[y]:
        results[y] = content[x].text

  sunrise,sunset,time = RetSun(location[0],location[1])

  return (results,location,sunrise,sunset,time)

def RetSun(lat,lng):
  _l = urlopen("http://api.geonames.org/timezone?lat=" + lat +"&lng=" + lng +"&username=jed")
  _xml = _l.read()
  _l.close()
  content = ET.fromstring(_xml)

  sunrise = ""
  sunset = ""
  time = ""

  for x in range(len(content[0][0].tag)):
    if content[0][x].tag == "sunrise":
      sunrise = content[0][x].text

    if content[0][x].tag == "sunset":
      sunset = content[0][x].text

    if content[0][x].tag == "time":
      time = content[0][x].text

  return (sunrise,sunset,time)
 
results,location,sunrise,sunset,datetime = LocalTemp("IGRANDLA87")

for x in results:
  print(x)

for x in location:
  print(x)

print(sunrise)
print(sunset)
print(datetime)

The input is very minimal, I only need my local wunderground station ID.
I get my coordinates from the xml and use that with geonames to get sunrise/set and time
It outputs
Code:
59.4 F (15.2 C) #Temp
15.2 #Temp C
59.4 #Temp F
98 #Humidity
From the NNW at 3.0 MPH #Wind direction and speed
44.881039 #Lat
-63.575298 #Long
2017-11-19 07:16 #Sunrise
2017-11-19 16:42 #Sunset
2017-11-19 16:02 #Current Time
>>>
 
How's the door working for you? I plan on making a mock door to test in my living room. I'm probably gonna use a pi zero w though, and use the pi 3 for inside projects. I've started making some random functions that I plan on using with my coop pi. You already know I'm not familiar with Python, so I'm probably doing this a dumb way haha, as long as it works.

Door and lights are working fine, I had to adjust the magnet sensor on the up sensor it was a tad too far away sometimes.

Your code looks ok to me, I think I need to get Pi Zero to play with... I made a copy of your code to learn even more.

So you have a wunderground weather station at your place like these?
https://www.wunderground.com/weatherstation/buyingguide.asp

JT
 
Last edited:
Check this out, made an app in C# to ssh to my pi. I know other apps exist, just wanted something custom. Makes debugging scripts super easy.

Mostly functional terminal. Not exactly like the pi terminal but close enough.
Made it from scratch the best I could. Also prints error outputs which is useful for debugging.
mypi screen5.jpg

Read files on the pi in real time by clicking on them in the file list.
mypi screen.jpg

Able to show if any .py script is currently running.
mypi screen2.jpg

Can upload/download to and from the pi, kill and run scripts. Also when a script is run this way, it'l output to the terminal, including errors.
mypi screen3.jpg
 
I just open a terminal and type ssh name@address but you may be on a Windoze PC I don't know.

I also mount the pi drive from my linux pc with this command.
Code:
sudo sshfs -o allow_other,default_permissions name@address:/home/username/bin /home/username/rpibin
After that I can just use my linux file manager to open the files on the pi.

JT
 
I just open a terminal and type ssh name@address but you may be on a Windoze PC I don't know.

I also mount the pi drive from my linux pc with this command.
Code:
sudo sshfs -o allow_other,default_permissions name@address:/home/username/bin /home/username/rpibin
After that I can just use my linux file manager to open the files on the pi.

JT
I access my pi, using my win pc, via PuTTY. Opens the terminal without any issues.
 
I've never used the pi, but I do routinely ssh to linux machines using windows boxes. I like Cygwin; you can get base terminal functionality, but if you also download the Cygwin/X capability, it runs an X server and you can start GUI applications from the remote box and have them appear on your local desktop (not sure if that applies in this case).
 
For the tinkerers here I've decided to move away from the Ardunio Nano and ESP8266 to Single Board Computer (SBC) to control the chicken coop automation. Please spare me your tales of whipping out your CC and ordering a door. I went with the Raspberry pi 3 and I'm quite happy with the device and the programming. So much easier to program than the Nano and ESP8266.

I've tried to attach the code but can't figure out how so I put it in the message for now.

Code:
#!/usr/bin/env python

# GPIO.VERSION '0.6.3'
# Raspberry Pi 3 Model B Rev 1.2

import RPi.GPIO as GPIO
import schedule
import astral
import datetime
from pytz import timezone
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(True)
GPIO.setup(4, GPIO.OUT) # Motor FWD
GPIO.output(4, False)
GPIO.setup(5, GPIO.OUT) # Motor REV
GPIO.output(5, False)
GPIO.setup(6, GPIO.OUT) # Door Lock
GPIO.output(6, False)
GPIO.setup(7, GPIO.OUT) # Lights
GPIO.output(7, False)
GPIO.setup(26, GPIO.IN,pull_up_down=GPIO.PUD_DOWN) # Up Door Switch
GPIO.setup(27, GPIO.IN,pull_up_down=GPIO.PUD_DOWN) # Down Door Switch

# Construct our location.  Longitude west and latitude south are negative
coordinates = ["Poplar Bluff", "USA", 36.763084, -90.413871, "US/Central", 110]
pbmo = astral.Location(info=(coordinates))
pbmo.solar_depression = "civil"

egglight = 840 # minutes of daylight desired
timeformat = "%I:%M %p"

sunrise = timezone('US/Central').localize(datetime.datetime.now())
sunset = timezone('US/Central').localize(datetime.datetime.now())
lighton = timezone('US/Central').localize(datetime.datetime.now())

def update():
    global sunrise
    global sunset
    global lighton
    sunrise = pbmo.sunrise(datetime.date.today())
    sunset = pbmo.sunset(datetime.date.today())

    # amount of daylight in HH:MM:SS
    daylight = sunset - sunrise
    print "Daylight {}".format(daylight)

    lightmin = daylight.seconds / 60
    if egglight > lightmin:
        extralight = egglight - lightmin
        lighton = sunrise - datetime.timedelta(minutes=extralight)

    now = timezone('US/Central').localize(datetime.datetime.now())

    print "Lights On {}".format(lighton.strftime(timeformat))
    print "Sunrise {}".format(sunrise.strftime(timeformat))
    print "Sunset {}".format(sunset.strftime(timeformat))

def status(runtime):
    global sunrise
    global sunset
    global lighton
    now = timezone('US/Central').localize(datetime.datetime.now())
    #print now.tzinfo
    #print lighton.tzinfo
    print(datetime.datetime.now().strftime("%H:%M"))
    #print lighton < now
    #print now < sunrise
    #print now > sunrise
    #print now < lighton
    #print now > sunset

    if lighton < now and now < sunrise:
        print "The light should be on"
        GPIO.output(7, True) # Lights

    if now > sunrise:
        print "The light should be off"
        GPIO.output(7, False) # Lights

    if lighton < now and now < sunset:
        if not GPIO.input(26) and runtime < 60: # Up Door Switch
            print "The door is opening"
            GPIO.output(4, True) # Motor FWD
            GPIO.output(6, True) # Door Lock

    if now < lighton or now > sunset:
        if not GPIO.input(27) and runtime < 60: # Down Door Switch
            print "The door ise closing"
            GPIO.output(5, True) # Motor REV
            GPIO.output(6, True) # Door Lock

    if GPIO.input(26): # Up Door Switch
        print("The Door is Open")
    else:
        print("Pin 26 is LOW")

    if GPIO.input(27): # Down Door Switch
        print("The Door is Closed")
    else:
        print("Pin 27 is LOW")

    if GPIO.input(4): # Motor FWD
        print "GPIO 4 is HIGH"

def motor(): # monitor the motor when moving
    pass
update()

runtime = 0
schedule.every(1).minutes.do(status, runtime)
schedule.every().hour.do(update)
schedule.every().day.at("02:00").do(update)

try:
    while True:
        schedule.run_pending()
        #global runtime
        if GPIO.input(6): # the door lock is on meaning the door is moving
            runtime += 1
            print runtime

        if runtime >= 60:
            GPIO.output(4, False) # Motor FWD
            GPIO.output(5, False) # Motor REV
            GPIO.output(6, False) # Door Lock

        if GPIO.input(4) and GPIO.input(26): # Motor FWD and Up Door Switch
            GPIO.output(4, False) # Motor FWD
            GPIO.output(6, False) # Door Lock
            runtime = 0

        if GPIO.input(5) and GPIO.input(27): # Motor REV and Down Door Switch
            GPIO.output(5, False) # Motor REV
            GPIO.output(6, False) # Door Lock
            runtime = 0

        if not GPIO.input(4) and not GPIO.input(5) and not GPIO.input(6):
            if GPIO.input(26) or GPIO.input(27):
                runtime = 0

        time.sleep(1)

except KeyboardInterrupt:
    # here you put any code you want to run before the program
    # exits when you press CTRL+C
    print "\nKeyBoard Interrupt"

except Exception,e:
    # this covers all other exceptions
    print str(e)

finally:
    GPIO.cleanup() # this ensures a clean exit

JT
Hi JT,
For the tinkerers here I've decided to move away from the Ardunio Nano and ESP8266 to Single Board Computer (SBC) to control the chicken coop automation. Please spare me your tales of whipping out your CC and ordering a door. I went with the Raspberry pi 3 and I'm quite happy with the device and the programming. So much easier to program than the Nano and ESP8266.

I've tried to attach the code but can't figure out how so I put it in the message for now.

Code:
#!/usr/bin/env python

# GPIO.VERSION '0.6.3'
# Raspberry Pi 3 Model B Rev 1.2

import RPi.GPIO as GPIO
import schedule
import astral
import datetime
from pytz import timezone
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(True)
GPIO.setup(4, GPIO.OUT) # Motor FWD
GPIO.output(4, False)
GPIO.setup(5, GPIO.OUT) # Motor REV
GPIO.output(5, False)
GPIO.setup(6, GPIO.OUT) # Door Lock
GPIO.output(6, False)
GPIO.setup(7, GPIO.OUT) # Lights
GPIO.output(7, False)
GPIO.setup(26, GPIO.IN,pull_up_down=GPIO.PUD_DOWN) # Up Door Switch
GPIO.setup(27, GPIO.IN,pull_up_down=GPIO.PUD_DOWN) # Down Door Switch

# Construct our location.  Longitude west and latitude south are negative
coordinates = ["Poplar Bluff", "USA", 36.763084, -90.413871, "US/Central", 110]
pbmo = astral.Location(info=(coordinates))
pbmo.solar_depression = "civil"

egglight = 840 # minutes of daylight desired
timeformat = "%I:%M %p"

sunrise = timezone('US/Central').localize(datetime.datetime.now())
sunset = timezone('US/Central').localize(datetime.datetime.now())
lighton = timezone('US/Central').localize(datetime.datetime.now())

def update():
    global sunrise
    global sunset
    global lighton
    sunrise = pbmo.sunrise(datetime.date.today())
    sunset = pbmo.sunset(datetime.date.today())

    # amount of daylight in HH:MM:SS
    daylight = sunset - sunrise
    print "Daylight {}".format(daylight)

    lightmin = daylight.seconds / 60
    if egglight > lightmin:
        extralight = egglight - lightmin
        lighton = sunrise - datetime.timedelta(minutes=extralight)

    now = timezone('US/Central').localize(datetime.datetime.now())

    print "Lights On {}".format(lighton.strftime(timeformat))
    print "Sunrise {}".format(sunrise.strftime(timeformat))
    print "Sunset {}".format(sunset.strftime(timeformat))

def status(runtime):
    global sunrise
    global sunset
    global lighton
    now = timezone('US/Central').localize(datetime.datetime.now())
    #print now.tzinfo
    #print lighton.tzinfo
    print(datetime.datetime.now().strftime("%H:%M"))
    #print lighton < now
    #print now < sunrise
    #print now > sunrise
    #print now < lighton
    #print now > sunset

    if lighton < now and now < sunrise:
        print "The light should be on"
        GPIO.output(7, True) # Lights

    if now > sunrise:
        print "The light should be off"
        GPIO.output(7, False) # Lights

    if lighton < now and now < sunset:
        if not GPIO.input(26) and runtime < 60: # Up Door Switch
            print "The door is opening"
            GPIO.output(4, True) # Motor FWD
            GPIO.output(6, True) # Door Lock

    if now < lighton or now > sunset:
        if not GPIO.input(27) and runtime < 60: # Down Door Switch
            print "The door ise closing"
            GPIO.output(5, True) # Motor REV
            GPIO.output(6, True) # Door Lock

    if GPIO.input(26): # Up Door Switch
        print("The Door is Open")
    else:
        print("Pin 26 is LOW")

    if GPIO.input(27): # Down Door Switch
        print("The Door is Closed")
    else:
        print("Pin 27 is LOW")

    if GPIO.input(4): # Motor FWD
        print "GPIO 4 is HIGH"

def motor(): # monitor the motor when moving
    pass
update()

runtime = 0
schedule.every(1).minutes.do(status, runtime)
schedule.every().hour.do(update)
schedule.every().day.at("02:00").do(update)

try:
    while True:
        schedule.run_pending()
        #global runtime
        if GPIO.input(6): # the door lock is on meaning the door is moving
            runtime += 1
            print runtime

        if runtime >= 60:
            GPIO.output(4, False) # Motor FWD
            GPIO.output(5, False) # Motor REV
            GPIO.output(6, False) # Door Lock

        if GPIO.input(4) and GPIO.input(26): # Motor FWD and Up Door Switch
            GPIO.output(4, False) # Motor FWD
            GPIO.output(6, False) # Door Lock
            runtime = 0

        if GPIO.input(5) and GPIO.input(27): # Motor REV and Down Door Switch
            GPIO.output(5, False) # Motor REV
            GPIO.output(6, False) # Door Lock
            runtime = 0

        if not GPIO.input(4) and not GPIO.input(5) and not GPIO.input(6):
            if GPIO.input(26) or GPIO.input(27):
                runtime = 0

        time.sleep(1)

except KeyboardInterrupt:
    # here you put any code you want to run before the program
    # exits when you press CTRL+C
    print "\nKeyBoard Interrupt"

except Exception,e:
    # this covers all other exceptions
    print str(e)

finally:
    GPIO.cleanup() # this ensures a clean exit

JT
Hi JT,
I'm a 55yr old coding infant... Just experimenting with your code above, as it does enough to meet my initial needs.... All the imports complete, I've had a play with the astral elements, I can see how that works and can generate the correct sunrise/sunset times for my location - tested and working.
However, when i try and run the code above, either modified for my location, or unmodified as above, i get a syntax error that I can't unravel:
>>> %FastDebug 'coop code full.py'
Traceback (most recent call last):
File "/home/pi/Python_tests/coop code full.py", line 157
print "Daylight {}".format(daylight)
^
SyntaxError: invalid syntax

(The ^ appears under the second")
I'm testing the code in Thonny running on a RPi Model 2 B ver 1.1 on Buster.
Any idea's why it's bombing out??
 

New posts New threads Active threads

Back
Top Bottom