Things used in this project

Schematics

Screenshot of Web Application
Cloud 5mhgtlhppd
Block Diagram of Smart Parking System
Block diagram (1) l2nmp0w2dn
Parking lot using ultrasonic sensor
Sonar lot bb updated amyeoueksd

Code

Raspberry Pi (staff-parking)JavaScript
Source code for Raspberry Pi
//staff parking data transfer
var webSocketUrl = "wss://api.artik.cloud/v1.1/websocket?ack=true";
var device_id = "Your Device id"; // staff  parking DEVICE ID
var device_token = "Your Device Token"; //staff parking DEVICE TOKEN
// require websocket module to connect 
// execute following two commands to your pi's terminal
// sudo apt-get update
// npm install websocket
var WebSocket = require('ws');
var isWebSocketReady = false;
var data="";
var ws = null;
// require serialport module to raspberry pi 
// execute following command to terminal
// npm install serialport
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var sp = new SerialPort("/dev/ttyACM0", { //for serial communication with arduino 
    baudrate: 9600,  
// we are using UNO so baudrate is 9600, you might need to change according to your model
    parser: serialport.parsers.readline("\n")
});

var parking_state=0;// variable to check for parking state_gate

/**
 * Gets the current time in millis
 */
function getTimeMillis(){
    return parseInt(Date.now().toString());
}

/**
 * Create a /websocket connection and setup GPIO pin
 */
function start() {
    //Create the WebSocket connection
    isWebSocketReady = false;
    ws = new WebSocket(webSocketUrl);
	// this function invoked on successful connection
    ws.on('open', function() {
        console.log("WebSocket connection is open ....");
		// you must register for successful data transmission
		// registration is for authentication or secure data transfer
        register();
    });
    ws.on('message', function(data) {
      //this loop is called whenever the client sends some message
         handleRcvMsg(data); //data is send to the function handleRcvMsg()
    });
    ws.on('close', function() {
        console.log("WebSocket connection is closed ....");

    });      
    
}

/**
 * Sends a register message to /websocket endpoint
 */
//Client will only work when device gets registered from here
function register(){
    console.log("Registering device on the WebSocket connection");
    try{
        var registerMessage = '{"type":"register", "sdid":"'+device_id+'", "Authorization":"bearer '+device_token+'", "cid":"'+getTimeMillis()+'"}';
        console.log('Sending register message ' + registerMessage + '\n');
        ws.send(registerMessage, {mask: true});
        isWebSocketReady = true;
    }
    catch (e) {
        console.error('Failed to register messages. Error in registering message: ' + e.toString());
    }    
}


//data after receiving is sent here for processing
// in our case this function will not be used as we will not receive any action
// in raspberry pi. This is for future modification.
function handleRcvMsg(msg){
	// you have to parse the received string
    var msgObj = JSON.parse(msg);
    if (msgObj.type != "action") return; //Early return;

    var actions = msgObj.data.actions;
    var actionName = actions[0].name; 
    console.log("The received action is " + actionName);
  
    //you must know your registered actions in order to perform accordinlgy
    if (actionName.toLowerCase() == "settext") 
    { 
        // do something here after receiving 'parking_state'
		console.log('receving recognized action' + actionName);
    }
    else {
         //this loop executes if some unregistered action is received
         //so you must register every action in cloud
        console.log('Do nothing since receiving unrecognized action ' + actionName);
        return;
    }
   
}



/**
 * Send one message to ARTIK Cloud
 */
//This function is responsible for sending commands to cloud
//function sendStateToArtikCloud(parking) sends number of free parking slot to artik cloud
function sendStateToArtikCloud(parking_slot){
    try{
        ts = ', "ts": '+getTimeMillis();
        var data = {
            "parking_slot": parking_slot
            //setting the parking value from argument to our cloud variable "parking_value"

            };
        var payload = '{"sdid":"'+device_id+'"'+ts+', "data": '+JSON.stringify(data)+', "cid":"'+getTimeMillis()+'"}';
        console.log('Sending payload ' + payload + '\n');
        ws.send(payload, {mask: true});
    } catch (e) {
        console.error('Error in sending a message: ' + e.toString() +'\n');
    }    
}



function exitClosePins() {
    
        console.log('Exit and destroy all pins!');
        process.exit();
    
}


start();
//exectes every time when data is received from arduino (30 sec programmed delay from arduino)
sp.on("open", function () {
    sp.on('data', function(data) {
            console.log("Serial port received data:" + data);
            //sendStateToArtikCloud(data);//free parking slot
			var parking_slot = parseInt(data);
            sendStateToArtikCloud(parking_slot);
           
    });
});

