Author Topic: Send PWS Data to My Own Server  (Read 1513 times)

0 Members and 1 Guest are viewing this topic.

Offline messickc

  • Member
  • *
  • Posts: 2
Send PWS Data to My Own Server
« on: August 14, 2021, 10:43:20 PM »
Hello,

I am not sure if this is a hardware or a software... issue, but I think it's more software.

Anyway, I have my own web server in my basement, with a static IP address, and URL.  I am running Apache Web Server, PHP 6.0 and, and MySQL on a Fedora Linux box.  I own an Ambient Weather WS-2000.  I have it connected to both Ambient Weather and Weather Underground's data services.  However, I was wanting to know if anyone can point me to resources (API's, libraries, documentation, etc.) that will allow me to send the data to MY server so that the server can store the data in a MySQL database so that I can do whatever I want with it via PHP/HTML on my website(s).  I know how to do the "Customized Server" thing on my PWS tablet, but I don't have any scripts to handle the forwarded data.  I'm interested in any scripts I might need server-side to parse data packets from the PWS, or documentation that lays out the structure of these packets, and the protocols they use.

So far, all my searches haven't returned much anything useful.  So I'm not sure this can even be done with my hardware, or at all, or maybe its proprietary information or whatever.

Thanks,
Christopher Messick
« Last Edit: August 14, 2021, 10:49:06 PM by messickc »

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: Send PWS Data to My Own Server
« Reply #1 on: August 15, 2021, 03:09:17 AM »
Hello,

I am not sure if this is a hardware or a software... issue, but I think it's more software.

Anyway, I have my own web server in my basement, with a static IP address, and URL.  I am running Apache Web Server, PHP 6.0 and, and MySQL on a Fedora Linux box.  I own an Ambient Weather WS-2000.  I have it connected to both Ambient Weather and Weather Underground's data services.  However, I was wanting to know if anyone can point me to resources (API's, libraries, documentation, etc.) that will allow me to send the data to MY server so that the server can store the data in a MySQL database so that I can do whatever I want with it via PHP/HTML on my website(s).  I know how to do the "Customized Server" thing on my PWS tablet, but I don't have any scripts to handle the forwarded data.  I'm interested in any scripts I might need server-side to parse data packets from the PWS, or documentation that lays out the structure of these packets, and the protocols they use.

So far, all my searches haven't returned much anything useful.  So I'm not sure this can even be done with my hardware, or at all, or maybe its proprietary information or whatever.

Thanks,
Christopher Messick

Attached a .zip of a modified index.php script adapted from the script we use for "ecowitt" custom upload
It should be in the folder you use for your ambient custom upload.

I assume it works similar as the ecowitt custom upload were we specify
  • the server  => in your case the ip-address
  • the folder path f.i.  => /ambient/
This script will store all $_GET and $_POST data in an array and uses a log file to store all problems.

You can add one or more key fields which should always be available in the uploaded data, otherwise the upload is logged only and skipped
The reason for this:  After a firmware update it can take a few uploads before the data is complete again.

At the bottom you should add the code for storage of the acceptable data in your data base.

Wim
Code: [Select]
<?php  $scrpt_vrsn_dt  'index.php|01|2021-08-15|';  # ambient receive script
# -----------------     SETTINGS for this script
$always_exists  'tempf';              # this field should always be in the data ! 
$test_mode      false;                # set to true if you encounter strange problems 
#
$message        'Ambient data received:'.PHP_EOL;;
$data_stored    './data.arr';         # filename for correct data, check the path
$data_false     './test.arr';         # if upload not correct we store it here
$problem_log    './log.txt';          # all problems are logged here
$errors         0;  
#
header('Content-type: text/plain; charset=UTF-8');
#-----------------------------------------------
#         PWS-Dashboard - Updates and support by 
#     Wim van der Kuil https://pwsdashboard.com/
#-----------------------------------------------
#       display source of script if requested so
#-----------------------------------------------
if (isset($_REQUEST['sce']) && strtolower($_REQUEST['sce']) == 'view' ) {
        
$filenameReal __FILE__;  
        
$download_size filesize($filenameReal);
        
header('Pragma: public');
        
header('Cache-Control: private');
        
header('Cache-Control: no-cache, must-revalidate');
        
header('Content-type: text/plain; charset=UTF-8');
        
header('Accept-Ranges: bytes');
        
header("Content-Length: $download_size");
        
header('Connection: close');
        
readfile($filenameReal);
        exit;}
