Skip to content

Instantly share code, notes, and snippets.

@andrey-utkin
Created September 15, 2024 22:46
Show Gist options
  • Select an option

  • Save andrey-utkin/bbda5ef4729a37eb65556b88769f1cc4 to your computer and use it in GitHub Desktop.

Select an option

Save andrey-utkin/bbda5ef4729a37eb65556b88769f1cc4 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
if [[ $# != 1 ]]; then
echo "Usage: $0 <ADDRESS>" >&2
exit 1
fi
MAC_ADDRESS=$1
# microclimate=> create table temperature (time timestamptz default CURRENT_TIMESTAMP, temperature_centicelsius smallint);
listen_temperature() {
stdbuf --output=L gattcat notify "$MAC_ADDRESS" 181A 2A6E \
| stdbuf --output=L tee /tmp/temperature.log \
| stdbuf --output=L grep 0000: \
| stdbuf --output=L awk "{ print \"INSERT INTO temperature (temperature_centicelsius) VALUES (x'\" \$3 \$2 \"'::int);\" }" \
| stdbuf --output=L tee /tmp/temperature.sql \
| psql --dbname microclimate
}
# microclimate=> create table humidity (time timestamptz default CURRENT_TIMESTAMP, humidity_percent smallint);
listen_humidity() {
stdbuf --output=L gattcat notify "$MAC_ADDRESS" 181A 2A6F \
| stdbuf --output=L tee /tmp/humidity.log \
| stdbuf --output=L grep 0000: \
| stdbuf --output=L awk "{ print \"INSERT INTO humidity (humidity_percent) VALUES (x'\" \$3 \$2 \"'::int);\" }" \
| stdbuf --output=L tee /tmp/humidity.sql \
| psql --dbname microclimate
}
gattcat discover "$MAC_ADDRESS"
echo Discovery completed.
tail -F /tmp/{temperature,humidity}.{log,sql} &
listen_temperature &
sleep 10
listen_humidity &
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment