Raspberry Pi Coop Automation

Pics

jthornton

Free Ranging
6 Years
Aug 30, 2017
5,042
10,312
682
Poplar Bluff, MO
My Coop
My Coop
This thread is to show how I did my coop automation. I'm going to start as basic as I can. I have tried Arduino's and ESP8266 and other control devices and finally settled in with the Raspberry Pi 3B as my control of choice. The programming is in Python which to me is a bit easier to read than C which you must use for the Arduino and other devices that support Arduino code.

Some things to know up front, I do all my programming on a Linux PC. If you connect a keyboard, mouse and monitor to the Raspberry Pi you can program directly on the Rpi (Raspberry Pi). The Rpi uses a Linux Operating System, nothing to be afraid of, it just works better. The Rpi needs a connection to the Internet at bootup to get the current date and time, the Rpi doesn't have an onboard real time clock or battery/capacitor backup for date and time. The Rpi is a SBC (Single Board Computer), yes it's a fully functioning PC with USB ports, HDMI monitor connection and 40 pins that are used for I/O (Input/Output), these are use to connect the Rpi to the real world. You use the I/O to run your motor, turn lights on and off, get input from limit switches etc. The Rpi has some special pins, I2C or One Wire which can connect to things like temperature sensors (you can connect more than one as they have a unique address). The Rpi I/O is 3.3vdc so you need to be careful and select 3vdc relays, while 5v relays may work it's better to use the 3v versions.
pi3_gpio.png

Python is a programming language not the snake. The most simple program you could run is howdy.py
Python:
#!/usr/bin/python3
print("Howdy")

The first line is the shebang line that tells the system what to use to run the code.
The second line is a print statement that does what it looks like it should do.
To run the program the executable permission must be set. The easy way is to right click on the file in the file manager and select permissions then check off executable.
Then open a terminal in the same directory and type in ./howdy.py the ./ is needed otherwise the file will not be found.
Screenshot at 2020-10-30 08-25-25.png

You can also test snippets of code in a terminal without creating a file and I do that all the time to just test syntax and see my results. To start a Python session just type in python3 and press enter. Then you can test your code snippet.
Screenshot at 2020-10-30 08-29-15.png

Time to start my day, next up is the event loop and you will need one...

Got ahead of myself...

JT
 
Last edited:
This thread is to show how I did my coop automation. I'm going to start as basic as I can. I have tried Arduino's and ESP8266 and other control devices and finally settled in with the Raspberry Pi 3B as my control of choice. The programming is in Python which to me is a bit easier to read than C which you must use for the Arduino and other devices that support Arduino code.

Some things to know up front, I do all my programming on a Linux PC. If you connect a keyboard, mouse and monitor to the Raspberry Pi you can program directly on the Rpi (Raspberry Pi). The Rpi uses a Linux Operating System, nothing to be afraid of, it just works better. The Rpi needs a connection to the Internet at bootup to get the current date and time, the Rpi doesn't have an onboard real time clock or battery/capacitor backup for date and time. The Rpi is a SBC (Single Board Computer), yes it's a fully functioning PC with USB ports, HDMI monitor connection and 40 pins that are used for I/O (Input/Output), these are use to connect the Rpi to the real world. You use the I/O to run your motor, turn lights on and off, get input from limit switches etc. The Rpi has some special pins, I2C or One Wire which can connect to things like temperature sensors (you can connect more than one as they have a unique address). The Rpi I/O is 3.3vdc so you need to be careful and select 3vdc relays, while 5v relays may work it's better to use the 3v versions.
View attachment 2392585

Python is a programming language not the snake. The most simple program you could run is howdy.py
Python:
#!/usr/bin/python3
print("Howdy")

The first line is the shebang line that tells the system what to use to run the code.
The second line is a print statement that does what it looks like it should do.
To run the program the executable permission must be set. The easy way is to right click on the file in the file manager and select permissions then check off executable.
Then open a terminal in the same directory and type in ./howdy.py the ./ is needed otherwise the file will not be found.
View attachment 2392591

You can also test snippets of code in a terminal without creating a file and I do that all the time to just test syntax and see my results. To start a Python session just type in python3 and press enter. Then you can test your code snippet.
View attachment 2392596

Time to start my day, next up is the event loop and you will need one...

Got ahead of myself...

JT
This is fabulous!!! Thank you! My son is in second year of Computer engineering at university and we have Raspberry pi and Arduino stuff here at home. Methinks I will put the kid to work!!
 
one more question: What type of webcam do you use if any?
Hubby just bought a camera to put in the coop and has been enjoying their antics so far, but that is wifi and phone based.
 
one more question: What type of webcam do you use if any?
Hubby just bought a camera to put in the coop and has been enjoying their antics so far, but that is wifi and phone based.
I use POE IP cameras except for one USB camera in the nest box. I use this program to view 6 IP cameras at once on any Linux PC I have on my LAN. The program needs to run on a Debian like Linux PC that is Debian 10 or later. I tried it on a Raspberry Pi 4B but it used up all the memory and the kernel shut it down.
Screenshot at 2022-05-30 05-07-40.png

JT
 
When you get a Raspberry Pi 3B you need to flash the Micro SD card with the Operating System. I usually get the CanaKit Raspberry Pi 3 B+ (B Plus) with 2.5A Power Supply and Heat Sinks. I have a tutorial for flashing the Rpi with the OS and instructions on setting up the OS to make it user friendly. If you don't have a way to flash the Micro SD card fear not, simply put it in an envelope with a stamped return envelope and mail it to me. This is one occasion I use PM's for, just PM me for my address and I'll be happy to flash the Micro SD card and return it to you.

@veedabowlu you can ask your questions in this thread. I've developed a much better code for my door and will be discussing it here and share it on my GitHub site.

Note: I do not have an associate account with Amazon and receive no kickback if you follow the link like most do. I only link to products that I have purchased for my own use with my money.

JT
 
Last edited:
Ok time for the event loop. An event loop is a loop that runs every interval set by the code. The reason you need and even loop is to check to see if something needs to be done based on some reason typically it's time of day but in this example it will be the minute being even. This example is coop3a on my github account repository chicken-coop coop3.

Python:
#!/usr/bin/python3

import sys, time, signal
from datetime import datetime
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTimer

class main():
    def __init__(self):
        #exit the program when ctrl c is pressed
        signal.signal(signal.SIGINT, signal.SIG_DFL)

        # Set date on startup
        self.today = datetime.today().date()
        #initialize the self.done variable to False at start up
        self.done = False

        # Setup the heartbeat timer to run the update function every 100ms
        # 1 second has 1000 milliseconds
        updateTimer = QTimer()
        updateTimer.timeout.connect(self.update)
        updateTimer.start(100)

    def printMinute(self, m):
        print(f'The current even minute is {m}')

    def update(self):
        # this example shows a way to do something one time when an event happens
        # store the current minute in a local variable
        minute = datetime.now().minute
        # if the remainder of minute / 2 is equal to 0 and self.done is not True
        if minute % 2 == 0 and not self.done:
            # call the printMinute function and pass the minute to it
            self.printMinute(minute)
            # now set self.done to True so it only happens once
            self.done = True
        # else if the remainder of minute / 2 is not equal to 0 set self.done to False
        elif minute % 2 != 0:
            self.done = False

if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = main()
    sys.exit(app.exec_())

So when you run this code the update function is ran every 0.1 second and tests to see if the current minute is even if so the printMinute function is ran one time. This is important to grasp how this works. To exit the running program press Ctrl c.

Screenshot at 2020-10-30 18-07-48.png

Not to fancy but you gotta start with the basics... now that you have a program that has an event loop you can test for what ever you want and execute a function based on the result of that test.

Next up do something at sunrise...

JT
 
@jthornton
I appreciate what you are doing... this is "python3 101"...
1) " #!/usr/bin/python3"
No such file or directory...
So I guess I have to make a new directory...how?
2) I did bounce into your coop3 GitHub...
Requirements...
Touch screen or monitor and keyboard and mouse.
is VNC ok?

Remember not a coder here... pullet steps.
 
1) " #!/usr/bin/python3"
No such file or directory...

I need more information on what you have and what you did. Keep in mind that I do all my Python programs on Linux based Operating Systems. Python does work on Winddoze but I don't use it there. Every modern Linux based OS comes with Python 3.

When you run an executable file from a terminal in Linux the shebang refers to the characters "#!" when they are the first two characters in an interpreter directive as the first line of a text file. In a Unix-like operating system, the program loader takes the presence of these two characters as an indication that the file is a script, and tries to execute that script using the interpreter specified by the rest of the first line in the file. You might try #!/usr/bin/env/python3 if your on some strange Linux OS.

Well we are at chick steps now, no feathers yet... so I need to know what OS your using, what you did exactly.

I don't use VNC for anything anymore so I don't know.

If you have a PC running some flavor of Linux you can use SSH to connect to the Rpi and do most of the programming with that.

If your not using any kind of GUI (Graphical User Interface) in your program you can do everything over SSH.

Did you setup the Rpi per my tutorial?

JT
 
Last edited:

New posts New threads Active threads

Back
Top Bottom