Jump to content

SENSBLUE ATLAS/Node-RED: Difference between revisions

No edit summary
No edit summary
 
(23 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{DISPLAYTITLE:Node-RED}}
[[File:Node-RED Logo.png|frameless|150x150px]]
== Node-RED Simplified Flows ==
== Node-RED Simplified Flows ==
'''Flow Node-RED'''
'''Flow Node-RED'''


These easy-to-implement flows aim to speed up the integration of ATLAS in any scenario by adding an abstraction layer between the user and the stack. This is achieved through the use of subflows, which contain the necessary code for the functionalities and allow changes if needed, providing a node that "hides" the more complex integration.
These easy-to-deploy flows aim to make the integration of the ATLAS into any scenario faster by adding anabstraction layer between the user and the stack. This is achieved through the use of subflows, which containthe code needed for the features and where changes can be made if necessary, exposing a node that "hides"that more complex integration.


=== 1. Import Flows to Example Folder ===
=== 1. Import Flows to Example Folder ===
To import the flows and add them to the Node-RED examples folder, execute the following:
To import the flows and add them to the Node-RED examples folder, execute the following:


1.1 First, copy the ATLAS folder to a folder with permissions, e.g., /pi
1.1 First copy the ATLAS folder to a folder with permissions, i.e. /pi


1.2 Copy the folder to the Node-RED examples folder using the command:<syntaxhighlight lang="bash">
1.2 Copy the folder to the Node-RED examples folder using the command:<syntaxhighlight lang="bash">
Line 13: Line 16:
</syntaxhighlight>
</syntaxhighlight>
[[File:Node-RED Simplified Flows img 1.png|center|frameless|1037x1037px]]
[[File:Node-RED Simplified Flows img 1.png|center|frameless|1037x1037px]]
<table style="border: none; background: transparent; margin: 15px 0; border-collapse: collapse;">
    <td style="padding: 0 0 0 10px; border: none; background: transparent;">
        <span class="atlas-btn">[[Media:Simplified Flows.zip|Simplified Flows]]</span>
    </td>
