Compare commits

...

11 Commits
v1.0 ... main

Author SHA1 Message Date
zart 31f36c3a33 build(smp_init.sh): 添加 fswebcam 安装步骤并增强脚本功能
2 months ago
zart a3e8e681a3 fix(smp_coder):拍照脚本优化
2 months ago
zart f742b1fbcc fix(smp_init.sh):增加重启开机重载服务的操作
2 months ago
zart 23da3939ef service: 更新 download_and_execute.service配置
2 months ago
zart cc5bf0d18c 改进脚本
2 months ago
zy 347cd2ba6b build(smp_coder): 更新相机脚本使用 fswebcam捕获图像
2 months ago
zart 5367daabc8 改进脚本
3 months ago
zart 6ec8f0b06e 改进脚本
3 months ago
zart 8c85f4b2da 改进脚本
3 months ago
zart 1b7f4c0ad7 改进脚本
3 months ago
zart 88a175c7be 添加 重启 重载 服务
3 months ago

@ -0,0 +1,11 @@
#!/bin/bash
# 转移文件
# 定义变量
cp ../smp_pc_20240625/download_and_execute.service ./
cp ../smp_pc_20240625/smp_init.sh ./
cp ../smp_pc_20240625/test_reload.sh ./

@ -0,0 +1,13 @@
#!/usr/bin
sudo systemctl status download_and_execute.service
sudo systemctl stop download_and_execute.service
sudo systemctl disable download_and_execute.service
sudo rm /etc/systemd/system/download_and_execute.service
sudo systemctl daemon-reload
echo "Service deleted successfully!"

@ -0,0 +1,14 @@
[Unit]
Description=Run download_and_execute script at startup
After=network.target
[Service]
ExecStart=bash test_reload.sh
Type=simple
WorkingDirectory=/home/pi/smp_pc
Restart=on-failure
RestartSec=10s
RemainAfterExit=true
[Install]
WantedBy=multi-user.target

@ -82,12 +82,10 @@ fi
# 进入仓库目录
cd $local_dir || { echo "无法进入目录 $local_dir"; exit 1; }
# 检查smp_init.sh是否存在
if [ -f "smp_init.sh" ]; then
# 如果没有执行权限,尝试添加执行权限

@ -61,7 +61,7 @@ client.loop_start()
response_received = False
while not response_received:
print("等待响应...")
client.publish("request/{}".format(user_id), "image produce", qos=2)
client.publish("request/{}".format(user_id), "image produce", qos=0)
time.sleep(3)
# 停止网络循环并断开连接

@ -2,11 +2,14 @@ import paho.mqtt.client as mqtt
import time
import os
# 检查是否存在edu_coder/user.txt文件如果不存在则执行get_user.sh脚本
if not os.path.exists("edu_coder/user.txt"):
os.system("cd edu_coder && bash get_user.sh")
# os.system("bash edu_coder/get_user.sh")
# MQTT代理地址
broker = '121.40.129.71'
# MQTT代理端口
port = 48835
# 回调函数,当客户端收到连接响应时被调用
@ -19,6 +22,7 @@ def on_connect(client, userdata, flags, rc, properties=None):
def on_message(client, userdata, msg):
print("收到消息: " + msg.topic + " " + str(msg.payload))
# 如果收到的消息是"Set user.txt success!"则设置response_received为True
if msg.payload.decode() == "Set user.txt success!":
global response_received
response_received = True
@ -39,7 +43,8 @@ client.connect(broker, port, 60)
# 启动一个后台线程来处理网络流量和回调
client.loop_start()
with open("edu_coder/user.txt", "r") as f:
# 读取edu_coder/user.txt文件中的用户信息
with open("edu_coder/user.txt", "r", encoding="utf8") as f:
user = f.read().strip()
# # 发布消息
@ -49,7 +54,7 @@ with open("edu_coder/user.txt", "r") as f:
response_received = False
while not response_received:
# 发布消息
client.publish("request/topic", user, qos=2)
client.publish("request/topic", user, qos=0)
print("等待响应...")
time.sleep(1)
@ -57,4 +62,4 @@ while not response_received:
client.loop_stop()
client.disconnect()
print("收到响应,客户端退出。")
print("收到响应,客户端退出。")

@ -1,3 +1,16 @@
#!/usr/bin/bash
libcamera-still -o /tmp/image.jpg --timeout 10
IMAGE_PATH="/tmp/image.jpg"
if [ -f "$IMAGE_PATH" ]; then
echo "Image already exists at $IMAGE_PATH"
if sudo rm "$IMAGE_PATH"; then
echo "Successfully deleted $IMAGE_PATH"
else
echo "Failed to delete $IMAGE_PATH"
exit 1
fi
fi
fswebcam -r 1280x720 --no-banner -S 20 /tmp/image.jpg

