Updates from April, 2022 Toggle Comment Threads | Keyboard Shortcuts

  • ThomasPowell 10:13 pm on April 7, 2022 Permalink | Reply
    Tags: motion sensing camera, pir,   

    An Easy Raspberry Pi Motion Sensing Camera with a PIR sensor and the Pi Camera Module 

    The basic parts for a motion sensing camera

    While you can also drive a motion sensing camera via image processing on a webcam, a less processor and power intensive means is to only activate the camera when motion is sensed instead. For this a PIR Motion Sensor can be used and processing can track the signal from that device instead.

    For this example I used:


    Raspberry Pi Motion Sensing Camera schematic
    Wiring of PIR to GPIO header

    If the PIR sensor is oriented sensor upward and pins on the bottom and sensitivity and timing potentiometers on the top in the drawing:

    • The top left pin (5V) on the GPIO header goes to the left pin on the PIR sensor.
    • The 3rd pin from the top left on the top row of the GPIO header (GND) goes to the right pin on the PIR sensor.
    • The middle pin on the PIR sensor connects to the 6th pin from the left on the bottom row of the GPIO header (GPIO 17)

    Enabling the Motion Sensing Camera

    • You may need to enable SSH if you have a fresh full install of Raspbian 11 so that you can remotely manage things.
    • dtoverlay=imx219 may have to be added to /boot/config.txt to properly recognize the camera:
    # Enable DRM VC4 V3D driver
    • If you’re trying to use libcamera-hello to test the camera out, you may need to enable Glamor:

    To do this enter sudo raspi-config at a terminal window and then choose Advanced OptionsGlamor and Yes. Finally quit raspi-config and let it reboot your Raspberry Pi.


    Capturing on motion detect

    The following python code will capture images to /home/pi/image_captures when the motion sensor is triggered.

    import RPi.GPIO as GPIO
    import time
    import os
    import datetime
    def my_callback(channel):
        # yes, datetime.datetime... the first is the datetime module
    	today = datetime.datetime.now()
        # format a timestamp for each image capture
    	date_time = today.strftime("%Y-%d-%m-%H:%M:%S")
        # be sure /home/pi/image_captures exists!
    	os.system(f'libcamera-still -n -t 1 -o /home/pi/image_captures/capture{date_time}.jpg')
    	GPIO.add_event_detect(SENSOR_PIN, GPIO.RISING, callback=my_callback)
    	while True:
    except KeyboardInterrupt:

    Adjusting sensitivity and debouncing/time delay

    The left potentiometer (when facing it with the dome up) adjusts the delay between successive triggers. Turning the farthest counterclockwise reduces the time delay between triggers to the shortest time possible and clockwise the longest. If the triggers are too frequent, turn clockwise. I left mine turned to shortest time possible.

    The right potentiometer adjusts the sensitivity (amount of motion) required to trigger an event. I found a couple different PIR sensors behaved significantly differently, but generally wanted this as far clockwise (most sensitive) as possible, but I wanted to trigger with as little motion as possible.

    Auto starting the script

    For experimentation, you’ll probably SSH in or connect to a keyboard and monitor… but for a more “production” setup, you’ll want the script to start running as soon as it boots. You can add an invocation to just before the exit 0 in /etc/rc.local:

    sudo -u pi python /home/pi/pir.py &
    exit 0

    Note that I’m switching to the pi user with sudo -u pi so that I write to the pi user’s home directory with pi user permissions.

    Next Steps

    • Synchronize the output from your motion sensing camera to cloud storage (with S3)
    • Notify that new motion has been detected using SNS
    • Explore a less expensive option using a Linode server with a flat bandwidth allocation instead of PUT object and per GB egress charges
  • ThomasPowell 9:06 pm on July 13, 2021 Permalink | Reply
    Tags: envoy solar, , raspberrypi, ,   

    Enphase Envoy Solar Array monitoring with a Raspberry Pi to Save $100 or More from Downtime 

    The need for an Enphase Envoy Solar monitor

    I decided to set up an Envoy solar panel monitoring system for my solar installation after a fuse and the breaker panel broke down leaving me without solar generation for a couple stretches during near-peak, up to about 1,400 kWh, or about $140-210 worth of solar generation. Here’s the output of the Enphase Envoy Solar report:

    solar output from the envoy monitor
    Missing output

    Monitor Components (physical and software)

    • A RaspberryPi Zero W on the same wireless network as the Envoy controller was set up on (initially used PiBakery to configure hostname/wifi/username/password, but the project is a little bit stale at this point).
    • A Nexmo account (part of Vonage APIs now) to allow for SMS alerts on zero output when the sun is up.
    • RubySunrise for only emailing alerts from dusk until dawn.
    • Ruby Gmail and a Gmail account for email informational “down” alerts just to be aware that the cron job is running.
    • cron and gmail

    Connections and Configuration for the Raspberry Pi Script

    These are described in the source code repo as well

    • ENVOY_HOST for me was envoy.local, but depending on your DNS situation, your mileage may vary. I got my local DNS in a weird enough state that I just looked up the envoy.local IP on my wireless router’s status page and used that.
    • USERNAME and PASSWORD are the Gmail username and app-specific password credentials I generated for the gmail account I used.
    • INVERTER_COUNT is compared to the number of inverters you should have so that even if the array is producing, you can still generate an error if one of them isn’t reporting (only valid when producing)
    • LATITUDE and LONGITUDE plucked from a site that displays your geolocation… this, along with your TZ represented in a form within the TZInfo::Timezone list, and RubySunrise allow you to figure out if the sun’s up.
    • NEXMO* are api keys and config from the Nexmo site (NEXMO_SMS_TO is your personal mobile to alert to)
    • TO_EMAIL is the email to actually mail to

    Code for the Raspberry Pi Solar Monitor script

    .config must be of the form that follows but the rest of the code can be cloned from envoy-rpi-zero-monitor

        PASSWORD='[email protected]@$$w0rd'
        TO_EMAIL='[email protected]'
    # crontab runs every hours and inits rbenv to use the right ruby version because
    # I didn't really care about "production readiness"... it's a Raspberry Pi Zero W
    0 * * * * cd /home/twill/envoy-rpi-zero-monitor && eval "$(rbenv init -)" && ruby read-envoy.rb

    This is specific to building for the Enlighten Envoy

    This all depends on having an Enphase Enlighten Envoy (and a bunch of other random “E” names) as your solar monitor, but if you have a relatively recent solar install and your technician needed to configure the monitor for your wifi, then you probably have a similar device with a pollable endpoint. Look at your wireless router’s web console and you’ll see that monitor:

    If you browse to that name or the IP address associated, you’ll probably get a web page with status. If you reload with the network tab up, you’ll probably see it retrieve the data via a .json endpoint. Fortunately there is no envoy solar login for the data collector at envoy.local for my system, so the data can be accessed directly via that json, and there was no need for an Enphase Envoy manual.

    From there, you can build your own monitor around it.

Compose new post
Next post/Next comment
Previous post/Previous comment
Show/Hide comments
Go to top
Go to login
Show/Hide help
shift + esc
%d bloggers like this: