-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Luca Brandimarti
committed
May 25, 2020
1 parent
e9b4cc4
commit 3ad087b
Showing
1 changed file
with
53 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,57 @@ | ||
const { Board, Thermometer } = require("johnny-five"); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const { Board, Thermometer } = require('johnny-five') | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
const board = new Board() | ||
|
||
|
||
board.on("ready", () => { | ||
|
||
const thermometer = new Thermometer({ | ||
controller: "TMP36", | ||
pin: "A0" | ||
}); | ||
|
||
thermometer.on("change", () => { | ||
|
||
const ts = Date.now(); | ||
const date = new Date(ts); | ||
let day = date.getDate(); | ||
let month = date.getMonth() + 1; | ||
let hours = date.getUTCHours() +2; | ||
let year = date.getFullYear() | ||
const {celsius} = thermometer; | ||
let record = day + ',' + month + ',' + year + ',' + hours + ',' + celsius; | ||
|
||
console.log("Thermometer"); | ||
console.log(" day : ", day); | ||
console.log(" month : ", month); | ||
console.log(" year : ", year) | ||
console.log(" hours : ", hours); | ||
console.log(" celsius : ", celsius); | ||
console.log("--------------------------------------"); | ||
|
||
var fs = require('fs'); | ||
var logStream = fs.createWriteStream(path.resolve('./src/database/log.csv'), {flags: 'a'}); | ||
logStream.write(record); | ||
logStream.end('\n'); | ||
sleep(1000); | ||
|
||
}); | ||
|
||
|
||
function sleep(milliseconds) { | ||
const date = Date.now(); | ||
let currentDate = null; | ||
do { | ||
currentDate = Date.now(); | ||
} while (currentDate - date < milliseconds); | ||
} | ||
|
||
}); | ||
|
||
|
||
module.exports = {board}; | ||
|
||
function start() { | ||
board.on('ready', () => { | ||
const thermometer = new Thermometer({ | ||
controller: 'TMP36', | ||
pin: 'A0', | ||
}) | ||
|
||
thermometer.on('change', () => { | ||
const ts = Date.now() | ||
const date = new Date(ts) | ||
let day = date.getDate() | ||
let month = date.getMonth() + 1 | ||
let hours = date.getUTCHours() + 2 | ||
let year = date.getFullYear() | ||
const { celsius } = thermometer | ||
let record = day + ',' + month + ',' + year + ',' + hours + ',' + celsius | ||
save(day,month,hours,year,celsius,record); | ||
}) | ||
}) | ||
} | ||
|
||
function save(day,month,hours,year,celsius,record) { | ||
|
||
console.log('Thermometer') | ||
console.log('day: ', day) | ||
console.log('month: ', month) | ||
console.log('year: ', year) | ||
console.log('hours: ', hours) | ||
console.log('celsius: ', celsius) | ||
console.log('--------------------------------------') | ||
|
||
var fs = require('fs') | ||
var logStream = fs.createWriteStream(path.resolve('./src/database/log.csv'), { | ||
flags: 'a', | ||
}) | ||
logStream.write(record) | ||
logStream.end('\n') | ||
sleep(60000); | ||
} | ||
|
||
function sleep(milliseconds) { | ||
const date = Date.now() | ||
let currentDate = null | ||
do { | ||
currentDate = Date.now() | ||
} while (currentDate - date < milliseconds) | ||
} | ||
|
||
|
||
module.exports = { board, start } | ||
|