I install PHP client for MQTT using Mosquitto-PHP
Library in raspberry mosquitto php library/
after that create pub.php in /var/www/html/mqtt
when write in browser http://xxx.xxx.xxx.xx/mqtt/pup.php
and no see any data
I then tried
$mosquitto_pub -h localhost -t "/mqtt" -m "HelloWorld"
and the get
bash: -h: command not found
I don't know where the problem is, for information write in command
$mosquitto -v
1464002857: mosquitto version 1.4.8 (build date Tue, 17 May 2016 11:26:59 +0100) starting
1464002857: Using default config.
1464002857: Opening ipv4 listen socket on port 1883.
1464002857:Error: Address already in use
I also have the same problem in my windows I install mosquitto, see this link step-by-step-installing-and-configuring-mosquitto-with-windows-7
I see the service for Mosquitto Broker(MQTT v3.1 broker) is runnng and
check the example pub.php to testing the result is error Mosquitto\Client()
is not found
I searched and found mosquitto.php and put the I put this code in pub.php
require "mosquitto.php";
I don't know where the problem is, can any one help me to running mosquitto in my windows and raspberry.
I need to try mqtt connected with localhost and check connection is OK or not also to subscribe and publish and reading the message
this information to install mosquittolib with php in raspberry
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo apt-get update
apt-get install mosquitto
sudo apt-get install mosquitto-clients
sudo apt-get install php5-dev
sudo apt-get install libmosquitto-dev
sudo pecl install Mosquitto-alpha
and in /etc/php5/mods-available/mosquitto.ini
add this code
extension=mosquitto.so
this code for check version of the Mosquitto library
dpkg -l | grep mosquito
and the end enabled that with sudo php5enmod mosquitto
sudo php5enmod mosquitto
echo "<?php phpinfo(); ?>" > ~/tester.php
php ~/tester.php
code pub.php
<?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('/hasan', "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";
}
code sub.php
<?php
$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect("localhost", 1883, 60);
$client->subscribe('/hasan', 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";
}