Skip to content

cmiles/PiSlicedDayPhotos

Repository files navigation

Pi Sliced Day Photos

This is a small .NET Core (C#) program designed to be run on a Raspberry Pi that takes photographs at Sunrise, Sunset, a number of specified intervals in-between and at custom times specified either by clock time or minutes +/- from sunrise/sunset.

The in-between intervals are calculated based on sunrise/sunset times provided as a CSV file. Since the provided sunrise/sunset times may take topography (or other factors) into account the photographs might not be at the same clock time/sun angle each day, but will be at the same relative time between sunrise/sunset. Living living in the shadow of a mountain peak inspired this setup, 'half way thru the available sunlight for the day' is more interesting to me than a photo taken at a certain clock time or sun angle.

For this program to work it requires:

  • A Raspberry Pi where .NET Core can run and an attached camera that responds to libcamera-still. This program has only been confirmed to run on a Raspberry Pi 3 A+ with a Camera Module 3 Wide - but I believe any 2+ version Pis with current versions of the Raspberry Pi OS should work with any of the official Pi camera modules...
  • A settings file named PiSlicedDaySettings.json - an example is included in the code and there are notes below
  • A CSV file named SunriseAndSunset.csv with the calendar day, sunrise time (local) and sunset time (local).

Sunrise and Sunset are always photographed - you can also specify:

  • How many photographs you want between Sunrise and Sunset (during the day - 0 is valid)
  • How many photographs you want between Sunset and Sunrise (during the night - 0 is valid)
  • Times relative to sunrise and sunset - for example Sunrise+10 for a photograph 10 minutes after Sunrise
  • Clock Times

The image below was created in Photoshop by combining the output of this program from 3 Raspberry Pi 3 A+ computers with Wide Angle Camera Module 3 cameras - the cameras face, approximately, west, north and east

OutputExample

SunriseAndSunset.csv

This program uses a file of Sunrise/Sunset times to allow you to input any sunrise/sunset times that you want - a strong use case for this is generating topography compensated Sunrise/Sunset times for your location. For example today at my home we didn't see the sun come over the mountains to the east until 40+ minutes after the sun rise time calculated for the 'true' horizon...

The SunriseAndSunset.csv file should be formatted like the the sample file included with the program:

DAY, SUNRISE, SUNSET
2023-01-01,08:15:00-0700,17:23:00-0700
2023-01-02,08:15:00-0700,17:24:00-0700

If you are interested in generating Topography compensated Sunrise/Sunset times for your location try:

Settings File

DaySlices - Takes an integer and determines the number of photos taken between sunrise and sunset.

NightSlices - Takes an integer and determines the number of photos taken between sunset and sunrise.

PhotoStorageDirectory - The path to where the photos are written.

SunriseSunsetCsvFile - The name (including extension) of the csv file of Sunrise and Sunset times.

PhotoNamePrefix - Prefix for the photo names. The date and time will follow the prefix and .jpg to create the filename. It is especially useful to change this if you have multiple cameras running.

LogFullExceptionsToImages - The assumption is that this program will run largely unattended and that most of the time the only thing you will see is the photographs. The program will try to alert you of errors by writing exception information to an image file in the PhotoStorageDirectory. This setting determines whether the program writes all of the exception information or only an abbreviated message. This option exists because writing full exception information may leak information about your setup!

LibCameraParameters - Day/Night/Sunset/Sunrise - command line parameters for libcamera-still.

CustomTimes - Custom times can be specified either as clock times (3:45 pm) or as minutes before/after sunset (sunset +10). Each custom time also gets LibCamera parameters.

"CustomTimes": [
    {
      "Time": "Sunset+10",
      "LibCameraParameters": "--autofocus-mode manual --lens-position 0 --awb auto --metering average --denoise cdn_hq"
    },
    {
      "Time": "Sunrise-10",
      "LibCameraParameters": "--autofocus-mode manual --lens-position 0 --awb auto --metering average --denoise cdn_hq"
    }
  ]

Setup Notes

Suggested setup: Clone, build and publish this project to a folder - then on the Pi:

  • In your home directory - make a directory for the program and the photos:
    mkdir PiSlicedDayPhotos
    mkdir SlicedPhotos
    
    • Copy the published output of this solution into the PiSlicedDayPhotos folder - then change the permissions for the program to be executable:
    chmod +x PiSlicedDayPhotos
    
  • Run the program as a service: Edit the pisliceddayphotos.service replacing [Your Directory Here], copy it to /etc/systemd/system/, start and follow the service to check for any errors:
    nano pisliceddayphotos.service
    sudo cp pisliceddayphotos.service /etc/systemd/system/
    sudo systemctl daemon-reload
    sudo systemctl start pisliceddayphotos
    sudo systemctl enable pisliceddayphotos
    journalctl -u pisliceddayphotos -f
    

I like to disable the LEDs to make sure that the glass covering the lens opening won't pick up any light from the LEDS - How To Easily Disable Status LEDs On RaspberryTips

My preference is for Automatic/Unattended Upgrades - do this long enough and something unexpected will break, but I would rather stay up to date and have something break sooner rather than later. Secure your Raspberry Pi by enabling automatic software updates – Sean Carney and UnattendedUpgrades - Debian Wiki

If you've worked in years gone by with the Pi Camera and C# you might know the very useful techyian/MMALSharp: C# wrapper to Broadcom's MMAL with an API to the Raspberry Pi camera - unfortunately without choosing an older version of Raspberry Pi OS that library no longer works. The Pi has moved on to libcamera. I didn't find a C# wrapper for libcamera and since I didn't need to do anything other than write stills to the Pi's storage calling libcamera-still 'command line style' seemed to be the best option.

I didn't find a single great place for libcamera-still documentation - frustrating until I figured out that (beyond 'getting started' content) running 'libcamera-still --help' was really the best single source of information.

Backstory

For a number of years my wife and I used a previous (now-archived) project - cmiles/PiDropLapse - and a Raspberry Pi 4 Model B to take periodic photographs and sensor readings to monitor an area inside our house.

Since moving to a more rural property I have wanted to do a similar project but outside and solar powered - Raspberry Pi shortages, never quite finding an in-stock dedicated Pi solar setup that I loved and other house projects delayed that idea...

Recently we installed a 12V/200aH solar system near our parking area. The main purpose of this system is to power the rodent deterrent lights for our trucks - but luckily it has more than enough power to also power several Pis for photo purposes!

EnclosureExample

My Setup with Notes

  • Raspberry Pi 3 Model A+, 5V 2.5A Switching Power Supply with 20AWG MicroUSB Cable, 32 GB MicroSD Card and a case from Adafruit: This is about $60 USD plus shipping - I like the $25 USD price of the 3 A+, the full sized HDMI port and the slim profile.
  • Raspberry Pi Camera Module 3 - 12MP 120 Degree Wide Angle Lens: I love photography - you can see some of my work over on Pointless Waymarks - so I considered a number of choices for this project but in the end the cost/convenience/size/performance of going with a $35 official camera module won out.
  • Wooden Enclosure: Hopefully weatherproof (enough)! Built with spare/scrap wood, bug screen sitting in a closet and the paint for our deck. The main feature is that I recycled existing materials for this... As you can see in the photo above the carpentry is very (very!) basic so no details are included. Mounting the camera and weatherproofing the exit hole for the camera/lens took a few tries:
    • I tried using plexiglass for the entire front panel of the enclosure - but at least with the plexiglass I had the images were never sharp. I was using plexiglass left over from another (not camera oriented) project and I didn't want to dive into figuring out 'best optical quality plexiglass' (and didn't want glass for durability reasons) so I moved on.
    • I moved from the plexiglass front panel to a wooden one. I drilled a hole large enough for the lens and mounted the camera tightly to the front panel - which didn't work, mounting the front of the camera package tightly against something can end up impacting focus...
    • In the end for focus and to make sure the wide angle camera has a clear view I made a larger hole for the lens and used spacers to ensure the front of the camera isn't against anything. This approach made it important to find something to cover the hole for weatherproofing.
    • I tried a plexiglass dome off of Amazon to cover the exit hole for the camera - this was great for part of the photograph but distorted the edges. It's possible that the distortion would go away if I mounted the camera farther into the dome or found a the right dome, but I wasn't interested in either of those options.
    • The solution that finally worked for me was using a UV lens filter and hot gluing it to the outside of the enclosure. I used an $8 Tiffen 55mm UV Protector Filter - it is easy to find smaller diameter filters but after some experiments I liked this size because it was very easy to position it so that the edge of the filter didn't end up in the photographs.
  • With the mostly recycled enclosure the cost of the system is around $105 - potentially a bit more with tax and shipping.
  • Solar: As mentioned above powering the system with solar was a goal for this project - but it turns out that the solar system powering my setup isn't dedicated primarily to running the Pis... So only tangentially related, but for the sake of fully documenting the project, the main components of the solar system are listed below. This system is massive overkill if you just want to run a few Pis, like most real world systems I had many constraints and goals that are not in line with 'build the world's best small solar system'. This is not a recommendation regarding anything below, I don't have enough experience to do that, so use this list with caution (wiring and fuses omitted - btw if you are building a system like this for the first time be sure to look up wiring and fuse/breaker cost - it was much more than I guessed...).

Other Projects

Fundamentally this project is just taking photographs with the Raspberry Pi which is not hard to do and you can find other great free projects and code to take stills, timelapses and more! One of my favorites is GitHub - thomasjacquin's allsky: A Raspberry Pi operated Wireless Allsky Camera - I hope to build on of these in the future... Also see Roll Your Own All-Sky, Raspberry Pi Camera - IEEE Spectrum and the Hacker News discussion.

Tools and Libraries

This program would not be possible without the amazing resources available for creating Free software! Used in this project:

Tools:

Core Technologies:

Libraries:

About

A C# .NET Core program to take photos with a Raspberry Pi at sunrise, sunset and intervals thru the day and night.

Topics

Resources

License

Stars

Watchers

Forks

Languages