As reported elsewhere on this blog,  we run a small, cheap weather station here at home. This is not just because the weather is always an interest at 58° North, but also because, being off-grid, our electrical power comes from the weather, so knowing the state of the weather assumes a greater importance.  We display the weather station information via a web page. We have one internal to our little network at home, but also make it available via the Internet at the location shown at the top of this page.

The weather software is weewx - and again, I have documented on this site migrating to this system a little while back. Some twitter followers seem to be interested in the weather, so a while back, I thought, as an exercise, I would try to generate a little statement of current weather conditions that could be tweeted automatically directly from the server. I quite enjoy using scripting tools to tease out information from a different source, but my script was becoming clumsy and unreliable. It was then that I realised that the information weewx produces is driven by templates. If I changed the template, I could add weather information in a suitable format, but which would not display when viewed via a web browser, but which would be easy to extract and tweet.

There are a number of command-line twitter clients. Most require you to have a twitter developer account, but Twitter demands far too much personal information from you to set up such an account, so many of the options were not available to me.  However, one, twidge, was. It is an older client, written by John Goerzen, and last updated some years ago.  It is in the Debian Stretch repositories (but not in Buster) so was easy to install. However, as it has not been updated in some years, it still restricts tweets to 140 characters. More on this later.

Now I must get my excuses in early - I enjoy scripting, but I'm really bad at it. I have no illusions about any beauty of my scripts, and if you have ideas of how to beautify it, please let me know.  Here is the script that does the work:


#!/bin/bash

LC_ALL=C.UTF-8

SRCFILE="/var/www/weather/index.html"
TMPFILE="/home/stevan/tweettmpfile.txt"
TWEETFILE="/home/stevan/tweetweather.txt"

## Make sure no extraneous old files are lying around
rm -f $TMPFILE
rm -f $TWEETFILE

##Start extracting info from the source file
grep -A8 "TweetThis" $SRCFILE >> $TMPFILE
HEAD=`head -n1 $TMPFILE | cut -c 10-`
WIND=`sed -n 2p $TMPFILE`
TEMP=`sed -n 3p $TMPFILE | sed 's/°C //g'`
HUMID=`sed -n 4p $TMPFILE`
PRESS1=`sed -n 5p $TMPFILE`
PRESS2=`sed -n 6p $TMPFILE`
RAIN=`sed -n 7p $TMPFILE`
SUN=`sed -n 8p $TMPFILE`
MOON=`sed -n 9p $TMPFILE`

##Create the file to tweet
echo $HEAD $WIND $TEMP $HUMID $PRESS1 $PRESS2 $RAIN >> $TWEETFILE

##Do the tweeting
echo "$(cat $TWEETFILE)" | twidge update

#Clean up after yourself
rm -f $TMPFILE
rm -f $TWEETFILE
This little script is simple enough to be self-explanatory to those who have an interest in doing this. I then edited the template. I use the standard "skin" in weewx, just fettled to my liking. At the end of the template file, located at /etc/weewx/skins/Standard/index.html.tmpl I added the following:
<!--
TweetThis Clachtoll $current.dateTime.format("%R") weatherbot.
Wind $current.windDir.ordinal_compass $word_beaufort F$beaufort.
Temp $current.outTemp C.
Humidity $current.outHumidity.
Pressure $current.barometer,
        #if $trend.barometer.raw > 6 rising very rapidly.
                #elif $trend.barometer.raw > 3.5 rising quickly.
                #elif $trend.barometer.raw > 1.5 rising.
                #elif $trend.barometer.raw > 0.1 rising slowly.
                #elif $trend.barometer.raw > -0.1 Steady.
                #elif $trend.barometer.raw > -1.5 falling slowly.
                #elif $trend.barometer.raw > -3.5 falling.
                #elif $trend.barometer.raw > -6 falling quickly.
                #else falling very rapidly.
                #end if
Rain today $day.rain.sum.
Day $almanac.sunrise.format("%R") to $almanac.sunset.format("%R").
$almanac.moon_phase Moon
-->

Again, those who are happy altering the weewx python templates should see what's going on here. I have created a comment block of information that, as it is a comment, will not display.  But it is in an easy format to extract with the little bash script above.

Now it was just a case if setting up a cron job to run the script at the chosen times, a little after the actual time, to allow weewx to generate the data. So the script runs at 6.01am, 12.01pm and 6.01pm, and appears in my twitter timeline @WithAVeeAy

I ran into some issues trying to restrict the information to 140 characters, and It was easy to find in the source code where those restrictions were in place. Good, I thought, I will have a little more room to play with if I just compiled up the code.  But then I discovered the dreadful pit that seems to Haskell programming, or at least Haskell compiling.  I have now spent several hours trying to get this to compile for a raspberry Pi, but have completely failed, with varying and constant error messages.  Worse, I had to strip down a new Pi installation to try to squeeze every bit of memory from the system to provide for the demands of the compile environment. It's not been at all satisfying.  I suspect that i might succeed if trying to compile for x86, but for the Arm-based Raspberry Pi, it seems impossible.

No matter, the basics of the weather are there, so it's adequate and a little bit of interest along the way.

Here is an example tweet.

Addendum:  I have recently joined Mastodon, an interesting alternative to Twitter. I have altered the above script to allow the weather data to be uploaded to Mastodon as well. This is largely the same, but Mastodon allows 500 characters in a "toot" (like a tweet), so there are fewer constraints. The tool used to upload i madconctl, available here - https://github.com/McKael/madonctl

I am https://mastodon.social/@withaveeay on Mastodon