Пример #1
0
def init():
    global movimiento
    global detecciones
    global noDetecciones
    global sensor
    global threadsT
    global threadsS
    global segundosDeEspera
    movimiento = False  # Variable que indica cuándo hay movimiento y cuando no.
    detecciones = 0  # Variable que indica las detecciones que hay, se usa para evitar falsas alarmas.
    noDetecciones = 0  # Variable que indica las veces que no se ha detectado movimiento.
    sensor = gpiozero.MotionSensor(24)  # Variable del sensor de movimiento.
    threadsT = []  # Lista que almacena los threads del temporizador.
    threadsS = []  # Lista que almacena los threads del stream.
    segundosDeEspera = 5  # Numero de segundos y veces que se comprueba que movimiento antes de parar la grabacion.
    # Iniciar thread del detector de movimiento
    threadM = threading.Thread(target=thread_movimiento).start()
    # Iniciar thread del la cámara
    threadRefresh = threading.Thread(target=gui.refresh).start()
    threadServer = threading.Thread(target=gui.checkConnection).start()
    threadMain = threading.Thread(target=main).start()
Пример #2
0
import threading
import subprocess
import ftplib
import os
import time
import datetime
from PIL import Image
import gpiozero

pir = gpiozero.MotionSensor(14)
running = True


def movement():
    print("movement")
    foto_cmd = "raspistill -t 2000 -a 12 -md 5 -dt -p 200,200,200,200 -q 100 -o /home/pi/Pictures/cam/%d.jpg"
    subprocess.run(foto_cmd, shell=True)


while running == True:
    pir.when_motion = movement
Пример #3
0
            score = ''
            keyword = ''
            i = j
        i += 1


parse_text(response.text)

servo1 = gpiozero.AngularServo(17, min_angle=-90,
                               max_angle=90)  #right platform servo
servo1.angle = 0
servo2 = gpiozero.AngularServo(18, min_angle=-90,
                               max_angle=90)  #left platform servo
servo2.angle = 0

