当前位置: 首页 > news >正文

php 对接mqtt 完整版本,订阅消息,发送消息

首先打开链接如何在 PHP 项目中使用 MQTT

根据文章让所用依赖安装一下: composer require php-mqtt/client

安装之后弄一个部署
在这里插入图片描述
之后在工具里边可以相应链接上
在这里插入图片描述
接下来是代码:

 /***
     * 订阅消息
     * @return void
     * @throws \PhpMqtt\Client\Exceptions\ConfigurationInvalidException
     * @throws \PhpMqtt\Client\Exceptions\ConnectingToBrokerFailedException
     * @throws \PhpMqtt\Client\Exceptions\DataTransferException
     * @throws \PhpMqtt\Client\Exceptions\InvalidMessageException
     * @throws \PhpMqtt\Client\Exceptions\MqttClientException
     * @throws \PhpMqtt\Client\Exceptions\ProtocolNotSupportedException
     * @throws \PhpMqtt\Client\Exceptions\ProtocolViolationException
     * @throws \PhpMqtt\Client\Exceptions\RepositoryException
     */
    public function dingyue()
    {
        $server = '*********.aliyun.emqxcloud.cn';//连接地址
        $port = 1883;
        $clientId = rand(5, 15);
        $username = '用户名';
        $password = '密码';
        $clean_session = false;
        $mqtt_version = MqttClient::MQTT_3_1_1;

        $connectionSettings = (new ConnectionSettings)
            ->setUsername($username)
            ->setPassword($password)
            ->setKeepAliveInterval(60)
            ->setLastWillTopic('emqx/test/last-will')
            ->setLastWillMessage('client disconnect')
            ->setLastWillQualityOfService(1);


        $mqtt = new MqttClient($server, $port, $clientId, $mqtt_version);

        $mqtt->connect($connectionSettings, $clean_session);
        //订阅消息

//      下边是轮询订阅,订阅多个设备

//        查数据库中设备列表
        $list = DeviceBall::where(['topic'=>['<>','']])->field('topic')->select();
//        获取topic
        $list = array_column($list, 'topic');
        //去重
        $list = array_unique($list);
//        dump($list);exit;
        foreach ($list as $i) {
            $mqtt->subscribe('CIOT/2000/DEVICE/' . $i, function ($topic,
                $message) use ($mqtt, $i) {
                printf("Received message on topic [%s]: %s\n", $topic, $message);
                $message = json_decode($message, true);
                if (isset($message['Body']['Info']['Msg']['Pass'])) {
                    if ($message['Body']['Info']['Msg']['Pass'] == 1) {
//                        关闭订单
                        $ball_id = DeviceBall::where(['topic' => $i])->find();
                        $order_no = BallOrder::where(['ball_id' => $ball_id['id']])->order('id DESC')->where('status', 1)->value('order_no');
                        $this->confirmorder($order_no);
                        //语音指令,提前关闭,电源关闭
                        $payload = array(
                            'Header' => [
                                "Code" => '1004',
                                "Time" => time() . '000',
                                "Sign" => "lvzaina"
                            ],
                            'Body' => [
                                "MsgId" => time(),
                                'Cmd' => "Voice",
                                'Index' => 41
                            ]
                        );
                        $mqtt->publish(
                        // topic
                            'CIOT/2000/HOST/' . $i,
                            // payload
                            json_encode($payload),
                            // qos
                            0,
                            // retain
                            true
                        );
                        //可以关闭锁球器
                        //发送关闭指令
                        $payload = array(
                            'Header' => [
                                "Code" => '1004',
                                "Time" => time() . '000',
                                "Sign" => "lvzaina"
                            ],
                            'Body' => [
                                "Mode" => "0",
                                "MsgId" => time(),
                                'Cmd' => "Order",
                                'ID' => 1,
                                'Delay' => 0,
                                'Type' => 0,
                                "DCL" => 0,
                            ]
                        );
                        $mqtt->publish(
                        // topic
                            'CIOT/2000/HOST/' . $i,
                            // payload
                            json_encode($payload),
                            // qos
                            0,
                            // retain
                            true
                        );

                    } else {
                        //异常,播报语音,不关闭锁球器,关锁失败请检车
                        $payload = array(
                            'Header' => [
                                "Code" => '1004',
                                "Time" => time() . '000',
                                "Sign" => "lvzaina"
                            ],
                            'Body' => [
                                "MsgId" => time(),
                                'Cmd' => "Voice",
                                'Index' => 45
                            ]
                        );
                        $mqtt->publish(
                        // topic
                            'CIOT/2000/HOST/' . $i,
                            // payload
                            json_encode($payload),
                            // qos
                            0,
                            // retain
                            true
                        );
                    }
                }
            }, 0);
        }

        $mqtt->loop(true);
    }

发送指令

static function publish()
    {
        $server = '********ted.aliyun.emqxcloud.cn';
        $port = 1883;
        $clientId = rand(5, 15);
        $username = '用户名';
        $password = '密码';
        $clean_session = false;
        $mqtt_version = MqttClient::MQTT_3_1_1;

        $connectionSettings = (new ConnectionSettings)
            ->setUsername($username)
            ->setPassword($password)
            ->setKeepAliveInterval(60)
            ->setLastWillTopic('emqx/test/last-will')
            ->setLastWillMessage('client disconnect')
            ->setLastWillQualityOfService(1);


        $mqtt = new MqttClient($server, $port, $clientId, $mqtt_version);

        $mqtt->connect($connectionSettings, $clean_session);
        $payload = array(
            'Header' => [
                "Code" => '1004',
                "Time" => time() . '000',
                "Sign" => "lvzaina"
            ],
            'Body' => [
                "Mode" => "0",
                "MsgId" => time(),
                'Cmd' => "Check",
            ]
        );
        $mqtt->publish(
        // topic
            'CIOT/2000/HOST/RE2BBEEC78',//推送的地址
            // payload
            json_encode($payload),
            // qos
            0,
            // retain
            true
        );
    }

上边是代码,下边事最重要的

1首先如果在本地测试的话,新建一个.bat文件,放在根目录
在这里插入图片描述
.bat文件内容,然后双击文件执行,这里执行的是上边订阅消息的方法,执行之后,可以在小黑窗里边看到打印的信息

chcp 65001
@echo off
:loop
E:\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe public/index.php api/Billiards/dingyue
ping -n 3 127.1 >nul
goto loop
pause

2,如果是在linux中执行

新建一个.sh文件
在这里插入图片描述
.sh文件内容

#!/bin/bash
while true
do
    # 这里是要执行的代码
    /www/server/php/72/bin/php public/index.php api/Billiards/dingyue
    # 这里是要执行的代码
    endDate=`date +"%Y-%m-%d %H:%M:%S"`
    echo "At [$endDate] Successful"
    echo "----------------------------------------------------------------------------"
    sleep 3
done

在这里插入图片描述

开启列队:运行队列的代码

nohup bash run-api.sh >> run-api.out &
然后敲回车敲两下
查看执行文件返回内容
tail -f run-api.out
在这里插入图片描述
执行前,要看下是否已经在运行:
ps aux | grep bash
在这里插入图片描述
如果已经在执行
结束命令:
kill -9 进程号(256300)

然后再执行ps aux | grep bash 看是否已经停止,重新启动即可。

这样一个.sh文件就启动了

相关文章:

  • vue实现根据点击或滑动展示对应高亮
  • 【Rust中级教程】2.10. API设计原则之受约束性(constrained) Pt.1:对类型进行修改、`#[non_exhaustive]`注解
  • QT中的事件
  • 基于Java+SpringBoot+Vue的前后端分离的租房网站
  • Shell基础
  • 2011-2019年各省人口数数据
  • vue3动态引入图片
  • 前端依赖nrm镜像管理工具
  • 软考程序员考试内容和备考策略
  • 补充:文件上传、下载传输给前端之直接传递图片二进制数据:网络中的图片、音频、视频等非字符数据的传输
  • 港科大提出开放全曲音乐生成基础模型YuE:可将歌词转换成完整歌曲
  • 每日Attention学习24——Strip Convolution Block
  • 嵌入式开发工程师笔试面试指南-Linux系统移植
  • 计算机组成与接口10
  • 深入探索C语言中的sscanf和sprintf函数
  • 【C++笔记】C++11智能指针的使用及其原理
  • 2025年SCI1区TOP:真菌生长优化算法FGO,深度解析+性能实测
  • java23种设计模式-观察者模式
  • Spring MVC框架六:Ajax技术
  • 用 DeepSeek 打样!KubeSphere LuBan 用 3 天/3 分钟“干掉”大模型部署焦虑
  • 智能终端出海服务创新联合体成立
  • 史学巨擘的思想地图与学术路径——王汎森解析梁启超、陈寅恪、傅斯年
  • 俄罗斯纪念卫国战争胜利80周年阅兵式首次彩排在莫斯科举行
  • 2024“好评中国”网络评论大赛结果揭晓
  • 贵州茅台一季度净利268亿元增长11.56%,系列酒营收增近两成
  • 深入贯彻中央八项规定精神学习教育中央指导组培训会议召开