emphillips18
Posts: 15
Joined: Mon Jan 14, 2019 6:11 pm

BrightPi Schedule On/Off in Python?

Sat Jun 01, 2019 5:04 pm

I am trying to use Bright Pi with a NOIR and PIR Pi camera system at night. It is battery-powered so it is important to preserve the battery, hence turning off the IR lights during the daytime. I am trying to add Bright Pi to my python code for the camera schedule, but I keep getting a name error.

I found a question with the same issue, and they say the files need to be in the same directory, which I believe they are (/home/pi) but is still not picking it up. I also am not sure where to add the schedule to the code I have. Any thoughts would be appreciated.

Code: Select all

# Import required Python libraries
import RPi.GPIO as GPIO
import time
from gpiozero import MotionSensor
import logging
from datetime import datetime
from subprocess import call
import picamera
import os
import shutil
import io
pir = MotionSensor(4, threshold = .8)
print (pir)
from brightpi import *

##Set up code for lights
brightPi = BrightPi()
brightPi.reset()
leds = LED_IR
brightPi.set_led_on_of(leds, OFF)


# Set the hours that you want
hour_start_1 =0000
hour_end_1 =0700
hour_start_2 =1800
hour_end_2 = 2359

print ("starting camera")

print (int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)))
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_PIR = 7

print ("PIR Module Test (CTRL-C to exit)")

# Set pin as input
GPIO.setup(GPIO_PIR,GPIO.IN)  # Echo
Current_State  = 0
Previous_State = 0

print ("Waiting for PIR to settle ..")
  # Loop until PIR output is 0
while GPIO.input(GPIO_PIR)==1:
        Current_State  = 0
while True:
    while int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) > hour_start_1 and int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) < hour_end_1 or int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) > hour_start_2 and int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) < hour_end_2:
	   try: brightPi.set_led_on_off(leds, ON)
        with picamera.PiCamera() as camera:
            try:
                time.sleep(80)
			   while True:
                    camera.resolution = (640,480)
				camera.rotation = 270
                    camera.framerate = 60
                    camera.annotate_background = picamera.Color('black')
                    #stream seconds is the buffer seconds (before motion detected)
                    stream = picamera.PiCameraCircularIO(camera, seconds = 5)
                    camera.start_recording(stream, format = "h264")

				   print ("streaming")
                    while True:
                        print("looking for motion")
                        if pir.motion_detected:
                            print ("motion detected!")
                            #camera.wait seconds indiates how much video to take AFTER buffer
                            camera.wait_recording(10)
                            timestamp = datetime.now().strftime('%d-%m-%y_%H-%M-%S')
                           #stream.copy_to seconds indicates how many seconds of video to save (buffer seconds + post-buffer seconds). without it i$
                            stream.copy_to('video.h264', seconds= 15)
                            outputvideo='/mnt/usb/videos/{}.mp4'.format(timestamp)
                            print(outputvideo)
                            os.system("sudo MP4Box -fps 60 -add video.h264 {}".format(outputvideo))
                            print ("copied to usb")
                            print ("sleeping")
                            time.sleep(3)
                   
				  
					  if int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) < hour_start_2 and int(str(datetime.now().hour) + str (datetime.now().minute).zfill(2)) > hour_end_1:
                                print ("time for sleep now")
                                break

                    break


            except KeyboardInterrupt:
                print ("Quit")
                 # Reset GPIO settings
                camera.stop_recording()

    print("zzzzzzzz")
  


pcmanbob
Posts: 6256
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: BrightPi Schedule On/Off in Python?

Sat Jun 01, 2019 6:15 pm

Try posting the error in its entirety so we can see which error you are getting, just telling us you keep getting a name error, does not tell us enough to be able to help you.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

emphillips18
Posts: 15
Joined: Mon Jan 14, 2019 6:11 pm

Re: BrightPi Schedule On/Off in Python?

Sat Jun 01, 2019 9:45 pm

Woops.
brightpierror.PNG
brightpierror.PNG (10.85 KiB) Viewed 295 times

pcmanbob
Posts: 6256
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: BrightPi Schedule On/Off in Python?

Sat Jun 01, 2019 10:17 pm

So looking at this on my tablet one line in your code that looks wrong is

Code: Select all

brightPi.set_led_on_of(leds, OFF)
It should be

Code: Select all

brightPi.set_led_on_off(leds, OFF)
There may be other errors but not checked every line .
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

scotty101
Posts: 3599
Joined: Fri Jun 08, 2012 6:03 pm

Re: BrightPi Schedule On/Off in Python?

Sat Jun 01, 2019 10:20 pm

Try the following import line

Code: Select all

from brightpi.brightpilib import *
As per the example of the Bright Pi GitHub https://github.com/PiSupply/Bright-Pi/b ... pi-test.py
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

emphillips18
Posts: 15
Joined: Mon Jan 14, 2019 6:11 pm

Re: BrightPi Schedule On/Off in Python?

