tfmini-plus

2.1.0 • Public • Published

tfmini-plus

A Node.js library for the TFMini Plus.

This little device can measure long distances and is reasonably priced.

The TFMini Plus supports UART and I2C communication. The factory default is UART and can be changed programmatically.

tfmini-plis
(A tfmini plus connected to a Raspberry Pi Zero W)

tfmini-plus
(An i2cdriver controlling an ssd1327 display with the measurement)

BYO[protocol]

Since you can communicate using I2C or UART- I've opted to leave the communication protocol libraries out to keep from installing extra cruft that isn't needed.

This means you will need to install the i2c-bus-promise or @serialport/bindings independently.

Interface

All functions return a Promise.

DON'T FORGET TO await (or use .then())!!

I2C(bus, address = 0x10, provider = 'i2c-bus')

Creates an I2C tfminiplus instance.

bus:
· (Object) An existing bus instance
· (String or Number) The bus number to open
address: (Number) The i2c device address (factory default is 0x10)
provider: (String) The Node module to use

Returns: a Promise that resolve with the tfminiplus instance.

To use I2C, you will need to install i2c-bus OR i2c-bus-promise

.. OR, for development- Check out an i2cdriver!

npm install tfmini-plus i2c-bus
const tfminiplus = require('tfmini-plus')
 
tfminiplus.I2C(1, 0x10)
.then(async (tfmp) => {
  const measurements = await tfmp.measure()
  console.log('distance:', measurements.distance)
  console.log('strength:', measurements.strength)
  console.log('temperature:', measurements.temperature)
})
.catch(console.error)

... with i2cdriver:

const tfminiplus = require('tfmini-plus')
 
tfminiplus.I2C('/dev/tty.usbserial-DO010000', 0x10, 'i2cdriver/i2c-bus')
.then(async (tfmp) => {
  console.log('distance:', await tfmp.distance())
})
.catch(console.error)

UART(path, options)

Creates an UART tfminiplus instance.

path: The device path
options: The serialport options

Returns: a Promise that resolve with the tfminiplus instance. To use UART, you will need to install @serialport/bindings

npm install tfmini-plus @serialport/bindings
const tfminiplus = require('tfmini-plus')
tfminiplus.UART('/dev/serial0', { baudRate: 115200 }).then(async (tfmp) => {
  const measurements = await tfmp.measure()
  console.log('distance:', measurements.distance)
  console.log('strength:', measurements.strength)
  console.log('temperature:', measurements.temperature)
})
.catch(console.error)

The tfminiplus instance

version()

Returns the device version number (string)

measure(unit = 'mm')

Gets a measurement from the device.

returns:

{
    distance: number,
    strength: number,
    temperature: number
}

tfminiplus.distance(unit = 'mm')

Does the same thing as measure(), but only returns the distance (number).

tfminiplus.address(addr)

Changes the I2C device address. You must call save() to have the setting persist.

tfminiplus.save()

Saves the current device settings.

tfminiplus.reset()

Resets the device.

tfminiplus.restore()

Restores the factory settings.

tfminiplus.mode(type='i2c')

Sets the mode: i2c or uart

References

https://acroname.com/sites/default/files/assets/sj-pm-tfmini_plus_a04_product_mannual_en.pdf

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i tfmini-plus

Weekly Downloads

4

Version

2.1.0

License

MIT

Unpacked Size

7.59 MB

Total Files

13

Last publish

Collaborators

  • williamkapke