cloudy cloudy

Author Topic: Weather Display Conditions Messenger PHP Script  (Read 34113 times)

0 Members and 1 Guest are viewing this topic.

Offline MCHALLIS

  • Posts: 2,145
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Weather Display Conditions Messenger PHP Script
« on: July 12, 2009, 07:36:11 AM »
Weather Display Conditions Messenger PHP Script by Mike Challis

Perfect for sending your current conditions to Twitter or Email.
Ken True recently posted a similar script, this one has more features.

Partially based on a variation of the following scripts:
NOAA Top Weather Advisory Alerter PHP Script by Mike Challis
http://www.642weather.com/weather/scripts-noaa-top-weather-alerter.php
RSS Top Warning PHP Script by Ken True http://saratoga-weather.org/scripts.php
PHP NOAA Advisory Text by Tom Chaplin http://www.carterlake.org/weatherphp.php
Twitter Code by http://morethanseven.net/2007/01/20/posting-to-twitter-using-php/


What does it do?
Ever want to receive current weather conditions by cell phone, blackberry, Twitter, or email? Now you can.

This PHP script is used to check the Weather Display Conditions on a timed schedule,
then send a message.
Uses Weather Display Conditions data from your own personal weather station's testtags.php.
The message is short enough to send to twitter, a cell phone, blackberry, or email.
Supports sending messages by Email and Twitter, each method can be enabled or disabled.
Messages can be customized in the script settings.

Includes some of the features of the NWS Weather Forecast Messenger PHP Script by Mike Challis
www.642weather.com/weather/scripts-nws-forecast-messenger.php


What do I need?
PHP 5 with CURL support is required
.. personal weather station with weather-display software uploading testtags.php to your web site
.. Web server hosting account with PHP support and ability to send emails from PHP
.. Ability to schedule a cron job or a scheduler program on your PC that can request a URL every 1 hour.
.. your PHP server MUST allow PHP scripts permission to write files.
... PHP safe_mode is recommended to be off


How to use it:
1) Set your settings in the "settings" of the PHP script.
Be sure to set your email address and the optional twitter user password.
Also set the settings for Email and Twitter to customize the messages, each method can be enabled or disabled.

2) Upload this php file to your web server

3) Test from a URL, see if you can see the HTML output
Example: http://www.yoursite.com/wd-conditions-messenger.php
After testing, you can move it to a private non-public web server folder.
Warning: it is recommended that you put the PHP file in a non-public www directory!
because you do not want people clicking the URL and flooding you with emails and twitters!!
If you put it in a non-public www directory, then the only way you can call it is from a cron job
There is a flood filter setting that will prevent the flood problem and allow you to have it in a public folder,
but make sure you test it first, don't just "set it and forget it".

4) It is recommended that you test an email or twitter to make sure you will receive one.

5) Schedule automatic timed updates
see readme.txt for more info

Download
www.642weather.com/weather/scripts/wd-conditions-messenger.zip

Live Demo
www.642weather.com/weather/wd-conditions-messenger.php
« Last Edit: July 12, 2009, 10:22:24 PM by MCHALLIS »

Offline saratogaWX

  • Global Moderator
  • Posts: 5,600
  • Saratoga, CA, USA 37:16:28N, 122:01:23W - Elev: 374ft.
    • Saratoga Weather
Re: Weather Display Conditions Messenger PHP Script
« Reply #1 on: July 12, 2009, 10:04:56 PM »
Very nice, Mike!

Best regards,
Ken
Ken True
Saratoga Weather
CWOP: CW1792
WeatherUnderground: KCASARAT1
Free weather website PHP scripts and WD website AJAX templates

Offline MCHALLIS

  • Posts: 2,145
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: Weather Display Conditions Messenger PHP Script
« Reply #2 on: July 12, 2009, 10:19:52 PM »
I wish I could get the wind and the gust rounded without a decimal place.
I thought that normally there should not be decimals on mph wind units anyway?

Offline saratogaWX

  • Global Moderator
  • Posts: 5,600
  • Saratoga, CA, USA 37:16:28N, 122:01:23W - Elev: 374ft.
    • Saratoga Weather
Re: Weather Display Conditions Messenger PHP Script
« Reply #3 on: July 12, 2009, 10:25:05 PM »
I switched my twitter updater to do additional formatting (and use 'Calm' in place of '0->0mph') by using
Code: [Select]
include_once('testtags.php');
$uomTemp = 'F';
$uomWind = 'mph';
$uomBaro = 'in';
$uomRain = 'in';

