最新消息:重新回归WordPress,我要比较认真的开始更新我的博客了。

klipper使用mqtt实现自动开关机

杂七杂八 hanlei 1668浏览

之前给klipper装了个继电器来控制打印机的开关机,但是我买的这个继电器好像是质量不太好,用了几天就不工作了。我用的是斐讯的dc1插板,可以通过mqtt控制开关。继电器出问题后一直用手动开关机,今天有空就想看看能不能通过GCODE执行脚本,然后直接通过mqtt控制插板开关。

在网上找了很长时间,什么有用的都没找到。于是只能自己想办法。因为有安装timelapse延时摄影插件,就想看看它是怎么调用的脚本程序,于是发现了action_call_remote_method,并且在moonraker的目录中看到一个mqtt.py的文件,然后到moonraker官网看文档,发现moonraker本来是支持mqtt。这下好办多了。

文档地址:https://moonraker.readthedocs.io/en/latest/configuration/#mqtt

下面简单说说实现流程:

首先在moonraker.conf中填加mqtt的服务器配置

# moonraker.conf

[mqtt]
address:
#   MQTT服务器地址
port:
#   端口号  Default is 1883.
username:
#   用户名
password:
#   密码
mqtt_protocol: v3.1.1
#   协议版本
enable_moonraker_api: False
#   这应该是通过mqtt控制打印机
instance_name:
#   mqtt主机名?
#status_objects:
#   不知道有什么用
default_qos: 0
#   The default QOS level used when publishing or subscribing to topics.
#   Must be an integer value from 0 to 2.  The default is 0.
#api_qos:
#   The QOS level to use for the API topics. If not provided, the
#   value specified by "default_qos" will be used.

再将原来的M80,M81宏命令修改成调用自带mqtt函数

[gcode_macro M80]
gcode:
  {action_call_remote_method("publish_mqtt_topic",
                             topic="dc1/dc1_000000/cmnd/POWER4",
                             payload="ON",
                             qos=0)}
#gcode: SET_PIN PIN=ATX VALUE=1  #开机M80
[gcode_macro M81]
gcode:
  {action_call_remote_method("publish_mqtt_topic",
                             topic="dc1/dc1_000000/cmnd/POWER4",
                             payload="OFF",
                             qos=0)}
#gcode: SET_PIN PIN=ATX VALUE=0  #关机M81

就这么简单就完成了。

转载请注明:HANLEI'BLOG » klipper使用mqtt实现自动开关机