Reading the weight from an Ohaus Defender 5000 scale with PHP

Please consider: A single-threaded, synchronous programming language with blocking I/O calls like PHP may not be the best choice for communicating with a scale. The following post is not meant as a recommendation for PHP.

Defender 5000 scales can be equipped with a WiFi or wired Ethernet interface. The scale acts as a TCP server and your script establishes a persistent connection as a TCP client.

You can then use one of the following methods to send the weight from the scale to your PHP application:

  • Send a command to the scale to request the weight (described in this post).
  • Wait until the operator presses the PRINT button.
  • Configure the scale to send each stable weight automatically.
  • Set the scale to a continuous transmission mode (it will send several values per second).

Please see the Defender 5000 manual (PDF) for further information.

You can easily test the communication via wired Ethernet or WiFi by using PuTTY in “raw” mode:

PuTTY raw tcp connection
The WiFi dongle uses port 6060 (wired Ethernet: port 9761), the IP address is assigned via DHCP by default.
Defender 5000 scale: weight in PuTTY
IP (immediate print) command used to request the weight and reply sent by the scale (N: net weight, G: gross weight, T: tare weight).

Using PHP to communicate with the scale

For demonstration purposes, I’ve adapted example #2 (simple TCP/IP client) from the official documentation:

<?php
error_reporting(E_ALL);

echo "TCP/IP Connection\n";

/* Ohaus Defender 5000 WLAN interface uses port 6060 by default, Ethernet interface uses port 9761 */
$service_port = 6060;

/* IP address of the scale, can be displayed/set in the menu */
$address = "192.168.0.195";

/* 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";
}

$in = "IP\r\n"; //IP command (Immediate Print) - supported commands depend on the manufacturer, model and LFT settings
$out = '';

echo "Sending command...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";

echo "Reading response:\n\n";
while ($out = socket_read($socket, 128, PHP_NORMAL_READ)) {
    echo $out;
}

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

When socket_read encounters carriage return or line feed, it returns the data received from the scale. By default, these characters are sent by the scale at the end of each line.

The PHP script produced the following output:

Defender 5000 weight requested with PHP

You can modify the output template through the menu of the scale or by using the Ohaus Scale Mate software, e.g. to send just the net weight and date/time:

Ohaus Defender 5000 custom template

You’ll find a list of commands supported by the scale in the user manual (chapter 5, “interface command”). Note that each command has to be followed by carriage return and line feed.

In the script, I used the “IP” (immediate print) command, which tells the scale to send the weight immediately (whether it is stable or not). This command is available on all recent Ohaus scales. One notable exception are legal-for-trade (LFT) models, which only support the P command (similar to pressing the print button) and in some cases the SP command (stable print, send weight as soon as it has stabilized).

Tip: The Ohaus Defender 5000 also supports many MT-SICS commands. This means that you could send commands such as “SI” (send immediately) and receive a reply in a standardized format which is independent of the template set in the scale:
MT-SICS response Ohaus Defender 5000

Please note that I’m not a PHP expert and that I don’t recommend using PHP. This article is only supposed to be a very basic demonstration. Other programming languages such a C#, Java, server-side JavaScript (node.js) or Python may be more suitable.


The header photo shows a Defender 5000 communication test case (a hard case with a built-in Defender 5000 indicator with all optional communication interfaces installed).

‘World with weighing’ campaign lauched by CECIP

CECIP, the European weighing industry association, has launched a campaign to “to make the general public and businesses aware of the importance of reliable and accurate weighing”.

From the announcement on the CECIP website (released on March 15, 2021):

The CECIP campaign focusses on the impact of weighing in daily life and in the operations of businesses. Although businesses generally understand the need for weighing, it is not always understood what the impact of inaccurate weighing could be. Yet, it can impact the core business when weighing results are not correct. For production companies inaccurate weighing of ingredients or parts can harm the quality of the final products. This is not always clear and CECIP hopes more business will be conscious about the importance of reliable weighing and the consequences weighing accuracy can have on their operations.

Overall, the campaign aims at making consumers, users and buyers of weighing instruments understand that weighing is not just the value you see on the display of a scale, but a vital process that contributes to our world functioning smoothly.

As part of the campaign the key messages will be spread via social media, magazines and the dedicated website: www.worldwithweighing.com.

The campaign website is currently showing a suspension notice:

worldwithweighing.com on March 19, 2021