Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
HI 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://210.201.4.120/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 | Sharing of tech stuff[^]

I see the service for Mosquitto Broker(MQTT v3.1 broker) is runnng and check the example pub.php to testing

Mosquitto-PHP/pub.php at master · mgdm/Mosquitto-PHP · GitHub[^]

the result is error Mosquitto\Client() is not found

I searched and found mosquitto.php

mosquitto-stub/mosquitto.php at master · Nakard/mosquitto-stub · GitHub[^]

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 runnig mosquitto with php and can be connected with localhost and check connection is OK or not also to subscribe and publish and reading the message

What I have tried:

PHP
require "mosquitto.php";
$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect("localhost", 1883, 5);
$client->subscribe('/#', 1);
while (true) {
	$client->loop();
	$mid = $client->publish('/hello', "Hello from PHP at " . date('Y-m-d H:i:s'), 1, 0);
	echo "Sent message ID: {$mid}\n";
	$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("Got a message ID %d on topic %s with payload:\n%s\n\n", $message->mid, $message->topic, $message->payload);
}
function disconnect() {
	echo "Disconnected cleanly\n";
}
Posted
Updated 3-Jun-16 21:32pm
v2

1 solution

I resolve this problem

this information to install mosquittolib with php in raspberry

PHP
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 create mosquitto.ini in path /etc/php5/mods-available


add this code in /etc/php5/mods-available/mosquitto.ini

PHP
extension=mosquitto.so


this code for check version of the Mosquitto library
PHP
dpkg -l | grep mosquito


and the end enabled that with sudo php5enmod mosquitto
PHP
sudo php5enmod mosquitto
echo "<?php phpinfo(); ??>" > ~/tester.php
php ~/tester.php
 
Share this answer
 
v2
Comments
Member 12666152 2-Aug-16 12:54pm    
Thanks for the solution.. Tired of searching but nothing was working but this solution worked perfectly..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900