1
Mach4 General Discussion / Re: Mach4 MQTT Client
« 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:
Added the following to the end of the Screen Load Script:
Added this line to plc script.
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.
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.
Hope this helps.
-Dustin
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