OpenSensors now capture seismic data from the Euro-Med Seismic Centre (EMSC) and the United States Geological Survey (USGS). Every ten minutes we are polling the latest information of major and minor earthquakes around the globe and make this information available via our programming interface (API) or as MQTT feed.
In this short tutorial, we’re showing you how to use OpenSensors together with Node-RED to receive email alerts whenever there’s a major incident in a region of interest. You can use this guide as starting point for further experiments with Node-RED and develop your own earthquake-triggered workflows. Let’s shake it.
On OpenSensors:
First, you need to login to your account on OpenSensor or sign up for one if you haven’t done so already at https://opensensors.io.
Next, it’s good practice to have a new ‘device’ for this application, i.e. a dedicated set of credentials you’re going to use to log in to OpenSensors for this particular set of MQTT feeds.
In the panel on the left, click My Devices in the Devices menu. Click the yellow Create New Device button at the top of the page.
Optional: Add some optional descriptions and press the disk icon to save your new device.
Take a note of your ‘Client id’ and ‘Password’ as you’re going to need them in your Node-RED workflow.
For Node-RED:
Install node.js and Node-RED on your system. There’s a very good guide for this on the Node-RED website: http://nodered.org/docs/getting-started/installation.html. Follow the instructions, including the separate section on Running Node-RED.
Once you’re ready, open a web browser and direct it to localhost:1880, the default address and port of the Node-RED GUI on your system.
(A very basic description of the Node-RED vocabulary can also be found at SlideShare: http://www.slideshare.net/BorisAdryan/node-red-workflowcoursetoulouse).
Developing a workflow:
From the input panel of your nodes library on the left side, drag and drop a pink mqtt input node into the work area named Sheet 1.
Double-click the mqtt node. A window with configuration details opens.
Click the pen symbol next to ‘Add new mqtt-broker...’. Your Broker is mqtt.opensensors.io, your Client ID and Password those you generated in the previous step on the OpenSensors website, and User is your OpenSensor user name.
Once the Broker is defined, enter /orgs/EMSC/+ into the Topic field. This is going to instruct Node-RED to subscribe to all MQTT topics generated by the EMSC.
Optional: Set the Name of this node to ‘EMSC’.
Drag and drop a second mqtt input node. When you double-click the node, you will realise that the Broker settings default to the ones you previously entered.
Enter /orgs/USGS/+ in the Topics field and ‘USGS’ as optional Name.
Drag and drop a dark green debug node from the output panel on the left. While debugging has the connotation of fixing a problem, in Node-RED it’s the default way of directly communicating messages to the user.
Draw connection lines (“pipes”) from both mqtt nodes to the debug node.
Press the red Deploy button in the upper right corner. This starts your Node-RED flow. If everything worked, you should see ‘connected’ underneath the mqtt nodes and your debug panel (on the right) should soon produce the following JSON-formatted output if there’s an event (which may take a while!):
While it is pleasing to be informed about every time the earth shakes, it soon becomes tedious staring at the debug panel in expectation of an earthquake. Also, you may not be interested in events in remote areas of the world, or exactly in those - whatever interests you.
We are going to extend our flow with some decision making:
First, we need to parse the information from the EMSC and USGS. For this example, we’re going to be particularly interested in the fields region and magnitude. There are plenty more fields in their records, and you may want to adjust this flow to your needs.
Drag and drop a pale orange function node from the functions panel into your flow. Connect both mqtt nodes to the input side (the left side) of your function node. Function nodes allow you directly interact with your data using JavaScript.
Enter the following code (or download the OpenSensors workflow).
Here be a JavaScript course… :-) In a nutshell, this code takes data from the ‘payload’ of the incoming message (read up on the topic and payload concept of Node-RED in the SlideShare article suggested earlier). The payload is then parsed for the region and magnitude fields using standard regular expressions. If we can successfully extract information (in this case: the region containing ‘ia’ somewhere in it’s name), we’re going to set the outgoing message’s payload to the magnitude, its topic to ‘EVENT in ‘ plus the name of the region and pass it on (‘return msg’) to the next node.
Drag and drop a lime green switch node from the function panel into your workflow. Connect the output of the function node to the input of the switch node. Configure (by double-clicking) the switch node to assert if the payload (being the magnitude of the earthquake) is greater than 2. Only then the message is going to be passed on.
Last, we’re going to drag and drop a light green e-mail output node from the social panel and configure it like an e-mail client, but with a default recipient: here in this case, ohmygodithappend@gmail.com.
Connect the output of the switch node to our debug node, as well as to the outgoing e-mail node.
We can then deploy the new workflow and should see something like this after a while:
In this case, an event was detected ‘off the coast of Northern California’ with a magnitude of 4.4 and at the same time, you should receive an e-mail with the region as subject and the magnitude in the body of the e-mail.
We hope that this flow is getting you started! Remember that Node-RED is superbly suited to interact with hardware… ...imagine LEDs and buzzers indicating an earthquake.
Comments