Jump to content

Reading variables from another file


agentaustin

Recommended Posts

So here is what I have going.

 

#1 - My weather station software processes a file and enters variables and then uploads it as a php file.

#2 - Currently the station also just uploads every file to the site and enters the data in the html, but I would rather have it only upload this one file and the other pages grab data from it and print it out. I have a few php scrips in there to preform certain functions (if temp <= 32 it echos that data in blue and so on) but would rather make most of it php so I have less files being uploaded every 5 minutes.

 

This is what the station sends to the web, but I have no idea how to get the other pages to read it and input the variables from it.

 

[attachment deleted by admin]

Link to comment
Share on other sites

I am hoping someone (Pika maybe) can offer a better solution.

 

if the file is consistently the same (ie only the values of the variables will change, but location of variables, number of variables, etc etc remain constant) this may help get you started

<?PHP
/* this is the name of the raw data file the station uploads */
$myFile = "cumulus_raw.php"; 
$lines = file($myFile);
$w = count($lines) - 1;

/* if the raw data file is ALWAYS consistent in structure this should be where the first variable starts */
$start = 49;

/* this is used to get rid of lines containing the ============== */
$needle = "====";
while($start<$w) {
$show = 1;
$ww = strlen(trim($lines[$start]));
if($ww<1) { /* this removes empty lines */
	$show = 0;
}
$pos = strpos($lines[$start],$needle);
if( $pos === false) {
}else{
	$show = 0;
}
if($show == 1) { /* this creates a new array containing only valid information */
	$new_lines[] = $lines[$start];
}
$start ++;
}
$data = implode("\n",$new_lines); /* this condenses the new array into 1 long string with appropriate new lines */
$data = "<?PHP" . "\n" . $data . "?>"; /* this adds the opening and closing php tags */
$ts = time();
$file = $ts . '.txt'; /* this creates the file name to be used by your other scripts */
file_put_contents($file, $data); /* this does the actual writing to the file */
include($file); /* this is simply showing how you can INCLUDE the newly created file to access the variables */
echo "Today's high temperature of " . $tempTH . " occured at " . $TtempTH;
?>

Link to comment
Share on other sites

How often does the weather station upload?

 

Is there are reason you are not adding the information to a database? Rather than creating a new file, you could store the information into a database table (one of the table fields could be the timestamp of when the data was added to the table), that would eliminate having so many files.

 

Link to comment
Share on other sites

I would put this down to bad design. You shouldn't have to upload a file every 48 seconds to maintain data. You then have to convert these files?

 

I can only assume there is a very good reason you have not tried to use a database. Another idea - you could use http://www.sqlite.org/ as an alternative.

 

Again though, the main issue here is that you are using FTP.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.