Author Topic: submit_weather.php file  (Read 4134 times)

mattm

  • Gentle Breeze
  • **
  • Posts: 21
  • Station Details: 2011 MacMini, Core2 duo, Vantage Pro 2 with 5 stations
submit_weather.php file
« on: March 09, 2012, 03:32:08 AM »
I'm trying to get a dynamic wind speed/direction Flash indicator to work through the submit_weather.php file. I want to re-write the file to output a php file instead of the .txt file. The submit_weather.php file has lines of code for "output" and it's these I'd like to change to output a new php file. How can I use the "output" lines so each "output" is on a separate line? I'm trying:

                                $output = "";   
            $output .= "<slickboard>" ;
            $output .= "<object offset_x='240' offset_y='75'>";
            $output .= "<text x='-5' y='-28' size='12'>N</text>";
                                etc....
                                etc...

but the resulting file appends each output on the same line like this:

<slickboard><object offest_x='240' offset_y='75><text x='-5' y='-28' size='12'>N</text>...etc...etc.

I'm a newbie php coder, obviously. :)

thanks for any help.


Tornado Tim

  • Strong Breeze
  • ***
  • Posts: 125
    • Matangi Weather
  • Station Details: Davis VP2 with Solar and WLIP Datalogger (via VVP)
Re: submit_weather.php file
« Reply #1 on: March 09, 2012, 08:38:40 AM »
I am a Webprogrammer/Designer myself so I can help you with this :)

I believe your using the CGI feature of weather cat yes?
if your using that feature then what WC is doing is appending the data onto the URL which makes a query string.

PHP can grab this info from the querie string and it can be used to update a database for example or written to a plaintext file.

To get the data from the query string you need to know what data has been sent and what format it is in:

so if the query string looked like this: http://blah-blah.com/submitdata.php?humidity=18&temperature=-15
you could grab the data using PHP with this code:
Code: [Select]
<?php
if (isset($_GET['humidity']) && isset($_GET['temperature']) ){//check if humidity and humidity has been appended onto to the url
//do something with the data
//$_GET['humidity'] contains the humidity value and $_GET['temperature'] contains the temperature value
}
now once that check is complete inside the squiggly brackets for the if statement, these: {
you need to decide what you want to do with the data
1) put in a DB (i wouldnt recommend)
2) write to a txt file (preferred)

to write to txt file or anyfile for that matter is very very simple in PHP:
just do something similar to this:
Code: [Select]
<?php
file_put_contents
('file path to where you want stuff stored including the file',$variablewiththedatainsideit);
?>

Hope this helps, let me know if you want more information.

Tim
http://matangiweather.org

mattm

  • Gentle Breeze
  • **
  • Posts: 21
  • Station Details: 2011 MacMini, Core2 duo, Vantage Pro 2 with 5 stations
Re: submit_weather.php file
« Reply #2 on: March 09, 2012, 02:30:07 PM »
Thanks, Tim.

Well, I'm trying to stay within the confines of the original submit_weather.php file rather than reinvent the wheel there. If you look at the original submit_weather.php file in Lightsoft (I'm fairly certain the file in LWC is the same as in WC) it reads:

<?php
   // Script by Lightsoft - http://www.lightsoft.co.uk
   // Use with LWC Custom CGI feature
   //
   // Provides receipt of weather data to the server and stores in a simple file. Has to be a server side
   // script but theoretically need not be PHP. Other options are Perl CGI, Python, etc - depending upon
   // what your web server supports.
   //
   // We assume security of the username/password is not critical (sent as plain text). We limit what
   // data we accept and try to ensure all sequences from the GET are properly stripped before use
   // on a web page.
   //
   // We use a GET rather than POST since data is very limited and doesn't overly affect server operation.
   //
   $success = 0;
   $line_end = "<br>\r\n";
   if(htmlspecialchars($_GET['action']) === 'update') // what do we want to do?
   {
      if(htmlspecialchars($_GET['ID']) === 'joeblog')   // the user name allowed to update the data (***CHANGE THIS!***)
      {
         if(htmlspecialchars($_GET['PASSWORD']) === 'xxxxxx')   // the password of that user (***CHANGE THIS!***)
         {
            $output = "";            
            $output .= "Date (UTC): " . htmlspecialchars($_GET['dateutc']) . $line_end; // 2008-02-10+11%3A20%3A39
            $output .= "Wind dir (deg): " . (float)($_GET['winddir']) . $line_end; // 230
            $output .= "Wind Speed (mph): " . (float)($_GET['windspeedmph']) . $line_end; //12
            $output .= "Wind Speed (mphAv10mins): " . (float)($_GET['windspeedmphav_10m']) . $line_end; //12
            $output .= "Wind Gust (mph): " . (float)($_GET['windgustmph']) . $line_end; // 12
            $output .= "Wind Gust (dir): " . (float)($_GET['windgustdir']) . $line_end; // 12
            $output .= "Wind Gust (mph10mins): " . (float)($_GET['windgustmph_10m']) . $line_end; // 12
            $output .= "Wind Gust (dir10mins): " . (float)($_GET['windgustdir_10m']) . $line_end; // 12
            $output .= "Humidity: " . (float)($_GET['humidity']) . $line_end; // 90
            $output .= "Dew Point (F): " . (float)($_GET['dewptf']) . $line_end; // 68.2
            $output .= "Temp (F): " . (float)($_GET['tempf']) . $line_end; // 12
            $output .= "Rain (in): " . (float)($_GET['raininch']) . $line_end; // 0
            $output .= "Rain24h (in): " . (float)($_GET['rain24hinch']) . $line_end; // 0
            $output .= "Barom (in): " . (float)($_GET['baroinch']) . $line_end; // 29.1
            $output .= "Conditions: " . htmlspecialchars($_GET['weather']) . $line_end; // Sunny
            //echo $output;      // for testing
            
            if(($handle = fopen("weather_data.txt","wb")) !== FALSE)
            {
                $write_status = fwrite($handle, $output);   // we ignore write status (FALSE on fail or bytes written)
               $close_status = fclose($handle);         // we ignore close status (FALSE failure, TRUE on success)
                $success = 1;
            }
         }
      }
   }
   if($success==0)
   {
      echo 'Fail';
   }
   else
   {
      echo 'OK';
   }
