Page 1 of 1

GPIO voltage sensors

Posted: Mon Mar 23, 2026 2:22 am
by Vagoussontb
Looking for some help with a small project. Have a raspberry pi and some voltage sensors hooked up to gpio headers. Does anyone know what plug-in would be needed to read the voltage on those sensors? Any help would be greatly appreciated.

Re: GPIO voltage sensors

Posted: Mon Mar 23, 2026 9:25 am
by DoubleDoubleA
What voltage sensor do you have? Is there a driver for that sensor? I feel like the sensor is probably a 3-wire, one of those being the data wire. You'd write a custom plugin to call the sensor driver, parse the driver's output for your performance data, and then send that back in the plugin return.

https://nagios-plugins.org/doc/guidelines.html

Re: GPIO voltage sensors

Posted: Tue Mar 24, 2026 7:23 am
by -SN
When sending GPIO data to Nagios I normally opt for the "passive" vs the active.

Adding a webhook that can send data to NRDP is always an option.

Code: Select all

#!/usr/bin/env python3
import RPi.GPIO as GPIO
import requests
import time
import sys

# -----------------------------
# CONFIGURATION
# -----------------------------
OUTPUT_PIN = 18  # BCM pin number for output
WEBHOOK_URL = "https://example.com/webhook"  # Replace with your webhook endpoint
POST_TIMEOUT = 5  # seconds

# -----------------------------
# SETUP
# -----------------------------
GPIO.setmode(GPIO.BCM)
GPIO.setup(OUTPUT_PIN, GPIO.OUT, initial=GPIO.LOW)

def send_webhook(state):
    """Send a POST request to the webhook with GPIO state."""
    payload = {"pin": OUTPUT_PIN, "state": "HIGH" if state else "LOW"}
    try:
        response = requests.post(WEBHOOK_URL, json=payload, timeout=POST_TIMEOUT)
        response.raise_for_status()
        print(f"Webhook sent: {payload} | Response: {response.status_code}")
    except requests.exceptions.RequestException as e:
        print(f"Error sending webhook: {e}", file=sys.stderr)

def toggle_output():
    """Toggle GPIO output and send webhook."""
    current_state = GPIO.input(OUTPUT_PIN)
    new_state = not current_state
    GPIO.output(OUTPUT_PIN, new_state)
    send_webhook(new_state)

try:
    print("Press Ctrl+C to exit.")
    while True:
        toggle_output()
        time.sleep(5)  # Change interval as needed

except KeyboardInterrupt:
    print("\nExiting...")

finally:
    GPIO.cleanup()
You'd have to format your payload for NRDP etc.
There is an Arduino sketch on where I walk through how to add data with a timer for Arduino on Exchange if you want to get crafty and port that to rpi.

--SN

Re: GPIO voltage sensors

Posted: Mon Mar 30, 2026 2:33 am
by lyraellington12
I think it's important to add another key point: if the goal is to read analog voltage, the Raspberry Pi doesn't have a built-in ADC, so you'll usually need an additional ADC module (e.g., MCP3008 or similar). In that case, the plugin/script will read the data via SPI/I2C rather than directly from the digital GPIOs. This is easily overlooked when discussing GPIOs in general.
Also, if you're aiming for Nagios/NCPA/NRDP, standardizing the output format (performance data) right from the plugin is crucial for easier graphing later (PNP4Nagios, Grafana, etc.).