Can you make a Raspberry Pi Security Camera?

This weekend’s geeky challenge: Making a Raspberry Pi Security Camera.

TL;DR – Here’s a basic, practically-ready-to-deploy, open-source Raspberry Pi security camera.

Brief

Mostly because I’m cheap (but partly to reduce the scope and therefore increase chance of success) I added a few constraints:

  • Periodic still image capture is OK (streaming video will be tackled in a future iteration)
  • Securely store images in the cloud (so I can check them when I’m away from home)
  • Be configurable
  • Support the cheap official Raspberry Pi camera modules, particularly the “NoIR” variants which come without the Infrared filter for night-time captures when complemented with an IR light pack

Let’s get started.

Design

After a quick napkin sketch it becomes clear we need to build 4 key parts:

  • A .NET Core console application.
    • This is our entrypoint, and using .NET Core lets us compile to the “linux-arm” runtime target, i.e. Raspbian.
  • A timer
    • To periodically take the photos. I have some ideas for other conditions or events that could trigger the camera to take a snapshot (such as a PIR sensor detecting heat) so I’ve implemented the timer as a “TimerTrigger“, which implements an ITrigger interface. That way it’s nice and extendable.
  • Some code that speaks to the Raspberry Pi camera module
    • Again I’ve written a “CameraImageSource“, which implements the IImageSource interface.
    • I also wrote a TestImageSource which loads an image from the current working directory for testing.
  • A client for the cloud storage API
    • I’ve chosen Google Drive because it offers 15GB free storage, a decent SDK that supports .NET Standard (1.3) and has good documentation.
    • As with the other components, this is written as an implementation of the “IDataStore” interface, so it’s easy to expand to other cloud storage providers in the future.

And this is how they all communicate:

Implementation

The code is here:

https://github.com/richstokoe/pispy

Once you’ve cloned that repository, there are some things we need to do before we can run the code.

Cloud Storage Credentials

To use the Google Drive API, you need an OAuth2 client ID and client secret. Follow these instructions to get a client ID and client secret, then hit the Download button in the “Credentials” area of the API console and save the file as “client_secrets.json” in the src/PiSpy/ folder of the repository you just cloned.

This file will be copied to the output directory when the project is built, per MSBuild instructions in the PiSpy.csproj project file.

Configure the Raspberry Pi Security Camera

If you want to change the default options, open up the appSettings.json file in the project.

Timer interval

You can change the interval between camera shots by modifying the Triggers:TimerTrigger:Interval setting. This is in milliseconds (seconds / 1000 so 60 seconds = 60000).

The default is 180 seconds (3 minutes).

Camera output directory

You can change the output directory by modifying the CameraModule:OutputPath setting.

The default assumes you will copy the console app to /home/pi/pispy, if you want to copy it somewhere else, change the path accordingly, otherwise you’ll see an error from the “mmal” process.

Raspbian with a Desktop

For now, we need an operating system on the Pi with an interactive desktop to complete the Google OAuth2 authorization flow.

Follow the steps in my last blog to set up a Pi with Raspbian and the .NET core runtime – but with one minor difference: grab Raspbian Stretch, NOT Raspbian Stretch Lite. This gives you a Desktop – that’s important for when the Google authorization flow pops open a browser window to enter your Google account details.

(In a future iteration we’ll add a Kestrel HTTP endpoint to the service to negate the need for the non-Lite version of Raspbian with a desktop).

You can either plug in a HDMI-capable screen and keyboard, or you can enable VNC by running:

sudo raspi-config

at the command line (or via SSH) and enabling ‘VNC’ under the ‘Interfacing options’ menu.

I also found I had to set Chromium as my default browser for the authorization flow to work properly – to do that navigate to chrome://settings, choose “Set Chromium as my default browser” and then restart the pi by running:

sudo reboot

Deploy and run

As in my last blog, build the project targeting the linux-arm runtime:

dotnet publish -r linux-arm

and copy the the bin/Debug/netcoreapp2.0/linux-arm/publish folder via FTP to the pi. I copied it to /home/pi/pispy.

We’re going to need access to Raspbian’s PIXEL desktop in a moment, so connect to the Pi’s VNC server by following the instructions here. Once you’re in, open a Terminal and navigate to the folder you copied the /publish folder to. Create a new folder within it called stills, which is where the CameraImageSource code will write the pictures to before handing us a Stream.

cd /home/pi/pispy
mkdir stills

(I’m cheating a bit by simply running the “raspistill” executable that comes with Raspbian to take the pictures. More info here.)

Next, start the service by running:

dotnet /home/pi/pispy/PiSpy.dll

After the time specified in the Triggers:TimerTrigger:Interval appSetting elapses, a photo will be taken and then the GoogleDriveDataStore will trigger the authorization flow (this only needs to happen once). Once you’ve logged in future photos will stream up to the Google Drive.

