2016-05-23 244 views
0

我树莓 mosquitto php library/Mosquitto-PHP库树莓和Windows

使用Mosquitto-PHP

图书馆/var/www/html/mqtt当在浏览器http://xxx.xxx.xxx.xx/mqtt/pup.php写入和安装PHP客户端MQTT创造pub.php后无看到任何数据

我又试图

$mosquitto_pub -h localhost -t "/mqtt" -m "HelloWorld" 

和得到

bash: -h: command not found 

我不知道问题出在哪里,信息写入命令

$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 

我也有我的窗户我在安装mosquitto同样的问题,看到这个链接step-by-step-installing-and-configuring-mosquitto-with-windows-7

我看到Mosquitto Broker(MQTT v3.1 broker)的服务正在运行,并且 检查示例pub.php来测试结果是错误Mosquitto\Client()未找到

我搜查,发现mosquitto.php,把我把这个代码在pub.php

require "mosquitto.php"; 

我不知道是哪里的问题,任何一个可以帮助我在我的窗户和覆盆子运行mosquitto 。

我需要尝试与本地主机连接MQTT和检查连接正常与否还订阅和发布和阅读消息

+0

的,情报 我觉得mosquitto.php从这个链接 https://github.com/Nakard/mosquitto-stub/ blob/master/mosquitto.php 另外Iam使用此链接的代码pub.php https://github.com/mgdm/Mosquitto-PHP/blob/master/examples/pub.php –

+0

这是一个有点散漫,最好的问题涵盖一个单一的问题。如果你可以分解成不同的部分,你可能会做得更好 – hardillb

+0

谢谢你sir 问题是我尝试运行蚊子与PHP连接本地主机,可以发布和subsribe,对我来说,我尝试在覆盆子或窗户,但没有找到问题, 我需要用php运行蚊子,并可以连接localhost和检查连接是否可以订阅和发布和阅读消息 –

回答

1

此信息覆盆子与PHP安装mosquittolib

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 

,并在/ etc/PHP5/MODS的可用/ mosquitto。INI

添加以下代码

extension=mosquitto.so 

启用此代码为Mosquitto库

dpkg -l | grep mosquito 

和最终的检查版本,使用sudo php5enmod mosquitto

sudo php5enmod mosquitto 
echo "<?php phpinfo(); ?>" > ~/tester.php 
php ~/tester.php 

代码酒馆.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"; 
} 

代码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"; 
} 

test sub.php and pub.php

0

错误启动代理是因为mosquitto已经作为服务运行时。如果你想以详细模式在前台运行它,你必须先停下来。

在TRaspberry Pi上运行以下命令来停止服务。

sudo service stop mosquitto 

mosquitto_pub的错误意味着您按mosquitto-h localhost...

PHP的问题,应提高作为一个单独的问题之间的回报。

+0

谢谢先生,我尝试写命令sudo服务停止蚊子,并且再次尝试测试和相同的问题 –

+0

可以使用此信息或这个旧的 https://rasspberrypi.wordpress.com/2012/09/16/mosquitto-mqtt-on-raspberry-pi-broker-publish-and -subscribe-client/ –

0

现在是工作 在安装此命令在这个环节 http://mosquitto.org/2013/01/mosquitto-debian-repository/

也是用这个命令测试

$ mosquitto_sub -h 192.168.1.2 -d -t hello/world 

安装mosquitto的客户端

sudo apt-get install mosquitto-clients 

那荫后

$ mosquitto_pub -h 192.168.1.2 -d -t hello/world -m "Message To Send" 

其中192.168.1。2是你的树莓派的IP地址 请访问以下链接 https://rasspberrypi.wordpress.com/2012/09/16/mosquitto-mqtt-on-raspberry-pi-broker-publish-and-subscribe-client/

PI @树莓派:〜$ mosquitto_sub -h 210.201.4.114 -d -t你好/世界

客户mosqsub/1796-raspberryp发送CONNECT

客户mosqsub/1796-raspberryp接收CONNACK

客户mosqsub/1796-raspberryp发送SUBSCRIBE(中:1,主题:你好/世界,服务质量:0)

客户mosqsub/1796-raspberryp接收SUBACK 订阅(中间:1):0

客户mosqsub/1796-raspberryp发送PINGREQ

客户mosqsub/1796-raspberryp接收PINGRESP

客户mosqsub/1796 -raspberryp发送PINGREQ

客户mosqsub /接收PINGRESP

1796-raspberryp

客户mosqsub/1796-raspberryp收到PUBLISH(D0,Q0,R0,M0,'你好/世界,...(1 5个字节))

消息以发送

客户mosqsub/1796-raspberryp发送PINGREQ

客户mosqsub/1796-raspberryp接收PINGRESP

=========== =================================

pi @ raspberrypi:〜$ mosquitto_pub -h 210.201.4.114 - d -t hello/world -m“要发送的消息”

客户端mosqp UB/1800-raspberryp发送CONNECT

客户mosqpub/1800-raspberryp收到CONNACK

客户mosqpub/1800-raspberryp发送PUBLISH(D0,Q0,R0,M1,'你好/世界,...( 15个字节))

客户mosqpub/1800-raspberryp发送断开

0

现在我需要测试代码pub.php和sub.php怎么能这样做

我把sup.php & pub.php在/ var/www/html

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('/#', 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"; 
} 

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('/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"; 
} 
+0

我写这个命令 pi @ raspberrypi:/ var/www/html/t $ php pub.php ,结果是PHP警告:PHP启动:无法加载动态库'/usr/lib/php5/20131226/mosquitto.so ' - /usr/lib/php5/20131226/mosquitto.so:无法打开共享对象文件:没有这样的文件或目录在未知的行0 PHP致命错误:类'Mosquitto \ Client'找不到/ var/www/HTML /吨/ pub.php上线3 –

+0

现在是工作, 1-写入在写入PHP sub.php anthor开窗口之后,命令cd的/ var/www/html等 2-和也写 1 - 写入命令cd/var/www/html 2-之后写php php.php –