Mosquitto-PHP 覆盆子库和 windows

Mosquitto-PHP Library in raspberry and windows

我使用 Mosquitto 安装 PHP MQTT 客户端-PHP

树莓派中的图书馆 mosquitto php library/

之后在 /var/www/html/mqtt 中创建 pub.php 当在浏览器中写入时 http://xxx.xxx.xxx.xx/mqtt/pup.php 并且看不到任何数据

然后我尝试了

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

并得到

bash: -h: command not found

不知道问题出在哪里,在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

我的 windows 我也有同样的问题 我安装了 mosquitto,看这个 link step-by-step-installing-and-configuring-mosquitto-with-windows-7

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

我搜索并找到 mosquitto.php 并把我把这段代码放在 pub.php

require "mosquitto.php";

我不知道问题出在哪里,谁能帮我在我的windows和覆盆子里运行蚊子。

我需要尝试与本地主机连接的 mqtt 并检查连接是否正常以及订阅、发布和阅读消息

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

运行以下在TRAspberry Pi上停止服务

sudo service stop mosquitto 

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

之间按 return

PHP 个问题应作为单独的问题提出。

现在上班 在此 link 中安装此命令 http://mosquitto.org/2013/01/mosquitto-debian-repository/

同时安装 mosquitto-clients

sudo apt-get install mosquitto-clients

之后我将使用此命令进行测试

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

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

其中 192.168.1.2 是您 Raspberry Pi 的 IP 地址 请看这个link https://rasspberrypi.wordpress.com/2012/09/16/mosquitto-mqtt-on-raspberry-pi-broker-publish-and-subscribe-client/

pi@raspberrypi:~ $ mosquitto_sub -h 210.201.4.114 -d -t hello/world

客户端mosqsub/1796-raspberryp发送连接

客户端 mosqsub/1796-raspberryp 收到 CONNACK

客户端 mosqsub/1796-raspberryp 正在发送订阅(中间:1,主题:hello/world,QoS:0)

客户端 mosqsub/1796-raspberryp 收到 SUBACK 已订阅(中:1):0

客户端 mosqsub/1796-raspberryp 发送 PINGREQ

客户端 mosqsub/1796-raspberryp 收到 PINGRESP

客户端 mosqsub/1796-raspberryp 发送 PINGREQ

客户端 mosqsub/1796-raspberryp 收到 PINGRESP

客户端 mosqsub/1796-raspberryp 收到 PUBLISH(d0、q0、r0、m0、'hello/world'、...(15 字节))

要发送的消息

客户端 mosqsub/1796-raspberryp 发送 PINGREQ

客户端 mosqsub/1796-raspberryp 收到 PINGRESP

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

pi@raspberrypi:~ $ mosquitto_pub -h 210.201.4.114 -d -t hello/world -m "Message To Send"

客户端 mosqpub/1800-raspberryp 发送 CONNECT

客户端 mosqpub/1800-raspberryp 收到 CONNACK

客户端 mosqpub/1800-raspberryp 发送 PUBLISH(d0、q0、r0、m1、'hello/world'、...(15 字节))

客户端 mosqpub/1800-raspberryp 发送 DISCONNECT

现在我需要测试代码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";
}

此信息用于在树莓派 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-available/mosquitto.ini

添加此代码

extension=mosquitto.so

此代码用于检查 Mosquitto 库的版本

dpkg -l | grep mosquito

最后用 sudo php5enmod mosquitto

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

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

代码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