$sTemp = round(strip_units($temperature),0);;
$sHum  = "$humidity";
$sWind = "$dirlabel " . round($avgspd,0)."->".round(strip_units($maxgst),0).$uomWind;
  if (preg_match('|0->0|',$sWind) ){
$sWind = "Calm";
  }
$sBaro = sprintf("%01.2f",round(strip_units($baro),2));
$sRain = sprintf("%01.2f",strip_units($dayrn));

$message = fixup_time($time)." $sTemp$uomTemp (H ".round(strip_units($maxtemp),0)."/L ".round(strip_units($mintemp),0).
   ") $Currentsolardescription Hum:$sHum% Wind:$sWind ".
           "Baro: $sBaro$uomBaro Rain: $sRain$uomRain http://bit.ly/NKksn #weather";
and adding two functions from the ajax-dashboard script
Code: [Select]
//=========================================================================
// change the hh:mm AM/PM to h:mmam/pm format
function fixup_time ( $WDtime ) {
  if ($WDtime == "00:00: AM") { return ''; }
  $WDtime = preg_replace('|^0:|','12:',$WDtime);
  return date('g:ia' , strtotime($WDtime));
}

//=========================================================================
// strip trailing units from a measurement
// i.e. '30.01 in. Hg' becomes '30.01'
function strip_units ($data) {
  preg_match('/([\d\.\+\-]+)/',$data,$t);
  return $t[1];
}

Hope this helps...

Best regards,
Ken
Ken True
Saratoga Weather
CWOP: CW1792
WeatherUnderground: KCASARAT1
Free weather website PHP scripts and WD website AJAX templates

Offline MCHALLIS

  • Posts: 2,145
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: Weather Display Conditions Messenger PHP Script
« Reply #4 on: July 15, 2009, 05:13:56 PM »
Thanks Ken, I added some of your code in here.

Version: 1.01 - 15-Jul-2009 - improved units of measures handling
- added rounding of wind units
- added settings for units of measure indicators
- added setting for testtags file path
- added setting for selecting one of two wind display options
- added timestamp to message

Offline MCHALLIS

  • Posts: 2,145
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: Weather Display Conditions Messenger PHP Script
« Reply #5 on: July 15, 2009, 07:21:14 PM »
Version: 1.02 - 15-Jul-2009 - added high/low temp to messages: (H84/L59)

abaldish

  • Guest
Re: Weather Display Conditions Messenger PHP Script
« Reply #6 on: July 16, 2009, 01:51:43 AM »
Just tested this one, works fine no problems at all!

abaldish

  • Guest
Re: Weather Display Conditions Messenger PHP Script
« Reply #7 on: July 16, 2009, 07:54:18 PM »
Just a side note, If you use a Davis VP 2+ and want to use the info that comes from the solar info, just replace $weathercond with $Currentsolardescription as the $weathercond is based on the metar for the airport you are using. and using the $Currentsolardescription will use the info that is coming from your unit.

the tag to replace is $weathercond :
Code: [Select]
$wxconditions = fixup_time($time)." $weathercond - Temp $sTemp$uomTemp$sHL, Wind $sWind, Baro $sBaro$uomBaro, RainToday $sRain$uomRain, Hum $sHum%";
Run a few test runs to make sure you are still under the 140 cap that twitter uses.

a sample of my output is:
2:10am Temp 68F (H 71/L 68) Night Time/Dry/Cloudy with clear patches Hum 80% Winds S 0->2mph Baro 29.88in Rain Today 0.00in

As above using the solar tag showed Night Time/Dry/Cloudy with clear patches, and using $weathercond showed only Dry.

Now using the solar here is my line i have set:
Code: [Select]
$wxconditions = fixup_time($time)."  $sTemp$uomTemp $sHL $Currentsolardescription  Hum $sHum% Winds $sWind Baro $sBaro$uomBaro Rain Today $sRain$uomRain";

Which will output like this:

1:55pm 88F (H 88/L 68) Sunny/Dry Hum 46% Winds WNW 8->16mph Baro 29.80in Rain Today 0.09in

Works well for twitter and text message to your phone.

Thanks goes to Ken and Mike for the newer script that does the twitter and email.