elseif (!isset (
$_REQUEST['test'])) 
     {  
ini_set('display_errors'0);   error_reporting(0);}
#-----------------------------------------------
#      CREDIT - DO NOT REMOVE WITHOUT PERMISSION
#-----------------------------------------------
#       This script will save all $_REQUEST data
#                 as sent by the ambient station
#
# Read: https://www.php.net/manual/en/reserved.variables.request.php
#-----------------------------------------------

#   check if data fields are there and are valid
#
if (!isset ($always_exists)  )
     {  
$always_exists = array();}
elseif (!
is_array ($always_exists) )
     {  
$always_exists = array ($always_exists); }
foreach (
$always_exists as $check)
     {  if ( 
trim($check) <> '' 
          
&& !array_key_exists ($check,$_REQUEST) )
             {  
$message        .= PHP_EOL.' Problem-'.__LINE__.': Missing item in upload: '.$check;
                
$errors++;
                continue;}
        elseif (
$filter_value == false)
             { continue;}
        
$value  trim($_REQUEST[$check]);
        if (  
$value == NULL
           
|| (string) $value == '' 
           
|| (float)  $value == $filter_value)      // empty data, check the log file regurly  
             
{  $message        .= PHP_EOL.' Problem-'.__LINE__.': Item in upload: '.$check.' with value between next brackets =>'.$value.'<= is invalid';
                
$errors++;} 
        } 
if ( 
$errors <> 0)
     {  
$data_stored    $data_false# if upload not correct we store it here
        
if (count ($_REQUEST) > 0
             {  
$string print_r($_REQUEST,true);
                
$from   = array('Array','('.PHP_EOL,')'.PHP_EOL);
                
$string PHP_EOL.' Problem-'.__LINE__.': Invalid data: '.str_replace ($from'',$string);}
#
        
else {  $string PHP_EOL.' Problem-'.__LINE__.': No data uploaded.';}
        
$message        .= $string.PHP_EOL;
        
$test_mode      true;
        }
#
#-----------------------------------------------
#                        store all received data 
$return file_put_contents ($data_stored,serialize($_REQUEST)); 
if (
$return == false 
    {   
$message        .=  ' Setup error-'.__LINE__.': Failed writing ambient data to '.$data_stored;
        
$test_mode      true;}
#
#-----------------------------------------------
#                       add all problem messages 
#       and unknown and invalid data to text-log  
if ($test_mode == true )          
     {  
$sf fopen($problem_log,'a');// append to log file
        
if($sf
             {  
$time   gmdate ('r');
                
fwrite($sf,$time.' = '.$message."\n");
                
fclose($sf);}
        echo 
$message;
        return;}
#
#-----------------------------------------------
#  now  we can process all data store in $return
#   and saved in "$data_stored" file, see line 8
#

« Last Edit: August 15, 2021, 03:18:21 AM by wvdkuil »

Offline messickc

  • Member
  • *
  • Posts: 2
Re: Send PWS Data to My Own Server
« Reply #2 on: August 16, 2021, 10:51:32 AM »
Thanks, wvdkuil!

I will play with this for a bit.  Much appreciated!

Offline sky_watcher

  • Contributor
  • ***
  • Posts: 138
Re: Send PWS Data to My Own Server
« Reply #3 on: August 16, 2021, 07:09:02 PM »
I'm interested in any scripts I might need server-side to parse data packets from the PWS, or documentation that lays out the structure of these packets, and the protocols they use.
I have some code that will read the data from a HP2550 and store into a database from the index.php script. It should be adaptable for what you want, although it is probably a bit more complex than you need. The archive file contains the index.php file which will handle the ecowitt format messages. You can ignore the updateweatherstation.php because that is for the HP1000 using the WU format (if I recall correctly). The file db_passwords.php holds the username and password required to access the database.

You can look at the script source code before you download the archive to see if it is of any use to you..

Where it is probably more complex than you require: (1) It looks up a table in the database for a matching ecowitt passkey before it processes the message - it can handle multiple weather stations. (2) It converts from imperial to metric before saving into the database. (3) A flag (trim_readings) in the  station table holding the ecowitt passkey can be turned off so that the battery readings and software descriptions which are stored in separate tables are dumped, rather than stored. As you will see, the code can be simplified quite a bit depending on what you want to do.

If the database references don't make much sense, this description may help, or perhaps confuse you even more.
“The more a man knows, the more willing he is to learn. The less a man knows, the more positive he is that he knows everything...” ― Robert G. Ingersoll

 

anything