Next steps

Pull requests are not only welcome, they are encouraged.

A good place to start: I haven’t had much time this holiday season to debug (same excuse for shoddy blogging 🙂 ), but there appears to be an async bug in the TimerTrigger whereby the subscribed actions are invoked on a separate thread and the Timer is restarted even though the CameraImageSource still hasn’t finished taking its picture.

And don’t forget to turn off your LEDs:

I hope you’ve enjoyed this slightly rushed guide to building a Raspberry Pi security camera!

aspnetcore.dll failed to load. The data is the error

This morning, while trying to debug our big ol’ web project in Visual Studio 2015 I encountered a problem – it held me up for a while so I wanted to quickly blog about the solution in case it hits you too. When hitting F5 to start debugging, Chrome launched but then immediately Visual Studio detached from IIS Express and showed the following error:

A process with the ID of <id> is not running

True enough, IIS Express wasn’t running…

Open Wide and Say ‘Ahh!’, Mr Windows

I ran a Repair on IIS Express 10.0 in case it was an issue with that, or the self-signed SSL certificate it uses to host web projects over a secure connection…. but still had the same problem.

I then created a brand new ASP.NET MVC 5 project and hit F5… but that ran fine. Hmm, curious. That let me know IIS Express was fundamentally OK, and the issue lay with the big ol’ web project.

Microsoft are usually pretty good at logging when things go wrong so I fired up eventvwr, the Windows Event Viewer, and saw the following  error being thrown by IIS Express:

The Module DLL C:\Program Files (x86)\Microsoft Web Tools\AspNetCoreModule\aspnetcore.dll failed to load. The data is the error

Strange… We don’t have any ASP.NET Core projects in that solution so why aspnetcore.dll is being loaded was beyond me. Furthermore, that path didn’t exist on my disk. Continue Reading “aspnetcore.dll failed to load. The data is the error”

Things I learned at my last job

Today I closed a chapter in my life. After nearly 4 years tenure at a company I wanted to reflect on the things I learned over that time.

I have been very lucky to have a few excellent – world-class even – mentors here who have taught me things that will stay with me for the rest of my life, and I wanted to share the reflection process with you in the hope you gain something valuable too.

Individual Success Isn’t Success

For a long, long time I  adopted the ‘aircraft oxygen mask’ approach to my career: I’ll get to where I want to be first, then I’ll help others. This company has taught me that isn’t the right thing to do.

ubuntumeme

My thinking was always “I’ll be in a better position to help others” once I hit my objectives, but that simply doesn’t work in practice: without respectful, cooperative development across your team(s), you risk yourself hitting your goals at all, and if you haven’t helped others hit theirs too, nobody wins.

Dare I use the management-bullshit-bingo term ‘synergy’?

My current role here is a technical leadership role – that means I don’t have people reporting to me but I do have authority over technology direction and a remit to ensure conceptual integrity of the solution. I have led project teams before, I have even run small businesses before, but being a leader in a larger company was new to me when I began this chapter of my life, and I wanted to be good at it.

I’ve seen all the memes about the difference between a boss and a leader but for some reason I struggled to enact the differences. However, after some time spent being (in retrospect) a terrible boss, some sage advice from one of those mentors made everything ‘click’, and I was given the mental tools to develop the techniques required to become a good leader instead. (Note, a good mentor won’t give you the answer, but the means of finding it on your own!).

boss-vs-leader

“Take people with you.”

So what does that look like in practice? Last year I was offered the chance to travel to our American HQ to present some new work to 1,500 customers. ‘Prestigious’ isn’t even close – this is a huge event, so compelling that our customers pay us to listen to our plans and roadmap. The trip dripped with a significant amount of attached ‘kudos’ and the opportunity to rub shoulders with the highest of the high in the business. Not only that – the opportunity to ask probing questions to 1,500 customers about our technology direction is such a rare occurrence it was unmissable. The old me would have started packing immediately. Continue Reading “Things I learned at my last job”

Learn to Code (An Intermission): Code.org

The founders of Facebook, Microsoft, Dropbox, Twitter and many more top tech companies have provided their voices and recognisable fizzogs to this latest video from Code.org. Code.org promotes the principle that nobody is born with the ability to code, or play basketball, or drive a racing car: it is a learned skill. The biggest hurdle is that first step overcoming the apprehension of the unknown. That’s something that all of these people have done. From humble beginnings and all that jazz…

 

Learn to Code: Chapter 2 – Black Tie Function

She only wants you for your brain…

This is the third part of my Learn to Code series. The previous part is here: Chapter 1 – Meet Bob.

 

See Bob. See Bob Run. Run Bob, Run!

We last spoke about making Bob run 5 miles. We were going to do that by performing an action – a “function” in programmer parlance – called “run”, and give the number 5 (meaning the miles he is to run) to the function. We do that like this:

