Skip to content

Commit

Permalink
Added tare() command to zero the scale
Browse files Browse the repository at this point in the history
  • Loading branch information
Aldaviva committed Apr 10, 2023
1 parent 680a8d2 commit cf0cfb8
Show file tree
Hide file tree
Showing 6 changed files with 522 additions and 49 deletions.
Binary file added .github/images/SC05500000_small.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 33 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,58 @@ WebScale

Measure weight using a Stamps.com digital USB postage scale.

![Stamps.com 5-pound digital postage scale](http://store.stamps.com/web/images/catalog/sku/SC05500000_small.jpg)
![Stamps.com 5-pound digital postage scale](.github/images/SC05500000_small.jpg)

## Hardware
*I successfully performed these steps in [September 2014](https://twitter.com/Aldaviva/status/516771809962651648). Today, it may work differently, or not at all.*

# Hardware
1. Sign up for free trial at [Stamps.com](https://registration.stamps.com/registration/).
2. During signup, request a complimentary [5-pound digital postage scale](http://store.stamps.com/Store/catalog/product4.jsp?id=5lb-digital-postal-scale). Pay $9.99 shipping and handling.
3. Before the four-week trial is up, [cancel your account](https://stamps.custhelp.com/app/answers/detail/a_id/114/kw/cancel) and avoid the monthly fee.
2. During signup, request a complimentary [5-pound digital postage scale](https://store.stamps.com/collections/hardware/products/5lb-scale). Pay $9.99 USD shipping and handling.
3. Before the four-week trial is up, [cancel your account](https://www.stamps.com/postage-online/faqs/) and avoid the monthly fee.

# Installation
$ npm install webscale
## Installation
[This package is available on npm.](https://www.npmjs.com/package/webscale)
```sh
$ npm install webscale
```
In the event of installation trouble, refer to the [node-hid documentation](https://github.com/node-hid/node-hid/blob/master/README.md).

# Usage
## Usage
```javascript
var WebScale = require('webscale');
var webScale = new WebScale();
const WebScale = require('webscale');
const webScale = new WebScale();

webScale.on('change:weight', function(ounces){
console.log(ounces + ' oz.');
webScale.on('change:weight', ounces => {
console.log(`${ounces} oz.`);
});
```
See also `test-client.js`.
See also [`test-client.js`](https://github.com/Aldaviva/webscale/blob/master/test-client.js).

# Events
## Events

## change:weight
### change:weight
Called whenever the scale's weight reading changes.
### Arguments
- **ounces** how much weight is on the scale, in ounces. Can be negative if the scale was tared with weight on it.
#### Arguments
- **ounces** how much weight is on the scale, in ounces. Floating-point number with precision of one digit after the decimal place (tenths of ounces). Can be negative if the scale was tared with weight on it.

## connected
### connected
Called when the program first starts and connects successfully to the device, or when it reconnects after losing the connection.

## disconnected
### disconnected
Called when the device is unplugged or powered off, and once per reconnect attempt.

## error
### error
An error with the USB device occurred (except disconnections, see above).
### Arguments
#### Arguments
- **error** The error from node-hid.

# Methods
## Methods

## disconnect()
### tare()
Reset the scale so the current weight will be read as zero. The scale will also automatically tare itself each time it powers on.

### disconnect()
Disconnect from the USB device.

## See Also
- **[Aldaviva/WebScale.net](https://github.com/Aldaviva/WebScale.net)** - this library ported from Node.js to .NET
37 changes: 20 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,46 @@ var events = require('events');
var VENDOR_ID = 9332;
var PRODUCT_ID = 1360;


function WebScale(deviceIndex){
this._prevWeight = null;
this._deviceIndex = deviceIndex || 0;
this._device = null;

this._connectWithRetry();
setTimeout(this._connectWithRetry.bind(this), 0);
}

util.inherits(WebScale, events.EventEmitter);

WebScale.prototype.disconnect = function(){
if(this._device){
this.emit('disconnected');
this._device.close();
this._device = null;
this.emit('disconnected');
this._device.close();
this._device = null;
}
};

WebScale.prototype._connectWithRetry = function(){
try {
this._connect();
this._connect();
} catch(e){
setTimeout(this._connectWithRetry.bind(this), 2000);
setTimeout(this._connectWithRetry.bind(this), 2000);
}
};

WebScale.prototype._connect = function(){
var devices = HID.devices(VENDOR_ID, PRODUCT_ID);

if(!devices.length || !devices[this._deviceIndex]){
this.emit('disconnected');
throw new Error('could not find device');
this.emit('disconnected');
throw new Error('could not find device');
}

var devicePath = devices[this._deviceIndex].path;
try {
this._device = new HID.HID(devicePath);
this._device = new HID.HID(devicePath);
} catch(e){
this.emit('disconnected');
throw e;
this.emit('disconnected');
throw e;
}
this._onConnect(this._device);
};
Expand All @@ -60,18 +59,22 @@ WebScale.prototype._onData = function(data){
var ounces = data.readInt16LE(4)/10;

if(this._prevWeight !== ounces){
this._prevWeight = ounces;
this.emit('change:weight', ounces);
this._prevWeight = ounces;
this.emit('change:weight', ounces);
}
};

WebScale.prototype._onError = function(error){
if(error.message == 'could not read from HID device'){
this.emit('disconnected', error);
this._connectWithRetry();
this.emit('disconnected', error);
this._connectWithRetry();
} else {
this.emit('error', error);
this.emit('error', error);
}
};

WebScale.prototype.tare = function(){
this._device.write([0x04, 0x01]);
};

module.exports = WebScale;
Loading

0 comments on commit cf0cfb8

Please sign in to comment.