LAN DS18B20 WEB Temperature Monitor 1 meter Cable
www.WEB-Relays.com
www.Facebook.com/KMtronic

Reading Voltage from SDM120 Modbus Power Energy Meter using KMtronic TCPRS485 bridge via PHP

Spread the love

 

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


 

KMtronic LAN WEB 8 Channel Relay board