Pointless Waymarks Software

Software from the Desert Southwest

Vibration Monitor - Raspberry Pi, SW420 Vibration Sensor

Created by Pointless Waymarks CMS on 9/19/2024. Updated on 12/23/2024.

2024 September Monitoring Vibrations with an SW420 and Raspberry Pi Zero 2 W
2024 September Monitoring Vibrations with an SW420 and Raspberry Pi Zero 2 W . Charles Miles. 9/10/2024.

This C# .NET Core project runs on a Raspberry Pi, records vibration periods from the SW-420 and has a simple web api to retrieve the data.

The SW-420 Vibration Sensor is an inexpensive package (search SW-420 on Amazon - as of late 2024 prices are under $10 for a pack of 5) that can be powered from a Raspberry Pi (this has been tested on the Pi Zero 2 W, 3 A+ and 3 B+) and outputs its state on a GPIO pin. The sensor has a potentiometer to adjust the sensitivity of the sensor.

This program uses several settings to record 'Vibration Periods' to a SQLite database and serve that data via a web api:

This is NOT designed to record each individual vibration that the SW-420 records - and is also not designed to record single very short vibrations - rather it is designed to record something like a motor running where the SW-420 may switch between vibrating/not-vibrating multiple times over the motor's run. Once the program receives a vibration (how often it checks is set with the Polling Frequency) it will consider the vibration ongoing as long as it receives another vibration within the Minimum Period (even if every check of the sensor does not register as vibrating).

Vibration Periods (and program errors) are written to the database and can be retrieved via the web api.

Command Line Arguments

Software for the Raspberry Pi

The code for this software is available on GitHub - can either clone and build the code from GitHub or download the program below.

If you clone the code from GitHub there is a PublishAll.ps1 script in the root directory that will publish both the VibrationMonitor and VibrationMonitorApi projects for the Raspberry Pi to M:\PointlessWaymarksPublications\VibrationMonitorProject using the FolderProfile.pubxml file in the VibrationMonitor\Properties\PublishProfiles and VibrationMonitorApi\Properties\PublishProfiles. You can either map a directory to M: or edit the script and the FolderProfile.pubxml files to point to another directory.

The publish profiles are setup to publish self contained, single file, executables for linux-arm64. These settings will not produce impressively small files, but they will run without needing any additional packages installed on the Pi.

Pi Setup

Properly setting up and securing a Raspberry Pi is beyond the scope of this post- suggested below are some basic steps to get the program up and running on a Raspberry Pi.

sudo apt update
sudo apt upgrade
sudo apt-get install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
chmod +x ./VibrationMonitorProject/VibrationMonitor/VibrationMonitor
nano VibrationMonitorProject/VibrationMonitor/vibrationmonitor.service
sudo cp VibrationMonitorProject/VibrationMonitor/vibrationmonitor.service /etc/systemd/system/
sudo systemctl daemon-reload
 sudo systemctl enable vibrationmonitor --now
journalctl -u vibrationmonitor -f
chmod +x ./VibrationMonitorProject/VibrationMonitorApi/VibrationMonitorApi
nano VibrationMonitorProject/VibrationMonitorApi/vibrationmonitorapi.service
sudo cp VibrationMonitorProject/VibrationMonitorApi/vibrationmonitorapi.service /etc/systemd/system/
sudo systemctl daemon-reload
 sudo systemctl enable vibrationmonitorapi --now
journalctl -u vibrationmonitorapi -f

To setup the SW420 sensor setup:

In the picture at the top I am using a Raspberry Pi Zero 2 W - for weatherproofing I used a small tupperware container with vent and cable holes drilled out (I hot glued window screen over the vent holes and did some electrical tape over the cable holes to try to limit the number of insects inside the container). This setup is over a month old now and working well, I had two issues that might relate to the details of my setup and hardware but might be worth noting:

Web API

** This program assumes that the Web API will be used on an internal network and that there is ZERO need to secure Vibration API data!! **

The easiest way to get started with the api is to visit http://[Your Pi's IP Address/name]:[7171 or the port you specified]/ - this will show the Swagger UI for the API.

API Endpoints:

Background

Our house has a an alternative septic system - I had never even heard of an 'alternative' septic system until a few years ago! The system we have includes control panel with a timer for a pump in the holding tank. Overall this is a durable and elegant system - but it doesn't log information, and if you have a problem or a question (and aren't an expert on the system) this can be frustrating. I wrote this system to monitor the pump in our holding tank. There are two important reasons I used a vibration sensor: attaching the vibration sensor to the output pipe from the tank means the monitor is more likely to measure 'water flowing out of the tank' which is ultimately what matters (vs for example monitoring current to the motor, which would probably be cleaner, but really I only care about water leaving the tank...) and it allows monitoring without any changes to, or extra equipment in, the control panel - a critical detail for a system under warranty...

Packages Used

Even this fairly simple program owes an incredible debt to the amazing open source projects available for .NET and all of the people who have contributed to them!

Tools:


Tags: