Appliance Inference, php problem

Hi

I have search for a tutorial to upload data from arduino to a website and found these two, consumption on the web and Appliance Inference.

But I can't get any of them to work, when I paste the web address in the config file enom.php is bland and post.php returns "Content-Type: text/html; charset=ISO-8859-1"

 

I have tried with minimum databse privileges and maximum, files are set to 755.

Can anyone  please tell me what I'm doing wrong?

 

Regards,

Martin

<?php

// 1) Get values from url
// Example url: http://yoursite.org/emon.php?L=20.0&C=20.0&R=20.0&P=20.0

//Last and current power consumption.
$lastvalue = $_GET["L"];
$currentvalue = $_GET["C"];

//change data
$rpChange = $_GET["R"];
$pfChange = $_GET["P"];

// 2) Open the database

//Connect to mysql
$con = mysql_connect("localhost","snikaboh_db","25842648");
if (!$con) {die('Could not connect: ' . mysql_error());}
//Select the database
mysql_select_db("snikaboh_power",$con);

// 3) Insert the data!

//Inserts current power consumption data into DATA table
//This is consumption data that is updated on significant step change
//should be renamed to something more sensible like POWER
//A = power values, B = timestamp
$vtime = (time()*1000)-1000; // current time -1 second
mysql_query("INSERT INTO DATA (A, B) VALUES ($lastvalue,$vtime)");
$vtime = time()*1000; // current time
mysql_query("INSERT INTO DATA (A, B) VALUES ($currentvalue,$vtime)");

//Inserts event information into EVENT log table.
//RP = real power change, PF = power factor change, CRP = current real power, TIME = timestamp
mysql_query("INSERT INTO EVENT (RP1, PF1, TIME1, CRP1) VALUES ($rpChange,$pfChange,$vtime,$currentvalue)");

//close!!
mysql_close($con);

?>

 

 

#!/usr/bin/perl
use CGI::Carp qw( fatalsToBrowser );

# PERL MODULES WE WILL BE USING

use Data::Dumper;
use CGI qw/:standard/;

use DBI;
use DBD::mysql;

# HTTP HEADER
print "Content-type: text/html \n\n";
print header;

my $lastvalue = param('L')    or die "Value1 not in parameter list";
my $currentvalue = param('C')    or die "Value2 not in parameter list";

# CONFIG VARIABLES
$platform = "mysql";
$database = "snikaboh_power";
$host = "localhost";
$port = "3306";
$tablename = "DATA";
$user = "snikaboh_db";
$pw = "25842648";

# DATA SOURCE NAME
$dsn = "dbi:$platform:$database:$host:$port";

# PERL DBI CONNECT
$connect = DBI->connect($dsn, $user, $pw) or die "Unable to connect: $DBI::errstr\n";

$vtime = (time()*1000)-1000;

# PREPARE THE QUERY
$query = "INSERT INTO DATA (A, B) VALUES ($lastvalue,$vtime)";
$query_handle = $connect->prepare($query);

# EXECUTE THE QUERY
$query_handle->execute();

$vtime = time()*1000;

# PREPARE THE QUERY
$query = "INSERT INTO DATA (A, B) VALUES ($currentvalue,$vtime)";
$query_handle = $connect->prepare($query);

# EXECUTE THE QUERY
$query_handle->execute();

 

 

Mattia Rossi's picture

Re: Appliance Inference, php problem

Hi Martin,

the php script you posted does not output anything back to the browser, and there's no check to see whether the inserts are succeeding or failing

 

what name did you save the script with, and where did you  place it on your web server ?

If you named it post.php and placed it in /var/www/post.php then you should call it with http://localhost/post.php?L=20.0&C=20.0&R=20.0&P=20.0

 

try adding some debug prints like this:

 

<?php

// 1) Get values from url

// Example url: http://yoursite.org/emon.php?L=20.0&C=20.0&R=20.0&P=20.0

 

var_dump($_GET);

 

//Last and current power consumption.
$lastvalue = $_GET["L"];
$currentvalue = $_GET["C"];

//change data
$rpChange = $_GET["R"];
$pfChange = $_GET["P"];

// 2) Open the database

//Connect to mysql
$con = mysql_connect("localhost","snikaboh_db","25842648");
if (!$con) {die('Could not connect: ' . mysql_error());}
//Select the database
mysql_select_db("snikaboh_power",$con);

// 3) Insert the data!

//Inserts current power consumption data into DATA table
//This is consumption data that is updated on significant step change
//should be renamed to something more sensible like POWER
//A = power values, B = timestamp
$vtime = (time()*1000)-1000; // current time -1 second

echo "Executing query: " . ""INSERT INTO DATA (A, B) VALUES ($lastvalue,$vtime)";

$result = mysql_query("INSERT INTO DATA (A, B) VALUES ($lastvalue,$vtime)");

if (!$result) {
    echo 'Invalid query: ' . mysql_error();
}

$vtime = time()*1000; // current time

echo "Executing query: " . "INSERT INTO DATA (A, B) VALUES ($currentvalue,$vtime)";

$result = mysql_query("INSERT INTO DATA (A, B) VALUES ($currentvalue,$vtime)");

if (!$result) {

    echo 'Invalid query: ' . mysql_error();
}

echo "Executing query: " . "INSERT INTO EVENT (RP1, PF1, TIME1, CRP1) VALUES ($rpChange,$pfChange,$vtime,$currentvalue)";

//Inserts event information into EVENT log table.
//RP = real power change, PF = power factor change, CRP = current real power, TIME = timestamp
$result = mysql_query("INSERT INTO EVENT (RP1, PF1, TIME1, CRP1) VALUES ($rpChange,$pfChange,$vtime,$currentvalue)");

 

if (!$result) {
    echo 'Invalid query: ' . mysql_error();
}

//close!!
mysql_close($con);

?>

 

Try and see what output you get with this version

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.