2016-05-23 67 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 –