bob.run(5);

 

The problem is, we haven’t said what “run” actually means or does. Later we’ll use code that already exists but this time it’s all up to us. We’re going to have to write this “run” function ourselves. Again, this is a very easy thing to do once you understand the pattern you use.

function (miles) {
}

 

Here we say we want to create a “function” and that the function will take some information in brackets that we can refer to by the name “miles”. You see, when information (such as the number 5) is “passed” into the function, it is important that we can get to it because we can use it to change how the function operates. You can write a function once, but pass different things to it each time and it will behave differently every time. That is the real beauty of programming.

You will notice there aren’t any semi-colons in the bit of code above, this is because we are using a pattern that the computer will understand – those curly brackets (called “braces”) tell the computer when you’re starting and stopping to describe your function. However, when we write code inside those braces, we still must use semi-colons to differentiate one .

The devil is very much in the details when programming. Different programming languages have different requirements for WHERE those curly braces go. Sometimes they should be on the same line as the start of your function, and sometimes they go on the next line down. Like this:

function (miles)
{
}

 

But the language we are learning needs them to be on the same line as the word “function”.

Here’s an example of combining curly braces and semi-colons. Gosh it’s getting complicated… but stick with it, this is as complex as it gets. let’s write some code inside the function that uses that number 5 that was passed in, which we can refer to as “miles”:

function (miles) {
    return miles + miles;
}

 

This is now a fully-functioning function. If you’ll pardon the pun. Although… not a particularly useful one. Yet.  Continue Reading “Learn to Code: Chapter 2 – Black Tie Function”

Learn to Code: Chapter 1 – Meet Bob

You understand that, right?

This is the second part of a series of articles called Learn to Code. Click here to go to Chapter 0, “Wax on, Wax off”.

 

Learn to Code

Well done. You’re now a World class programmer! And you’ve been given a brief  by your customer who wants you to write some software for them. The following sentence is the first line of their brief to you:

“Bob is a man. He is 30 years old. He can run 5 miles.”

What a peculiar start!! But bear with me. Remember, in order to start programming, we have to fully understand the real-world thing we are transforming into some computer code. Let’s take that sentence apart and write some bullet points about what we know about Bob:

  • Bob is a “something”.  A real thing. A proper object.
  • That thing is categorised as a “man” (whatever that is).
  • Bob has something called an “age”. The value of his “age” thing is “30”.
  • Bob can perform an action known as “running”.
  • Bob can do that action for “5 miles”.

 

Sorry for all the quotation marks and playing-dumb, but all will become clear as we move through this lesson.

Like pretty much anything in the universe, Bob can be defined by a set of “properties” (like his age, height, inside leg measurement) and the actions that he can perform (like “run”). In proper programming, we call these “properties”, “properties” (hahaha, surprise! I slipped a programming term into your vocabulary!). Slightly more complex, we call the actions he is capable of performing, “methods” or “functions”, depending on which programming “language” you’re using. We’ll use the term “functions” to start with because the first programming language you’ll use demands this terminology.

The programming language we’ll start with also prefers things to start with a lowercase letter. For now, just accept that, we’ll cover why later. Let’s start by creating Bob in code.  Continue Reading “Learn to Code: Chapter 1 – Meet Bob”

Learn to Code: Chapter 0 – Wax on, Wax off

Warning! Programming might not make you rich. On a completely different topic, here’s Paul Allen’s private yacht, “Octopus”. (He’s a programmer).

So you want to learn to code? Well this is the right place. I assume no prior knowledge, except knowing how to turn on a computer, open a text editor such as Notepad, and have some idea of the operation of a keyboard. It may not be immediately obvious WHY I’m covering some of the topics in this article, but rest assured that, like Daniel-san painting fences or waxing Myagi’s car, it is all about preparing your state of mind.

Actually, while we’re on the subject of films, before you begin, make sure you watch The Social Network and The Pirates of Silicon Valley. They’re essential viewing for wannabe programmers!

 

Basic Mathematics

WAIT! Stop! Don’t be afraid, we’re only going to touch the absolute basics. Primary school stuff. Nothing else. That’s all you need!

Really? I hear you ask. Yes!! You don’t need to be a maths whiz to be a great programmer. Leave all that business to the computer!!

If you’ve ever written this:

1 + 1

…you’ve written a “program”. A program is simply a set of “instructions” for the computer to perform. Even if it’s just 1 + 1! Sure, most programs are a lot more complicated than adding 1 to 1, but at its most basic, that’s it. Easy!

Computer programming (also known as “coding”) is usually done to replace human effort with automation. The first step in any coding project is usually to break the real world problem down to its smallest parts. That way you can better understand what it really is that you’re turning into code.  Continue Reading “Learn to Code: Chapter 0 – Wax on, Wax off”