linuxcnc > mqtt and mqtt > pixelblaze scripts
This commit is contained in:
commit
712fddeec8
2 changed files with 87 additions and 0 deletions
54
main.py
Normal file
54
main.py
Normal file
|
@ -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))
|
33
pb_proxy.py
Normal file
33
pb_proxy.py
Normal file
|
@ -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
|
Loading…
Reference in a new issue