From f110922a734b3699c99048256d56fc26c152def2 Mon Sep 17 00:00:00 2001 From: bszutu Date: Sat, 7 Jan 2017 08:52:04 -0800 Subject: [PATCH 1/2] add new script that will work with grep --- wu-pws-grep.sh | 112 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 wu-pws-grep.sh diff --git a/wu-pws-grep.sh b/wu-pws-grep.sh new file mode 100644 index 0000000..560ac07 --- /dev/null +++ b/wu-pws-grep.sh @@ -0,0 +1,112 @@ +#!/bin/bash + +## JH: v1.0 - Weather Underground to pwsweather.com script test +## BKS: v.1.1 - remove dependency for jq and test on busybox so it will run on openwrt + +WORKINGDIR=/tmp/pws + +# this is to handle busybox to make sure the working directory exist after a reboot +mkdir -p $WORKINGDIR + +# Wunderground API key +# Register for a free Stratus plan here: https://www.wunderground.com/weather/api +WUAPI=PutYourWUAPIKeyHere + +# Wunderground PWS to pull weather from +# Navigate to your preferred weather station on Weather Underground and pull the pws:XXXXXXXXXX from the URL +# ex. https://www.wunderground.com/cgi-bin/findweather/getForecast?query=pws:KFLLAKEW61&MR=1 +# ex. WUPWS would be the query value - "pws:KFLLAKEW61" +#WUPWS="pws:KFLLAKEW53" +WUPWS="pws:StationIDhereAndKeeptheQuotes" + +#PWS station ID - sign up and create a station at pwsweather.com +PWSID=PutYourPWSStationIDhere + +#PWS password - password for pwsweather.com +PWSPASS=PutYourPWSPasswordHere + +#============================================================ +#= +#= NOT NECESSARY TO CHANGE ANY LINES BELOW +#= +#============================================================ + +# Construct & Execute Weather Underground API call +WUJSON=http://api.wunderground.com/api/$WUAPI/conditions/q/$WUPWS\.json +echo "Grabbing JSON using the following URL: $WUJSON" +echo "" +wget -O $WORKINGDIR/wu.json $WUJSON + +# ALL json extractions below REQUIRE the jq cmd line tool - on Ubuntu 'apt-get install jq' +# +# extract observation time and convert to UTC +# jq .current_observation.observation_epoch wu.json |tr -d '"' +# TZ=UTC date -d @1459187921 +'%Y-%m-%d+%H:%M:%S'|sed 's/:/%3A/g' +PWSDATEUTC=$(TZ=UTC date -d @$(grep "observation_epoch" $WORKINGDIR/wu.json | cut -d ":" -f 2 | tr -d '"' | tr -d ",") +'%Y-%m-%d+%H:%M:%S'|sed 's/:/%3A/g') +echo "PWSDATEUTC=$PWSDATEUTC" + +# extract winddir +PWSWINDDIR=$(grep "wind_degrees" $WORKINGDIR/wu.json | cut -d ":" -f 2 | tr -d '"' | tr -d ',') +echo "PWSWINDDIR=$PWSWINDDIR" + +# extract windspeed +PWSWINDSPEEDMPH=$(grep "wind"_mph $WORKINGDIR/wu.json | cut -d ":" -f 2 | tr -d '"' | tr -d ',') +echo "PWSWINDSPEEDMPH=$PWSWINDSPEEDMPH" + +# extract windgustmph +PWSWINDGUSTMPH=$(grep "wind_gust_mph" $WORKINGDIR/wu.json | cut -d ":" -f 2 | tr -d '"' | tr -d ',') +echo "PWSWINDGUSTMPH=$PWSWINDGUSTMPH" + +# extract tempf +PWSTEMPF=$(grep "temp_f" $WORKINGDIR/wu.json | cut -d ":" -f 2 | tr -d '"' | tr -d ',') +echo "PWSTEMPF=$PWSTEMPF" + +# extract hourly rainin - Hourly rain in inches +PWSRAININ=$(grep "precip_1hr_in" $WORKINGDIR/wu.json | cut -d ":" -f 2 | tr -d '"' | tr -d ',') +echo "PWSRAININ=$PWSRAININ" + + +# extract daily rainin - Daily rain in inches +PWSDAILYRAININ=$(grep "precip_today_in" $WORKINGDIR/wu.json | cut -d ":" -f 2 | tr -d '"' | tr -d ',') +echo "PWSDAILYRAININ=$PWSDAILYRAININ" + +# extract baromin - Barometric pressure in inches +PWSBAROMIN=$(grep "pressure_in" $WORKINGDIR/wu.json | cut -d ":" -f 2 | tr -d '"' | tr -d ',') +echo "PWSBAROMIN=$PWSBAROMIN" + +# extract dewptf - Dew point in degrees f +PWSDEWPTF=$(grep "dewpoint_f" $WORKINGDIR/wu.json | cut -d ":" -f 2 | tr -d '"' | tr -d ',') +echo "PWSDEWPTF=$PWSDEWPTF" + +# extract humidity - in percent +PWSHUMIDITY=$(grep "relative_humidity" $WORKINGDIR/wu.json | cut -d ":" -f 2 | tr -d '"' | tr -d ',' |tr -d '%') +echo "PWSHUMIDITY=$PWSHUMIDITY" + +# extract solarradiation +PWSSOLARRADIATION=$(grep "solarradiation" $WORKINGDIR/wu.json | cut -d ":" -f 2 | tr -d '"' | tr -d ','|tr -d '-') +echo "PWSSOLARRADIATION=$PWSSOLARRADIATION" + +# extract UV +PWSUV=$(grep "UV" $WORKINGDIR/wu.json | cut -d "," -f 1 | cut -d ":" -f 2 | tr -d '"' | tr -d ',') +echo "PWSUV=$PWSUV" + +# construct PWS weather POST data string + +PWSPOST="ID=$PWSID&PASSWORD=$PWSPASS&dateutc=$PWSDATEUTC&winddir=$PWSWINDDIR&windspeedmph=$PWSWINDSPEEDMPH&windgustmph=$PWSWINDGUSTMPH&tempf=$PWSTEMPF&rainin=$PWSRAININ&dailyrainin=$PWSDAILYRAININ&baromin=$PWSBAROMIN&dewptf=$PWSDEWPTF&humidity=$PWSHUMIDITY&solarradiation=$PWSSOLARRADIATION&UV=$PWSUV&softwaretype=wu_pws_ver1.0&action=updateraw" +#echo $PWSPOST + +RESULT=$(wget -O /dev/null --post-data=$PWSPOST http://www.pwsweather.com/pwsupdate/pwsupdate.php) +echo wget -O /dev/null --post-data=$PWSPOST http://www.pwsweather.com/pwsupdate/pwsupdate.php + +# retains 10 backup cycles for debugging +rm $WORKINGDIR/wu.json.10 +mv $WORKINGDIR/wu.json.9 $WORKINGDIR/wu.json.10 +mv $WORKINGDIR/wu.json.8 $WORKINGDIR/wu.json.9 +mv $WORKINGDIR/wu.json.7 $WORKINGDIR/wu.json.8 +mv $WORKINGDIR/wu.json.6 $WORKINGDIR/wu.json.7 +mv $WORKINGDIR/wu.json.5 $WORKINGDIR/wu.json.6 +mv $WORKINGDIR/wu.json.4 $WORKINGDIR/wu.json.5 +mv $WORKINGDIR/wu.json.3 $WORKINGDIR/wu.json.4 +mv $WORKINGDIR/wu.json.2 $WORKINGDIR/wu.json.3 +mv $WORKINGDIR/wu.json.1 $WORKINGDIR/wu.json.2 +mv $WORKINGDIR/wu.json $WORKINGDIR/wu.json.1 From cb1d35a668165633bdad1c57db2a53f6dac8c941 Mon Sep 17 00:00:00 2001 From: bszutu Date: Sat, 7 Jan 2017 09:13:41 -0800 Subject: [PATCH 2/2] update the readme file --- README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bc83a7e..e14de59 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,17 @@ # wupws simple shell script that parses current observations from a weather underground station and posts to pwsweather.com. Needed so Rachio smart irrigation controller can use nearby weather underground data to adjust watering schedule. -Mandatory Linux Tools Required: -- jq +Updated 2017-01-07 to add wu-pws-grep.sh in order to remove the dependency on jq so it can run on busybox or any other linux +Also add mkdir -p to make sure the weather tmp folder is created if does not exist + +Mandatory Linux Tools Required: +- jq (if you are using wu-pws.sh, jq not required for wu-pws-grep.sh) - wget Mandatory variables to set in the script (more info in the .sh): - WORKINGDIR=/home/ABC123 - WUAPI={your weather underground API key - no braces} -- WUPWS={"your weather underground station identifier - no braces - yes quotes"} +- WUPWS={"your weather underground station identifier - no braces - yes quotes - prefix with "pws:"} - PWSID={your pwsweather.com station ID} - PWSPASS={your pwsweather.com password}