Aha, that makes it a lot easier.
For example via Bash and GPIO (this is just an untested piece of code, but gives you an idea):
#!/bin/bash
# Define GPIO pin
GPIO_PIN=17
# Set GPIO pin as input
gpio mode $GPIO_PIN in
# Function to make HTTP request to URL
make_request() {
URL="$1"
# Make HTTP request using curl
curl -s "$URL" >/dev/null
echo "Request sent to $URL"
}
# Main loop
while true; do
# Read GPIO pin value
PIN_VALUE=$(gpio read $GPIO_PIN)
# Check if pin value is low (button pressed)
if [ "$PIN_VALUE" -eq 0 ]; then
# URL to call when button is pressed
REQUEST_URL="http://example.com/api"
make_request "$REQUEST_URL"
sleep 1 # Sleep to debounce button press
fi
# Sleep to avoid high CPU usage
sleep 0.1
done
Bash script runs as a service and every time you press a button connected to GPIO 17, it fires.
Here you need a 2nd script that monitors the event for which you want this to run. For example with a rotaty encoder or switch. (this one does work as I use it to load some preselected radio channels)
const Gpio = require('onoff').Gpio;
const axios = require('axios');
// Define GPIO pin (change this to the appropriate pin)
const gpioPin = 17;
// Define the URL to call when the button is pressed
const apiUrl = 'https://example.com/api';
// Initialize the GPIO pin as an input with pull-up resistor
const button = new Gpio(gpioPin, 'in', 'rising', { debounceTimeout: 10 });
// Function to be executed when the button is pressed
const handleButtonPress = async () => {
try {
// Make an HTTP request to the specified URL
const response = await axios.post(apiUrl, { message: 'Button pressed' });
console.log('HTTP request successful:', response.data);
} catch (error) {
console.error('Error making HTTP request:', error.message);
}
};
// Attach the button press event listener
button.watch(handleButtonPress);
// Handle program termination (cleanup GPIO)
process.on('SIGINT', () => {
button.unexport();
console.log('GPIO cleanup complete');
process.exit();
});
console.log('GPIO script is running. Press the button to make an HTTP request.');
Also just some scribble, not tested. Not a big JS fan …