@ -13,6 +13,8 @@ with open("smp_coder/user.txt", "r") as f:
user_id = f.read().strip()
user_id = user_id.split("=")[-1]
print("user_id: ", user_id)
# topic = "request/{}".format(user_id)
# 回调函数,当客户端收到连接响应时被调用
@ -21,6 +23,8 @@ def on_connect(client, userdata, flags, rc, properties=None):
# 订阅请求主题
client.subscribe("request/{}".format(user_id), qos=2)
print("订阅请求主题: request/{}".format(user_id))
# 回调函数,当客户端收到消息时被调用
def on_message(client, userdata, msg):
print("收到消息: " + msg.topic + " " + str(msg.payload))
@ -63,11 +67,11 @@ while not response_received:
time.sleep(2)
# 停止网络循环并断开连接
client.loop_stop()
client.disconnect()
# # 停止网络循环并断开连接
# client.loop_stop()
# client.disconnect()
print("收到响应,客户端退出。")
# print("收到响应,客户端退出。")

@ -55,7 +55,8 @@ client.publish("response/topic", "Set user.txt success!", qos=2)
command = ["sudo", "myenv/bin/python3", "smp_coder/extur_smp.py"]
# command = ["sudo", "/home/pi/smp_pc/myenv/bin/python", "/home/pi/smp_pc/smp_coder/extur_smp.py"]
command = ["sudo", "myenv/bin/python", "smp_coder/extur_smp.py"]
with open("logex.txt", "w") as log_file:
process = subprocess.Popen(command, stdout=log_file, stderr=log_file, bufsize=1, universal_newlines=True)

@ -1,10 +1,34 @@
#!/bin/bash
# 检查test_reload.sh是否存在
if [ -f "test_reload.sh" ]; then
# 如果没有执行权限,尝试添加执行权限
if [ ! -x "test_reload.sh" ]; then
echo "test_reload.sh 没有执行权限,尝试添加执行权限..."
chmod +x "test_reload.sh" || { echo "无法添加执行权限"; exit 1; }
fi
fi
echo "test_reload.sh 存在,并具有执行权限"
#!/bin/bash
# 安装 fswebcam
# 检查是否已经安装
if ! command -v fswebcam &> /dev/null; then
echo "fswebcam 未安装,正在安装..."
sudo apt-get update
sudo apt-get install -y fswebcam
if [ $? -eq 0 ]; then
echo "fswebcam 安装成功"
else
echo "fswebcam 安装失败"
exit 1
fi
else
echo "fswebcam 已安装"
fi
# 创建一个目录用于存放虚拟环境
env_dir="myenv"
@ -28,6 +52,8 @@ else
exit 1
fi
# 安装 paho-mqtt 库
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple paho-mqtt==2.1.0
@ -41,15 +67,29 @@ fi
# 提示如何退出虚拟环境
echo "要退出虚拟环境,请运行 'deactivate'"
# 检查服务是否已启用
if sudo systemctl is-enabled download_and_execute.service; then
echo "开机重载服务已启动"
echo "正在重启开机重载服务"
sudo systemctl restart download_and_execute.service || { echo "无法重启服务"; exit 1; }
else
echo "开机重载服务未启动"
echo "正在启动开机重载服务"
sudo cp download_and_execute.service /etc/systemd/system || { echo "无法复制文件"; exit 1; }
sudo systemctl enable download_and_execute.service || { echo "无法启用服务"; exit 1; }
sudo systemctl start download_and_execute.service || { echo "无法启动服务"; exit 1; }
fi
# 运行第一关的mqtt_client.py
# 运行第一关的mqtt_client.py
nohup sudo $env_dir/bin/python3 smp_coder/mqtt_smp.py > log.txt 2>&1 &
if [ $? -eq 0 ]; then
echo "mqtt_smp.py 启动成功"
else
echo "mqtt_smp.py 启动失败"
fi
#nohup sudo $env_dir/bin/python3 smp_coder/mqtt_smp.py > log.txt 2>&1 &
#if [ $? -eq 0 ]; then
# echo "mqtt_smp.py 启动成功"
#else
# echo "mqtt_smp.py 启动失败"
#fi
# 运行第二关的extur_smp.py
#nohup sudo $env_dir/bin/python3 smp_coder/extur_smp.py > logex.txt 2>&1 &

@ -0,0 +1,22 @@
#!/bin/bash
cd /home/pi/smp_pc|| exit
# 检测某个文件是否存在
if [ -f smp_coder/user.txt ]; then
echo "User file exists"
else
sudo myenv/bin/python smp_coder/mqtt_smp.py
fi
if [ $? -eq 0 ]; then
echo "MQTT SMP is running"
else
echo "MQTT SMP is not running"
exit 1
fi
sudo myenv/bin/python smp_coder/extur_smp.py
Loading…
Cancel
Save