PHP receive script for KMTronic DS1820 LAN POST version module

 

Receive POST data using PHP

KMtronic LAN DS18B20 Temperature Monitor

 

 

LAN DS18B20 High-Precision 1-Wire Digital Temperature Monitor

 


 

PHP file:

<?php


$date = date('Ymd');
$time = date('H:i:s');
$hourtime = date('H:i');
$unixtime = time();

$file = "/var/www/html/data/".$date.".log";
$current = file_get_contents($file);

$current .= $date;
$current .= "-";
$current .= $time;
$current .= "\t";
$current .= $unixtime;
$current .= "\t";

$temp = $_POST['temperature'];

$temp2 = implode("",$_POST['temperature']);

$current .=  $temp2;
$current .= "\r\n";

// Write the contents back to the file
file_put_contents($file, $current);

// a:2:{s:9:"device_id";s:10:"B3D53A5348";s:11:"temperature";a:1:{s:18:"'2874EDD0040000F3'";s:5:"15.43";}}

?>

 


 

DATA file:
20161229-00:00:21    1482962421    22.75
20161229-00:00:56    1482962456    22.75
20161229-00:01:30    1482962490    22.75
20161229-00:02:04    1482962524    22.75
20161229-00:02:38    1482962558    22.75
20161229-00:03:12    1482962592    22.75
20161229-00:03:46    1482962626    22.75
20161229-00:04:20    1482962660    22.75
20161229-00:04:54    1482962694    22.75
20161229-00:05:28    1482962728    22.81
20161229-00:06:02    1482962762    22.81




 

 

 

Create a Crontab server side PHP script for uploading temperature to a MySQL database

 

KMtronic LAN DS18B20 WEB 1-Wire Digital Temperature Monitor
KMtronic LAN DS18B20 WEB 1-Wire Digital Temperature Monitor

 


 

Temperature Monitor STATUS XML data

http://XXX.XXX.XXX.XXX/status.xml
XXX.XXX.XXX.XXX is IP ADDRESS Temperature monitor

 


 

MySQL table

Create a Crontab server side PHP script for uploading temperature to a MySQL database
Create a Crontab server side PHP script for uploading temperature to a MySQL database
Create a Crontab server side PHP script for uploading temperature to a MySQL database
Create a Crontab server side PHP script for uploading temperature to a MySQL database

 


 

Cron is a system daemon used to execute desired tasks (in the background) at designated times. 

A crontab file is a simple text file containing a list of commands meant to be run at specified times. It is edited using the crontab command. The commands in the crontab file (and their run times) are checked by the cron daemon, which executes them in the system background.

Crontab PHP file:

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "temperature_database";


// Loading the XML file
$xml = simplexml_load_file("http://XXX.XXX.XXX.XXX/status.xml");
// XXX.XXX.XXX.XXX is IP ADDRESS Temperature monitor

foreach($xml->children() as $sensor)
 {
  if ($sensor->id == "2874EDD0040000F3" )
  {
  $temp_value = $sensor->temp;
  }
 }
// "2874EDD0040000F3" is ID DS18B20 sensor

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO km_temperature (temp_id, temp_value, temp_time )
VALUES ('2874EDD0040000F3', $temp_value , NOW())";
// "km_temperature" is TABLE name


