linuxcnc-mqtt-dust-control/main.py

54 lines
1.2 KiB
Python

import linuxcnc
import time
import paho.mqtt.client as mqtt
lcnc_stat = linuxcnc.stat()
MQTT_HOST='192.168.1.1'
MQTT_PORT=1883
CMND_TOPIC='cmnd/tasmota_E74A79/POWER'
MSG_ON='ON'
MSG_OFF='OFF'
#STAT_TOPIC='stat/tasmota_E74A79/POWER'
DELAY = 5
we_started = False
running = False
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
try:
client.subscribe(STAT_TOPIC)
except: pass
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
s = str(msg.payload)
print(s)
if s == MSG_ON:
running = True
elif s == MSG_ON:
running = False
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect_async(MQTT_HOST, MQTT_PORT, 60)
client.loop_start()
while True:
lcnc_stat.poll()
#print(lcnc_stat.spindle[0])
#continue
if lcnc_stat.spindle[0]['enabled'] == 1 and not running and not we_started:
client.publish(CMND_TOPIC, MSG_ON)
we_started = True
elif lcnc_stat.spindle[0]['enabled'] == 0 and we_started == True:
time.sleep(DELAY)
client.publish(CMND_TOPIC, MSG_OFF)
we_started = False