描述你需要的功能
安卓物联网开发 手机远程控制
为什么你需要这个功能
物联网开发硬件
这个是我的代码,我把官方链接方进去,应该放哪里啊,放了就报错。代码新建的如下
package com.example.xinmqtt
import org.eclipse.paho.android.service.MqttAndroidClient
import android.content.Context
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets →
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
println("aaa")
}
private lateinit var mqttClient: MqttAndroidClient
// TAG
companion object {
const val TAG = "AndroidMqttClient"
}
}
官方的是
fun connect(context: Context) {
val serverURI = “tcp://broker.emqx.io:1883”
val username = “emqx”
val password = “public”
mqttClient = MqttAndroidClient(context, serverURI, "kotlin_client")
mqttClient.setCallback(object : MqttCallback {
override fun messageArrived(topic: String?, message: MqttMessage?) {
Log.d(TAG, "Receive message: ${message.toString()} from topic: $topic")
}
override fun connectionLost(cause: Throwable?) {
Log.d(TAG, "Connection lost ${cause.toString()}")
}
override fun deliveryComplete(token: IMqttDeliveryToken?) {
}
})
val options = MqttConnectOptions()
options.userName = username
options.password = password.toCharArray()
try {
mqttClient.connect(options, null, object : IMqttActionListener {
override fun onSuccess(asyncActionToken: IMqttToken?) {
Log.d(TAG, "Connection success")
}
override fun onFailure(asyncActionToken: IMqttToken?, exception: Throwable?) {
Log.d(TAG, "Connection failure")
}
})
} catch (e: MqttException) {
e.printStackTrace()
}
}
然后我的参数python可以运行的import random
import time
from PySide6.QtCore import QTimer
from PySide6.QtGui import QPixmap
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QTextBrowser, QLineEdit, QFrame,
QVBoxLayout, QWidget, QMessageBox
from window_capture import capture_window
from ultralytics import YOLO
import os
import playsound
import threading
from playsound import playsound
import random
import pygame
从 paho.mqtt.client 模块导入 MQTT 客户端类
from paho.mqtt import client as mqtt_client
MQTT 服务器的地址
broker = ‘i02f291f.ala.dedicated.tencent.emqxcloud.cn’
MQTT 服务器的端口号
port = 1883
发布消息的主题
topic = ‘demo/a’
生成带有 ‘pub’ 前缀的随机客户端 ID
client_id = ‘mqttx_6bba46b6’
MQTT 用户的用户名
username = ‘on’
MQTT 用户的密码
password = ‘123’
class MainWindow(QMainWindow):
def init(self):
super().init()
self.setObjectName(“mainWindow”)
self.move(120, 0)
self.resize(1105, 900)
self.setMinimumSize(110, 183)
self.setMaximumSize(2805, 2830)
# Central widget
self.centralwidget = QWidget(self)
self.centralwidget.setEnabled(True)
self.setCentralWidget(self.centralwidget)
# 以下是各种界面部件的初始化,与原代码类似,此处省略详细注释
self.label_2 = QLabel(self.centralwidget)
self.label_2.setGeometry(710, 0, 171, 51)
self.label_2.setStyleSheet("background-color: rgb(255, 0, 0);\nfont: 24pt \"楷体\";")
self.label_2.setText("运行日志")
self.textBrowser = QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(740, 60, 311, 151)
self.pushButton = QPushButton(self.centralwidget)
self.pushButton.setGeometry(640, 150, 81, 31)
self.pushButton.setText("后台截屏测试")
self.pushButton.clicked.connect(self.check_jt)
self.label_4 = QLabel(self.centralwidget)
self.label_4.setGeometry(0, 390, 1071, 20)
self.label_4.setText(
"---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------")
self.lineEdit_ts = QLineEdit(self.centralwidget)
self.lineEdit_ts.setGeometry(210, 220, 421, 31)
self.lineEdit_ts.setText(r'C:\4.jpg')
self.lineEdit_4 = QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(210, 150, 411, 31)
self.lineEdit_4.setText(r'C:\window_screenshot.png')
self.lineEdit_ck = QLineEdit(self.centralwidget)
self.lineEdit_ck.setGeometry(130, 90, 321, 31)
self.frame = QFrame(self.centralwidget)
self.frame.setGeometry(20, 430, 1105, 1830)
self.frame.setFrameShape(QFrame.StyledPanel)
self.frame.setFrameShadow(QFrame.Raised)
self.label_5 = QLabel(self.frame)
self.label_5.setGeometry(20, 0, 1105, 1830)
self.label_5.setStyleSheet("background-color: rgb(255, 0, 0);\nfont: 12pt \"楷体\";")
self.label_5.setText("实时帧")
self.image_label = QLabel(self.frame)
self.closebtn = QPushButton(self.frame)
self.closebtn.setGeometry(830, 320, 151, 41)
self.closebtn.setStyleSheet(
"background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 0, 209, 255), stop:1 rgba(255, 255, 255, 255));\nfont: 12pt \"楷体\";")
self.closebtn.setFlat(False)
self.closebtn.setText("关闭窗口")
self.closebtn.clicked.connect(self.close)
self.yuanqiubtn_2 = QPushButton(self.centralwidget)
self.yuanqiubtn_2.setEnabled(True)
self.yuanqiubtn_2.setGeometry(650, 220, 201, 31)
self.yuanqiubtn_2.setStyleSheet(
"background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 225, 0, 255), stop:1 rgba(255, 255, 255, 255));\nbackground-color: rgb(85, 255, 127);\nfont: 18pt \"楷体\";")
self.yuanqiubtn_2.setText("开灯")
self.yuanqiubtn_2.clicked.connect(self.ts)
self.label_6 = QLabel(self.centralwidget)
self.label_6.setGeometry(20, 0, 1100, 51)
self.label_6.setStyleSheet(
"background-color: rgb(170, 255, 255);\nfont: 12pt \"楷体\";\ncolor: rgb(255, 0, 0);")
self.label_6.setText(
"多窗口多模式 YOLOV11(不同的 pt 运行不同模式只需要选择不同模型即可)***一个窗口一个句柄支持无限多开监控 带*的都必须填写 其他可以不填")
self.lineEdit_6 = QLineEdit(self.centralwidget)
self.lineEdit_6.setGeometry(210, 290, 421, 31)
self.lineEdit_6.setText(r'C:\best.pt')
self.JBlabel_2 = QLabel(self.centralwidget)
self.JBlabel_2.setGeometry(10, 330, 198, 60)
self.JBlabel_2.setText("MP3路径【报警铃声*不能带中文拼音都行】")
self.lineEdit_mp3 = QLineEdit(self.centralwidget)
self.lineEdit_mp3.setGeometry(200, 350, 421, 31)
self.lineEdit_mp3.setText(r'C:\mp3.mp3')
self.lineEdit_time = QLineEdit(self.centralwidget)
self.lineEdit_time.setGeometry(600, 450, 61, 31)
self.lineEdit_time.setText("10000")
self.yuanqiubtn_3 = QPushButton(self.centralwidget)
self.yuanqiubtn_3.setEnabled(True)
self.yuanqiubtn_3.setGeometry(650, 350, 131, 41)
self.yuanqiubtn_3.setStyleSheet(
"background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 225, 0, 255), stop:1 rgba(255, 255, 255, 255));\nbackground-color: rgb(255, 85, 255);\nfont: 22pt \"楷体\";")
self.yuanqiubtn_3.setText("关灯")
self.yuanqiubtn_3.clicked.connect(self.off)
self.widget = QWidget(self.centralwidget)
self.widget.setGeometry(13, 70, 200, 261)
self.verticalLayout = QVBoxLayout(self.widget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.JBlabel = QLabel(self.widget)
self.JBlabel.setText("*窗口句柄")
self.verticalLayout.addWidget(self.JBlabel)
self.HTlabel = QLabel(self.widget)
self.HTlabel.setText("【不要填写 自动会获取】截图")
self.verticalLayout.addWidget(self.HTlabel)
self.PTlabel_2 = QLabel(self.widget)
self.PTlabel_2.setText("单独图片测试用【工程师调试用】")
self.verticalLayout.addWidget(self.PTlabel_2)
self.PTlabel = QLabel(self.widget)
self.PTlabel.setText("*PT 模型【填写路径即可】")
self.verticalLayout.addWidget(self.PTlabel)
self.yuanqiubtn = QPushButton(self.centralwidget)
self.yuanqiubtn.setEnabled(True)
self.yuanqiubtn.setGeometry(820, 280, 211, 101)
self.yuanqiubtn.setStyleSheet(
"background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 225, 0, 255), stop:1 rgba(255, 255, 255, 255));\nbackground-color: rgb(255, 85, 127);\nfont: 22pt \"楷体\";")
self.yuanqiubtn.setText("运行监控")
self.yuanqiubtn.clicked.connect(self.main)
# 创建并保存共用的MQTT客户端实例
self.mqtt_client = connect_mqtt()
def check_jt(self):
"""后台截屏测试按钮点击事件对应的函数,这里暂时只是简单打印,可以根据实际需求完善功能"""
print("执行后台截屏测试")
def ts(self):
"""开灯按钮点击事件对应的函数,这里暂时只是简单打印,可以根据实际需求完善功能"""
print("执行开灯操作")
def off(self):
"""关灯按钮点击事件对应的函数,调用发布消息函数发送关灯对应的消息"""
print("关灯")
publish_off(self.mqtt_client)
def main(self):
"""运行监控按钮点击事件对应的函数,这里暂时只是简单打印,可以根据实际需求完善功能"""
print("执行运行监控操作")
定义连接 MQTT 服务器的函数
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
print(“Connected to MQTT Broker!”)
else:
print(“Failed to connect, return code %d\n”, rc)
client = mqtt_client.Client()
# client.tls_set(ca_certs='./server-ca.crt')
client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client
定义发布消息的函数
def publish_off(client):
# 初始化消息计数器
msg = 0
# 发布消息到指定的主题
result = client.publish(topic, msg)
# result 包含状态码和消息 ID
status = result[0]
# 如果状态码为0,表示消息发送成功
if status == 0:
print(f"Send {msg}
to topic {topic}
“)
print(“发送成功”)
else:
# 打印发送失败的消息
print(f"Failed to send message to topic {topic}”)
print(“发送失败”)
定义运行脚本的函数
def run():
client = connect_mqtt()
client.loop_start()
publish_off(client)
if name == ‘main’:
app = QApplication()
window = MainWindow()
window.setWindowTitle(“软件硬件开视觉开发 linux AI 视觉 PLC 视觉检测 OCR 识别提取 OpenCV YoloV11 开发 13760473654”)
window.show()
app.exec()
run()