« Last Edit: July 16, 2009, 08:16:23 PM by JtwnPaWeather »

Offline MCHALLIS

  • Posts: 2,145
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: Weather Display Conditions Messenger PHP Script
« Reply #8 on: July 16, 2009, 09:53:13 PM »
Thanks Albert.

I am testing a version that has an improved shorten_twitter function
http://www.642weather.com/weather/wd-conditions-messenger.php

It incorporates this setting:
Code: [Select]
// array of words to shorten, but only when the twitter message is more than 140 characters.
// array is formatted is like this: 'Long Word' => 'Short Word',
// you can add or comment out lines from this array as you like
// the shorten_twitter function will loop through the word replacements one at a time and break early
// if the message is shortened enough.
$twitter_shorten = array(
'Showers'     => 'Shwrs',
'Rain'        => 'Rn',
'Chance'      => 'Chce',
'Partly'      => 'Ptly',
'Cloudy'      => 'Cldy',
'Forecast'    => 'Frcst',
'Night Time/' => '',
'Cloudy with clear patches' => 'Ptly Cldy',
);

I switched on $Currentsolardescription my demo.
I got busy so I cannot release it today.
« Last Edit: July 17, 2009, 06:52:47 PM by MCHALLIS »

abaldish

  • Guest
Re: Weather Display Conditions Messenger PHP Script
« Reply #9 on: July 17, 2009, 12:25:27 AM »
No problem, I was tinkering with the script till about 3am lol. Since i am a night owl all the time. and was tweaking the settings. Just have to watch for the longest one of them all i seen so far and that is the: Night Time/Dry/Cloudy with clear patches
« Last Edit: July 17, 2009, 12:28:31 AM by JtwnPaWeather »

Offline MCHALLIS

  • Posts: 2,145
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: Weather Display Conditions Messenger PHP Script
« Reply #10 on: July 17, 2009, 06:51:50 PM »
Version: 1.03 - 17-Jul-2009 - improved shorten_twitter function
- added configurable array of shorten_twitter words to settings.
- changed current condition variable to use $Currentsolardescription instead of $weathercond,
this can be reversed on about line 256:
Code: [Select]
// uncomment only one of these two condition tags
$sCond = $Currentsolardescription; // current condition: Dry/Cloudy
//$sCond = $weathercond;           // current condition: Dry

abaldish

  • Guest
Re: Weather Display Conditions Messenger PHP Script
« Reply #11 on: July 18, 2009, 01:49:42 AM »
Mike works great here. Love the idea for the lines to strip anything that is above 140 for twitter.
As for the email via text to a cell phone took notice they also have a limit. So i used what you had in there and have the email set for 120 chars. So both work.

Quote
Version: 1.03 - 17-Jul-2009

Offline MCHALLIS

  • Posts: 2,145
  • Long Beach, WA USA
    • Weather for Long Beach, WA USA
Re: Weather Display Conditions Messenger PHP Script
« Reply #12 on: July 18, 2009, 04:44:13 AM »
OK Albert, thanks for the suggestion.

Version: 1.04 - 17-Jul-2009 - shorten_twitter function changed to a shorten_message function that can shorten emails and twitters
- added two more settings:
// number of characters to attempt to shorten twitter message to
$twitter_shorten = '140';

// number of characters to attempt to shorten email message to
$email_shorten = '120';

abaldish

  • Guest
Re: Weather Display Conditions Messenger PHP Script
« Reply #13 on: July 18, 2009, 05:00:20 AM »
You way was cleaner than mine lol.
« Last Edit: July 18, 2009, 05:07:52 AM by JtwnPaWeather »

abaldish

  • Guest
Re: Weather Display Conditions Messenger PHP Script
« Reply #14 on: July 18, 2009, 05:41:52 AM »
Did notice that the Cloudy with clear patches still shows up. But i was working on it, when it went overcast just my luck lol. and noticed that it adds a extra space after the $sCond, so you end up with Cloudy with clear patches , Temp.

Also in the Array you have 'Night Time/' => '', twice is this correct? wasn't sure.
Also seen that it seems wd is adding it's own little space in there, as i did have $sCond, in there and now with a update shows Night Time/Dry/mostly cloudy - , Temp 61F

Yea i like tinkering with things. and i am up 1/2 the night lol
« Last Edit: July 18, 2009, 06:09:18 AM by JtwnPaWeather »

 

cumulus