Connecting your Device to eChain IoT Cloud

Description

Connect IoT Device and/or RFID Reader to the eChain Technology cloud to enable automated asset location and traceability using HTTP POST from your device.

Configure your device (Web Client Application) to send HTTP POST request to the eChain web server PHP application. The instructions here will help you to connect directly to the eChain Development environment. The PHP application writes the values from the request into the eChain database when new POST requests are received. View the log file to troubleshoot post success messages or errors.

eChain Database IoT Staging Table – with sample data:

Reader NameMac AddressAntenna PortepcFirst_Seen TimeStampPeak RssiTIDDateAdded
echain-10-e9-6000:16:25:10:E9:601E200300005100089202048911476393903-57E20034120137F0000B0443BF2016-10-13 18:45:05
echain-10-e9-6000:16:25:10:E9:601E2008367671501690260EE561476393978-58E20034120139F600007DEE552016-10-13 18:46:20
echain-10-e9-6000:16:25:10:E9:602E2008367671501690260EE561476395681-66 2016-10-13 19:14:43
echain-10-e9-6000:16:25:10:E9:602E200300005100089202048911476395683-58E20034120137F0000B0443BF2016-10-13 19:15:13
echain-10-e9-6000:16:25:10:E9:602E200300005100089202048911476395691-60E20034120137F0000B0443BF2016-10-13 19:15:13
echain-10-e9-6000:16:25:10:E9:601E2008367671501690260EE561476396348-62E20034120139F600007DEE552016-10-13 19:25:50
echain-10-e9-6000:16:25:10:E9:601E200300005100089202048911476396349-56E20034120137F0000B0443BF2016-10-13 19:26:19

Table Structure SQL

— Table structure for table `ASSET_TAG_DATA`