process.on('SIGINT', exitClosePins);
Arduino (sonar)C/C++
Source code for sonar sensor interfacing using Arduino
#define echoPin1 2 // Echo Pin for sonar 1
#define trigPin1 3 // Trigger Pin for sonar 1
#define echoPin2 4 // Echo Pin for sonar 2 
#define trigPin2 5 // Trigger Pin for sonar 2
#define echoPin3 6 // Echo Pin for sonar 3
#define trigPin3 7 // Trigger Pin for sonar 3
//#define echoPin4 9 // Echo Pin for sonar 4
//#define trigPin4 8 // Trigger Pin for sonar 4


long duration1, distance1; // Duration used to calculate distance
long duration2, distance2;
long duration3, distance3;
//long duration4, distance4; 

int count=0;
int freeSlot =0;

void setup() {
 Serial.begin (9600); // initiate serial communication to raspberry pi
 pinMode(trigPin1, OUTPUT); // trigger pin as output
 pinMode(echoPin1, INPUT);  // echo pin as input
 pinMode(trigPin2, OUTPUT);
 pinMode(echoPin2, INPUT);
 pinMode(trigPin3, OUTPUT);
 pinMode(echoPin3, INPUT);
 //pinMode(trigPin4, OUTPUT);
 //pinMode(echoPin4, INPUT);
 
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 

/* At least 10 microsecond high level signal is required to trigger 
 * pin. 
 *               _____
 *              |     |
 * -------------!     !---------
 *         .....|10us |........
 * the module then produce eight 40KHz pulse signal and waits to receive echo
 */
 digitalWrite(trigPin1, LOW); 
 delayMicroseconds(2); 
 digitalWrite(trigPin1, HIGH);
 delayMicroseconds(10); 
 digitalWrite(trigPin1, LOW);
 // pulseIn( ) function determines a pulse width in time
 // duration of pulse is proportional to distance of obstacle
 duration1 = pulseIn(echoPin1, HIGH);

 digitalWrite(trigPin2, LOW);
 delayMicroseconds(2); 
 digitalWrite(trigPin2, HIGH);
 delayMicroseconds(10); 
 digitalWrite(trigPin2, LOW);
 duration2 = pulseIn(echoPin2, HIGH);
 
 digitalWrite(trigPin3, LOW);
 delayMicroseconds(2); 
 digitalWrite(trigPin3, HIGH);
 delayMicroseconds(10); 
 digitalWrite(trigPin3, LOW);
 duration3 = pulseIn(echoPin3, HIGH);

 //digitalWrite(trigPin4, LOW);
 //delayMicroseconds(2); 
 //digitalWrite(trigPin4, HIGH);
 //delayMicroseconds(10); 
 //digitalWrite(trigPin4, LOW);
 //duration4 = pulseIn(echoPin4, HIGH);
 
 //  distance = (high level timevelocity of sound (340M/S) / 2, 
 //  in centimeter = uS/58
 distance1 = duration1/58.2;
 if(distance1<10)
   distance1 = 1;
 else distance1 = 0;
 
 distance2 = duration2/58.2;
 if(distance2<10)
   distance2 = 1;
 else distance2 = 0;
 
 distance3 = duration3/58.2;
 if(distance3<10)
   distance3 = 1;
 else distance3 = 0;

 //distance4 = duration4/58.2;
 //if(distance4<10)
   //distance4 = 1;
 //else distance4 = 0;

 // add the result from all sensor to count total car
 count = distance1 + distance2 + distance3 ;;

 // free slot = total slot - total car
 freeSlot = 3 - count;
 // number of total slot is sent to raspberry pi using usb
 Serial.println(freeSlot);
 // the status is updated every 30 seconds.
 delay(5000);
 //freeSlot = 0;
 //distance1 = 0;
 //distance2 = 0;
// distance3 = 0;
//distance4 = 0;
}
Web App (HTML)HTML
Source code for Front End Web Application
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>ARTIK Cloud based Smart Parking System</title> 
        <script src="websocket_client.js"></script>
		<style>
		rainbowParking {
			display: block;
			background-color: #dddddd;
			padding: 30px;
			font-size: 70px;
			line-height: 60%;
			margin-left:40px;
			margin-right:40px;
			border-style: groove;
		}
		
		indigoParking {
			display: block;
			background-color: #dddddd;
			padding: 30px;
			font-size: 70px;
			line-height: 60%;
			margin-left:40px;
			margin-right:40px;
			border-style: groove;
			}
		div.container {
            margin: 15px;   
        }
        div.left, div.right {
            float: left;
            padding: 10px;    
        }
        div.left {
            background-color:white;  
            width:900px;  
            height:500px;
        }
        div.right {
            background-color: #dddddd; 
            width:360px;  
            height:400px;	
            margin-top:20px;	
            margin-bottom:100px;			  
        }
        </style>
    </head>
    <body onload="javascript:init()">
        <h2 style="color:green;text-align:center;font-size: 50px">ARTIK Cloud based Smart Parking System</h2>   

        <div class="container">
            <div class="left">
	          <h1 style="line-height: 30%; color:blue;margin-left:40px">Staff Parking</h1>
			  <h4 style="line-height: 10%; margin-left:40px">Near Main Cmapus</h4>
			  <rainbowParking id="rainbow">Receiving Data...</rainbowParking>
			  <h1 style="line-height: 30%; color:white;margin-left:40px">1</h1>
			  <h1 style="line-height: 30%; color:blue;margin-left:40px">Student/Guest Parking </h1>
			  <h4 style="line-height: 10%; margin-left:40px">Front Gate, Near Union bank</h4>
			  <indigoParking id="indigo">Receiving Data...</indigoParking>
	        </div>
            <div class="right">
			 <div id="map" style="width:100%;height:100%">
			 
			 </div>
			</div>
        </div>
          
			    
        <div id = "websocketelements">
        <div id="attributes_log"> </div>
        </div>
		
		<div id="close">
                <a href="javascript:onClose()">Close connection to Parking System</a>
        </div>

        <h4>Output Console</h4>

        <div id="output"></div> 
		
		<script>
          function myMap() {
          var mapCanvas = document.getElementById("map");
          var mapOptions = {
          center: new google.maps.LatLng(11.476534,77.999759 ),
          zoom: 11
          }
          var map = new google.maps.Map(mapCanvas, mapOptions);
		  
		  var rainbow_marker = new google.maps.Marker({
          // The below line is equivalent to writing:
          // position: new google.maps.LatLng(-34.397, 150.644)
          position: {lat: 11.477627, lng: 78.005038},
          map: map
          });
		
		  var indigo_marker = new google.maps.Marker({
          // The below line is equivalent to writing:
          // position: new google.maps.LatLng(-34.397, 150.644)
          position: {lat: 11.476765, lng:78.000328 },
          map: map
          });
		  
		  var infowindow = new google.maps.InfoWindow({
          content: '<p>Staff Parking, 17, Free School Street</p>'
          });
          var infowindowindigo = new google.maps.InfoWindow({
          content: '<p>Indigo Parking, 29, Progoti Street</p>'
          });
		
          google.maps.event.addListener(rainbow_marker, 'click', function() {
          infowindow.open(map, rainbow_marker);
          });
		
		  google.maps.event.addListener(indigo_marker, 'click', function() {
          infowindowindigo.open(map, indigo_marker);
          });
		
		}
        </script>

        <script src="https://maps.googleapis.com/maps/api/js?callback=myMap"></script>
		
    </body>
