PHP code example of cosmicvibes / xtrem-scale

1. Go to this page and download the library: Download cosmicvibes/xtrem-scale library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cosmicvibes / xtrem-scale example snippets


use CosmicVibes\XtremScale\XtremScale;

// Get weight from configured scale
$result = app('xtrem-scale')->getWeight();

if ($result['success']) {
    echo "Weight: " . $result['weight'];
} else {
    echo "Error: " . $result['error'];
}

use CosmicVibes\XtremScale\XtremScale;

$scale = new XtremScale('192.168.1.100');
$result = $scale->getWeight();

use CosmicVibes\XtremScale\XtremScale;

$result = XtremScale::read('192.168.1.100');

Route::get('/scale/weight', function () {
    $result = app('xtrem-scale')->getWeight();

    return response()->json($result);
});

[
    'weight' => '0.133 kg',   // What the scale's own display shows
    'gross' => 0.133,         // Gross weight
    'tare' => 0.0,            // Tare
    'net' => 0.133,           // Gross minus tare
    'unit' => 'kg',
    'stable' => true,         // False while the reading is still settling
    'net_displayed' => false, // True when the scale is displaying net, not gross
    'status_code' => null,    // Scale status code when it cannot report a weight
    'success' => true,
    'error' => null,
]

$scale = new XtremScale('192.168.1.100', 4444, 5555);
$scale->openStream();          // binds the socket, starts the stream

while (true) {
    // Multiplex with socket_select() across several scales if needed
    while (($reading = $scale->readStreamFrame()) !== null) {
        $latest = $reading;    // the scale pushes ~14 frames/sec
    }

    $scale->keepStreamAlive(); // periodically re-assert the stream
    // publish $latest somewhere shared (cache, Redis, ...)
}

$scale->closeStream();
bash
php artisan vendor:publish --tag=xtrem-scale-config