</table>
'''Analog Inputs'''
This flow implements a demonstration of acquisition and control of analog ports AI1..AI4 via MQTT and theNode-RED Dashboard 2.0. The system is divided into two main subflows: '''Analog Inputs''', for readingchannels AI1 to AI4, and '''Analog Outputs''', for configuring/reading channels AO1 and AO2. The main logic isencapsulated in subflows, allowing reuse in other tabs or projects.
=== 2. Main Components ===
{| class="wikitable mix-table"
!Component
!Function
|-
|Analog Inputs
|Subflow responsible for requesting AI1..AI4 readings, receiving results, and forwarding values to gauges/UI and live topic.
|-
|Analog Outputs
|Subflow responsible for sending AO1/AO2 currents, synchronising sliders/gauges, and reading the current state of outputs.
|-
|Analog Demo
|Main tab that instantiates both subflows and displays the Dashboard widgets.
|-
|MQTT broker atlas
|Local broker at 127.0.0.1:1883
|-
|Dashboard 2.0
|Analog Demo ports page
|}
=== 3. Subflow Analog Inputs ===
==== 3.1 Function ====
The Analog Inputs subflow allows selecting which AI channels should be read and in which unit/type eachchannel should operate: voltage ('''voltage''') or current ('''current'''). The subflow has four outputs, one perchannel: AI1, AI2, AI3 and AI4.
==== 3.2 ENV Variables ====
{| class="wikitable tech-table"
!Variable
!Type
!Default Value
!Description
|-
|'''readAI1'''
|bool
|'''true'''
|Enables/disables reading of channel AI1.
|-
|'''AI1_type'''
|string
|'''voltage'''
|Type of AI1 reading: '''voltage''' or '''current'''.
|-
|'''readAI2'''
|bool
|'''true'''
|Enables/disables reading of channel AI2.
|-
|'''AI2_type'''
|string
|'''voltage'''
|Type of AI2 reading: '''voltage''' or '''current'''.
|-
|'''readAI3'''
|bool
|'''true'''
|Enables/disables reading of channel AI3.
|-
|'''AI3_type'''
|string
|'''voltage'''
|Type of AI3 reading: '''voltage''' or '''current'''.
|-
|'''readAI4'''
|bool
|'''true'''
|Enables/disables reading of channel AI4.
|-
|'''AI4_type'''
|string
|'''voltage'''
|Type of AI4 reading: '''voltage''' or '''current'''.
|}
==== 3.3 AI channel acquisition ====
Example request payload:<syntaxhighlight lang="json">
{
  "id": 1780000000000,
  "origin": "Dev",
  "task": {
    "taskParams": {
      "AI1": { "action": "READ" },
      "AI2": { "action": "READ" },
      "AI3": { "action": "READ" },
      "AI4": { "action": "READ" }
    }
  }
}
</syntaxhighlight>If no channel is active, the function emits the warning "'''No AI channels selected'''" and does not send a message.
==== 3.4 Configuring the AI mode ====
The group '''Set acquisition mode''' allows sending the configuration of the analogue channels to <code>analogInputs/config/in</code>. The function '''Set AIX mode''' creates '''taskParams''' only for the active channels and sets each channel as:<syntaxhighlight lang="json">
{
  "mode": "single",
  "type": "voltage"
}
</syntaxhighlight>It also saves the configuration in flow context via <code>flow.set("AI_config", taskParams)</code>. If the configured type is neither <code>voltage</code> nor <code>current</code>, the function uses <code>voltage</code> as a fallback and emits a warning.
=== 4. Subflow Analog Outputs ===
==== 4.1 Function ====
The subflow '''Analog Outputs''' controls and monitors two analogue current outputs: AO1 and AO2. It has one input, used by the Dashboard sliders, and two outputs, one for AO1 and another for AO2.
==== 4.2 ENV Variables ====
{| class="wikitable tech-table"
!Variable
!Type
!Default value
!Description
|-
|AO1_enabled
|bool
|true
|Enables/disables output AO1.
|-
|AO1_current
|num
|5.5
|Initial/default current of AO1 in mA.
|-
|AO2_enabled
|bool
|true
|Enables/disables output AO2.
|-
|AO2_current
|num
|5.5
|Initial/default current of AO2 in mA.
|}
The function '''set current output''' receives messages from the sliders with values between 0 and 20 mA. Invalid values are rejected with a warning in the debug panel.
Payload sent to <code>analogOutputs/config/in</code>:<syntaxhighlight lang="json">
{
  "id": 1780000000000,
  "origin": "APP",
  "task": {
    "taskParams": {
      "mode": "on",
      "AO1": { "current": 5.5 },
      "AO2": { "current": 5.5 }
    }
  }
}
</syntaxhighlight>If an output is disabled in the ENV, the current sent to that channel is <code>0</code>. If both are disabled, the <code>mode</code> field switches to <code>off</code>.
==== 4.5 Reading the AOs ====
The reading group periodically sends a request to <code>analogOutputs/runtime/in</code>:<syntaxhighlight lang="json">
{
  "id": 1780000000000,
  "origin": "APP",
  "task": {
    "taskParams": {
      "AO1": { "action": "READ" },
      "AO2": { "action": "READ" }
    }
  }
}
</syntaxhighlight>The response is received on <code>analogOutputs/runtime/out</code>. The parser extracts <code>AO1.current</code> and <code>AO2.current</code> and prepares a payload in the appropriate format for writing to a database or further processing:<syntaxhighlight lang="json">
[
  {
    "ao1": 5.5,
    "ao2": 5.5
  }
]
</syntaxhighlight>
=== 5. Operating flow ===
==== 5.1 Reading analogue inputs ====
# The inject on the subflow Analog Inputs triggers the function '''acquire AIx channels'''.
# The function checks which channels are active in the ENV variables.
# A request is published on <code>analogInputs/runtime/in</code>.
# The hardware/service responds on <code>analogInputs/runtime/out</code>.
# The parser separates the values by channel and updates the AI gauges.
==== 5.2 Configuring analogue outputs ====
# The user moves the AO1 or AO2 slider.
# The subflow Analog Outputs receives the value with <code>msg.topic</code> equal to the channel.
# The function validates whether the current is between 0 and 20 mA.
# The value is stored in flow context.
# The configuration is published on <code>analogOutputs/config/in</code>.
# The slider, gauge and text of the changed channel are synchronised.
==== 5.3 Reading analogue outputs ====
# A periodic inject sends a READ request for AO1 and AO2 on <code>analogOutputs/runtime/in</code>.
# The system responds on <code>analogOutputs/runtime/out</code>.
# The parser converts the values into a structured payload with fields <code>ao1</code>, <code>ao2</code> and tag <code>source_tag</code>.
=== 6. Validations and protections implemented ===
{| class="wikitable mix-table"
!Area
!Validation
!Behaviour on error
|-
|AI selection
|At least one active AI channel
|Returns null and warning "No AI channels selected".
|-
|AI type
|Only voltage or current
|Falls back to voltage.
|-
|AO current
|Finite number between 0 and 20 mA
|Returns null and warning "Invalid current".
|-
|AO enable
|Checks AO1_enabled / AO2_enabled
|Disabled channel sends current 0.
|}
=== 7. Requirements ===
* Node-RED with subflow support.
* '''@flowfuse/node-red-dashboard''' version '''1.30.2'''.
* MQTT broker.
* External service/hardware that stimulates/responds to the '''analogInputs/*''' and '''analogOutputs/*''' topics.
'''Digital Demo'''
=== 8. Objective ===
This flow demonstrates the control and monitoring of digital ports through
Node-RED, MQTT and Dashboard 2.0.
It is composed of two main subflows:
{| class="wikitable mix-table"
!Component
!Function
|-
|Digital Inputs
|Reads the states of DI1..DI4 and sends them to LEDs on the Dashboard.
|-
|Digital Outputs
|Controls DO1..DO4 through switches and shows the real state on LEDs.
|-
|Demo Digital ports
|Dashboard page where the digital widgets are displayed.
|-
|MQTT broker atlas
|Local broker at 127.0.0.1:1883.
|}
=== 9. General architecture ===
'''Digital Inputs:'''
Node-RED → digitalInputs/runtime/in → Hardware
Hardware → digitalInputs/runtime/out → Node-RED → LEDs DI1..DI4
'''Digital Outputs:'''
Switches Dashboard → Node-RED → digitalOutputs/runtime/in → Hardware
Hardware → digitalOutputs/runtime/out → Node-RED → LEDs DO1..DO4
=== 10. MQTT topics ===
{| class="wikitable mix-table"
!Topic
!Usage
|-
|digitalInputs/runtime/in
|Request to read the digital inputs.
|-
|digitalInputs/runtime/out
|Response with the states of DI1..DI4.
|-
|digitalOutputs/runtime/in
|'''SET''' commands or '''READ''' requests for the digital outputs.
|-
|digitalOutputs/runtime/out
|Response with the states of DO1..DO4.
|}
=== 11. Digital Inputs ===
The '''Digital Inputs''' subflow periodically sends a '''READ''' request to read DI1..DI4.
The '''Force Read''' inject runs every '''1 second'''.
Payload sent:<syntaxhighlight lang="json">
{
  "id": 1780000000000,
  "origin": "APP",
  "task": {
    "action": "READ"
  }
}
</syntaxhighlight>The expected response contains the states of the inputs in:
'''msg.payload.task.taskResult.DI1..DI4'''
The parser converts the states to booleans:
{| class="wikitable mix-table"
!State received
!Value sent to UI
|-
|'''high''' with success: '''true'''
|true
|-
|'''low''' with success: '''true'''
|false
|-
|Channel absent or error
|false
|}
The four outputs of the subflow update the LEDs DI1, DI2, DI3 and DI4.
=== 12. Digital Outputs ===
The '''Digital Outputs''' subflow allows controlling DO..DO4 through switches on the Dashboard.
Each switch sends:
{| class="wikitable mix-table"
!Switch
!msg.topic
!ON
!OFF
|-
|DO1
|DO1
|true
|false
|-
|DO2
|DO2
|true
|false
|-
|DO3
|DO3
|true
|false
|-
|DO4
|DO4
|true
|false
|}
The '''set digital outputs''' function converts the values into physical states:
{| class="wikitable mix-table"
!Logical value
!State sent
|-
|true
|high
|-
|false
|low
|}The command is sent to:
'''digitalOutputs/runtime/in'''
=== 13. ENV variables of the Digital Outputs ===
{| class="wikitable mix-table"
!Variable
!Default value
!Function
|-
|DO1
|true
|Initial state of DO1
|-
|DO2
|true
|Initial state of DO2
|-
|DO3
|true
|Initial state of DO3
|-
|DO4
|true
|Initial state of DO4
|}
On deploy/startup, a '''once''' inject initializes the outputs with these values.
=== 14. Dashboard ===
{| class="wikitable mix-table"
!Field
!Value
|-
|Page
|Demo Digital ports
|-
|Path
|/dashboard/page2
|-
|Theme
|Atronia
|-
|Layout
|grid
|}Groups on the page:
{| class="wikitable mix-table"
!Group
!Content
|-
|Digital Inputs
|LEDs DI1..DI4
|-
|Digital Outputs
|Switches DO1..DO4 and LEDs DO1..DO4
|}
Input LEDs:
{| class="wikitable mix-table"
!Value
!Color
|-
|true
|Green
|-
|false
|Red
|}
Output LEDs:
{| class="wikitable mix-table"
!Value
!Color
|-
|1
|Green
|-
|0
|Red
|}
=== 15. Operation sequence ===
'''Digital inputs'''
# A READ request is sent.
# The hardware responds with the states DI1..DI4.
# The parser converts '''high''' to true and '''low''' to '''false'''.
# The DI LEDs are updated.
'''Digital outputs'''
# On startup, the outputs use the ENV values.
# The user changes a switch on the Dashboard.
# The hardware applies the states.
# The DO LEDs show the real state received.