</html>
Web App (Client Side)JavaScript
// real-time interactions are possible using websocket
// Notice the ws:. This is the new URL schema for WebSocket connections. 
// There is also wss: for secure WebSocket connection the same way 
// https: is used for secure HTTP connections.
// you can get it from Artik cloud documentation page
var wsUri = "wss://api.artik.cloud/v1.1/websocket?ack=true";
var device_id = "Your Device id"; // raspi3 DEVICE ID
var device_token = "Your Device Token"; //raspi3 DEVICE TOKEN

var output;
var attributes_log;
var websocket;

function init() {
	// document.getElementById() write something to html page
    output = document.getElementById("output");
    attributes_log = document.getElementById("attributes_log");
    if (browserSupportsWebSockets() === false) {
		// check browser support websocket protocol or not
        writeToScreen("Sorry! your web browser does not support WebSockets. Try using Google Chrome or Firefox Latest Versions");

        var element = document.getElementById("websocketelements");
        element.parentNode.removeChild(element);

        return; //
    }
    //You open up a WebSocket connection simply by calling the WebSocket constructor
    websocket = new WebSocket(wsUri);
    //When the connection is open, function invoked automatically
    websocket.onopen = function() {		
        //writeAttributeValues('onOpen Event Fired');
        writeToScreen("Successfully connected to Parking System");
		// after connection is open, registration is required for secure data transmission
		register();
    };
    // invoked when new message received
    websocket.onmessage = function(evt) {
        onMessage(evt);
    };
    // when received error
	// You can handle any errors that occur by listening out for the error event.
    websocket.onerror = function(evt) {
        onError(evt);
    };
}

function onClose(evt) {
	// Once youre done with your WebSocket you can terminate the connection using the close() method.
    websocket.close();
    //writeAttributeValues('onClose Event Fired');
    writeToScreen("DISCONNECTED");
}

// When a message is received the message event is fired. 
function onMessage(evt) {
    writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data + '</span>');
    //writeAttributeValues('onMessage Event Fired');
	handleRcvMsg(evt.data); //data is send to the function handleRcvMsg()
}

function onError(evt) {
    writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
    //writeAttributeValues('onError Event Fired');
}

function doSend(message) {
	// To send a message through the WebSocket connection you call the send() method on your WebSocket instance
    websocket.send(message);
    //writeAttributeValues('onSend Event Fired');
    writeToScreen("SENT: " + message);
}

function writeAttributeValues(prefix) {
    var pre = document.createElement("p");
    pre.style.wordWrap = "break-word";
    pre.innerHTML = "INFO " + getCurrentDate() + " " + prefix + "<b> readyState: " + websocket.readyState + " bufferedAmount: " + websocket.bufferedAmount + "</b>";
    ;
    attributes_log.appendChild(pre);
}

function writeToScreen(message) {
    var pre = document.createElement("p");
    pre.style.wordWrap = "break-word";
    pre.innerHTML = message;
    output.appendChild(pre);
}

function getCurrentDate() {
    var now = new Date();
    var datetime = now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate();
    datetime += ' ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
    return datetime;
}

function browserSupportsWebSockets() {
    if ("WebSocket" in window)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function getTimeMillis(){
    return parseInt(Date.now().toString());
}

function register(){
    writeToScreen("Registering device on the WebSocket connection");
    try{
        var registerMessage = '{"type":"register", "sdid":"'+device_id+'", "Authorization":"bearer '+device_token+'", "cid":"'+getTimeMillis()+'"}';
        writeToScreen('Sending register message ' + registerMessage + '\n');
        websocket.send(registerMessage, {mask: true});
        isWebSocketReady = true;
		//document.getElementById("rainbow").innerHTML = "";
		//document.getElementById("rainbow").innerHTML = "Capacity:"+'<span style="color: red;">50</span> '+"Free Slot:"+'<span style="color: red;"></span>'+"50";
        //document.getElementById("indigo").innerHTML = "Capacity: 60,  Free Slot: 5";
	}
    catch (e) {
        writeToScreen('Failed to register messages. Error in registering message: ' + e.toString());
    }    
}

//data after receiving is sent here for processing
function handleRcvMsg(msg){
	// message is received as following string
	// {"actions":[{"name":"setText","parameters":{"text":"4", "text2": "5"}}]}
	// you have to parse it
    var msgObj = JSON.parse(msg);
    if (msgObj.type != "action") return; //Early return;

    var actions = msgObj.data.actions;
    var rainbowData = actions[0].parameters.text; 
	var indigoData = actions[0].parameters.text2; 
    console.log("The received action is " + actions);  
	document.getElementById("rainbow").innerHTML = "Capacity: 50,  Free Slot: "+rainbowData;
	document.getElementById("indigo").innerHTML = "Capacity: 60,  Free Slot: "+indigoData;
   
}
Soucre Code for Smart Parking System
All the necessary code and files are included in this Github repository.

Credits

H3 vqbizux1ha
Husinul

an avid lover of technology a diy enthusiastic . an arduino and raspi lover ...love to do cool projects

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Using OV7670 Camera Module With Arduino
Advanced
  • 798
  • 31

Protip

This is an Arduino camera module, using the surveillance camera's digital image processing chip-OV0706.

Using Nokia 3310 84x48 LCD with Arduino
Advanced
  • 6,632
  • 116

Protip

These 84 by 48 pixel LCDs are what you might have found in Nokia 3310. And what about using them in the Arduino projects?

Banana pi BPI-M2 Zero quad core singel-board computer
Advanced
  • 250
  • 4

Protip

Banana PI BPI-M2 Zero is the open source hardware platform,Banana PI BPI-M2 Zero is an quad core version of Banana Pi,it support WIFI on boa

Slot Car Track
Advanced
  • 48
  • 1

Slot Car Race Track!!! HURRY!!!!!!!!!!!!!!!!!!!!!!

A Sudoku Solver's Robot
Advanced
  • 2,491
  • 16

Full instructions

Sudoku-robot is a robot that solves and fills Sudoku grids!

Jrobot - Android and Arduino-Based Global Control Robot Car
Advanced
  • 2,078
  • 11

Jrobot adds wheels to mobile phone and makes it truly mobile. Jrobot on 4G has unlimited range and goes anywhere in the world.

Sign up / LoginProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlog