TCW242-S_R1.0 – July 2021
Page 30
9.2.
HTTP API
9.2.1.
HTTP Post
TCW242-S can execute HTTP/HTTPS Post to upload XML/JSON files to a dedicated server.
This functionality is very useful if the controller is behind the router without a public IP address
or the user doesn’t have access to the router configuration. The server should have a public IP
address.
The typical monitoring application is shown in the picture below:
HTTP/HTTPS post can be sent periodically or periodically plus on an alarm condition. As an
answer, the server can send HTTP Get with the appropriate command -
see 9.2.3. HTTP
commands
To test HTTP/HTTPS Post follow the steps below:
Save following code like post.php:
<?php
define("FILENAME", 'status.xml');
define("FOLDER", '');
define("SEPARATOR", '');
define("STR_SUCCESS", 'set FIN');
define("STR_ERROR", 'error');
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$datePrefix = date('YmdHis', strtotime('now'));
$pathname = FOLDER.SEPARATOR.$datePrefix.'_'.FILENAME;
$postdata = file_get_contents("php://input");
$handle = fopen($pathname, 'w+');
$content = var_export($postdata, true);
fwrite($handle, substr($content, 1, strlen($content)-2));
fclose($handle);
echo (($handle === false) ? STR_ERROR : STR_SUCCESS)."\r\n";
}
else {
echo "The PHP script is working!";
}
?>
Copy the post.php file on a public webserver with PHP support. To verify that the script is
working properly, you can type the URL ( for example www.yourserverURL.com/post.php )
in your web browser. If all is OK, a web page with “The PHP script is working!” will be
shown.
Set the controller to send an HTTP/HTTPS POST to your web server. Enter the address
(yourserverURL.com/post.php) in the URL field. Click on the “Test HTTP Post” button.
If the HTTP/HTTPS POST is received and processed, “OK” will be shown close to the button.
Along with this, an XML file will be created in the same directory, where post.php is
located. The file name will contain time information and looks like
20190420103318_status.xml.
Summary of Contents for TCW242-S
Page 1: ......
Page 52: ...TCW242 S_R1 0 July 2021 Page 52 Appendix A Fig 1 Fig 2...