if ($conn->query($sql) === TRUE) {
 echo "New record created successfully";
} else {
 echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?>

 

 

 

 

 

 

Reading Voltage from SDM120 Modbus Power Energy Meter using KMtronic TCP<>RS485 bridge via PHP

 

Reading Voltage from SDM120 Modbus Power Energy Meter using TCP<>RS485 bridge via PHP.

Bridge IP : 192.168.1.199

Bridge PORT : 9761

SDM120 Energy Meter Speed: 2400/8/N/1

 

Reading Voltage from SDM120 Modbus Power Energy Meter via TCP<>RS485 bridge via PHP
Reading Voltage from SDM120 Modbus Power Energy Meter via TCP<>RS485 bridge via PHP

 

Reading Voltage from SDM120 Modbus Power Energy Meter via TCP<>RS485 bridge via PHP
Reading Voltage from SDM120 Modbus Power Energy Meter via TCP<>RS485 bridge via PHP

 

PHP file

<?php

function hexTo32Float($strHex) {
 $v = ($strHex);
 $x = ($v & ((1 << 23) - 1)) + (1 << 23) * ($v >> 31 | 1);
 $exp = ($v >> 23 & 0xFF) - 127;
 return $x * pow(2, $exp - 23);
}

 
$msg = chr(0x01).chr(0x04).chr(0x00).chr(0x00).chr(0x00).chr(0x02).chr(0x71).chr(0xCB);
$len = strlen($msg); 

 
error_reporting(E_ALL);

echo "<h2>TCP/IP Connection</h2>\n";

/* Get the port for the WWW service. */
$service_port = 9761;

/* Get the IP address for the target host. */
$address = gethostbyname('192.168.1.199');

/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
 echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
 echo "OK.\n";
}

echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
 echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
 echo "OK.\n";
}


$out = '';

echo "Sending HTTP HEAD request...";
echo socket_sendto($socket, $msg, $len, 0, '192.168.1.199', 9761);
echo "OK.\n";

echo "Reading response:\n\n";
sleep(1);
$out = socket_read($socket, 9761);
 
 $hexstr = unpack('H*', $out);
 $data = array_shift($hexstr);
 $voltage1 = hexdec(substr($data, -6, 2));
 $voltage2 = hexdec(substr($data, -8, 2));
 $voltage3 = hexdec(substr($data, -10, 2));
 $voltage4 = hexdec(substr($data, -12, 2));

$voltage = ((256*256*256*$voltage4) + (256*256*$voltage3) + (256*$voltage2) + $voltage1);

$voltage = round(hexTo32Float($voltage),2);
 echo $voltage . "V";
 echo "\r\n";


echo "Closing socket...";
socket_close($socket);
echo "OK.\n\n";

?>

 

Result

kmtronic@ubuntu-sever2:~$ php /var/www/html/for-test/tcp-send-hex.php
<h2>TCP/IP Connection</h2>
OK.
Attempting to connect to '192.168.1.199' on port '9761'...OK.
Sending HTTP HEAD request...8OK.
Reading response:

229.7V
Closing socket...OK.

kmtronic@ubuntu-sever2:~$

 


Din rail meter

“I use this to monitor my solar array. I bought this one specifically because of the bidirectional monitoring and size. Small display but easily readable. Probably does more than I’ll ever use.”  by Doctorxerox


 

PHP Scripts: Control KMtronic LAN WEB Relay board via PHP

 

Ethernet Modbus IP 8 channels Modbus/TCP LAN Relay board

 

PHP code for STATUS Read WEB Relay board connected to

IP: 192.168.1.199

PORT:80

USER: admin

PASSWORD: admin

 

 

Create a new file.

<?php

$url = "http://192.168.1.199:80/status.xml";
$username = "admin";
$password = "admin";

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

//execute post
$result = curl_exec($ch);

print $result;

?>

 

Save the file as web_8relay_status.php and run. ( $ php web_8relay_status.php )

 

Result:

$ php web_8_relay_status.php 

0
0
0
0
0
0
0
0
0





 


 

 

PHP code for CONTROL WEB Relay board connected to

IP: 192.168.1.199

PORT:80

USER: admin

PASSWORD: admin

 

 

COMMANDS:
FF0101  // TURN ON Relay 1
FF0100  // TURN OFF Relay 1
FF0201  // TURN ON Relay 2
FF0200  // TURN OFF Relay 2

FF0801  // TURN ON Relay 8
FF0800  // TURN OFF Relay 8

 

Create a new file.

<?php

$url = "http://192.168.1.199:80/FF0101";  // TURN ON Relay 1
$username = "admin";
$password = "admin";

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

//execute post
$result = curl_exec($ch);

print $result;

?>

Save the file as web_relay_1_on.php and run. ( $ php web_relay_1_on.php )