Last active
July 14, 2017 11:53
-
-
Save zoutepopcorn/ea0824afc31681624aad2d97769bfb2f to your computer and use it in GitHub Desktop.
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
let app = require('express')(); | |
let server = require('http').Server(app); | |
let io = require('socket.io')(server); | |
let isRoot = require('is-root'); | |
const raspi = require('raspi'); | |
const Serial = require('raspi-serial').Serial; | |
let serial, sock, succes, error; | |
let current, tmp, line = ""; | |
const CMD = { "set_sms" : "AT+CMGF=1", | |
"sms" : "AT+CMGS=", | |
"pin" : "AT+CPIN=", | |
"at" : "AT", | |
"call" : "ATD>0<", | |
"hangup" : "ATH" }; | |
const RES = { "ok" : "OK", | |
"msg" : ">" }; | |
const ERR = { "error" : "ERROR" }; | |
if(!isRoot()) { | |
console.log(` You have to run this program as root.\r\nRestart with: sudo node`); | |
console.log('TODO: add serail to user group'); | |
process.exit(1); | |
} | |
let response = (res) => { | |
if(res == "") return; | |
//console.log(`>${res}<`); | |
for (let at in RES) { | |
if(res.indexOf(RES[at]) > -1) { | |
console.log("<< OK >>"); | |
succes(); | |
return; | |
} | |
} | |
for (let at in ERR) { | |
if(res.indexOf(ERR[at]) > -1) { | |
console.log("<< ERROR >>"); | |
error(); | |
return; | |
} | |
} | |
line = ""; | |
} | |
let sendAt = (command) => { | |
console.log(command); | |
if( ! command.startsWith("ctr") ) { | |
console.log(`cmd: ${command}`) | |
serial.write(command + "\r\n"); | |
} else { | |
console.log("ctrl"); | |
serial.write(0x1a); | |
//serial.write("\r"); | |
} | |
} | |
let getAt = (cmd, args) => { | |
for(let i = 0; i < args.length; i++) { | |
cmd = cmd.replace(`>${i}<`, args[i]); | |
} | |
return cmd; | |
} | |
let dataIn= (data) => { | |
const out = data.toString('utf8'); | |
line += out; | |
let arr = line.split("\r\n"); | |
if(arr.length > 1) { | |
console.log(`data: ${ arr[arr.length-1] }`); | |
for(i= 0; i < arr.length; i++) { | |
response(arr[i]); | |
} | |
line = arr[arr.length-1]; | |
} | |
} | |
sendSms = (nr, bericht) => { | |
console.log("sending sms"); | |
// sending a AT command | |
let at = function(value) { | |
console.log(`=> ${value}`); | |
return new Promise((resolve, reject) => { | |
succes = resolve; | |
error = reject; | |
sendAt(value); | |
}); | |
}; | |
// sending AT commands | |
at(CMD.set_sms) | |
.then(() => at(CMD.sms + nr)) | |
.then(() => at(bericht)) | |
.then(() => serial.write('\x1A')) | |
.catch((err) => console.log(err)); | |
} | |
raspi.init(() => { | |
console.log("init"); | |
serial = new Serial({"baudRate" : 115200 }); | |
serial.open(() => { | |
setTimeout(()=> { | |
sendSms("0031621863936", "hallo"); | |
}, 3000); | |
serial.on('data', (data) => { | |
dataIn(data); | |
}); | |
}); | |
}); | |
server.listen(8000); | |
app.get('/', function (req, res) { | |
res.sendFile(__dirname + '/index.html'); | |
}); | |
io.on('connection', function (socket) { | |
sock = socket; | |
socket.emit('news', { hello: 'world' }); | |
socket.on('my other event', function (data) { | |
console.log(data.my); | |
const out = data.my.toString('utf8') ; | |
serial.write(out + "\r"); | |
}); | |
}); |
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 raspi = require('raspi'); | |
const Serial = require('raspi-serial').Serial; | |
const term = require( 'terminal-kit' ).terminal ; | |
const express = require('express') | |
const app = express() | |
let serial; | |
let current = ""; | |
let cmd = { "set_sms" : "AT+CMGF=1", | |
"sms" : "AT+CMGS=" }; | |
let currentCb = () => { | |
}; | |
raspi.init(() => { | |
serial = new Serial({"boaudRate" : 115200 }); | |
serial.open(() => { | |
setTimeout(()=> { | |
serial.write( set_sms + '\r\n' ) | |
console.log( '>>' ); | |
}, 1000); | |
serial.on('data', (data) => { | |
console.log(data); | |
// process.stdout.write(data); | |
}); | |
}); | |
}); | |
app.get('/at/:cmd', function (req, res) { | |
if(serial) { | |
res.send('>> ' + req.params.cmd) | |
serial.write(req.params.cmd + "\r\n"); | |
} | |
}) | |
app.get('/sms/:cmd', function (req, res) { | |
if(serial) { | |
res.send('>> ' + req.params.cmd) | |
serial.write( cmd.set_sms + "\r\n" ) | |
} | |
}) | |
app.listen(3000, function () { | |
console.log('Example app listening on port 3000!') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment