PHP (version 7.4) is resident at my main webserver also at internet.
Seems opportunity to study a combination of those 2 elements, by copying a file performing data-extraction,
and then - as part of learning curve for LoraWAN&PHP - in small steps adapting/extending that file for other functions.
Original PHP-script below: lines 2 and 3 explain from what source and how applied.
- Spoiler: show
- <?php
// File (stand-alone) from https://github.com/descartes/TheThingsS ... ok.Tab.php
// Input = file fed from TTN-Console via Webhook as Uplink message to **/TTN/ttn0.php
$ver = "2021-05-26 v1.5";
ini_set("error_reporting", E_ALL);
// Get the incoming information from the raw input
$data = file_get_contents("php://input");
if ($data == "") { // So we can check the script is where we expect via a browser
die("TTS.Webhook.Tab version: ".$ver);
}
// Save a raw copy of the message in a debug directory (you have to create it)
// This will fill up quickly so don't leave it turned on for too long!
$pathNFile = "debug/".date('YmdHis')."-".uniqid().".txt";
file_put_contents($pathNFile, $data);
$json = json_decode($data, true);
// Get selected values from the JSON
// Lines are partial indented to reflect the nesting of the data
$end_device_ids = $json['end_device_ids'];
$device_id = $end_device_ids['device_id'];
$application_id = $end_device_ids['application_ids']['application_id'];
$received_at = $json['received_at'];
$uplink_message = $json['uplink_message'];
$f_port = $uplink_message['f_port'];
$f_cnt = isset($uplink_message['f_cnt']) ? $uplink_message['f_cnt'] : 0; // Zero & empty values are not included
$frm_payload = $uplink_message['frm_payload'];
$rssi = $uplink_message['rx_metadata'][0]['rssi'];
$snr = $uplink_message['rx_metadata'][0]['snr'];
$data_rate_index = isset($uplink_message['settings']['data_rate_index']) ? $uplink_message['settings']['data_rate_index'] : 0;
$consumed_airtime = $uplink_message['consumed_airtime'];
// NOTE: Adding to the text files assumes that the server does not get two incoming
// requests at the exact same time, which for a handful of nodes is quite unlikely
// A production version of this would insert the data in to multiuser database.
// Daily log of all uplinks
$file = date('Ymd').".txt";
$output = "$received_at\t$application_id\t$device_id\t$f_cnt\t$f_port\t$frm_payload\t$data_rate_index\t$consumed_airtime\t$rssi\t$snr\n";
if (!file_exists($file)) { // Put column headers at top of file
file_put_contents($file, "received_at\tapplication_id\tdevice_id\tf_cnt\tf_port\tfrm_payload\tdata_rate_index\tconsumed_airtime\trssi\tsnr\n");
}
file_put_contents($file, $output, FILE_APPEND | LOCK_EX);
// Application log
$file = $application_id.".txt";
$output = "$received_at\t$device_id\t$f_cnt\t$f_port\t$frm_payload\t$data_rate_index\t$consumed_airtime\t$rssi\t$snr\n";
if (!file_exists($file)) { // Put column headers at top of file
file_put_contents($file, "received_at\tdevice_id\tf_cnt\tf_port\tfrm_payload\tdata_rate_index\tconsumed_airtime\trssi\tsnr\n");
}
file_put_contents($file, $output, FILE_APPEND | LOCK_EX);
// Device log
$file = $application_id."__".$device_id.".txt";
$output = "$received_at\t$f_cnt\t$f_port\t$frm_payload\t$data_rate_index\t$consumed_airtime\t$rssi\t$snr\n";
if (!file_exists($file)) { // Put column headers at top of file
file_put_contents($file, "received_at\tf_cnt\tf_port\tfrm_payload\tdata_rate_index\tconsumed_airtime\trssi\tsnr\n");
}
file_put_contents($file, $output, FILE_APPEND | LOCK_EX);
?>
Datacake.de happily receives and processes the uplink-file from the TTN-Console,
as demonstrated by it's Dashboard and by generation of a csv-file.
Modified PHP-script below.
Again lines 2 and 3 explain source and application.
In the original scriptlines inserted numbers into the outputfilenames, to have separately visible output relative to the original script.
Experimental segments added starting at lines 20, 29 and 106 respectively.
- Spoiler: show
- <?php
// Example file copied from https://github.com/descartes/TheThingsS ... ok.Tab.php
// Modified version fed by Outgoing Webhook from Datacake, with Datacake being fed through Webhook from TTN-Console
$ver = "base 2021-05-26 v1.5, tuned 2023-03-29";
ini_set("error_reporting", E_ALL);
// Line 08 = Get the incoming information from the raw input
$data = file_get_contents("php://input");
if ($data == "") { // So we can check the script is where we expect via a browser
die("TTS.Webhook.Tab version: ".$ver);
}
// Line 15 = Save a raw copy of the message in a debug subdirectory (you have to create it)
// This will fill up quickly so don't leave it turned on for too long!
$pathNFile = "debug/".date('YmdHis')."-1-".uniqid().".txt";
file_put_contents($pathNFile, $data);
// Line 20 = Save a 2nd copy of the message in another subdirectory named data
// For experimentation change name and extension for this file
$path2File = "data/".date('YmdHis')."-File.json";
file_put_contents($path2File, $data);
$json = json_decode($data, true);
// Line 29 = Experimental rewrite JSON File in PHP to be used for processing-file
// Script-example segment copied from https://www.nidup.io/blog/manipulate-json-files-in-php
// Set name for derived, new JSON-file
$path = 'data/new-file.json';
// JSON data as an array
// Separate decoding of the incoming JSON-file from Datacake
$jsonData = json_decode($data, true);
// Convert JSON data from an array to a string
$jsonString = json_encode($jsonData, JSON_PRETTY_PRINT);
// Write in the new file
$fp = fopen($path, 'w');
fwrite($fp, $jsonString);
fclose($fp);
// Line 43 = Get selected values from the JSON
// Lines are partial indented to reflect the nesting of the data
$end_device_ids = $json['end_device_ids'];
$device_id = $end_device_ids['device_id'];
$application_id = $end_device_ids['application_ids']['application_id'];
$received_at = $json['received_at'];
$uplink_message = $json['uplink_message'];
$f_port = $uplink_message['f_port'];
$f_cnt = isset($uplink_message['f_cnt']) ? $uplink_message['f_cnt'] : 0; // Zero & empty values are not included
$frm_payload = $uplink_message['frm_payload'];
$rssi = $uplink_message['rx_metadata'][0]['rssi'];
$snr = $uplink_message['rx_metadata'][0]['snr'];
$data_rate_index = isset($uplink_message['settings']['data_rate_index']) ? $uplink_message['settings']['data_rate_index'] : 0;
$consumed_airtime = $uplink_message['consumed_airtime'];
$decoded_payload = $uplink_message['decoded_payload'];
$timestamp = $uplink_message['timestamp'];
$location = $uplink_message['location'];
// NOTE: Adding to the text files assumes that the server does not get two incoming
// requests at the exact same time, which for a handful of nodes is quite unlikely
// A production version of this would insert the data in to multiuser database.
// Line 71 = Daily log of all uplinks
$file = date('Ymd')."_File2.txt";
$output = "$received_at\t$application_id\t$device_id\t$f_cnt\t$f_port\t$frm_payload\t$data_rate_index\t$consumed_airtime\t$rssi\t$snr\n";
if (!file_exists($file)) { // Put column headers at top of file
file_put_contents($file, "received_at\tapplication_id\tdevice_id\tf_cnt\tf_port\tfrm_payload\tdata_rate_index\tconsumed_airtime\trssi\tsnr\n");
}
file_put_contents($file, $output, FILE_APPEND | LOCK_EX);
// Line 83 = Application log
$file = $application_id."_File2.txt";
$output = "$received_at\t$device_id\t$f_cnt\t$f_port\t$frm_payload\t$data_rate_index\t$consumed_airtime\t$rssi\t$snr\n";
if (!file_exists($file)) { // Put column headers at top of file
file_put_contents($file, "received_at\tdevice_id\tf_cnt\tf_port\tfrm_payload\tdata_rate_index\tconsumed_airtime\trssi\tsnr\n");
}
file_put_contents($file, $output, FILE_APPEND | LOCK_EX);
// Line 95 = Device log
$file = $application_id."_File2_".$device_id.".txt";
$output = "$received_at\t$f_cnt\t$f_port\t$frm_payload\t$data_rate_index\t$consumed_airtime\t$rssi\t$snr\n";
if (!file_exists($file)) { // Put column headers at top of file
file_put_contents($file, "received_at\tf_cnt\tf_port\tfrm_payload\tdata_rate_index\tconsumed_airtime\trssi\tsnr\n");
}
file_put_contents($file, $output, FILE_APPEND | LOCK_EX);
// Line 106 = JSON-file, Experimental setup
$file = $application_id."_File2_".$device_id.".json";
$output = "$decoded_payload\t$timestamp\t$location\n";
if (!file_exists($file)) { // Put column headers at top of file
file_put_contents($file, "root\n");
}
file_put_contents($file, $output);
?>
Modifications seem in same line as original, and therefore wondering why/where erroneous:
short hints appreciated for study & correction to enable running.