Data upload using the WebSocket plugin

In this tutorial, we show how to upload data collected from south devices to a WebSocket server using the Neuron WebSocket plugin.

Requirements

We use two PCs connected in a local area network in this tutorial. One is a Linux machine with Neuron installed, the other is a Linux machine with a WebSocket server. Consult the the installation instruction on how to install Neuron.

PC 1PC 2
Operating SystemLinuxLinux
IP address192.168.1.152192.168.1.107
SoftwareNeuronPython 3.7
NetworkConnectedConnected

We need a WebSocket server for demonstration. You may use any server software you like as long as it speaks the WebSocket protocol. To make things simple, we will use a simple python program in this tutorial. Our python server program requires Python >= 3.7, and depends on the websockets packageData upload using the WebSocket plugin - 图1 (opens new window).

Run the following command to install the dependencies.

  1. $ pip install websockets

WebSocket server

Following is our python server program, it listens at tcp port 8000.

  1. #!/usr/bin/env python
  2. import asyncio
  3. import websockets
  4. async def echo(websocket):
  5. async for message in websocket:
  6. print(message)
  7. async def main():
  8. async with websockets.serve(echo, "0.0.0.0", 8000):
  9. await asyncio.Future() # run forever
  10. asyncio.run(main())

Start the server program:

  1. $ python websockets.py

Setup Neuron

South devices

We need some south devices to collect data from. Any Neuron south bound plugin will do for this tutorial, for example the Modbus TCP plugin. In this tutorial, we will use the File plugin to create south devices collecting data from local files. This make things simple since we don’t need an actual device and we can fake tag data by just modifying file contents.

Add the file node

In the Neuron dashboard, click Configuration -> South Devices -> Add Device to add a node using the File plugin.

Add file node in Neuron dashboard

Fig.1 - Add file node in Neuron dashboard

Click the Device configuration icon to configure the file node.

Neuron dashboard sourth devices tab

Fig.2 - Neuron dashboard sourth devices tab

Using the default configuration is enough. Click Submit.

Configure the file node in the Neuron dashboard

Fig.3 - Configure the file node in the Neuron dashboard

Create a group

The file node should be in Connected state once configured. Click the file node to enter the Group List tab, then click Create to create a group.

Neuron dashboard sourth devices tab showing file node connected

Fig.4 - Neuron dashboard sourth devices tab showing file node connected

We set the group name to grp and the interval to 1000.

Add a group to the file node in Neuron dashboard

Fig.5 - Add a group to the file node in Neuron dashboard

Add tag

Click the created grp group to enter the Tag List tab, then click Create to create a tag.

Neuron dashboard group list

Fig.6 - Neuron dashboard group list

We name the tag tag0 with type STRING. The tag address is set to hello.txt which represents the file in the working directory of the Neuron process.

Add a tag to the file node in Neuron dashboard

Fig.7 - Add a tag to the file node in Neuron dashboard

North app

Add the websocket node

In the Neuron dashboard, click Configuration -> North Apps -> Add Application to add a node using the WebSocket plugin.

Add websocket node in Neuron dashboard

Fig.8 - Add websocket node in Neuron dashboard

Click the Application Configuration icon to configure the websocket node.

Neuron dashboard north apps tab

Fig.9 - Neuron dashboard north apps tab

We set the host parameter to 192.168.1.107 and port to 8000, which is the address of our WebSocket server.

Configure websocket node in Neuron dashboard

Fig.10 - Configure websocket node in Neuron dashboard

Once the configuration is submitted, the websocket node connects to the WebSocket server successfully.

websocket node connected state in Neuron dashboard

Fig.11 - Neuron dashboard north apps tab showing websocket node connected

Subscribe to the file node

Click the websocket node to enter the Subscription tab. Then click Add subscription, select the file node and the grp group.

websocket node subscribe to file node

Fig.12 - websocket node subscribe to file node

Validate server output

After subscribing to the grp group of the file node, the websocket node will begin pushing data to the WebSocket server. In the Neuron dashboard, click Monitoring -> Data monitoring, then select the file node and the grp group. We see that Neuron reports an error code 1011 indicating the file hello.txt does not exist.

Neuron dashboard data monitoring tab

Fig.13 - Neuron dashboard data monitoring tab showing error

We can check the server output that it receives the data correctly. And sure, the received data says that tag0 encounters the error code 1011.

WebSocket server output

Fig.14 - WebSocket server output showing error code 1011

In the Neuron process working directory, create the hello.txt file with the content world.

  1. $ echo world > hello.tx

Now we can see that Neuron updates tag0 correctly in the Data monitoring tab.

Neuron dashboard data monitoring tab

Fig.15 - Neuron dashboard data monitoring tab showing updated value

And the WebSocket server receives the correct tag data, world, which is expected.

WebSocket server output

Fig.16 - WebSocket server output showing correct value