Sat Jun 01, 2019 11:09 pm

No luck for either. New error:
brightpierror.PNG
brightpierror.PNG (11.02 KiB) Viewed 270 times
I have the folder "Bright-Pi" in my directory, and I installed it via the shell script, but nothing has changed. Tried running the brightpi.py in the folder as well, still giving the same error. Either the error here or the previous one.

User avatar
DougieLawson
Posts: 35381
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: BrightPi Schedule On/Off in Python?

Sat Jun 01, 2019 11:12 pm

Try using sudo pip3 ... to install it and python3 to run it.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

emphillips18
Posts: 15
Joined: Mon Jan 14, 2019 6:11 pm

Re: BrightPi Schedule On/Off in Python?

Sun Jun 02, 2019 12:09 am

It was unable to install under pip3:
brightpierror.PNG
brightpierror.PNG (7.8 KiB) Viewed 253 times

And when I tried running it under python3, it gave me this syntax error:
1.PNG
1.PNG (6.26 KiB) Viewed 253 times
I changed all the spaces I had to indents, so I am not sure what the problem is for that one.

pcmanbob
Posts: 6256
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: BrightPi Schedule On/Off in Python?

Sun Jun 02, 2019 9:34 am

Might be a good idea to post your code again because you have made changes.

the line in the error message is not line 63 in your posted code.

looks like it might only be supported under python 2
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

emphillips18
Posts: 15
Joined: Mon Jan 14, 2019 6:11 pm

Re: BrightPi Schedule On/Off in Python?

Wed Jun 05, 2019 11:33 pm

Hi all,

Thanks for the input. I figured out the problem, being that I was not adding 'brightPi' to the begin some of the functions. Wanted to post an update of the code in case anyone else has a similar problem.

Code: Select all

# Import required Python libraries
import RPi.GPIO as GPIO
import time
from gpiozero import MotionSensor
import logging
from datetime import datetime
from subprocess import call
import picamera
import os
import shutil
import io
pir = MotionSensor(4, threshold = .8)
print (pir)
import smbus
from brightpi import *

brightPi=BrightPi()
brightPi.reset()
all=(1,2,3,4,5,6,7,8)
leds=(5,6,7,8)
white=(1,2,3,4)
ON = 1
OFF = 0 
brightPi.set_led_on_off(white,ON)
time.sleep(3)
brightPi.set_led_on_off(white, OFF)

# Set the hours that you want
hour_start_1 =0000
hour_end_1 =0600
hour_start_2=2030
hour_end_2=2359

print ("starting camera")

print (int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)))
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_PIR = 7
print ("PIR Module Test (CTRL-C to exit)")

# Set pin as input
GPIO.setup(GPIO_PIR,GPIO.IN) # Echo
Current_State = 0
Previous_State = 0

print ("Waiting for PIR to settle ..")

# Loop until PIR output is 0
while GPIO.input(GPIO_PIR)==1:
Current_State = 0
while True:
while int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) > hour_start_1 and
int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) < hour_end_1 or int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) > hour_start_2 and
int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) < hour_end_2:
brightPi.set_led_on_off(leds, ON)
with picamera.PiCamera() as camera:
try:
time.sleep(80)

while True:
camera.framerate = 60
camera.annotate_background = picamera.Color('black')
#stream seconds is the buffer seconds (before motion detected)
stream = picamera.PiCameraCircularIO(camera, seconds = 5)
camera.start_recording(stream, format = "h264")
print ("streaming")

while True:
print("looking for motion")
if pir.motion_detected:
print ("motion detected!")
#camera.wait seconds indiates how much video to take AFTER buffer
camera.wait_recording(10)

timestamp = datetime.now().strftime('%d-%m-%y_%H-%M-%S')
#stream.copy_to seconds indicates how many seconds of video to save (buffer

seconds + post-buffer seconds). without it i$

stream.copy_to('video.h264', seconds= 15)
outputvideo='/mnt/usb/videos/{}.mp4'.format(timestamp)
print(outputvideo)
os.system("sudo MP4Box -fps 60 -add video.h264 {}".format(outputvideo))
print ("copied to usb")
print ("sleeping")
time.sleep(3)

if int(str(datetime.now().hour) +

str(datetime.now().minute).zfill(2)) < hour_start_2 and int(str(datetime.now().hour) + str
(datetime.now().minute).zfill(2)) > hour_end_1:
brightPi.set_led_on_off(white, ON)
time.sleep(3)
brightPi.set_led_on_off(all,OFF)
print ("time for sleep now")
break
break

except KeyboardInterrupt:
print ("Quit")
# Reset GPIO settings
camera.stop_recording()
print("zzzzzzzz")

emphillips18
Posts: 15
Joined: Mon Jan 14, 2019 6:11 pm

Re: BrightPi Schedule On/Off in Python?

Thu Jun 13, 2019 11:31 pm

Hello again,

