Friday, March 08, 2019

Weather Prediction Like It's 1850

I've been wearing one of Shira's old watches and I've become fascinated by the weather function. Without any connection to the web and minimal sensors, it boldy offers predictions. See, it's going to be partly sunny today! (OK, that's not the boldest of predictions I suppose.)

How does it do that?

The watch in question is an old tech4o model that fits in the Altitude-Barometer-Compass class of watches, so called ABC watches. Back in the day, these were high tech and provided next level features when compared to a typical sport watch.

My hunch was that the sensor powering the predictions is the barometer. This fascinating article on the topic Fair or Foul? How to Use a Barometer to Forecast the Weather confirmed my suspicion. What a game changer this must have been in the mid 1800's when suddenly forces that seemed godlike were reduced to quantifiable phenomena. Even now, when it seems like weather forecasting should be in the domain of scientists and super-computers, I find it remarkable that you can offer predictions, however crude, using one commonly available sensor. That's too cool.

Most of the time I'm only a few keystrokes away from knowing a precise forecast. That is, however, until I'm off in the woods and there's no cell or WiFi signal. In that case, having something like Shira's watch could be pretty handy. But what if I didn't want to depend on her watch? The Physics Toolbox App on my phone tells me that my Galaxy S9+ has a pressure sensor built in. Could I use my phone as glorified barometer?

Looking on the Android Play Store I found a number of apps that promise to turn my phone into a weather station. But many of them seem quite sketchy, and even the ones that look reputable leave me with questions of efficiency and battery life impact. Fortunately, there's a lighter-weight option than installing one of these apps: Tasker. When configured, Tasker will populate the variable %PRESSURE with the current value of the phone's pressure sensor. Capturing this variable in a simple text file is easy:

(Download this Tasker Profile)

You'll want to make sure that you've setup Tasker to monitor the pressure sensor. Also, in order for the %PRESSURE variable to be populated it needs be referenced by a profile. Or at least that's what I had to do to get the variable to be set.

I got a little overzealous and had Tasker capture the %PRESSURE every 5 minutes. I think hourly is probably sufficient. Regardless, after couple of days I had a data file filled with entries that looked like so:

1552003293:1026.6213

That's standard unix time and pressure in millibars. All that was left to do was to turn that text file into of something human friendly. I kicked off Termux, fired up emacs, and wrote the following script:

#!/usr/bin/env bash

##
## A script for working with barometric pressure data
##
## Assumes standard in is a stream of unix timestamps and barometric pressures
##
## See also:
## https://www.artofmanliness.com/articles/fair-or-foul-how-to-use-a-barometer/
## https://taskernet.com/shares/?user=AS35m8l5RQjTRe3TULACE9yEJktuWU6aAnSRKY16uyMSAb3bzu7qeuaheX4adap2hJHO4tg%3D&id=Profile%3APressure+Collection
## https://github.com/holman/spark
##
last=''

MODE=show

while getopts "d" OPTION ; do
  case $OPTION in
    d) MODE=data ;;
    *) echo "Usage: `basename $0` [-d]" ; exit 1 ;;
  esac
done

while read line ; do
  s=$(echo $line | cut -d: -f 1)
  v=$(echo $line | cut -d: -f 2)
  hg=$(echo "($v * 0.0295)" | bc)
  p=$(echo "($v * 0.0295 * 100)" | bc | sed 's/[.].*//')
  t=$(date "+%Y-%m-%d %H:%M" -d @$s)
  if [ $MODE = "data" ] ; then
    echo $p
  else
    if [ -z "$last" ] ; then
      c="?"
    else
      if [ "$last" -eq $p ] ; then
        c="-"
      elif [ "$last" -lt $p ] ; then
        c="^"
      else
        c="v"
      fi
    fi
    last=$p
    echo "$t $c ($hg)"
  fi
done

The script maps the stream of raw times and pressures into a readable timestamp, rising/falling/steady indicator and pressure in inHg. Consider the example below that tells me that after dipping overnight, the pressure has started rising this morning. That bodes well for today!

While my Tasker + Termux solution lacks the the cute icons Shira's watch provides, it makes up for this with something more valuable: simplicity. Tasker is giving me raw access to the sensor, and good old fashion unix scripting let's me process it. In other words, I've turned my $800 cell phone into a $20 barometer. Most importantly, whether I have Internet access or not, I can play armchair sea-captain and offer up weather predictions. Welcome to the 1850's baby!

No comments:

Post a Comment