From 712fddeec8991b21884a972411b1c6f91675e7a3 Mon Sep 17 00:00:00 2001 From: "Morgan 'ARR\\!' Allen" Date: Wed, 19 Apr 2023 13:24:37 -0700 Subject: [PATCH] linuxcnc > mqtt and mqtt > pixelblaze scripts --- main.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ pb_proxy.py | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 main.py create mode 100644 pb_proxy.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..96037d4 --- /dev/null +++ b/main.py @@ -0,0 +1,54 @@ +import linuxcnc +import time +import paho.mqtt.client as mqtt + +lcnc_stat = linuxcnc.stat() + +MQTT_HOST='192.168.1.1' +MQTT_PORT=1883 +TOPIC='lcnc/job/progress' + +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): + print('mqtt connected') + +client = mqtt.Client() +client.on_connect = on_connect + +client.connect_async(MQTT_HOST, MQTT_PORT, 60) +client.loop_start() + +print("Starting main loop") + +time.sleep(1) + +filename = None +last_line = None +global lc +state = { + 'lc': float(0) +} + +while True: + lcnc_stat.poll() + + if filename is None or filename != lcnc_stat.file: + print("Updating file: {}".format(lcnc_stat.file)) + filename = lcnc_stat.file + + f = open(filename, 'r') + state['lc'] = sum(1 for line in f) + print("line count: {}".format(state['lc'])) + + if last_line is None or last_line != lcnc_stat.motion_line: + completion = int(lcnc_stat.motion_line) / float(state['lc']) + print(state['lc'], lcnc_stat.motion_line, completion) + print("Progress: {}%".format(completion * 100)) + last_line = lcnc_stat.motion_line + + print(type(completion), str(completion)) + client.publish(TOPIC, str(completion)) diff --git a/pb_proxy.py b/pb_proxy.py new file mode 100644 index 0000000..c88cc4b --- /dev/null +++ b/pb_proxy.py @@ -0,0 +1,33 @@ +from pixelblaze import * +import paho.mqtt.client as mqtt + +MQTT_HOST='192.168.1.1' +MQTT_PORT=1883 +TOPIC = 'lcnc/job/progress' + +PB_HOST = '192.168.0.161' +pb = Pixelblaze(PB_HOST) + +def on_connect(client, userdata, flags, rc): + print('MQTT Connected') + + try: + client.subscribe(TOPIC) + except: pass + +# The callback for when a PUBLISH message is received from the server. +def on_message(client, userdata, msg): + p = float(msg.payload.decode('utf-8')) + pb.setVars({ + 'progress': p + }) + +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: pass