-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCNC_publisher_py2_v2.py
49 lines (35 loc) · 1.17 KB
/
LCNC_publisher_py2_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 20 18:55:25 2018
@author: aangris
"""
import time
import paho.mqtt.publish as publish
import linuxcnc
import linecache
s = linuxcnc.stat()
Broker = "iot.eclipse.org"
sub_topic = "sensor/instructions" # receive messages on this topic
pub_topic = "LCNC/data" # send messages to this topic
############### Linuxcnc inputs ##################
#Function to parse through the file being executed and display the gCode being run at the time it is accessed
def showGcode( Line,filename ):
Gcode = linecache.getline(filename, Line)
return Gcode
def status():
s.poll()
if getattr(s,'state')==2 and getattr(s,'interp_state')!=1:
status = "cutting"
GcodeLine=int(getattr(s,'id'))
total_lines=s.read_line
perc_complete = GcodeLine*100/total_lines
else:
status = "idle"
perc_complete = 0
return(perc_complete,status)
############### MQTT section ##################
while True:
sensor_data = [status()]
print sensor_data
publish.single(pub_topic, str(status), hostname="iot.eclipse.org")
time.sleep(0.1*60)