-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (47 loc) · 1.73 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
const algosdk = require('algosdk');
require('dotenv').config();
const mcu = process.env.SERIAL_DEVICE;
const serialPort = new SerialPort(mcu, { baudRate: JSON.parse(process.env.SERIAL_SPEED) });
const parser = new Readline();
serialPort.pipe(parser);
parser.on('data', temperature => {
temperature = temperature.split(' ').map(Number);
temperature = String.fromCharCode(temperature[0], temperature[1]);
sendToAlgorandBlockchain(temperature);
});
sendToAlgorandBlockchain = (value) => {
const baseServer = process.env.NODE;
const port = "";
const token = {
'X-API-Key': process.env.APIKEY
}
const algodclient = new algosdk.Algod(token, baseServer, port);
var mnemonic = process.env.MN;
var account = algosdk.mnemonicToSecretKey(mnemonic);
let note = algosdk.encodeObj(value);
(async () => {
let params = await algodclient.getTransactionParams();
let endRound = params.lastRound + parseInt(1000);
let txn = {
"from": account.addr,
"to": account.addr,
"fee": 10,
"amount": 0,
"firstRound": params.lastRound,
"lastRound": endRound,
"genesisID": params.genesisID,
"genesisHash": params.genesishashb64,
"note": note,
};
const txHeaders = {
'Content-Type': 'application/x-binary'
}
let signedTxn = algosdk.signTransaction(txn, account.sk);
let tx = (await algodclient.sendRawTransaction(signedTxn.blob, txHeaders));
console.log("Transaction : " + tx.txId);
})().catch(e => {
console.log(e);
});
}