How-To Guides
Flows Guides

Save Multiple Register Values to a CSV File

8min

You can use Flows to save multiple register values to a CSV file on your Edge device.

User Scenario

In an industrial environment, engineers must keep a close eye on the performance of different machines in a factory. To achieve this, they collect raw data at regular intervals from various Programmable Logic Controller (PLC) registers. This data can be aggregated, saved in a file, and used by other applications using Flows. Even if the collection intervals differ, the data can be conveniently accessed using global variables. By analyzing this data, engineers can make informed decisions to improve the productivity of the factory.

Step 1: Add a Device

Follow the steps to Connect a Device and configure the following parameters:

  • Device Type: Simulator
  • Driver Name: Generator
  • Enable Alias Topics: Select the checkbox.

Step 2: Add Tags

After connecting the device, Add Tags the device. Each tag will collect data from different types of inputs.

Tag 1: input1

  • Name: Select S - Random value generator
  • Value Type: Select int64
  • Polling Interval: Enter 5
  • Tag Name: Enter input1

Tag 2: input2

  • Name: Select S - Random value generator
  • Value Type: Select float64
  • Polling Interval: Enter 5
  • Tag Name: Enter input2

Click the copy icon to copy the raw topic of both tags you created. Store them securely as you will refer to them later when configuring the flows.

DeviceHub Tags page
DeviceHub Tags page


Step 3: Create Analytics Flows

You can use multiple input nodes in a flow to save values in a database or save values to a CSV file.

To save values from multiple registers to a CSV file:

Note: A similar method could be used to save values in a MySQL database.

  1. In Manufacturing Connect Edge, navigate to the Flows Manager and create a new flow. See Create a Flow to learn more.
  2. Drag two Function nodes and two Datahub Subscribe nodes to the canvas and wire them together.

    Flows canvas with nodes
    Flows canvas with nodes
    
  3. Double-click one of the Function nodes. The Edit function node dialog box appears.
    • On Message: Add the following lines to save the value to a Global Variable (output1), which can be accessed by other flows, nodes, and functions: var temp = (JSON.parse(msg.payload)).value; global.set("output1",temp); return msg;
    • Click Done.

      Edit function node dialog box
      Edit function node dialog box
      
  4. Double-click the other Function node. The Edit function node dialog box appears.
    • On Message: Enter the following lines of code. var temp = (JSON.parse(msg.payload)).value; global.set("output2",temp); return msg;
    • Click Done.

      Edit function node dialog box
      Edit function node dialog box
      
  5. Double-click one of the DataHub Subscribe nodes. The Edit Datahub Subscribe node dialog box appears.
    • Datahub: Configure the Datahub connection if needed. See the Step 3: Configure Connector Nodes section in Create a Flow to learn more.
    • Topic: Paste the topic for input1 tag.
    • Name: Enter Input1.
    • Click Done.

      Edit Datahub Subscribe node dialog box
      Edit Datahub Subscribe node dialog box
      
  6. Repeat the last step for the other DataHub Subscribe node and paste the topic for input2 tag.

    Flows canvas with nodes
    Flows canvas with nodes
    
  7. On the same canvas, drag an inject node, a Function node, and a Write file node and wire them together as shown.

    Flows canvas with nodes
    Flows canvas with nodes
    
  8. Double-click the inject node. The Edit inject node dialog box appears.
    • Repeat: Select an interval for every 10 seconds to read the global variable values.
    • Click Done.

      Edit inject node dialog box
      Edit inject node dialog box
      
  9. Double-click the Function node. The Edit function node dialog box appears.
    • On Message: Enter the following lines of code to set up the output for the CSV file. //create CSV var temp = global.get("output1") + "," + global.get("output2"); msg.payload = temp; return msg;
    • Click Done.

      Edit function node dialog box
      Edit function node dialog box
      
  10. Double-click the Write file node. The Edit write file node dialog box appears.
    • Filename: Enter a filename prefaced with a forward slash ( / ).
      • The forward slash is required in the filename. Example: /ftp-data/sample.csv
      • The file is created on the Manufacturing Connect Edge device.
      • If a subdirectory already exists, you can supply the full path to the file. Example: /ftp-data/sampleor /logfiles/sample.
    • Action: Select append to file from the drop-down menu and click Done.

      Edit write file node dialog box
      Edit write file node dialog box
      
  11. Click Deploy to save the flow.
  12. Drag the Sidebar up from the bottom of the canvas and select the Debug icon.

    Completed flows canvas with debug message icon
    Completed flows canvas with debug message icon
    
  13. Click the button on the left side of the inject (timestamp) node to inject the current timestamp into the write file node, which creates the CSV file locally if it does not already exist. In this example, the /ftp-data/sample.csv file is saved.

Step 4: Read Contents of CSV File

To read the output of the CSV file, refer to Create, Write to, and Read from a Log File.

Note: Change the name of the file with the CSV file you created to save multiple register values.

The CSV file holds the current register values from the two inputs and updates them in real-time.

Output of flows canvas
Output of flows canvas