Hello Guest it is March 28, 2024, 07:30:46 PM

Author Topic: Mach4 MQTT Client  (Read 4821 times)

0 Members and 1 Guest are viewing this topic.

Re: Mach4 MQTT Client
« Reply #10 on: March 16, 2018, 02:13:46 AM »
Yes. Thats why I say, I have missed this options  :)
Re: Mach4 MQTT Client
« Reply #11 on: December 28, 2018, 04:29:57 AM »
Hi @SebDE,

Is your offer of the modified code and the installation instructions still valid? I would really appreciate it if it was...

I have been messing about with MQTT and Node-Red recently - if I could use this network based/ loosely coupled system to control my cutting table - fantastic!

Thanks

Teak
Re: Mach4 MQTT Client
« Reply #12 on: January 01, 2019, 02:47:42 AM »
Hi Seb
I decided to follow the initial instructions you posted...

I got the library loaded and the errors fixed in the library that were not compatible with the current version of Mach 4... and I got Mach 4 to post a massage the MQTT server (I attached a routine to a button for testing). This worked fine - but I could not get Mach 4 to respond to an incoming message - did you manage to get this to work?

This is the code I added to the screen load routine...
Code: [Select]
function mqttCallback(
topic,    -- string
message)  -- string

wx.wxMessageBox("mqttCallback");

--mc.mcCntlLoadGcodeFile(inst, "C:\\temp\\AliLive6.nc")

--mc.mcCntlSetLastError(inst, "Topic: " .. topic .. ", message: '" .. message .. "'");
mc.mcCntlSetLastError(inst, "message - test");
end

function SendMessage(msg)
--wx.wxMessageBox("mqttCallback");

    mc.mcCntlSetLastError(inst, msg);
    mqtt_client:publish("mach4", msg)
end

function MQTT_Connect()
    --mqtt_client = mqtt.client.create("192.168.0.55", nil, mqttCallback)
mqtt_client = mqtt.client.create("192.168.0.3", nil, mqttCallback)
    mqtt_client:connect("mach4")
    mqtt_client:publish("mach4", "online")
    mqtt_client:subscribe({"mach4/set"})

mqtt.Utility.set_debug(true)
end

MQTT_Connect()

I also added this to the screen load routine...
Code: [Select]
--socket module
package.loaded.socket = nil
socket = require "socket"

--MQTT module
package.loaded.MqttModul = nil
mqtt = require "mqtt_library_mod"

and I added this to the beginning of the PLC script...

Code: [Select]
mqtt_client:handler()
Alas I have not been able to get Mach4 to respond to a message from my Node-Red server. I would be really cool if I could post a gcode file name to Mach4 and tell it 'go'...

BTW...this is my first attempt to do anything with Mach4 - so its quite possible that I have made some simple mistakes. Also - I am trying to do this with the demo version, since this kind of thing is the reason I would upgrade to Mach4 - I want to see if I could get it working first - it maybe the reason its not working!!
Re: Mach4 MQTT Client
« Reply #13 on: January 03, 2019, 11:16:36 AM »
i'm interested in setting up mqtt with mach4. is the bidirectional communication working?
Re: Mach4 MQTT Client
« Reply #14 on: January 08, 2019, 05:19:27 AM »
I haven't done anything since my last post... I could not get Mach4 to respond to an incoming message.

I notice in one of Newfangled promotional videos they talk about using MQTT with Mach4 - so maybe it is possible - maybe they have it working (in both directions)... Who knows!...they don't seem to pay much attention to this forum!
Re: Mach4 MQTT Client
« Reply #15 on: January 11, 2019, 07:56:28 PM »
I was able to get Mach to respond to incoming messages. Here is what worked for me.
Pretty much just a copy of what has already been posted.

Add mqtt_library.lua and utility.lua to modules folder in Mach4 directory.
Edit the mqtt_library, search for the following lines, and replace as shown:
Code: [Select]
if (return_code <= table.getn(MQTT.CONACK.error_message)) then     ----->     if (return_code <= #MQTT.CONACK.error_message) then

local topic_count = table.getn(outstanding[2])     ----->     local topic_count = #outstanding[2]

Added the following to the end of the Screen Load Script:
Code: [Select]
---------------------------------------------------------------
--MQTT module
package.loaded.MqttModul = nil
mqtt = require "mqtt_library"

--Socket module
package.loaded.socket = nil
socket = require "socket"
---------------------------------------------------------------


function mqttCallback(
  topic,    -- string
  message)  -- string
  mc.mcCntlSetLastError(inst,"".. topic ..": ".. message .."")   --Example,   Node-Red: Cycle Stop
end

function SendMessage(msg)
    mc.mcCntlSetLastError(inst, msg);
    mqtt_client:publish("Mach4",msg)
end

function MQTT_Connect()
    mqtt_client = mqtt.client.create("192.168.1.44", nil, mqttCallback)
    mqtt_client:connect("mach4")
    mqtt_client:publish("mach4", "online")
    mqtt_client:subscribe({"Node-Red"})

    mqtt.Utility.set_debug(true)
end

MQTT_Connect()

Added this line to plc script.
Code: [Select]
mqtt_client:handler()

To test sending a message to Node-red I changed the CycleStop function in the Screen Load Script as shown below.
This publishes "Cycle Stopped" when the Stop button is pressed in Mach.
Code: [Select]
---------------------------------------------------------------
-- Cycle Stop function.
---------------------------------------------------------------
function CycleStop()
    mc.mcCntlCycleStop(inst);
    mc.mcSpindleSetDirection(inst, 0);
    mc.mcCntlSetLastError(inst, "Cycle Stopped");
    msg = "Cylce Stopped";
    SendMessage(msg)
end

To test sending a message/command to Mach from Node-Red I added the following to the plc script.
This pulls the incoming message from the LastError dro. Then using the conditional statement it will stop or start the machine depending on the received message.
Code: [Select]
local last_error = mc.mcCntlGetLastError(inst);

if (last_error == 'Node-Red: Cycle Stop') then

CycleStop()

elseif (last_error == 'Node-Red: Cycle Start') then

CycleStart()

end

Hope this helps.

-Dustin
Re: Mach4 MQTT Client
« Reply #16 on: January 16, 2019, 04:41:41 AM »
Hi Dusty91...Sorry for the delay in getting to this...

I did think that trying to trigger a process from and incoming MQTT could cause a threading problem i.e - the message arrives on a thread that does not have access to the main UI thread. So the message actually arrives - but it is difficult to 'see' it. Your approach of using the 'last_error' could be a way around this problem - and you seem to have got it working...I haven't tried it yet - when I get to it, I will try your approach.

Have you been looking at MQTT and Node-Red before you read this post or did you try all this once had read the post? I would be interested in hearing what you are doing with it - and whether you are already integrating MACH4 with you workflows? My principle idea is to integrate my cutting bench with an indexing machine - so the whole process can run all day using some kind of hopper system - is anyone doing this already do you know?

Re: Mach4 MQTT Client
« Reply #17 on: April 04, 2019, 04:28:07 PM »
Hi made this work to send messages from Mach 4 thanks all!

has any one been able to have it send tool change messages? i have not found that part of the code yet.

cheers
Raptor
Re: Mach4 MQTT Client
« Reply #18 on: April 07, 2019, 12:15:52 AM »
So ive managed to get this to work receiving mqtt messages too. I setup estop remotely and also being able to set a screen text box from an mqtt message. Tool change is still eluding me however
Re: Mach4 MQTT Client
« Reply #19 on: January 24, 2022, 12:43:23 PM »
Will love a copy of the install instruction as I am in the progress of integrating MQTT for Mach4 with my existing mosquito environment. Thanks!