mqttwarn + pushplus:MQTT 消息转成微信通知
效果:订阅的 MQTT 主题有新报文时,mqttwarn 转调 pushplus,微信收到告警。
前置条件
- 一个 pushplus token(官网扫码获取)
- 已运行的 mqttwarn,且能访问
www.pushplus.plus
配置步骤
推荐用 mqttwarn 的 Apprise 服务,少写代码。在 mqttwarn.ini:
[defaults]
hostname = localhost
port = 1883
functions = 'usrfunc.py'
[config:apprise]
targets = {
'wx': [ 'pushplus://你的token' ]
}
[sensors/alert]
targets = apprise:wx
title = mqttwarn 告警
format = {payload}
没有 Apprise 插件时,用自定义函数 POST JSON。usrfunc.py:
import requests
def send_pushplus(srv, item):
requests.post(
"https://www.pushplus.plus/send",
json={
"token": "你的token",
"title": item.get("title") or "mqttwarn",
"content": item.get("message") or str(item.payload),
"template": "txt",
},
timeout=10,
)
return True
对应 topic 段写 targets = send_pushplus:,按你实际的 functions 挂载方式调整。
token 放环境变量或本地 ini,不要提交到仓库。
验证
curl -X POST "https://www.pushplus.plus/send" \
-H "Content-Type: application/json" \
-d '{"token":"你的token","title":"mqttwarn 配置测试","content":"token 可用","template":"markdown"}'
再用 mosquitto_pub -t sensors/alert -m "温度过高" 发一条,微信应收到。
常见问题
主题有消息但没推送? 看 mqttwarn 日志是否匹配到 section;确认 broker 地址、订阅通配符和 targets 写对。
通知太勤? 在 mqttwarn 侧加 filter / 去重,或只订阅告警主题,不要订阅全量遥测。