[ STEP 1 ] Read POST data and set AES key ( example ):
Read and decrypt the POST data sended from EchoSensor device is very easy. Copy the code in your remote hosting, start to receive the data from EchoSensor and make your big data server.
<?php
if (isset($_POST['iotdata'])) {
$POST_DATA = $_POST['iotdata'];
} else {
echo "Post data not found. Access Deniend.";
exit();
}
// prepare AES128-CBC
$dc_key = "your128bitkey123"; //128bit key
$iv_key = "your128bitivkey1"; //128bit key
$dc_str_iv=str_toIV($iv_key);
$dc_iv=hex2bin($dc_str_iv);
?>
[ STEP 2 ]Configure the UserAgent directly from the control panel of the EchoSensor device.
Read and filter it in your web application:
<?php
$USERAGENT = $_SERVER['HTTP_USER_AGENT'];
echo "User-Agent: ".$USERAGENT.PHP_EOL;
if ( $USERAGENT != “MYSENSOR_SERIES_NAME” ) {
echo “Header not accepted..bye”;
exit();
}
?>
[ STEP 3 ] Decrypt $POST_DATA to Plain text with openSSL:
<?php
$Zdec = openssl_decrypt(base64_decode($POST_DATA), "AES-128-CBC", $dc_key, OPENSSL_ZERO_PADDING, $dc_iv);
$Zdec=base64_decode($Zdec);
?>
[ STEP 5 ] Convert to array::
<?php
$str_arr = explode(",", $Zdec);
// $str_arr order:
// (assigned by echoSensor AP )
// 0 -> Api Key
// 1 -> Api Username
// 2 -> Api Password
// 3 -> Sensor Type (assigned by echoSensor)
// 4 -> Sensor Value
// 5 -> Time
// 6 -> Date
// 7 -> EchoSensor CPU Temperature
//
$APIKEY=array_values($str_arr)[0];
$APIUSERNAME=array_values($str_arr)[1];
$APIPASS=array_values($str_arr)[2];
$SENSOR_TYPE=array_values($str_arr)[3];
$SENSOR_TEMP=array_values($str_arr)[4];
$SENSOR_TIME=array_values($str_arr)[5];
$SENSOR_DATE=array_values($str_arr)[6];
$CPU_TEMP=array_values($str_arr)[7];
?>
Complete example:
<?php
// EchoSensor IOT Sensor Example
//
if (isset($_POST['iotdata'])) {
$POST_DATA=$_POST['iotdata'];
} else {
echo "Access Deniend.";
exit();
}
// continue...
function str_toIV($string){
$hex = '';
for ($i=0; $i<strlen($string); $i++){
$ord = ord($string[$i]);
$hexCode = dechex($ord);
$hex .= substr('0'.$hexCode, -2);
}
return strToUpper($hex);
}
$CLIENTHOST=$_SERVER['HTTP_HOST'];
$USERAGENT=$_SERVER['HTTP_USER_AGENT'];
//echo $_SERVER['HTTP_CONNECTION'].PHP_EOL;
// AES128-CBC KEY (16 char)
// FOR DECRYPTION:
$dc_key = "aes128key1234567";
$iv_key = "aes128key1234567";
$dc_str_iv=str_toIV($iv_key);
$dc_iv=hex2bin($dc_str_iv);
$Zdec=openssl_decrypt(base64_decode($POST_DATA), "AES-128-CBC", $dc_key, OPENSSL_ZERO_PADDING, $dc_iv);
// BASE64 DECODE:
$Zdec=base64_decode($Zdec);
//echo $Zdec.PHP_EOL;
// CONVERT POST DATA IN CSV FORMAT
$str_arr = explode(",", $Zdec);
//valori ricevuti:
// array order:
// (assigned by echoSensor AP )
// 0 -> Api Key
// 1 -> Api Username
// 2 -> Api Password
// 3 -> Sensor Type (assigned by echoSensor)
// 4 -> Sensor Value
// 5 -> Time
// 6 -> Date
// 7 -> EchoSensor CPU Temperature
//
print_r($str_arr);
$APIKEY=array_values($str_arr)[0];
echo "Apikey:".$APIKEY.PHP_EOL;
$APIUSERNAME=array_values($str_arr)[1];
echo "Username:".$APIUSERNAME.PHP_EOL;
$APIPASS=array_values($str_arr)[2];
echo "Password:".$APIPASS.PHP_EOL;
$SENSOR_TYPE=array_values($str_arr)[3];
echo "Type:".$SENSOR_TYPE.PHP_EOL;
$SENSOR_TEMP=array_values($str_arr)[4];
echo "Temperature:".$SENSOR_TEMP.PHP_EOL;
$SENSOR_TIME=array_values($str_arr)[5];
echo "Time:".$SENSOR_TIME.PHP_EOL;
$SENSOR_DATE=array_values($str_arr)[6];
echo "Date:".$SENSOR_DATE.PHP_EOL;
$CPU_TEMP=array_values($str_arr)[7];
echo "CPU TEMPERATURE:".$CPU_TEMP.PHP_EOL;
//
echo "CLIENT HOST: ".$CLIENTHOST.PHP_EOL;
echo "User-Agent: ".$USERAGENT.PHP_EOL;
//echo "__IV,".$dc_iv.PHP_EOL;
// Use and filter UserAgent
//+++++++++++++++++++++++++++++++++++++++++++++++
/*
if ( $USERAGENT != "your_userAgent" ) {
echo "UserAgent not found!";
exit();
}
*/
// +++++++++++++++++++++++++++++++++++++++++++++
// APPEND DATA IN FILESYSTEM OR YOUR DB
$file="./test.txt";
$output=$Zdec.",".$USERAGENT.",".$CLIENTHOST.PHP_EOL;
file_put_contents($file, $output, FILE_APPEND | LOCK_EX);
file_put_contents("./CPU_TEMP.txt",$CPU_TEMP);
// ...
// ...
// ...
exit();
?>