From 36685b9c53560350d38168976462c03713743217 Mon Sep 17 00:00:00 2001 From: "Morgan 'ARR\\!' Allen" Date: Tue, 14 Feb 2023 17:29:30 -0800 Subject: [PATCH] basic dust collection control over mqtt --- README.md | 12 ++++++++++++ main.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 README.md create mode 100644 main.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..8b09128 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# LinuxCNC and Tasmota Automatic Dust Control System + +Basic Python script that uses LinuxCNCs built in Python module. +It is expected to run this script on the same machine running LinuxCNC + +## Prerequisites + +``` +pip install paho-mqtt +``` + +Editing the variables at the top of the script should make it somewhat flexible. diff --git a/main.py b/main.py new file mode 100644 index 0000000..0940024 --- /dev/null +++ b/main.py @@ -0,0 +1,53 @@ +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