UV
Javascript Example
Device Compatibility
Overview
The UV driver reports values for:
- UV Index scale used in the United States conforms with international guidelines for UVI reporting established by the World Health Organization. From UV Index Scale
- UV Risk scale established by World Health Organization. From UV Index Scale
Available ZeroMQ Ports
Base port
: 20029Keep-alive port
: 20030Error port
: 20031Data Update port
: 20032
Code Example
The following sections show how to implement a connection to each of the UV driver's ports. You can download this example here.
Initial Variables
Before we go into connecting to each port, the variables defined below are needed in order to access the ZeroMQ and MATRIX Protocol Buffer libraries for Javascript. We also define a few helpful variables for easy references.
var zmq = require('zeromq');// Asynchronous Messaging Framework var matrix_io = require('matrix-protos').matrix_io;// Protocol Buffers for MATRIX function var matrix_ip = '127.0.0.1';// Local IP var matrix_uv_base_port = 20029;// Port for UV driver
Base Port
Here is where the configuration for our UV example goes. Once we connect to the Base Port, We will pass a configuration to the UV driver. With this we can set the update rate and timeout configuration.
// Create a Pusher socket var configSocket = zmq.socket('push'); // Connect Pusher to Base port configSocket.connect('tcp://' + matrix_ip + ':' + matrix_uv_base_port); // Create driver configuration var config = matrix_io.malos.v1.driver.DriverConfig.create({ // Update rate configuration delayBetweenUpdates: 2.0,// 2 seconds between updates timeoutAfterLastPing: 6.0,// Stop sending updates 6 seconds after pings. }); // Send driver configuration configSocket.send(matrix_io.malos.v1.driver.DriverConfig.encode(config).finish());
Keep-alive Port
The next step is to connect and send a message to the Keep-alive Port. That message, an empty string, will grant us a response from the Data Update Port for the current UV value. An interval for pinging is then set to continuously obtain that data.
// Create a Pusher socket var pingSocket = zmq.socket('push'); // Connect Pusher to Keep-alive port pingSocket.connect('tcp://' + matrix_ip + ':' + (matrix_uv_base_port + 1)); // Send initial ping pingSocket.send(''); // Send ping every 5 seconds setInterval(function(){ pingSocket.send(''); }, 5000);
Error Port
Connecting to the Error Port is optional, but highly recommended if you want to log any errors that occur within MATRIX CORE.
// Create a Subscriber socket var errorSocket = zmq.socket('sub'); // Connect Subscriber to Error port errorSocket.connect('tcp://' + matrix_ip + ':' + (matrix_uv_base_port + 2)); // Connect Subscriber to Error port errorSocket.subscribe(''); // On Message errorSocket.on('message', function(error_message){ console.log('Error received: ' + error_message.toString('utf8'));// Log error });
Data Update Port
A connection to the Data Update Port will allow us to receive the current UV data we want.
// Create a Subscriber socket var updateSocket = zmq.socket('sub'); // Connect Subscriber to Data Update port updateSocket.connect('tcp://' + matrix_ip + ':' + (matrix_uv_base_port + 3)); // Subscribe to messages updateSocket.subscribe(''); // On Message updateSocket.on('message', function(buffer){ var data = matrix_io.malos.v1.sense.UV.decode(buffer);// Extract message console.log(data);// Log new UV data });
Data Output
The javascript object below is an example output you'll receive from the Data Update Port.{ uvIndex: 0.0010000000474974513, omsRisk: 'Low' }