sensor = gpiozero.MotionSensor(4)
led = gpiozero.LED(14)
"""while True:
    if sensor.motion_detected():            
        led.on()
        #photo
        querystring = {"url":"https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2F1.bp.blogspot.com%2F_Od0_3vFBzHY%2FTJechRB6evI%2FAAAAAAAAAAM%2F3PKnboxFuTU%2Fs1600%2FPlasticBottle-PET.jpg&f=1&nofb=1"}        
        response = requests.request("GET", url, headers=headers, params=querystring)
        if "Bottle" in results.keys() and "Plastic" in results.keys():
            servo1.angle = -90
            servo2.angle = 90
            time.sleep(2)
            servo1.angle = 0
            servo2.angle = 0
            print(123)
        else:
Пример #4
0
import gpiozero as gpio
from time import sleep
import sys

led = gpio.LED(17)
pir = gpio.MotionSensor(4)
buzzer = gpio.Buzzer(27)
servo = gpio.AngularServo(22,min_angle=-90,max_angle=90)
sensor = gpio.DistanceSensor(echo = 18, trigger = 13)

def turnOn():
    led.on()
    sleep(10)
    print('Light was on, it just turned off')

def turnOff():
    led.off()
    print('Light is now off')

def servoMovement(angle):
    angle = int(angle)
    servo.angle = angle
    sleep(3 )
    print('Servo Moved')

def buzz():
    buzzer.on()
    sleep(10)
    print('buzzzz')

def noBuzz():
Пример #5
0
    print(val[0])


blynk.add_virtual_pin(19, write=jlog)

#----------------------------------

# PIR to phone notification


def pir_release_cb():
    blynk.notify("PIR alert: " + timeNow())  # beep the smartphone
    print("pir alert")


pir = GPIO.MotionSensor(14)
pir.when_motion = pir_release_cb  # call this func when motion event fires

#-------------------------------------------

is_rpi3 = len(
    os.popen(
        "cat /proc/cpuinfo | grep ARMv7 > /dev/zero && echo $?").read()) == 2

# Use V30 button to control the PCB activity LED - RPI3 only !!
# Instead of showing SD card activity, it blinks regularly
# We use Ticker to do the flashing.

if is_rpi3:

    def ACTLED_cb(val, pin, st):
Пример #6
0
class GGCam():
    clip_count = 0
    clip_start_time = None
    timestamp_filename = None
    show_msg = True
    converting_video = 0
    disk_usage_full = False
    is_motion = False
    is_recording = False

    button = gpiozero.Button(23)
    led_standby = gpiozero.LED(24)
    led_status = gpiozero.LED(4)
    led_recording = gpiozero.LED(19)
    pir = gpiozero.MotionSensor(21)

    def __init__(self):
        self.log_folder = Path('./logs')
        if not self.log_folder.exists():
            self.log_folder.mkdir()

        self.logging_file_renew()
        self.load_config_from_file()
        self.set_output_config()
        self.clean_h264_folder()

        if self.output_location == 'usb drive':
            self.usb_checker = Usb_check()

        self.led_standby.on()

        # create a thread for blinking led according to recording status
        thread_blink_when_recording = threading.Thread(
            target=self.blink_led_when_recording)
        thread_blink_when_recording.start()

        # create a thread for led according to converting or error status
        thread_led_show_converting_or_error = threading.Thread(
            target=self.led_show_converting_or_error)
        thread_led_show_converting_or_error.start()

        if self.record_mode == 'motion':
            thread_motion_detect = threading.Thread(target=self.motion_detect)
            thread_motion_detect.start()

    def clean_h264_folder(self):
        for each_file in self.temp_h264_folder.iterdir():
            each_file.unlink()

        logging.debug('H264 folder cleaned.')

    @property
    def trigger(self):
        if self.record_mode == 'non-stop':
            return self.button.is_pressed
        elif self.record_mode == 'motion':
            return self.is_motion

    def logging_file_renew(self):
        # delete existing handler and renew, for log to new file if date changes
        logger = logging.getLogger()
        for each in logger.handlers[:]:
            logger.removeHandler(each)

        logging_file = self.log_folder.joinpath(
            f'{datetime.now().strftime("%Y-%m-%d")}.log')
        logging_format = '[%(asctime)s] %(levelname)s: %(message)s'
        logging_datefmt = '%Y-%m-%d %H:%M:%S'
        logging.basicConfig(level=logging.DEBUG,
                            format=logging_format,
                            datefmt=logging_datefmt,
                            handlers=[
                                logging.FileHandler(logging_file),
                                logging.StreamHandler()
                            ])

    def load_config_from_file(self):
        try:
            self.duration = config['duration']
            self.output_location = config[
                'output_location']  # sd card or usb drive
            self.fps = config['fps']
            self.resolution = config['resolution']
            self.record_mode = config['record_mode']
            self.motion_interval = config['motion_interval']
        except Exception as e:
            logging.error(
                f'something wrong with config.py. {e.__class__}: {e}')
            logging.info(f'please run setup.py.')
            sys.exit(1)

    def set_output_config(self):
        '''
        there are total 3 location must set:
        1. recording .h264 file
        2. coverting temp file
        3. final output file        
        '''

        # determine final output file folder
        if self.output_location == 'sd card':
            self.output_folder = Path('/home/pi/videos')
            if not self.output_folder.exists():
                self.output_folder.mkdir()

        elif self.output_location == 'usb drive':
            self.output_folder = Path('/mnt/usb/videos')

        # set recording h264 file folder
        self.temp_h264_folder = Path('/mnt/ramdisk')  # best sulotion
        # self.temp_h264_folder = self.output_folder  # depends on output location
        # self.temp_h264_folder = Path('/home/pi')  # may cause frame dropping

        # set converting temp file folder
        self.converting_temp_folder = Path('/mnt/ramdisk')  # best sulotion
        # self.converting_temp_folder = self.output_folder  # depends on output location

    def GGCam_exit(self):
        logging.info('program ended.')
        self.led_standby.off()
        self.is_recording = False
        self.led_status.off()

    def check_disk_usage(self):
        if self.output_location == 'usb drive':
            partition_id = self.usb_checker.usb_status['partition_id'][
                'status']
            cmd = f"df | grep -P {partition_id} | awk '{{print $5}}'"
        elif self.output_location == 'sd card':
            cmd = "df | grep -P root | awk '{print $5}'"

        try:
            output = check_output([cmd], stderr=STDOUT,
                                  shell=True).decode('utf8').strip()
            self.disk_usage = int(output[:-1])
        except Exception as e:
            logging.error(
                f'something wrong with checking disk_usage. {e.__class__}: {e}'
            )

        logging.debug(f'Output disk usage is {self.disk_usage} %')

        if self.disk_usage >= 98:
            logging.error(f'Disk usage is almost full. stop recording.')
            self.disk_usage_full = True

    def led_show_converting_or_error(self):
        logging.debug('led for showing converting or error is ready.')
        while 1:
            if self.converting_video > 0:
                self.led_status.on()
                time.sleep(0.5)
                self.led_status.off()
                time.sleep(0.5)
            else:
                time.sleep(1)

            if self.disk_usage_full:
                self.led_status.on()
                while 1:
                    time.sleep(10)

    def blink_led_when_recording(self):
        logging.debug('led for recording status is ready.')
        while 1:
            if self.is_recording:
                self.led_recording.on()
                time.sleep(0.5)
                self.led_recording.off()
                time.sleep(0.5)
            else:
                time.sleep(1)

    def motion_detect(self):
        logging.debug('PIR Motion detection started.')
        self.last_motion_countdown = self.motion_interval
        show_one_time = True
        while 1:
            if self.pir.motion_detected:
                if show_one_time:
                    logging.info(
                        f'Motion detected: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}'
                    )
                    show_one_time = False

                self.last_motion_countdown = self.motion_interval
                self.is_motion = True

            if self.last_motion_countdown == 0:
                show_one_time = True
                if show_one_time:
                    logging.info(
                        f'No motion detected in {self.motion_interval} secs.')

                self.is_motion = False

            # print(self.last_motion_countdown, self.is_motion)
            if self.last_motion_countdown > -1:
                self.last_motion_countdown -= 1

            time.sleep(1)

    def record(self):
        def clip_renew():
            self.clip_count += 1
            self.clip_start_time = datetime.now()
            self.clip_start_time_string = self.clip_start_time.strftime(
                ("%Y-%m-%d_%H_%M_%S"))
            self.clip_file_object = self.temp_h264_folder.joinpath(
                f'{self.clip_start_time_string}.h264')

        def start_recording_session():
            self.check_disk_usage()
            if self.disk_usage_full:
                return

            clip_renew()
            self.is_recording = True
            cam.start_recording(str(self.clip_file_object))
            logging.info(
                f'Start recording clip {self.clip_count} at {self.clip_start_time.strftime("%Y-%m-%d %H:%M:%S")}, max duration: {self.duration} secs'
            )

        def split_recording():
            self.check_disk_usage()
            if self.disk_usage_full:
                return

            self.done_recorded = copy(self.clip_file_object)
            last_clip_count = self.clip_count
            clip_renew()
            logging.info(
                f'Start recording clip {self.clip_count} at {self.clip_start_time.strftime("%Y-%m-%d %H:%M:%S")}, max duration: {self.duration} secs'
            )
            cam.split_recording(str(self.clip_file_object))

            # start another thread to convert done recorded file
            th_1 = threading.Thread(target=self.convert_video,
                                    args=(self.done_recorded, last_clip_count))
            th_1.start()

        def stop_recording_session():
            logging.info('Stop recording ...')
            cam.stop_recording()
            self.is_recording = False
            self.convert_video(self.clip_file_object, self.clip_count)

        # avoiding show two times standby msg if directly into record process from program start
        self.show_msg = False

        with picamera.PiCamera() as cam:
            cam.resolution = self.resolution
            cam.annotate_background = picamera.Color('black')
            cam.framerate = self.fps
            cam.start_preview()

            start_recording_session()

            while True:
                if self.disk_usage_full:
                    self.is_recording = False
                    return

                if not self.trigger:
                    logging.debug('Trigger is off.')
                    stop_recording_session()
                    return

                if (datetime.now() -
                        self.clip_start_time).seconds >= self.duration:
                    split_recording()
                    self.logging_file_renew()

                cam.annotate_text = datetime.now().strftime(
                    '%Y-%m-%d %H:%M:%S')
                time.sleep(1)

    def convert_video(self, file_object, count):
        time.sleep(1)
        self.converting_video += 1
        logging.info(f'Start converting Clip {count} ...')
        ts = file_object.stem
        output = self.output_folder.joinpath(f'{ts}.mp4')
        MP4Box_temp_log = Path(f'./logs/temp_log_{ts}')
        cp = run([
            "MP4Box", "-tmp", f"{self.converting_temp_folder}", "-add",
            f'{file_object}:fps={self.fps}', output
        ],
                 stdout=DEVNULL,
                 stderr=open(MP4Box_temp_log, 'a'))
        if cp.returncode:
            logging.error(f'Clip {count} convert failed.')
        else:
            logging.info(f'Clip {count} convert done.')
            file_object.unlink()

        self.converting_video -= 1
        self.clean_up_mp4box_log(MP4Box_temp_log, count)

        if not self.trigger and self.converting_video == 0:
            logging.info('Standby for recording ...')

    def clean_up_mp4box_log(self, MP4Box_temp_log, count):
        with open(MP4Box_temp_log, 'r') as f:
            lines = f.readlines()

        target_pattern = re.compile(r'AVC Import results: (\d*) samples')
        for line in lines:
            if target_pattern.match(line):
                logging.info(f'Clip {count} info: {line.strip()}')

        MP4Box_temp_log.unlink()

    def run(self):
        logging.info('PiGGCam starting ...')
        logging.info(
            f'Video spec: {self.resolution} at {self.fps} fps, max duration: {self.duration} secs'
        )
        logging.info(f'Video will save to {self.output_folder}')
        logging.info(f'Record mode is: {self.record_mode}')

        while True:
            if self.disk_usage_full:
                time.sleep(2)
                continue

            if self.output_location == 'usb drive':
                self.usb_checker.usb_check()

                if self.usb_checker.is_usb_status_changed:
                    self.show_msg = True

                if self.trigger and self.usb_checker.is_ready_for_recording and self.converting_video == 0 and not self.disk_usage_full:
                    logging.debug('Trigger is on.')
                    self.record()
                else:
                    if self.show_msg and self.usb_checker.is_ready_for_recording and not self.disk_usage_full:
                        logging.info('Standby for recording ...')
                        self.show_msg = False

            elif self.output_location == 'sd card':
                if self.trigger and self.converting_video == 0 and not self.disk_usage_full:
                    logging.debug('Button pressed.')
                    self.record()
                else:
                    if self.show_msg and not self.disk_usage_full:
                        logging.info('Standby for recording ...')
                        self.show_msg = False

            self.logging_file_renew()
            time.sleep(1)
Пример #7
0
 def __init__(self):
     AbstractSensor.__init__(self)
     self.sensor = gpiozero.MotionSensor("GPIO4")
     self.measurements = {
         "Motion": [False, ""],
     }
Пример #8
0
class State:
    def __init__(self):
        self.game = Game("Sean", "Lauren", "Murderface", "Mitch")

    def redGoal(self):
        self.game.goal("red")
        red.on()

    def resetGame(self, game):
        #TODO: turn these into Players
        print("here")
        self.game = Game("Sean", "Lauren", "Murderface", "Mitch")


redPIR = gpiozero.MotionSensor(4)
red = LED(18)

sio = socketio.AsyncServer(cors_allowed_origins='*', async_handlers=False)
app = web.Application()
sio.attach(app)

sio.emit


async def startMsg():
    await sio.emit('game start', 'game started')


async def index(request):
    with open('index.html') as f:
Пример #9
0
 def __init__(self, pin):
     self._sensor = gpiozero.MotionSensor(pin)
Пример #10
0
import time
import threading
"""Tkinter Initialization"""
main = Tk()
main.title('Smart Lamp')
main.geometry('370x220')
main.resizable(width=FALSE, height=FALSE)
"""Raspberry Initialization"""
ldrpin = 27
pirpin = 4
relaypin = 17
times = 30
lightvalue = 5
ldr = gpiozero.LightSensor(
    ldrpin)  #Memanggil class MotionSensor dari Library gpiozero
pir = gpiozero.MotionSensor(
    pirpin)  #Memanggil class MotionSensor dari Library gpiozero
relay1 = gpiozero.OutputDevice(
    relaypin, active_high=False,
    initial_value=False)  #set relay sebagai OutputDevice


def time_sett():
    global times
    times = simpledialog.askinteger("TIME (s)",
                                    "Enter Time (in second)",
                                    initialvalue=times,
                                    minvalue=4)
    while times == None:
        messagebox.showwarning(message='Time tidak boleh kosong')
        times = simpledialog.askinteger("TIME (s)",
                                        "Enter Time (in second)",
Пример #11
0
#!/usr/bin/python3

import datetime
import gpiozero
import os
import random
import time
import subprocess
import sys

# Notes
# Set volume: sudo amixer cset numid=1 70%

os.system("killall -9 raspistill")
pir = gpiozero.MotionSensor(pin=4,
                            queue_len=100,
                            sample_rate=100,
                            threshold=.95)
camera = subprocess.Popen(
    ["raspistill", "-s", "-o", "/home/pi/halloweendalek/tmp.jpg"])
lastmovement = datetime.datetime.now()
silenceoriginalweight = 5

responses = []
weights = []


class response:
    def __init__(self, soundfile=None, printstatement="", weight=1):
        self.soundfile = soundfile
        self.printstatement = printstatement
        self.weight = weight