mercury/scripts/watch-sensor.sh

31 lines
917 B
Bash
Raw Normal View History

2025-01-19 18:17:37 +03:00
#!/bin/bash
while true; do
# Fetch API response
response=$(curl -s https://mercury.segin.one/sensor)
temperature=$(echo "$response" | jq -r '.temperature')
humidity=$(echo "$response" | jq -r '.humidity')
timestamp=$(echo "$response" | jq -r '.timestamp')
formatted_time=$(date -d @$((timestamp / 1000)))
# Calculate "seconds ago"
current_time=$(date +%s)
timestamp_seconds=$((timestamp / 1000))
time_diff=$((current_time - timestamp_seconds))
# Format time difference
if [ "$time_diff" -lt 60 ]; then
time_ago="${time_diff} seconds ago"
else
time_ago="$((time_diff / 60)) minutes ago"
fi
# Clear screen and print results
clear
echo "🌡️ Temperature: ${temperature}°C"
echo "💧 Humidity: ${humidity}%"
echo "🕒 Timestamp: ${formatted_time}"
echo "🕒 Data fetched: ${time_ago}"
sleep 1
done