|
CLASS DOCUMENTS
REPORTS & ASSIGNMENTS
CLASS CONTENT
USING THIS SITE
registered authors login here You are: (logout) For more on PMWiki, see pmwiki.org |
SH T11-Perl-My SQLIt was modified from Chris Sung's code by Keunyoung Oh This perl example takes the results of an HTTP GET/POST requests and saves them to a mySQL database. It was derived to link the SHT11 humidity sensor to a mySQL database.
#!/usr/bin/perl
#########################################################
# processing - perl to mySQL b 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 = "pic_to_sql.pl";
$pageTitle = "pic_to_sql example";
%INPUT_VARS = {};
&input_vars_receive;
&input_vars_parse;
#########################################################
# Define our DB info
#########################################################
use DBI();
$database = $mySqlUsername;
$data_source = "dbi:mysql:$database";
$username = $mySqlUsername;
$password = $mySqlPassword;
$tableName = 'humidity_loger';
$globalSensor ="sensorVar";
$globalAction ="action";
$SensorValue = $INPUT_VARS{$globalSensor};
$actionValue = $INPUT_VARS{$globalAction};
$oldvar = 0;
my %hash;
#########################################################
# Connect to the database.
#########################################################
my $dbh = DBI->connect($data_source, $username, $password,
{'RaiseError' => 1, 'PrintError' => 1});
#########################################################
# Get the list of instruments and put in $myData
#########################################################
if ($SensorValue ne "")
{ $myVar = $SensorValue;
print qq^$myVar^;
$SqlStatement = "INSERT INTO $tableName(Value,Date, Timestamp) ";
$SqlStatement .= "VALUES ('$myVar',DATE(NOW()), NOW())";
$affected_rows = $dbh->do($SqlStatement);
}
#########################################################
# 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;
}
}
|