Skip to content

A Javascript module for reading from Vernier Go Direct® Sensors

License

Notifications You must be signed in to change notification settings

minorgal/godirect-js

 
 

Repository files navigation

GODIRECT JS - Vernier Go Direct® Sensor Library Build Status Greenkeeper badge

A JavaScript library to easily interact with Vernier Go Direct® Sensors.

Requirements

This library is currently only implemented using WebBluetooth which has limited browser support of Chrome only.

Installation

Use the unpkg module version of library in your HTML document:

<script src="https://unpkg.com/@vernier/godirect/dist/godirect.min.umd.js"></script>

Use the library as a JavaScript module:

  1. Install the module via npm:
$ npm i @vernier/godirect
  1. Import the module into your JavaScript file:
import godirect from './node_modules/@vernier/godirect/dist/godirect.min.esm.js';

Example Usage

HTML

A user action is required to open the BLE device list in the browser (e.g. a user click event from a button). Create a button to initiate the request.

<button id="select_device">Select Go Direct Device</button>

JavaScript

Listen for the button click event.

const selectDeviceBtn = document.querySelector('#select_device');

selectDeviceBtn.addEventListener('click', async () => {
  // All following code will go here.
});

Open the browsers device list chooser. This call returns your selected device back. We'll assign the selected device to a constant called device;

NOTE: you cannot invoke godirect.selectDevice() outside of a user interaction, like a click event. This is part of the browser security model.

// opens the browser list of ble devices matching to GDX
const device = godirect.selectDevice();

// Get a filtered array of only the enabled sensor(s).
const enabledSensors = device.sensors.filter(s => s.enabled);

// Loop through the array of `enabledSensors` and log the value changes.
enabledSensors.forEach(sensor => {
  sensor.on('value-changed', (sensor) => {
    // log the sensor name, new value, and units.
    console.log(`Sensor: ${sensor.name} value: ${sensor.value} units: ${sensor.units}`);
  });
});

See this example and others (including our Python versions) at our examples repository: https://github.com/VernierST/godirect-examples or visit the live demo

License

GNU General Public License v3 (GPLv3)

Vernier products are designed for educational use. Our products are not designed nor are they recommended for any industrial, medical, or commercial process such as life support, patient diagnosis, control of a manufacturing process, or industrial testing of any kind.

About

A Javascript module for reading from Vernier Go Direct® Sensors

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HTML 55.0%
  • JavaScript 45.0%