Getting data from TTN to a webpage

Hello.

I have a temperature sensor that sends info over Lora to TTN. Everything works fine here.

I have been able to get this data to my own server, by using MQTT and node red. everything works fine here as well.

But now I also want to display this data (just one temperature), on a webpage that we host (from one.com). This is a webpage uses something called “web builder” (not wordpress).

This is where I run into problems, and desperately need helps!

I am a HW-guy, so I have made all the HW, so now when it comes to these hig level language am I quite lost.

To the web page can’t I use MQTT (no support from there side there), so I’m “stuck” on using theirs “code-box”, which is a text-box where I can type in some sort of code. I been told that JavaScript will work.

But how to I fetch the data from TTN?

I have tried to enable “Storage Integration” and get data by the GET-command, But I can’t get that to work.

To be able to test things, I have started to use node red and try to use JavaScript to get the info, but I only get the {“code”:12,“message”:“Not Implemented”} back.

I have function there with this code:

msg.method = “post”;

msg.url = “https://eu1.cloud.thethings.network/api/v3/as/applications/xxx/devices/eui-xxx/packages/storage/uplink_message”;

msg.payload = `-X ‘GET’ \

-H ‘Accept: text/event-stream’ \

-d ‘limit=10’ \

-d ‘after=2022-05-01T00:00:00Z’ \

-H ‘content-type: application/json’ \

-H ‘Authorization: Bearer NNSXS.xxx’ `;

msg.headers = null;

msg.cookies = null;

return msg;

Then this is sent to a HTTP-request.

But as I mention above, doesn’t it work (get back code 12).

Any suggestions on how I should solve this problem?

I thought it wouldn’t be that difficult to display one small message on a web-page. :slight_smile:

Can also mention that I have search in the forum, documentation and google for a couple of days, without solve it. Usually my stubbornness helps my solve it in a while, but now google starts to run out on pages…

It would be if you are using any form of web page builder, proprietary, Wix, WordPress etc. They aren’t really designed for someone to put in a Javascript XHR request and if you do get it working, your TTN Storage credentials will be available in the web page for all to see.

You could change the request to go to your server at home if you made it externally available.

Or you could use webspace that allows you to use a PHP script that can receive the uplink directly from TTN and make the data available in a web page. This would be the simplest & most efficient but requires that you can run PHP - which if you have web space that can host WordPress you’d be able to do

1 Like

Ah, thanks for the info.
I talked to the chat of the web host and they said php will work.
May I ask what you mean with a webspace (or do you simply mean another web host), and do you have some PHP script as an example? If I have an example, I usually manage to sort things out.

One of the many phrases to describe somewhere you can put files to be served up by a web server - whether it be some space for your files on a shared computer, a virtual private server or a dedicated server. Almost all but the most basic allow you to run scripts, most usually PHP as it was purposed designed for scripting web pages. Almost all other languages require a bit more setup or configuration.

There are various examples for PHP web hooks for TTN on GitHub that are linked here if you care to use the forum search.

You must try Webhooks, to your Server, and save data to MySql.
Sorry for my bad english.
Best regards.

This is a part of my code in the Webhook PHP:

$responseBody = file_get_contents('php://input');
$json = json_decode($responseBody);
//Save in json file for debug
if($json){
    $fp = fopen('results.json', 'w');
    fwrite($fp, json_encode($json));
    fclose($fp);
}

$data = $json;
$lat = $data->uplink_message->decoded_payload->latitude;
$lon = $data->uplink_message->decoded_payload->longitude;
$alt = $data->uplink_message->decoded_payload->altitude;
$hdop = $data->uplink_message->decoded_payload->hdop;
$sats = $data->uplink_message->decoded_payload->sats;
$rssi = $data->uplink_message->rx_metadata[0]->rssi;
$sr = $data->uplink_message->rx_metadata[0]->snr;

if($json){
$sql = "INSERT INTO gps (lat, lon, alt, fecha, rssi, sr, hdop, sats) VALUES ('".$lat."', '".$lon."', '".$alt."', NOW(), '".$rssi."', '".$sr."', '".$hdop."', '".$sats."')";
if(mysqli_query($con,$sql)){
    echo "OK"; 
}

//This save a file with the query for debug
if($json){
    $fp = fopen('sql.json', 'w');
    fwrite($fp, $sql);
    fclose($fp);
}
mysqli_close($con);
}