RPi: Streaming raspberry shake data to node-red

Goal:

Learn how to send seismology data from a raspberry shake into a separate raspberry pi with node-red. And how to use the data once it arrives.

 

Resources:

Raspberry shake, setup following this guide: https://t3alliance.org/rpi-setting-up-a-raspberry-shake/

A separate raspberry pi with node-red, running on the same network

Introduction:

Raspberry shake usually processes its own data, and publishes it to it’s own public databases. However it is possible to configure raspberry shake to send a raw data-stream from itself to any device your network can reach. Raspberry shake uses a UDP stream to send data from itself to any list of ip addresses and ports that happen to be listening. Using this raw data stream you can create your own graphs, or set up alerts and triggers in node-red.

This means that the raspberry shake could be physically located anywhere in a building, or using the proper networking setup anywhere in the world and you can still comfortably receive the data in node-red on your own computer.  Imagine an email alert being sent-out by your RPi at home, because your RPi shake at school detected some unusual vibrations.  Or you could trigger a camera to go off, and email you a picture whenever footsteps were detected in your room, the possibilities are endless!

 

What is UDP?

When stuff is sent over the internet it is sent using a protocol, which is a way of organizing data so that both parties know what’s going on. If you made an agreement with a friend to always use red envelopes when a message was urgent, that would be an example of a protocol. Most of the time when you download a file, or send an email it is sent using TCP protocol; which is rather strict. For example, if something messes up and the TCP packets (analogous to envelopes with letters inside) arrive out of order, they will be reorganized and only sent to the destination when everything is sorted out. UDP is much less strict, and is more like a raw flow of data vs an orderly sequence of letters. If UDP packets arrive out of order, or have some missing, or even if the data inside is messed up they’ll usually be sent straight along to the destination.  This is useful in projects like our seismometer because it is more lightweight with less latency, also it comes with a feature called multicast which allows a data stream to be sent to multiple destinations at the same time; which is exactly what is possible with the raspberry shake.

Getting Started:

The first thing you’ll need is the IP address of the pi that will listen for the data, this can be found just by hovering over the network applet here. The easiest way to ensure the listening pi and the shake-pi are on the same network is to plug them both into the same router using ethernet cables. Otherwise setting up a WiFi connection on the shake can be tricky and even interfere with the measurements.

Connecting to the rs.local  or raspberryshake.local or even the ip_address:80 of the shake,

From the settings page, come to the UDP streams tab

Here is where you’ll need the ip address of your listening pi, it’s also important to make sure it’s connected to the same network as the shake. If the shake is using LAN this means you’ll probably have to plug your pi into LAN to.

Here I’ve entered the IP of my listening pi, and used the default port 8888

Click the plus button and don’t forget to hit save.

 

Reading the stream on raspberry pi:

Starting with a fresh flow, we’ll drag the UDP IN node in.

 

For the configuration chose the same port as we used in the rs.local config, that would be 8888, and make sure to set output to ‘a String’ instead of ‘Buffer’.
If you hook your UDP RECIEVER up to a debug node you should already be able to see a stream of data coming in, if not something is wrong with your connection (probably raspi isn’t on the same network as raspberry shake).

Next we need the raspberry shake parser node, made by the guys who build the SHAKE.
This can be installed by going into the palette manager, as of now it’s the only result when you search ‘shake’.
The installation will take a few minutes, and then you can chain it with the UDP node like so.

All the rshake parser node does is take the raw thing outputted by the UDP node, and turn it into an object with various data sorted into properties. Using a a debug node we can examine the output. For example, the ‘channel’ property allows you to discriminate between different sensors on the shake. We are using a raspberry-shake ‘1D’  so we only get one channel ‘EHZ’.

 

Using the data:

[{"id":"d5d2efbb.2600e","type":"function","z":"b308f371.c9fa","name":"Average Magnitude","func":"acc = 0;\nnum = 0;\nfor(const p of msg.payload.packets)\n{\n    acc += Math.abs(p);\n    num++;\n}\n\nacc /= num;\n\nmsg.payload = acc;\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":360,"wires":[["c8911fad.47f72"]]},{"id":"c8911fad.47f72","type":"smooth","z":"b308f371.c9fa","name":"","property":"payload","action":"mean","count":"25","round":"0","mult":"single","x":780,"y":360,"wires":[["6270e4ac.2138fc","64858a7e.ecae14"]]}]

 

You can copy and paste this code-block to import an ‘Average Magnitude’ function-node, it takes the raw data from the rshake parser node and turns it into a more easily usable integer which describes the average amount of vibration over the past couple of seconds.

To import code-blocks like this, use the top right menu and select ‘import -> clipboard’ then a box will pop up allowing you to paste in the flow.

A flow that switches based on the average amplitude, and sets off a buzzer would look something like this.

We’ve plugged a buzzer into pin 16, and have a switch node set to measure when the average magnitude rises above a certain value. Then we either send 1 or 0 to the pin which turns it on or off; turning the buzzer on and off in the process.

 

Raspberry shake has their own examples that can be found on their GitHub account.

This one:  https://raw.githubusercontent.com/raspishake/raspshake-nodeRED-examples/master/realtime-plot-2.txt

Graphs seismic data onto node-red dashboard, you’ll need to install the ‘dashboard’ nodes through the palette manager.
When you import it make sure to replace their udp input node with yours, or change the configuration to match the one we made earlier.

One other change is the connection between the ‘switch’ node and the ‘format plot values’ node. The flow is designed to support a shake-3d which would be outputting 3 separate data streams (x, y, z).  If you followed this tutorial with a shake 1d then only one valid data stream will be coming out of the switch block, and it may not be one of those ones connected by default.

You can find the correct attachment point to use by trial and error, or by examining the data coming from the parser node and comparing it to the switch node.

To view the graphs generated by this flow, use the address you use to access node-red, with ‘/ui’ after it.
So in my case that would be 10.70.0.71:1880/ui

 

 

image_pdfGenerate PDFimage_printPrint
Posted in Uncategorized.

Start the discussion at community.t3alliance.org