CREATE TABLE `ASSET_TAG_DATA` (
`Reader_Name` varchar(50) DEFAULT NULL,
`Mac_Address` varchar(17) DEFAULT NULL,
`Antenna_Port` int(3) DEFAULT NULL,
`epc` varchar(24) DEFAULT NULL,
`First_Seen_TimeStamp` varchar(10) DEFAULT NULL,
`Peak_Rssi` varchar(5) DEFAULT NULL,
`TID` varchar(30) DEFAULT NULL,
`DateAdded` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Notes on POST Data:

  1. eChain system uses a combination of Reader Name and Antenna Port to specify “Zone” or location. Any specific reader can have multiple antennas representing multiple, different zones. These fields are crucial for identifying the location of the identified tag/beacon.
  2. Mac address is nice to have, but not really used by the application.
  3. EPC: Electronic Product Code. The primary identifier for the beacon/tag. This value is key to identifying the item being scanned.
  4. First Seen Timestamp – The timestamp recorded by the device. Not required. We typically use the system timestamp to record the time the record was received into the database.
  5. Peak Rssi – This is the signal strength represented as a negative value, typically in a range between -30 and -75
  6. TID – TagID – This is a secondary beacon/tag identifier. Can also be used for sensor data payload.  Not required.
  7. DateAdded – Timestamp added by the database. Not needed as part of the HTTP POST

POST Transaction Location (DEV)

 

Postrfdata.php

<?php

// Uncomment this block for debug.
// It writes the raw POST data to a file.

require_once(‘db.php’);
$obj_connection = new DB();

date_default_timezone_set(‘America/New_York’);
$fn = “log.txt”;
$fp = fopen($fn, “a”);
$rawPostData = file_get_contents(‘php://input’);
if ($obj_connection->get_log_flag() == ‘yes’) {
fwrite($fp, ‘*************** Enter HTTP POST ‘ . date(“l F d, Y, h:i A”) . “\n”);

fwrite($fp, ‘Write String from RFID’ . “\n”);
fwrite($fp, $rawPostData . “\n”);
fwrite($fp, “\n”);
/*

*/

// include ‘thoughtworks_post.php’;
// Define the user name, password,
// MySQL hostname and database name.
// Store the POST variables.
$readerName = $_POST[‘reader_name’];
$macAddress = $_POST[‘mac_address’];

$lineEnding = $_POST[‘line_ending’];
$fieldDelim = $_POST[‘field_delim’];
$fieldNames = $_POST[‘field_names’];
$fieldValues = $_POST[‘field_values’];

// Connect to the database.
//$con = mysql_connect($this->host, $this->user, $this->pass);

$con = $obj_connection->get_connection();

fwrite($fp, ‘Connect to DB’ . “\n”);
// fwrite($fp, “\n”);

if (!$con) {
fwrite($fp, ‘Could not connect: ‘ . mysql_error() . “\n”);
fwrite($fp, “\n”);
die(‘Could not connect: ‘ . mysql_error());
}

fwrite($fp, ‘Connection Made to DB’ . “\n”);
// fwrite($fp, “\n”);

mysqli_select_db($con,$obj_connection->get_db_name()) or die(“Unable to select database”);

 

// Replace the field delimiter with a comma.
str_replace($fieldDelim, “,”, $fieldNames);

// Break the field values up into rows.
$rows = explode(“\n”, $fieldValues);

// Remove the last row. It’s always blank
if (sizeof($rows))
array_pop($rows);
$fieldNames = “reader_name,mac_address,” . $fieldNames;
foreach ($rows as $row) {
$row = $readerName . “,” . $macAddress . “,” . $row;

fwrite($fp, ‘Field Values are ‘);
fwrite($fp, $row);
fwrite($fp, “\n”);
fwrite($fp, “\n”);

fwrite($fp, ‘Strip Field Values are ‘);
$fld_value = str_replace(“\\”, “”, $row);
fwrite($fp, $fld_value);
$row = $fld_value;
fwrite($fp, “\n”);
fwrite($fp, “\n”);

$query = “INSERT INTO ASSET_TAG_DATA ($fieldNames) VALUES ($row)”;
echo $query . “\n”;

fwrite($fp, ‘SQL Query is ‘);
fwrite($fp, $query);
fwrite($fp, “\n”);
fwrite($fp, “\n”);

// mysql_query($query);
$retval = mysqli_query($con,$query);
if (!$retval) {
fwrite($fp, ‘Could not enter data: ‘ . mysql_error() . “\n”);
fwrite($fp, “\n”);
die(‘Could not enter data: ‘ . mysql_error());
}
}
mysqli_close($con);

// post_to_thoughtworks($_POST);

fwrite($fp, ‘Added Entry to Database’ . “\n”);
fwrite($fp, ‘*************** Exit HTTP POST’ . “\n”);
fwrite($fp, “\n”);
fwrite($fp, “\n”);
fclose($fp);
}
else{
$readerName = $_POST[‘reader_name’];
$macAddress = $_POST[‘mac_address’];

$lineEnding = $_POST[‘line_ending’];
$fieldDelim = $_POST[‘field_delim’];
$fieldNames = $_POST[‘field_names’];
$fieldValues = $_POST[‘field_values’];

// Connect to the database.
//$con = mysql_connect($this->host, $this->user, $this->pass);

$con = $obj_connection->get_connection();

if (!$con) {
die(‘Could not connect: ‘ . mysql_error());
}

mysqli_select_db($con,$obj_connection->get_db_name()) or die(“Unable to select database”);

 

// Replace the field delimiter with a comma.
str_replace($fieldDelim, “,”, $fieldNames);

// Break the field values up into rows.
$rows = explode(“\n”, $fieldValues);

// Remove the last row. It’s always blank
if (sizeof($rows))
array_pop($rows);
$fieldNames = “reader_name,mac_address,” . $fieldNames;
foreach ($rows as $row) {
$row = $readerName . “,” . $macAddress . “,” . $row;

$fld_value = str_replace(“\\”, “”, $row);
$row = $fld_value;

$query = “INSERT INTO ASSET_TAG_DATA ($fieldNames) VALUES ($row)”;
echo $query . “\n”;
// mysql_query($query);
$retval = mysqli_query($con,$query);
if (!$retval) {
die(‘Could not enter data: ‘ . mysql_error());
}
}
mysqli_close($con);
}