Hi all, I recently purchased an el-cheapo weatherstation and I've been climbing the learning-curve hill since then. It was for my Dad for Christmas, and the object of the exercise was to enable him to add weather information to his Goldmining and homestay website. Google lead me to Sandaysoft and Cumulus, and I've been tinkering with it since Christmas day on and off, just getting the information I wanted in a way that I wanted it. I've done this the hard way... and that will teach me to not read the documentation
but hey, its a good way to learn.
Essentially I wanted the weather info in the same format as the rest of the site, and while Steve has done a great job of formatting the information I can't just use plain HTML due to the way Drupal (the CMS system I develop in) formats pages. So I needed to pull the info apart and reformat it the way I needed it.
Ideally, the following code would be in a 'module' (clip on application for Drupal), and eventually it probably will be... time and energy permitting... but for now it is effectively some PHP code pulling apart and reformatting some of the information from the realtime.txt file. I'm posting it here for a few reasons, not least of which is that it will be a safe backup of what I have written in case I break and/or lose it in the future
But also because I believe in open source and sharing knowledge... and this seems like a decent way to help a community that has helped me (with software, ideas and entertainment - reading the forum over the last few weeks).
Note: this code is pretty basic, and the display is not nearly completed... but its a proof of concept and is a springboard for the next level... Also note that I have incorporated a windchill calculation, but I'm not actually using it as yet...
So, here is the code for the page at (and this is a temporary development site - mine, actually) http://steve.dunford.org.nz/weather
(and note: you need to have the core-optional PHP evaluation module enabled, and be using the PHP input type in Drupal):
* Script to pull apart the realtime txt file and allow it to be
* used to show various weather items in Drupal
$tempFile = "<path to the directory relative to the server root>/realtime.txt";
$tempContents = fopen($tempFile,'r');
$realtimeContents = fgets($tempContents,1000);
$realtime = explode(" ",$realtimeContents);
$weatherTime = explode(":",$realtime);
$weatherTime = $weatherTime.$weatherTime;
$weatherDate = explode("/",$realtime);
$weatherMonth = array ("January","February","March","April","May","June","July","August","September","October","November","December");
$weatherMonthNum = -1 + (int)$weatherDate;
$windKPH = round((1.852 * (double)$realtime),1); //Wind is in Knots, convert to numeral, then KPH, then trim to 1dp
$tempC = (double)$realtime; //convert temp from string to numeral.
$windChill=(13.12 + 0.6215 * $tempC - 11.37 * pow($windKPH,0.16) + 0.3965 * $tempC * pow($windKPH,0.16)); //windchill calculation
$windChill = round($windChill,1); //round to 1dp
if ($windKPH < 5 || $windKPH > 100 || $tempC < -50 || $tempC > 5) $windChill = 0; //constraints for windchill measurement
<?php echo 'Latest update: '.$weatherTime.' hours on the '.((int)$weatherDate);
if ($weatherDate == "1" || $weatherDate == "21" || $weatherDate == "31") echo 'st';
else if ($weatherDate == "2" || $weatherDate == "22") echo 'nd';
else if ($weatherDate == "3" || $weatherDate == "23") echo 'rd';
else echo 'th';
echo ' of '.$weatherMonth[$weatherMonthNum].", 20".$weatherDate; ?>
<h4>Naseby, New Zealand - 45°01'47"S 170°08'19"E, Elevation 1968 ft</h4>
<p>Naseby weather, updated every 15 minutes when the local workstation is running</p>
<table style="width: 100%; max-width: 700px;">
<tr style="height: 15px;">
<td valign="top">Temperature: <?php echo $realtime; ?><sup>o</sup><?php echo $realtime; ?><br>
Humidity: <?php echo $realtime; ?>%<br>
Windchill: <?php echo $realtime; ?><sup>o</sup><?php echo $realtime; ?><br>
Dew Point: <?php echo $realtime; ?><sup>o</sup><?php echo $realtime; ?><br>
Barometric Pressure: <?php echo $realtime.$realtime; ?>
<td width="90px"><img src="/sites/default/files/weather/images/windgauge.png" alt="Wind Speed" title="Wind Speed" class="mceItem" width="95px" height="95px"><br><center>Wind Speed</center></td><td width="90px"><img src="/sites/default/files/weather/images/compass.png" alt="Weather Direction" title="Weather Direction" class="mceItem" width="95px" height="95px"><br><center>Wind Dir.</center></td>
<p>Note: The weather station is a typical Chinese wireless model, available from many sources here in NZ and abroad - often referred to as the WS1083 and/or Fine Offset model. The back end weather software is Cumulus by <a href="http://www.sandaysoft.com" target="_blank">Sandaysoft</a> with some custom PHP software written by <a href="http://www.essentialtech.co.nz" target="_blank">Essential Technology</a> to format the information to suit the site.</p>
<p> </p><ul><li><span style="font-size: x-small;">The wind speed needle shows the current snapshot of the wind speed, as read from the weather station. The green arc shows the current ten-minute average wind speed, and the red arc shows the recent highest gust (in the last ten minutes).</span></li><li><span style="font-size: x-small;">The wind direction indicator shows the current snapshot of the wind speed, as read from the weather station, with a red pointer; it also shows the average wind direction over the last ten minutes, with a blue pointer (Note that when the wind speed is low, the wind direction can be quite erratic and unreliable)/</span></li></ul>
I hope this helps someone, and I hope its ok to be using the realtime.txt file in this way. I will update this as I get things sorted a bit.
As a side note, with Drupal, a node.tpl.php file could also have been used to do the PHP stuff... and I did this first... but its an ugly way to do it (actually, the current way is ugly too). The proper (and next) way I will do it is using a Drupal module.