@arjanvanb
Sorry my fault, the Node Red TTN in my stream was over MQTT not HTTP, since the goal was to insert into mySQL data from TTN. Even if original question was with http integration worth mentioning other simple solution can work.
By the way, I do not consider my example as bad, since data (payload) is parsed by a function in between MQTT and mySQL Insert. Each payload byte is decoded to be transformed by number so whatever you push in to the function, I think you would never be able to do any SQL command injection with dangerous commands in this example
Afterward of course the transfer between the output of Node Red to MySQL is relevant to network security and all rules applies (authentication, encryption, firewalling, …)
I found your code, while working on a PHP script, that react on a uplink message (from HTTP Integration) and making a downlink message to the device.
I’ve tried to use your code, to make my webserver, though Curl, make a Downlink-message.
But i do not get any result and no error.
Could i ask you (or maybe someone else ) to make an example, how to do this, or maybe point out my errors in this code:
<?php
//API Url (Got it from Uplink http-Integration, removed the key for this public forum)
$url = "https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/hrmansen1/http?key=ttn-account-v2.[REMOVED]";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
$jsonDataEncoded = "{\"dev_id\":\"hrmansenuno\",\"port\":1,\"confirmed\":false,\"payload_raw\":\"00\"}";
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
//Execute the request
$result = curl_exec($ch);
echo "Result:<br/>";
echo $result;
echo "<br>";
echo "Errorcode: ";
echo curl_error($ch)
?>
Unfortunately it did’nt change anything. I changed the line for the Json message to: $jsonDataEncoded = "{\"dev_id\":\"hrmansenuno\",\"port\":\"1\",\"confirmed\":\"false\",\"payload_raw\":\"" . base64_encode("00") . "\"}";
But still get no result at the device, and no line in the Data log in the console.
Any ideas or php examples are welcome
Hi Again.
I have experimented a little more since last time. I have found that my post date could be reduced to just dev_id and payload_raw.
I also found some example code from another project, and now i got my downlink message to be shown in the Data section of my device. But somehow my base64 encoding, ends with a extra 30 added to every digit
Just now while i am typing this, realizing that its the 30hex from the ascii character “0”
Thanks for the help!
Here’s my corrected code if anybody else is interested:
That probably indeed does the job, assuming you might also want to send much larger values, up tot 255 for each value. If you want to set “on” or “off”, then you can combine 8 states in a single byte. See https://www.thethingsnetwork.org/docs/devices/bytes.html
Hi,
I’m not sure if I need to open a new topic, but all the above is also applicable for my question.
I’m trying to do the same, but I get stuck in the part before this topic starts;
I have webhook.site working, but now I can’t figure out what to fill in in the http integration window to make
$content = file_get_contents(“php://input”);
to work in my php-script.
I’m now locally working with xampp, so localhost. But i guess I need to place my php script somewhere on a server?
I have a Azure and visualstudio environment available (also where my database and API is). But dont know what is needed.
Thanks.
I have a question about how ACK messages (Device->Cloud) in the case of confirmed transmission (initiated in direction Cloud->Device) could be seen by our application via HTTP integration.
We send confirmed messages, but looks like TTN doesn’t send the ACK messages to our HTTP endpoint, or maybe we are logging the input messages incorrectly. We don’t see them due to that.
We are logging all HTTP input on our HTTP endpoint
(by HTTP endpoint, I refer to path which is being called by TTN once a message is received).
Sorry to bring up a new issue on an older form. I have tried absolutely everywhere online, and I still can’t seem to get uplink data from TTN sent to my website to work. In the TTN HTTP integration fields, I have used the default access key, my website with the PHP file on the end (e.g. http://websitename.com/file.php - obviously websitename is different) and the method as POST. On the PHP side, I have tried using MYSQL and writing to text files but I get absolutely nothing. When looking at the devices accessing my website I can clearly see TTN sending data. To simplify the code to find the root of the problem, I have used this code in by ‘file.php’ from the posts above (slightly modified):
<?php
//Make sure that it is a POST request.
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
throw new Exception('Request method must be POST!');
}
//Make sure that the content type of the POST request has been set to application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strcasecmp($contentType, 'application/json') != 0){
throw new Exception('Content type must be: application/json');
}
//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));
//Attempt to decode the incoming RAW post data from JSON.
$decoded = json_decode($content, true);
//If json_decode failed, the JSON is invalid.
if(!is_array($decoded)){
throw new Exception('Received content contained invalid JSON!');
}
//Process the JSON.
echo $decoded; // Can I simply echo the decoded?
?>
When I try to see if it works, I go to the website URL directly (http://websitename.com/file.php) and then simulate an uplink in TTN console. It only displays the thrown error ‘Request method must be POST!’ or if I remove this, then nothing. What am I doing wrong? Should I have any other files in my website directory which has code relevant to file.php? Could it have something to do with my hosting, which is GoDaddy? Let me know if I can make it clearer in any way or try anything.
Where do you expect the output to go to? The HTTP integration is a server-to-server interface; when TTN connects to your server, it’s not going to handle the response you’re sending using the line above. Rather than echo-ing the request back to TTN, you’ll need to store it somewhere.
It doesn’t make sense that you’re going to that URL: even when TTN also goes to that URL at exactly the very same time (unlikely), there’s nothing in your code to make you (the human) show whatever TTN sent to that URL. Each request is a standalone request; nothing is shared with others who happen to know the same URL.
Thank you so much. I failed to mention that I am a complete novice to PHP and I was thinking in terms of C++ and getting it simply to display data it receives. I feel very silly right now.
Im also trying to receive data on my Apache server. Im using wampserver (localhost). Can you give me some advices how to receive the data from the devices on my application. Maybe the steps you did…
@rego21: you need a server accessible through the web at some address. If you have it at home on an ADSL line, in principle you could, but if you do not know already how to do it, it is too much effort, it is easier to just use some free PHP hosting.
I do not use a specific platform, having my own server. Just google for PHP free hosting, or maybe wait for some other user suggestion. If what you have to do is simple, you might also use some already available platform (check those in the Integrations tab).