Create a php function in php to receive LoraWan Objenious callback

Lora-300x171I’m actually doing some experiment with the French LoraWan network operated by Objenious. To receive the message on your backend application, one of the way is to build a callback function to proceed the data.

Due to LoraWan protocol, you have different type of possible callback like for: joining network, uplink messages and downlink report.

This post gives an exemple on how to implement an uplink message callback handler, with php, for your application backend server.

The type of callback is given by the parameter “ty” [like TYpe] from the requested URI.

Then the body of the request contains the json data with all the informations about the uplink message. Many information are in the json document. Here, to make it simple I’ll just extract the plain text data and the GMT timestamp of the message. Timestamp is in MS since EPOC.

In this exemple a new json file obj.json is created with 3 entries per message : time, human readable time and plain text data.

The body of the request is read with file_get_contents(“php://input”) line.

The json is decoded with json_decode method. this one creates an object with all the json elements orgnised as in the json file in it. Then each element can be accessed by its own name as documented in the objenious API.

<?php

    $_callbackType = $_GET["ty"];

    if ( $_callbackType == "20020" ) {
       $_fileName = "obj.json";
       $_file = fopen($_fileName,'a');
       $postdata = file_get_contents("php://input");

       $json = json_decode($postdata);
       $_data  = $json->{'UplinkIndication'}->{'LORA-FRMPayloadClearText'};
       $_time  = round ( $json->{'UplinkIndication'}->{'GTW-Timestamp'} / 1000 ); 
       $_htime = gmdate("Y-m-d H:i:s",$_time);

       fwrite($_file,'{ "time" : "' . $_time .'", "htime" : "' . $_htime . '", "data" : "' . $_data . '" },');
       fwrite($_file,"\n");

       fclose($_file);
    }
?>

Sounds like simple isn’t it ?

In the current API version, there is not a lot of metadata about the message like geographical situation, rssi, receiving gateway… but apparently there is a second message “UplinkStatIndication” with many more and interesting metadata. I need to look at this and I will complete this post.

This entry was posted in LoRa and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.