|
Research & Learning Class pages
Shop Admin
ITP Help Pages |
Mysql OverviewMoving From a Physical Sensor to MySql: this tutorial covers the basics of taking data from a sensor and logging it on the web. The Sensor Your sensor/micro-controller must be connected to the internet via a device like a xport/wiport or have a connection to a computer.
There is a enormous amount of information on building this part of the system, though the best overall reference is Tom Igoe's book:
Making Things Talk.
The Mysql Side You need three main components here:
1.Getting a MYSQL DataBase
2.Posting data to the MYSQL DataBase Here is a simple php script called datapost.php. <?php
//datapost.php
//Get Data
$id_=$_GET["id"];
$value_=$_GET["value"];
//Variables for Connection
$hostname = "NAME OF HOST";
$dbname = "YOUR NETID"
$username = "YOUR NETID";
$password = "AS ASSIGNED TO YOU";
// Connect to the database
$mySql = mysql_connect($hostname, $username, $password) or die (mysql_error());
mysql_select_db($dbname, $mySql) or die(mysql_error());
$query = "insert into datalog (id, value, T) values ('".id_."','" . $value_ . "', NOW())";
echo($query);
// Execute the query, actually inserts the data
$result = mysql_query($query, $mySql);
echo $result
?>
To post a value to datalog You connect to URL_ROOT/simplelog.php?id=1&value=34.24 (1 and 34.24 are example values) in your web browser or connecting via an xport or through a program written in processing or another language. 3.Retrieving data from the MYSQL DataBase <?php data_retrieve.php
//function to insert data from mysql database into php variable
//from Shawn Van Every
function sqlQuery($query)
{
global $mySql;
$data = null;
$result = mysql_query($query, $mySql);
# This set's up an associative array (key->value pair) for all of the data returned
if (sizeof($result) > 0){
$num_fields = mysql_num_fields($result);
$row_cnt = 0;
while ($row_data = mysql_fetch_array($result)) {
for ($cnt = 0; $cnt < $num_fields; $cnt++) {
$field_name = mysql_field_name($result, $cnt);
$data[$row_cnt][$field_name] = $row_data[$cnt];
}
$row_cnt++;
}
}
return $data;
}
//Variables for connection
$hostname = "NAME OF HOST";
$dbname = "YOUR NETID"
$username = "YOUR NETID";
$password = "AS ASSIGNED TO YOU";
$mySql = mysql_connect($hostname, $username, $password) or die (mysql_error());
mysql_select_db($dbname, $mySql) or die(mysql_error());
$query = "select id, value, T from datalog";
$data = sqlQuery($query, $mySql);
//print data
for($i=0; $i<sizeof($data); $i++)
{
$d = "id: ".$data[$i]['id']." value: ".$data[$i]['value']." time: ".$data[$i]['T']."\n"
echo($d);
}
?>
|