#NR5V1: Read Variables from S7 and store it as CSV file. Send it through SFTP and Email.

Application-Note: #NR5V1 : Read Variables from S7 and store it as CSV file. Send it through SFTP and Email.

Related Documents:

  • Application-Note: #NR0V1 : Get Node-RED ready on my router
  • Application-Note: #NR1V1 : Hello World
  • https://nodered.org/docs/user-guide/
  • https://flows.nodered.org/node/node-red-contrib-s7

This application describes how to communicate with an S7 and display the value in a Node-RED dashboard. Also storing this values in a CSV file locally on the router and send this regularly by Email. Additional the file could be send via SFTP to an fileserver.

For this application-note we provide an Example Flow for Node-RED which displays widgets, reads values from a S7-1200 plc, read and write data to/from a file, send email and send a file via SFTP. Please consider that this application is running on the router and can be accessed via http or https://router-ip:1880/ui. You can access it also from outside through an Web2Go link from your mbCONNECT24 account.

Description 1 – Dashboard

This Dashboard displays widgets to visualize data. The control panel has the following buttons:

  • Delete History file : this deletes the csv data file
  • Send History file : this send the csv file via SFTP
  • Email history file : this send the csv file as an attachment to an email address
  • Email actual data : this sends an email with the actual data to an email address
  • Send History file every Day : this send the csv file every day at 23:30 to an email address and deletes the csv file
1. read from plc, display on dashboard and store it into a csv file

Description 2 - Node-RED Flow, read from plc

We are reading 3 variables from the plc. In the function node we store the value in flow-public variable (that we can access the value from other nodes asynchron).

Description 3 - function node Speed

After the function node it is connected directly to a gauge-widget to display the value on the dashboard. The value is also connected to a limiter node. We want only every 5 second the value pushed into the historical graph.

Description 4 - limiter node

We want all variables stored in csv formatted file and also here only every 5 seconds.

Description 5 - create csv file

The function node "Add Date/Time" adds the current Date and Time to the msg.payload and adds an array called msg.payload.Time.

Description 6 - function node "Add Date/Time"

The message at the function "csv" arrives then with the values and Date/Time stamp.

Description 7 - csv node

We need to define the columns, separator and the output format. The output is then stored in the file.

Description 8 - file node

The file is stored in the encrypted userspace on the SD card. The filename in this example is "mydata".

2. Send the csv file via SFTP

See below the flow on how to send the file to a SFTP server

Description 9 - Flow to send file

It starts with the button node "Send History File". This triggers the node "read mydata". This node outputs the file into msg.payload. The function node "Generate File to send" formats the payload that the SFTP node "Transfer via SFTP" can handle it. The SFTP node expect the following format:

Description 10 - Script for SFTP node preparation

The object "msg.payload.filename" describes the filename including path on the SFTP server (where to store and the name of the file). "msg.payload.filedata" contains the file content itself. There are a few more options through the object available, check the Node help.

The node "Generate File to send" also triggers the "Timeout 5s". If the timeout of 5s reached it sends a message to "message File transfer" message node. It will not send when the output of "Transfer via SFTP" will trigger within 5s and reset the timeout. In that case it sends a message "Set success Message" to the message node.

3. Send the csv file via Email

See below the flow on how to send the file via Email. You need an Email-Account with all information to send Email to your SMTP of your Email account. For example create an GMX Email account and they provide you the information that you can send Emails throught your account.

Description 11 - Flow to send file via Email

It is similar with the SFTP case. Different is, that we trigger all day at 23:30 clock to send an Email with the CSV file. With the button "Send History File every Day at 23:30" we set or reset a global flow flag. This flag is used in the function node "Is Flag 'Send every Day '?" to decide to send or not. Also the Error handling of Email-Sending is different to SFTP sending. Here we have a global Error handler "Errormessages" which listens on the Email node:

Description 12 - Error handler for Email

If an Email is send it triggers the node "Wait till delete file". This timeouts waits 10seconds if it was not reset then it triggers the node to delete the file. But if a error message arrived, this resets the timeout and the file will not be deleted. You can also delete the file while pushing the node button "Delete History File".

4. Send actual data via Email

See below the flow on how to send actual data via Email. You need an Email-Account with all information to send Email to your SMTP of your Email account. For example create an GMX Email account and they provide you the information that you can send Emails throught your account.

Description 13 - Send actual data via Email

The button node triggers the Email. The Email content is generated in the node "Generate Context to send".

Description 14 - Script to generate email

See below the Flow as a file to Import in your application.