This question is related to this one, I created the code below:

import paho.mqtt.client as mqtt
import time
import serial

# Open grbl serial port
s = serial.Serial('COM3',9600)

# Wake up grbl
time.sleep(2)   # Wait for grbl to initialize 
s.flushInput()  # Flush startup text in serial input

f = """
T1 M6
(Linear / Feed - Absolute)
G0 G90 G40 G21 G17 G94 G80
G54 X-75 Y-75 S500 M3  (Position 6)
G43 Z100 H1
G1 Z-20 F100
X-40                   (Position 1)
Y40 M8             (Position 2)
X40                    (Position 3)
Y-40                   (Position 4)
X-75                   (Position 5)
Y-75                   (Position 6)
G0 Z100

# Runs when the client receives a CONNACK connection acknowledgement.
def on_connect(client, userdata, flags, result_code):
    print "Successful connection."
    # Subscribe to your topics here.

# Runs when a message is PUBLISHed from the broker. Any messages you receive
# will run this callback.
def on_message(client, userdata, message):
    if message.topic == "hi":
        if message.payload == "run":

            # Stream g-code to grbl
            for line in f:
                l = line.strip() # Strip all EOL characters for consistency
                print 'Sending: ' + l,
                s.write(l + '\n') # Send g-code block to grbl
                grbl_out = s.readline() # Wait for grbl response with carriage return
                print ' : ' + grbl_out.strip()

                # Close file and serial 
            # You could do something here if you wanted to.
        elif message.payload == "STOP":
            # Received "STOP". Do the corresponding thing here.
            # Close file and serial 
            print "CNC is stopped."

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("iot.eclipse.org", 1883, 60)

But when I run it I got nothing but Successful connection. and then the code ended. Can you please tell me what's going on?

  • 1
    Well check what you see at the broker's side or even with wireshark. Commented Dec 28, 2017 at 15:57
  • 1
    Also as suggested earlier, this sort of question is better suited to Stack Overflow as it is generic software not IoT specific
    – hardillb
    Commented Dec 28, 2017 at 19:22

1 Answer 1


Change the last line from




Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.