Event detection utility

There was a question recently about event detection, which got me thinking about how I might go about doing this, in a simple way. It has been a long time since I sat down and actually wrote code, but I thought I would try to put my ideas into practice.

The result is a bit of code which reads the UDP packet stream that the R-Shake produces, picks out the ‘Z’ stream (vertical movement), where each packet of data contains the channel name, the time and the latest series of sensor values at 100Hz intervals.

Calculating a “running average” over the most recent 250 samples smooths out the value to give a fairly stable average. Then taking the running average of the last 10 data points removes data “spikes”.

Subtract the 10 sample “window” from the longer term “average” and look at the magnitude of the difference (ignore whether it is positive or negative) and we have a number we can use to compare to a trigger value (say 1000) if that trigger is exceeded, we just might have something worth looking at, so print a message saying something was detected, and the time/date.

Once triggered, it also checks for the existence of an executable file named “detect-alert”. If it exists, and is executable, that file is executed. It can do whatever you code it to do. E.g. send an email. It can be a shell script.

Once triggered, it is likely that every subsequent data point for some time is going to trigger again, so a pause of 30 seconds is started during which triggering is disabled.

This does not detect earthquakes. It detects movement events above some threshold. The time/date allows you to now fire up RS DataView or whatever your favorite tools is to go and see the event in detail.

Source, some documentation and compiled binaries can be found here:



Hello Philip,

Thank you for this very interesting contribution! This kind of detection can be very useful to monitor literally everything that, as you said, creates a movement event above a set threshold.

I’m sure that the community will find it helpful for some of their projects.

Hello Phillip,

I hope you are doing well, i once had a similar question because i a remotely monitoring units deployed in Zimbabwe and i stay in South Africa. my idea was that when the Raspberry shake determine an event or the velocity. i will look at what you have done and try to implement. is possible that i can consult with you if i ran into problems? like getting the code to work properly?

1 Like

Yes. Let me know if you have any problems with the code.

1 Like