?>

As you can see it outputs a txt file which is ultimately read as an HTML page. If you look at the second line down from the comments, you'll see: $line_end = "<br>\r\n"; 
This inserts a <br> into the text code so when the page is read via an HTML, each output line is on a separate line. But since I'm not interested in reading the file as an HTML, this isn't desirable.

I'm going to be rewriting this php file so it essentially creates this file almost exactly:

<slickboard>

<object offset_x='240' offset_y='75'>
      <text x='-5' y='-28' size='12'>N</text>
      <text x='-5' y='0' size='17' color='88ff00'>6</text>
      <circle x='0' y='0' radius='30' fill_alpha='0' line_thickness='5' shadow='default' />
      <radial_ticks x='0' y='0' radius='33' start='0' end='90' count='8' color='444444' length='6' thickness='2' />
      <object>
         <line x1='0' y1='-40' x2='0' y2='-30' color='ff3300' thickness='4' shadow='default' />
         <rotate x='0' y='0' start='230' span='175' step='5' shake_span='0' />
      </object>
   </object>
   
</slickboard>

That's what I want the submit_weather.php file to build (there will have to be some variable input here, but in essence this is what I'm looking for).

So, how to get the current php file to do this? How to get it to build that xml file. Is there something similar in PHP to the <br> that will force a new line for each "output" line in the submit_weather.php file? See?



mattm

  • Gentle Breeze
  • **
  • Posts: 21
  • Station Details: 2011 MacMini, Core2 duo, Vantage Pro 2 with 5 stations
Re: submit_weather.php file
« Reply #3 on: March 09, 2012, 02:35:25 PM »
Oh, in case you're curious, I'm building a small wind direction/speed dial very similar to weather underground's dial found on our station pages. When the direction and wind speed change, the dial's pointer spins to the new direction.

Steve

  • Global Moderator
  • Storm
  • *****
  • Posts: 1277
    • DW8454
    • KOHAVON11
    • Avon Weather
  • Station Details: Davis Wireless VP2 Plus w/24 hr FARS, Leaf & Soil Station, 2017 27" iMac 3.8 GHz i5, 40GB RAM, Mac OS 10.13, WeatherCat 3
Re: submit_weather.php file
« Reply #4 on: March 09, 2012, 02:38:48 PM »
Hi Matt,

Did you see the wonderful set of dials that I linked to in THIS THREAD? Some of your work may already be done for you. I'd sure like to know how to implement some of those tools on my web page.

Steve
Steve - Avon, Ohio, USA


CWOP: DW8454 • WU: KOHAVON11 • AWEKAS • CoCoRaHS • Skywarn •  UK Met Office
MWWN •PWSweather • WeatherBug • WeatherCloud • Twitter • Facebook

mattm

  • Gentle Breeze
  • **
  • Posts: 21
  • Station Details: 2011 MacMini, Core2 duo, Vantage Pro 2 with 5 stations
Re: submit_weather.php file
« Reply #5 on: March 09, 2012, 03:37:26 PM »
Ah, figured it out! I just needed another variable assignment like this:

$line_break = "\n";

That forces a break in the php file.

Getting closer, gents.

The wind direction is the tricky part.

mattm

  • Gentle Breeze
  • **
  • Posts: 21
  • Station Details: 2011 MacMini, Core2 duo, Vantage Pro 2 with 5 stations
Re: submit_weather.php file
« Reply #6 on: March 09, 2012, 03:39:03 PM »
Yeah, Steve. I saw those. I'll have to take a look at any code that they're using that might help. Matt

elagache

  • Global Moderator
  • Storm
  • *****
  • Posts: 4934
    • DW3835
    • KCAORIND10
    • Canebas Weather
  • Station Details: Davis Vantage Pro-2, MacBook Pro (Early 2011), macOS 10.13.6, WeatherCat 3
Aim for a solution to share? (Re: submit_weather.php file)
« Reply #7 on: March 09, 2012, 04:09:47 PM »
Hi Matt, Tim, Steve, and WeatherCat web page designers (at all levels),

Sure want to encourage this sort of thing.   [cheer]

There is another effort using LWC by Richard on MacWeather.  You can find the thread here: http://www.macweather.net/viewtopic.php?p=2793#p2793.  Also, Todd (aka Tigers) clearly had some PHP inclinations.  I think we all would really drool over something like a Weather Underground rapid fire display  .  It doesn't seem that difficult to implement with Stu's CGI interface and some basic PHP.

Perhaps you'all could shoot some emails in between each other and pool enough ideas together so that this dream becomes actually something practical to do?  If you could, you sure would be super-heroes to the rest of us!!  [bounce]

Cheers, Edouard  [cheers1]