In this example we will go over how to setup the basic flows for sending and receiving messages between the Unity touch-panel and the Node-RED server. Here we have 3 partition tabs for organizational purposes.
Tab 1: MQTT Broker contains the Mqtt Broker which handles the connection between the control processor and the touch-panel.
Tab 2: TouchPanel Handles the Inputs and Output signals for the Boolean, Float and String signals.
Tab 3: UiLogic Handles the logic for the program.
Mqtt Broker Tab:
Add the Aedes node:
How to add a node from the online repository.
Select Manage palette:
Search for the node you want to add:
Drag the node onto the workspace:
Aedes is a lightweight and efficient MQTT broker written in JavaScript for use in Node.js applications, included inNode-RED. It enables real-time communication between devices and applications in a distributed network of MQTT-enabled devices, simply by configuring Node-RED’s MQTT nodes to use Aedes as the broker.
Configure the Aedes Output Node:
NOTE: Currently Parallax Control supports MQTT V3.1.1, we plan to migrate to v5 in a future update.
Default port: defualt for web access is 1880,.
Programmatic Access: use port 1883 for programmatic access. In the MQTT Broker security tab, enter the credentials which will be required by MQTT Clients to connect to the broker.
First, In Unity, ensure you have imported the Parallax Plugin for Unity Software
Once the plugin has been imported, select Window > Parallax Control System > UI Setup Wizard
Add a Home page, add a Button and build the scene:
Select the UI Elements tab (Window > Parallax Control System > UI Elements). Setup the button with a digital join ID.
Configure the names for the Digital, Analog and Serial message topics. The topic names should match what you have setup in Node-RED.
Setup the Parallax Processor Connection:
Enter the correct Mqtt Broker Ip, Port, Username and Password.
Mqtt Topics To Subscribe To: To set feedback on the user-interface, subscribe to topics from the program logic. In this case, we have named it digital_Fb, analog_Fb, string_Fb. This means that the user-interface will listen for messages on that topic.
Mqtt Topics To Publish: The main 3 topics to publish are Bool, Float, String communication types. The naming of each one is left to the programmer. These are the inputs from the touch-panel that are sent to the back-end logic for processing. The logic can run on either the .net console program, or in node-red. Either way, the logic program has an Mqtt Client which must connect to the Broker and subscribe to the topics it wants to listen to. When the logic program subscribes to the Bool, Float and String topics, it will receive the messages for processing.
How the broker works: By default, the MQTT broker receives messages from publishing clients and forwards those messages to all clients subscribed to the corresponding topic. The broker itself does not typically subscribe or publish messages. Its primary role is to facilitate message distribution between publishers and subscribers.
TouchPanel Tab: If using Node Red for logic, you can add an MQTT client node to subscribe and publish to the broker. This means there is an MQTT Broker and MQTT Client running on Node Red. Use the MQTT Client nodes to subscribe to and publish topics. You can use the MQTT In and Out nodes and configure the server and topics accordingly:
The Mqtt Input and Output Nodes come with the Aedes Mqtt Broker. You will need to set the topics to match the topics set in the Unity Mqtt client.
Example configure the input node:
The Server address is the loopback address: 127.0.0.1 The Port is the programmatic access port: 1883
UiLogic Tab:
Add a Switch Node. Wire the digital, analog and string inputs to each Switch Node:
NOTE: The payload between the unity touch panel project and node red contains the following properties:
msg.payload.Type
msg.payload.InstanceId
msg.payload.Value
The switch node which will switch on the InstanceId (the unique id for each component in unity). Configure the switch node property:
msg.payload.InstanceId
This will route the full message (msg.payload) by the InstanceId. From here you can access the Type and Value properties.
Click the add button to set the rules:
The msg.payload will now output to the associated msg.payload.InstanceId. From here you can access the msg.payload.
Type and msg.payload.Value for your logic programming.
Current Data Types (msg.payload.Type): BOOL FLOAT, STRING, BOOL_ARRAY, FLOAT_ARRAY, STRING_ARRAY, BOOL_DICTIONARY, FLOAT_DICTIONARY, STRING_DICTIONARY.