Raspberry Pi Snow Gauge Project

This project turns a Raspberry Pi and an ultrasonic sensor (like the JSN-SR04T) into a web-enabled snow depth gauge.

It features a Flask web server that provides a live dashboard, automatic and manual storm tracking, per-storm CSV data logging, and integration with WeeWX.

Hardware Setup

You will need:

A Raspberry Pi (any model with GPIO pins)

An JSN-SR04T (or similar) ultrasonic sensor

Jumper wires

Wiring:

The ultrasonic sensor (JSN-SR04T) has 4 pins: VCC, TRIG, ECHO, and GND.

VCC -> Pin 2 (5V Power)

TRIG -> Pin 16 (GPIO 23) - Configurable in app.py

ECHO -> Pin 18 (GPIO 24) - Configurable in app.py

GND -> Pin 6 (Ground)

Important: The JSN-SR04T's ECHO pin outputs 5V, while the Raspberry Pi's GPIO pins are 3.3V. You must use a voltage divider (e.g., a 1kΩ and 2kΩ resistor) on the ECHO pin to safely step the voltage down to 3.3V. Connecting 5V directly to a Pi's GPIO pin can damage it.

Software Setup

Clone/Download: Get the app.py and the templates/ directory onto your Raspberry Pi.

project_directory/
├── app.py
├── templates/
│   └── index.html
└── README.md


Install Dependencies:

pip3 install flask requests RPi.GPIO


Configure app.py:
Open app.py and edit the configuration section at the top. This is REQUIRED.

GPIO_TRIG & GPIO_ECHO: Change these if you used different GPIO pins.

SENSOR_HEIGHT_INCHES: This is the most important setting. Mount your sensor in its final location (e.g., under the eaves of your house), then, with no snow on the ground, measure the exact distance from the sensor to the ground in inches. Enter that value here. The script calculates snow depth by (SENSOR_HEIGHT_INCHES - measured_distance).

STORM_PASSWORD: Change this to your own secret password.

WEEWX_ENABLED: Set to True or False.

WEEWX_URL: If WEEWX_ENABLED is True, set this to the URL of your WeeWX server.

Generate HTTPS Certificates (Required):
This server uses HTTPS. You must generate a self-signed certificate and key file. Run this command in your project directory (the same one as app.py):

openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365


This will create key.pem and cert.pem. You can fill in the prompts with any information you like (or just press Enter).

Run the Server:
You must use sudo to grant the script access to the GPIO pins.

sudo python3 app.py


Access the Dashboard:
From any device on the same network (your phone, computer, etc.), open a web browser and go to:
https://[YOUR_PI_IP_ADDRESS]:5000

Note: Your browser will show a security warning (e.g., "Not Secure" or "Your connection is not private") because the certificate is self-signed (not verified by a central authority). This is normal and expected. You will need to click "Advanced" and then "Proceed to [IP Address]" to access the site.

WeeWX Integration

This script can act as a data source for WeeWX. It sends an HTTP GET request to your specified WEEWX_URL with the snow depth if WEEWX_ENABLED: Set to True.

Example: http://192.168.1.100/weewx_data?snow_depth_in=1.23

You need to configure your WeeWX instance to listen for this data. The easiest way is using the WeeWX Interceptor Driver, which can be configured to catch these HTTP requests and map snow_depth_in to the correct field in your WeeWX database.