https://github.com/zebrajaeger/remote-i2c
https://github.com/zebrajaeger/remote-i2c
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/zebrajaeger/remote-i2c
- Owner: zebrajaeger
- License: mit
- Created: 2020-11-01T15:45:06.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-11-03T08:00:40.000Z (over 4 years ago)
- Last Synced: 2024-12-28T19:02:46.557Z (5 months ago)
- Language: JavaScript
- Size: 54.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# i2c-over-http
[](https://npmjs.org/package/@zebrajaeger/remote-i2c)
Transparent connection to local i2c-bus or remote i2c-bus via http rest interface.
## Start Server from commandline
```bash
$ i2c-server --help
Usage: i2c-server [options]Options:
-b, --busNumber I²C-Bus number (default: "0")
-p, --port Port to bind on (default: "8080")
-h, --host Host, IP, ... to bind on (default: "0.0.0.0")
--help display help for command
```## Use Client from commandline
```bash
$ i2c-read --help
Usage: i2c-read [options]Options:
i²C Address (default: "0")
-p, --port Port for send request to (default: "8080")
-h, --host Host for send request to (default: "localhost")
-a, --address
-c, --count Number of bytes to read (default: "1")
--help display help for command
```## Use Client from code
For remote connection, first start server on the remote device.
```javascript
// const {HardwareI2C} = require('@zebrajaeger/remote-i2c');
const {HardwareI2C, HttpI2C} = require('../src/client');// for local execution use this one
//const i2c = new HardwareI2C(1); // on raspberry pi, we use i2c-bus #1// for remote execution use this one
const i2c = new HttpI2C('192.168.178.69', 8080); // remote raspberry pi(async () => {
try {
// read two 16 bit values (from i2c-joystick)
const buffer = await i2c.read(0x30, 4);
const x = buffer.readInt16LE(0);
const y = buffer.readInt16LE(2);
console.log({x, y});// read as hex-string for whatever
const hexString = await i2c.readAsHex(0x30, 4);
console.log({hexString});
} catch (err) {
console.log('Error', err)
}
})();
```