Created
January 22, 2019 22:55
-
-
Save lights0123/b97d230879ba128b26314eb5bf97c5c5 to your computer and use it in GitHub Desktop.
TP9605 Datalogger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort(); | |
const readline = require('readline'); | |
const chalk = require('chalk'); | |
let start = new Date(); | |
let data = [['time', 'temp']]; | |
let go = false; | |
function log(...data){ | |
if(go) console.log(chalk.green(...data)); | |
else console.log(chalk.yellow(...data)); | |
} | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
rl.on('line', () => { | |
if (go) { | |
go = false; | |
console.log(chalk.blue.bold(data.map(d => d.join(',')).join('\n'))); | |
return; | |
} | |
data = [['time', 'temp']]; | |
start = new Date(); | |
go = true; | |
}); | |
btSerial.on('found', function (address, name) { | |
console.log(address, name); | |
if (name !== 'TP9605') return; | |
btSerial.findSerialPortChannel(address, function (channel) { | |
btSerial.connect(address, channel, function () { | |
console.log('connected'); | |
btSerial.write(new Buffer('my data', 'utf-8'), function (err, bytesWritten) { | |
if (err) console.log(err); | |
}); | |
let buf = ''; | |
btSerial.on('data', function (buffer) { | |
buf += buffer.toString('utf-8'); | |
const plus = buf.indexOf('+') === -1 ? Infinity : buf.indexOf('+'); | |
const minus = buf.indexOf('-') === -1 ? Infinity : buf.indexOf('-'); | |
if (Math.min(plus, minus) !== Infinity) { | |
buf = buf.substring(Math.min(plus, minus)); | |
if (buf.length >= 7 && buf.indexOf('\n') !== -1) { | |
if (buf.charAt(5) === ' ' && !Number.isNaN(parseInt(buf.charAt(6)))) { | |
let num = parseInt(buf); | |
const power = parseInt(buf.charAt(6)); | |
num = num * Math.pow(10, (3 - power) * 3 - 1); | |
num = num.toFixed(1); | |
if(go) data.push([(new Date() - start) / 1000, num]); | |
log(num); | |
} | |
buf = buf.substring(buf.indexOf('\n')); | |
} | |
} | |
}); | |
}, function () { | |
console.log('cannot connect'); | |
}); | |
// close the connection when you're ready | |
btSerial.close(); | |
}, function () { | |
console.log('found nothing'); | |
}); | |
}); | |
btSerial.inquire(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "multimeter", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"bluetooth-serial-port": "^2.1.7", | |
"chalk": "^2.4.2" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | |
# yarn lockfile v1 | |
"@types/node@^7.0.10": | |
version "7.10.2" | |
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.10.2.tgz#a98845168012d7a63a84d50e738829da43bdb0de" | |
integrity sha512-RO4ig5taKmcrU4Rex8ojG1gpwFkjddzug9iPQSDvbewHN9vDpcFewevkaOK+KT+w1LeZnxbgOyfXwV4pxsQ4GQ== | |
ansi-styles@^3.2.1: | |
version "3.2.1" | |
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d" | |
integrity sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA== | |
dependencies: | |
color-convert "^1.9.0" | |
[email protected]: | |
version "1.2.1" | |
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.2.1.tgz#14ad6113812d2d37d72e67b4cacb4bb726505f11" | |
integrity sha1-FK1hE4EtLTfXLme0ystLtyZQXxE= | |
bluetooth-serial-port@^2.1.7: | |
version "2.1.7" | |
resolved "https://registry.yarnpkg.com/bluetooth-serial-port/-/bluetooth-serial-port-2.1.7.tgz#bfc29c10547a7f4a3334aed88f462b636dda2e98" | |
integrity sha512-66Sz/aB9RNxxdhJusV29xvPmlYeG3pksRXSN6PJ6XYr+hK/1oejKhZz5sd1vKyzrwoUyHtyW6JLXn0iuPfJgew== | |
dependencies: | |
bindings "1.2.x" | |
nan latest | |
optionalDependencies: | |
"@types/node" "^7.0.10" | |
chalk@^2.4.2: | |
version "2.4.2" | |
resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424" | |
integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ== | |
dependencies: | |
ansi-styles "^3.2.1" | |
escape-string-regexp "^1.0.5" | |
supports-color "^5.3.0" | |
color-convert@^1.9.0: | |
version "1.9.3" | |
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8" | |
integrity sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg== | |
dependencies: | |
color-name "1.1.3" | |
[email protected]: | |
version "1.1.3" | |
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25" | |
integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU= | |
escape-string-regexp@^1.0.5: | |
version "1.0.5" | |
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" | |
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ= | |
has-flag@^3.0.0: | |
version "3.0.0" | |
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd" | |
integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0= | |
nan@latest: | |
version "2.12.1" | |
resolved "https://registry.yarnpkg.com/nan/-/nan-2.12.1.tgz#7b1aa193e9aa86057e3c7bbd0ac448e770925552" | |
integrity sha512-JY7V6lRkStKcKTvHO5NVSQRv+RV+FIL5pvDoLiAtSL9pKlC5x9PKQcZDsq7m4FO4d57mkhC6Z+QhAh3Jdk5JFw== | |
supports-color@^5.3.0: | |
version "5.5.0" | |
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f" | |
integrity sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow== | |
dependencies: | |
has-flag "^3.0.0" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment