Communication Protocols for MachineLogic and MachineMotion AI

Prev Next

In this document you will learn how to set up your MachineMotion controller and use MachineLogic to communicate with external devices using MQTT or HTTP


Introduction to MQTT

Definitions

  • Client: Device that can send (publish) and receive (subscribe) data

  • Packet: Data sent by a client

  • Topic: Subject line through which packets are sent

  • Broker: Piece of software running on a computer which acts as the transit between another device or another broker

Message Queuing Telemetry Transport (MQTT) is a bi-directional lightweight message protocol which consists of a set of rules that defines how Internet of things (IoT) devices can publish and subscribe to data over the Internet. MQTT is used for messaging and data exchange between IoT and industrial IoT (IIoT) devices, such as embedded devices, sensors, industrial PLCs, and now, MachineMotion.

Each client can produce and/or receive data by publishing and/or subscribing. A client can publish a packet for a given topic, and anyone who subscribes to it can receive a copy of all messages for that topic. Multiple clients can subscribe to a topic from a single broker, and a single client can register subscriptions to topics with multiple brokers. This helps in both sharing data and managing and controlling devices. A client cannot broadcast the same data to a range of topics and must publish multiple messages to the broker, each with a single topic given. With MQTT broker architecture, the client devices and server application become decoupled. This allows clients to communicate with a single common recipient, and therefore funnel all information from the same place.

Programming

MachineLogic allows you to easily program your machine through its graphical interface and its low-code infrastructure. When it comes to MQTT communication, the same simple programming approach applies. To create a MachineLogic program using MQTT communication, here are the main commands:

To subscribe to a given topic, the State Machine or Wait For command can be used.

Figure 4: Subscribe to a topic in MachineLogic

Figure 4: Subscribe to a topic in MachineLogic

To publish to a given topic, the Generate Event output command can be used.

Figure 5: Publish to a topic in MachineLogic

Figure 5: Publish to a topic in MachineLogic

For more complex applications, you can also include payloads via the Variables and Functions features.

Figure 6: Variables and functions feature on MachineLoigc

Figure 6: Variables and functions feature on MachineLogic


Introduction to HTTP

Definitions

  • Client: A software application or a program such as a web browser that initiates requests to web servers.

  • Requests: the request from the client to the server specifying the URL of the resource it wishes to retrieve as well as the request method (GET, POST, PUT, DELETE).

  • Servers: A software application or a program that listens for incoming requests from clients, processes those requests, and sends back corresponding HTTP responses.

  • Response: Message sent by the server to a client. An HTTP response contains the status line, the response header and response body.

HTTP stands for Hypertext Transfer Protocol. It is an application layer protocol used for data communication on the World Wide Web. HTTP facilitates the transfer of various resources, such as HTML documents, images, videos, and other types of data, between a client (usually a web browser) and a web server.

The basic concept behind HTTP is the request-response model. When a client wants to access a resource hosted on a web server, it sends an HTTP request to the server. The server processes the request and responds with the requested resource, along with an HTTP response containing the status of the request (e.g., success, error, redirection) and additional metadata about the resource. HTTP operates on top of the TCP/IP (Transmission Control Protocol/Internet Protocol) network stack and typically uses TCP as its transport protocol.

Request Methods

as of firmware version 2.12, MachineLogic supports the HTTP methods listed below:

  • GET: The GET method is used to request data from a specified resource. It retrieves data from the server without changing anything on the server’s side. It is commonly used for fetching web pages, images, or other resources.

  • POST: The POST method is used to submit data to be processed to a specified resource. It is often used when submitting forms on web pages, sending data to a server to create new resources or update existing ones.

  • PUT: The PUT method is used to update a resource on the server. It sends the data as a representation of the resource to be updated at a specific URL.

  • DELETE: The DELETE method is used to remove a resource from the server. It sends a request to delete the specified resource.


Programming

MachineLogic’s Code-Free programming allows you to easily program your machine through its graphical interface and its low-code infrastructure. When it comes to HTTP communication, the same simple programming approach applies. To send a HTTP request from MachineLogic, use the Add Message command and select URL in the Send message to field:

Figure 11: Add Message command

Figure 11: Add Message command

Example:
In the following example, we will show how to format application variables so they can be sent using a POST request from MachineLogic Code-Free programming interface. This can be used to send a log of an actuator’s position to an express server:

Step 1: create the application variables:

Figure 12: Creating application variables

Figure 12: Creating application variables

Step 2: Format the application variables in json format using lambda functions:

Figure 13: Formatting variables using Lambda Functions

Figure 13: Formatting variables using Lambda Functions

Step 3: Send the HTTP request using the Add Message command:

Figure 14: Send application variables as HTTP Request

Figure 14: Send application variables as HTTP Request

This examples assumes a server is running on port 3169 of the user’s machine listening on the route logPositionData. The server response is encapsulated in the UNPACK_MESSAGE.