Mosquitto-PHP with Ubuntu

This is just an extension to allow using the Eclipse Mosquitto™ MQTT client library with PHP

# For Installing pecl
sudo apt-get install php-pear

# Install PHP developer packages
sudo apt-get install php5-dev
or
sudo apt-get install php7-dev

# Mosquitto setup
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install libmosquitto-dev
sudo apt-get install mosquitto mosquitto-clients
sudo pecl install Mosquitto-alpha

Add extension=mosquitto.so to php.ini

sudo vim /etc/php5/cli/php.ini

Now test your setup, create new subscriber.php

$client = new Mosquitto\Client();
$client->onConnect(‘connect’);
$client->onDisconnect(‘disconnect’);
$client->onSubscribe(‘subscribe’);
$client->onMessage(‘message’);
$client->connect(“localhost”, 1883, 60);
$client->subscribe(‘/#’, 1);

while (true) {
$client->loop();
sleep(2);
}

$client->disconnect();
unset($client);

function connect($r) {
echo “I got code {$r}\n”;
}

function subscribe() {
echo “Subscribed to a topic\n”;
}

function message($message) {
printf(“\nGot a message on topic %s with payload:%s”,
$message->topic, $message->payload);
}

function disconnect() {
echo “Disconnected cleanly\n”;
}

Execute the subscriber.php

php subscriber.php

On executing the above command, you will get

I got code 0
Subscribed to a topic

Test the subscriber with mosquitto_pub command
mosquitto_pub -h localhost -t “/mqtt” -m “Hello World!”

On executing the above command, you will get

Got a message on topic /mqtt with payload:Hello World

Now create new publisher.php

$client = new Mosquitto\Client();
$client->onConnect(‘connect’);
$client->onDisconnect(‘disconnect’);
$client->onPublish(‘publish’);
$client->connect(“localhost”, 1883, 5);

while (true) {
try{
$client->loop();
$mid = $client->publish(‘/mqtt’, “Hello from PHP!”);
$client->loop();
}catch(Mosquitto\Exception $e){
return;
}
sleep(2);
}

$client->disconnect();
unset($client);

function connect($r) {
echo “I got code {$r}\n”;
}

function publish() {
global $client;
echo “Mesage published\n”;
$client->disconnect();
}

function disconnect() {
echo “Disconnected cleanly\n”;
}

Execute the publisher.php

php publisher.php

On executing the above command, we will get a message as shown below :

I got code 0
Mesage published
Disconnected cleanly

Also, we can see the published message as shown below at the terminal where the subscriber ( subscriber.php ) is running :
I got code 0
Subscribed to a topic
Got a message on topic /mqtt with payload:Hello from PHP!

For now only implemented few steps looking for how much I can dive into.

Advertisement

One thought on “Mosquitto-PHP with Ubuntu

  1. Hello, thank you for this article, it works but my prg stops after a random time (30minutes or 1h). I receive data every 30 seconds. I modified the keep-alive and Qos but nothing. I use php 7.0
    when the program stops I do “control c” and I restart the sub command.
    do you have an idea ?
    thank you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s