modified from Chris Sung's code by Keunyoung Oh

This perl code responds to a HTTP GET/POST request by reading a mySQL database and returning the requested results. It connects a SHT11 temperature and humidity sensor to a mySQL database and to a Flash-based visualization of the resulting data.

 
#!/usr/bin/perl

 #########################################################
 # Chris Sung's code(from DWD class), modifyed by K.
 #########################################################
if    ($0=~m#^(.*)\\#){ $execDir = "$1"; }  # Win/DOS
elsif ($0=~m#^(.*)/# ){ $execDir = "$1"; }  # Unix
else  {`pwd` =~ /(.*)/; $execDir = "$1"; }  # Unix

 # Get Login Info
require "$execDir/info.pl";

 # Use CGI Perl module to output any error msgs to browser
use CGI::Carp qw(fatalsToBrowser);

 # Define the name of the script just for reference
$scriptName = "flash_perl1.pl";
$pageTitle = "Class 10 Example - Send data for select widget";

 # Obtain any variables submitted either in the URL string
 #  or by an HTML form via the GET or POST method, and
 #  place in the %INPUT_VARS hash array
%INPUT_VARS = {};
&input_vars_receive;
&input_vars_parse;

print "Content-type: text/html\n\n";
use DBI();

$database = $mySqlUsername;
$data_source = "dbi:mysql:$database";
$username = $mySqlUsername;
$password = $mySqlPassword;

my $dbh = DBI->connect($data_source, $username, $password,
        {'RaiseError' => 1, 'PrintError' => 1});

 #########################################################
 # Get the list 
 #########################################################
$myData = "";
$nOptions = 0;
$SqlStatement = "SELECT max(id), Value FROM humidity_loger ";
$SqlStatement .= "GROUP BY id DESC ";
$SqlStatement .= "LIMIT 1";
$sth = $dbh->prepare($SqlStatement);
$sth->execute();
while (@row_array = $sth->fetchrow_array())
{       $currentId = $row_array[0];     # Get id of instrument
        $currentName = $row_array[1];   # Get name of instrument

         # Add to $myData var as a name-value pair.  URL-encode as


        $myData .= "myHumid=".&url_encode($currentName);
        $myData .= "&";
        $myData .= "value=".&url_encode($currentId);
        $myData .= "&";



        $nOptions++;
}
$sth->finish();

 # Tack on the number of total options
$myData .= "nOptions=".$nOptions;

 # Send it to Flash
print $myData;

 #########################################################
 # Disconnect from the database.
 #########################################################
 # Always the last thing you do before exiting your script
$dbh->disconnect();

exit(0);

 #########################################################
 # Takes data from an HTML Form or URL string
 #########################################################
sub input_vars_receive
{ $formData = $ENV{'QUERY_STRING'};

        if ($ENV{'REQUEST_METHOD'} eq 'POST')
        {       read(STDIN, $formData, $ENV{'CONTENT_LENGTH'});
        }
}

 #########################################################
 # Parses form info
 #########################################################
sub input_vars_parse
{       local($name,$value,$pair);
        local(@pairs) = split(/&/, $formData);

        # Get parameter names, their values and copy into $INPUT_VARS array
        foreach $pair (@pairs)
        {       ($name,$value)=split(/=/,$pair);
                $value=~tr/+/ /s;
                $value=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                $INPUT_VARS{$name}=$value;
        }
}

 #########################################################
 # Write To Log File
 #########################################################
sub write_to_log
 #########################################################
{  # Write to log file
        local($msg) = @_;
        open INPTR, ">>$logFile";
        print INPTR "$msg\n";
        close INPTR;
}
 #########################################################
 # Trim whitespace
 #########################################################
sub trim_it
{ local($msg) = @_;
  while ($msg=~/^\s/i) { $msg=~s/^\s//i;}
  while ($msg =~/\s$/)  { $msg =~s/\s$//; }

        return $msg;
}

 #########################################################
 # Escape single quotes for DB entry
 #########################################################
sub escape_quotes
{ local($msg) = @_;
        local($delim) = "'"; local($rep) = "''";

        $msg =~s/$delim/$rep/eg;

        return $msg;
}

 #########################################################
 # URL-encode data
 #########################################################
sub url_encode 
{ my($toencode) = @_;
  $toencode=~s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
  return $toencode;
}