User Tools

Site Tools


how_tos:export_to_custom_server

Exporting Data to a Custom Server

The Custom Data Export allows you to push data to your own server at an interval of your choice. You need to specify the URL of the script on your server that can handle the incoming data and respond with an appropriate success or failure message.

Replace the URL with your server's address and path to your script. In this example, we will use the name export-script.php. Set the interval at which you want Wattmon to export data to your server (in minutes). Make sure the status is set to Enabled.

The example below shows a sample PHP application that will take this data and store it in a csv file on the server. Copy the script into the file export-script.php on your server.

The format of the received data is in the form of an HTTP POST with the following fields:

key MAC ADDRESS - This is the mac address of the device sending the data
md5sum MD5 sum of the data contents of the data parameter
data CSV DATA - The CSV data contains the column headers in the first row followed by multiple rows of data. The first column contains the timestamp. The format of this file will depend on the fields being logged and can be configured in the Data Collection section of the Control Panel in wattmon.

Response: Send OK (2 bytes) to inform Wattmon that it can send the next block of data or send ERROR if there is any issue while processing the data. This will then mean that Wattmon will send the same block of data again until it receives OK.

<?
/**
* Change this to the filename and path of the CSV file you wish to create
*/
	define("DATA_FILE","data.txt");
 
	$f=fopen(DATA_FILE,"a+");
	if ($f) {
		// use $_POST['key'] to get the mac address of the device
		// use $_POST['md5sum'] to verify the mac address
		if ($_POST['data']) {
			if ($_POST['md5sum']) {
				if (md5($_POST['data'])!=$_POST['md5sum']) {
					die("ERROR: MD5 sum mismatch");
				}
			}
			fwrite($f,$_POST['data']);
		}	
		fclose($f);
		//print("ERR001");
		print("OK");
  } else die("Error opening file");
?>
how_tos/export_to_custom_server.txt · Last modified: 2021/11/12 10:30 by admin