PHP code example of domotruc / mqttgen

1. Go to this page and download the library: Download domotruc/mqttgen 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/ */

    

domotruc / mqttgen example snippets





    $filename = 'vendor/domotruc/mqttgen/topics.json';
    $mqttgen = new MqttGen\MqttGen($filename);
    do {
        $mqttgen->nextMessage();
        sleep(2);
    } while (true);
}
catch (\Exception $e) {
    print($e->getMessage() . PHP_EOL);
}



use  MqttPlay\MqttPlay;

domotruc/mqttgen/flow.txt';
    $mqttPlay = new MqttPlay($filename, true, ' ', 'localhost', 1883, 1);
    while (($msg = $mqttPlay->nextMessage()) != null) {
        print($msg[MqttPlay::S_TIME ] . " " . $msg[MqttPlay::S_TOPIC] . " " . $msg[MqttPlay::S_PAYLOAD] . PHP_EOL);
    }
}
catch (\Exception $e) {
    print($e->getMessage() . PHP_EOL);
}



use  MqttPlay\MqttPlay;

'15:27:10.358', 'N/pvinverter/20/Ac/L1/Voltage', '{"value": 240.59999999999999}'),
    array('15:27:10.386', 'N/pvinverter/20/Ac/L1/Power', '{"value": 1821.8742186612658}'),
    array('15:27:10.415', 'N/pvinverter/20/Ac/L1/Energy/Forward', '{"value": 4272.6587533761876}'),
    array('15:27:10.496', 'N/pvinverter/20/Ac/L1/Current', '{"value": 3.9399999999999999}')
);

try {
    $mqttPlay = new MqttPlay($mqtt_flow, true, ' ', 'localhost', 1883, 1);
    while (($msg = $mqttPlay->nextMessage()) != null) {
        print($msg[MqttPlay::S_TIME ] . " " . $msg[MqttPlay::S_TOPIC] . " " . $msg[MqttPlay::S_PAYLOAD] . PHP_EOL);
    }
}
catch (\Exception $e) {
    print($e->getMessage() . PHP_EOL);
}