basic dust collection control over mqtt
This commit is contained in:
commit
36685b9c53
2 changed files with 65 additions and 0 deletions
12
README.md
Normal file
12
README.md
Normal file
|
@ -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.
|
53
main.py
Normal file
53
main.py
Normal file
|
@ -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
|
Loading…
Reference in a new issue