Twinkle
New Coder
//Ultrasonic car parking data transfer
var webSocketUrl="wss://"
var device_id="Enter device id:";//Ultrasonic device id
var device_token="Enter device token:";//Ultrasonic device token
//import websocket module
var WebSocket=require('ws');
var isWebSocketReady=false;
var data="";
var ws=null;
//Import serialport module
var serialport=require("serialport");
var Serialport=serialport.Serialport;
var sp=new Serialport("/dev/ttyACM0",{//For serial communication
baudrate:9600,
//UNO is used so baudrate will be 9600
parser:serialport.parsers.readline("\n")
});
var parkstate=0;//To check parking state
//Get current time in milli seconds
function getTime(){
return parseInt(date.now().toString());
}
//Create web socket connection to set up GPIO pin
function start(){
//Create websocket connection
isWebSocketReady=false;
ws=new WebSocket(webSocketUrl);
ws.on('open',function(){
console.log("Web socket connection is open");
//After creating connection register with authorization information
register();
});
ws.on('message',function(data){
//Loop is executed whenever user sends any message
handleRcvMsg(data)//data is recieved to function handleRcvMsg()
});
ws.on('close',function(){
console.log("Web socket connection is closed");
});
//Send register message to websocket endpoint
function register(){
console.log("Registering device on the websocket connection");
try{
var registerMessage = '{"type":"register", "sdid":"'+device_id+'", "Authorization":"bearer '+device_token+'", "cid":"'+getTime()+'"}';
console.log('sending register message'+registerMessage+'\n');
ws.send(registerMessage,{mask:true});
isWebSocketReady=true
}
catch(e){
console.error("Error in registering message. Failed to register"+e.toString());
}
}
//Data after recxieving is sent for processing
function handleRcvMsg(msg){
var msgObj=JSON.parse(msg);
if(msgObj.type!="action")return; //Early return
var actions=msgObj.data.actions;
var actionName=actions[0].name; //Assume there is only one action in actions
console.log("The recieved action is"+actionName);
//Know the registered actions to perform accordingly
if(actionName.toLowerCase()=="parking state")
{
}
else{
//Loop will execute in case of unregistered action
//Register every action in cloud
console.log("Do nothing since unrecognized action"+actionName);
return;
}
}
//Send commands to cloud
function sendToCloud(parking){
try{
ts=', "ts":' +getTime();
var data={
"ultrasonic": parking
//Set the arguement to cloud variable
//Get the value from arduino
};
var payload='{"sdid":"'+device_id+'"'+ts+', "data": '+JSON.stringify(data)+', "cid":"'+getTime()+'"}';
console.log('Sending payload ' + payload + '\n');
ws.send(payload, {mask: true});
} catch (e) {
console.error('Error in sending a message: ' + e.toString() +'\n');
}
}
function exitpins() {
console.log('Exit and destroy all pins!');
process.exit();
}
start();
//executes every time when data is received from arduino
//There is a 30 sec programmed delay from Arduino
sp.on("open", function () {
sp.on('data', function(data) {
console.log("Serial port received data:" + data);
//sendToCloud(data);//parking data to cloud
sendToCloud(data);
});
});
process.on('SIGINT', exitpins)
}
var webSocketUrl="wss://"
var device_id="Enter device id:";//Ultrasonic device id
var device_token="Enter device token:";//Ultrasonic device token
//import websocket module
var WebSocket=require('ws');
var isWebSocketReady=false;
var data="";
var ws=null;
//Import serialport module
var serialport=require("serialport");
var Serialport=serialport.Serialport;
var sp=new Serialport("/dev/ttyACM0",{//For serial communication
baudrate:9600,
//UNO is used so baudrate will be 9600
parser:serialport.parsers.readline("\n")
});
var parkstate=0;//To check parking state
//Get current time in milli seconds
function getTime(){
return parseInt(date.now().toString());
}
//Create web socket connection to set up GPIO pin
function start(){
//Create websocket connection
isWebSocketReady=false;
ws=new WebSocket(webSocketUrl);
ws.on('open',function(){
console.log("Web socket connection is open");
//After creating connection register with authorization information
register();
});
ws.on('message',function(data){
//Loop is executed whenever user sends any message
handleRcvMsg(data)//data is recieved to function handleRcvMsg()
});
ws.on('close',function(){
console.log("Web socket connection is closed");
});
//Send register message to websocket endpoint
function register(){
console.log("Registering device on the websocket connection");
try{
var registerMessage = '{"type":"register", "sdid":"'+device_id+'", "Authorization":"bearer '+device_token+'", "cid":"'+getTime()+'"}';
console.log('sending register message'+registerMessage+'\n');
ws.send(registerMessage,{mask:true});
isWebSocketReady=true
}
catch(e){
console.error("Error in registering message. Failed to register"+e.toString());
}
}
//Data after recxieving is sent for processing
function handleRcvMsg(msg){
var msgObj=JSON.parse(msg);
if(msgObj.type!="action")return; //Early return
var actions=msgObj.data.actions;
var actionName=actions[0].name; //Assume there is only one action in actions
console.log("The recieved action is"+actionName);
//Know the registered actions to perform accordingly
if(actionName.toLowerCase()=="parking state")
{
}
else{
//Loop will execute in case of unregistered action
//Register every action in cloud
console.log("Do nothing since unrecognized action"+actionName);
return;
}
}
//Send commands to cloud
function sendToCloud(parking){
try{
ts=', "ts":' +getTime();
var data={
"ultrasonic": parking
//Set the arguement to cloud variable
//Get the value from arduino
};
var payload='{"sdid":"'+device_id+'"'+ts+', "data": '+JSON.stringify(data)+', "cid":"'+getTime()+'"}';
console.log('Sending payload ' + payload + '\n');
ws.send(payload, {mask: true});
} catch (e) {
console.error('Error in sending a message: ' + e.toString() +'\n');
}
}
function exitpins() {
console.log('Exit and destroy all pins!');
process.exit();
}
start();
//executes every time when data is received from arduino
//There is a 30 sec programmed delay from Arduino
sp.on("open", function () {
sp.on('data', function(data) {
console.log("Serial port received data:" + data);
//sendToCloud(data);//parking data to cloud
sendToCloud(data);
});
});
process.on('SIGINT', exitpins)
}