Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS Can someone explain this code for smart parking system

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)

}
 

Buy us a coffee!

Back
Top Bottom