Tried adding the code on a new board and the same thing happened again. I literally copy and pasted the code over and it started with this error message.
1.PNG
1.PNG (13.52 KiB) Viewed 143 times
When I run brightpi-test.py, it runs great. So I tried adding

Code: Select all

from brightpi.brightpilib import *
into my code instead and then this error showed up. I am running the same Raspian Stretch on both boards, Zero Ws. Any help with this will be much appreciated.
Capture.PNG
Capture.PNG (13.94 KiB) Viewed 143 times

Code: Select all

# Import required Python libraries
import RPi.GPIO as GPIO
import time
from gpiozero import MotionSensor
import logging
from datetime import datetime
from subprocess import call
import picamera
import os
import shutil
import io
pir = MotionSensor(4, threshold = .5)
print (pir)
import smbus
from brightpi import *

brightPi=BrightPi()
brightPi.reset()
all=(1,2,3,4,5,6,7,8)
leds=(5,6,7,8)
white=(1,2,3,4)
ON = 1
OFF = 0
brightPi.set_gain(15)
brightPi.set_led_on_off(white,ON)
time.sleep(1)
brightPi.set_led_on_off(white, OFF)

#os.system("sudo mkdir /mnt/usb/videos")
#os.system("sudo mount /dev/sda1 /mnt/usb/videos")


# Set the hours that you want
hour_start_1 = 1330
hour_end_1 = 2359
hour_start_2 = 0000
hour_end_2=1356
print ("starting camera")

print (int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)))
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_PIR = 7

print ("PIR Module Test (CTRL-C to exit)")

# Set pin as input
GPIO.setup(GPIO_PIR,GPIO.IN)  # Echo
Current_State  = 0
Previous_State = 0

print ("Waiting for PIR to settle ..")
  # Loop until PIR output is 0
while GPIO.input(GPIO_PIR)==1:
        Current_State  = 0
while True:
    try:
        while int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) > hour_start_1 and int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) <hour_end_1 or (str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) > hour_start_2 and int(str(datetime.now().hour) + str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) <hour_end_2:
            brightPi.set_led_on_off(leds, ON)
            with picamera.PiCamera() as camera:
                try:
                    time.sleep(10)
                    while True:
                        camera.resolution = (640,480)
                        camera.framerate = 60
                        camera.annotate_background = picamera.Color('black')
                        #stream seconds is the buffer seconds (before motion detected)
                        stream = picamera.PiCameraCircularIO(camera, seconds = 5)
			camera.start_recording(stream, format = 'h264')

                        print ("streaming")
                        while True:
                            print("looking for motion")
                            if pir.motion_detected:
                                print ("motion detected")
                                #camera.wait seconds indiates how much video to take AFTER buffer
                                camera.wait_recording(10)
                                timestamp = datetime.now().strftime('%d-%m-%y_%H-%M-%S')
                               #stream.copy_to seconds indicates how many seconds of video to save (buffer seconds + post-buffer seconds). without i$

                                outputvideo='/mnt/usb/videos/{}.h264'.format(timestamp)
                                # Mount the USB
                                print ("mounting usb")
                                os.system("sudo mkdir /mnt/usb/videos")
                                os.system("sudo mount /dev/sda1 /mnt/usb/videos")
                                stream.copy_to(outputvideo, seconds= 15)
                                print ("unmounting")
			        os.system("sudo umount /mnt/usb/videos")                                
                                print(outputvideo)
#                                os.system("sudo MP4Box -fps 60 -add video.h264 {}".format(outputvideo))
 				os.system("sudo MP4Box -fps 60 -add video.h264 {}".format(outputvideo,"{}.mp4".format(outputvideo)))
                                print ("copied to usb")
                                print ("sleeping")
                                time.sleep(5)


                            if int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) < hour_start_1 or int(str(datetime.now().hour) + str(datetime.now().minute).zfill(2)) > hour_end_2:
				brightPi.set_gain(1) 
                                brightPi.set_led_on_off(leds, OFF)
   			        print ("time for sleep now")
                                break

                        break


                except KeyboardInterrupt:
                    print ("Quit")
		    bright.Pi.set_gain(1)
		    bright.Pi.set_led_on_off(leds, OFF)
                     # Reset GPIO settings
#                   os.system ("sudo umount /mnt/usb/videos")
#                    print ("unmounting usb for removal")
                    camera.stop_recording()
#		    brightPi.set_led_on_off(leds, OFF)
                    #print ("unmounting usb for removal")
                    #os.system("sudo umount /mnt/usb/videos")


        print("zzzzzzzz")

    except KeyboardInterrupt:
         print ("Quit")
 	 brightPi.set_gain(1)
	 brightPi.set_led_on_off(leds, OFF)
#        os.system("sudo umount /mnt/usb/videos")
                     # Reset GPIO settings
         #camera.stop_recording()
#         print ("unmounting usb for removal")
#         time.sleep(5)         





Return to “Troubleshooting”