Want to learn 3D Printing, hurry up and register for our upcoming Workshop that starts Dec.16th

IOT:SMART PARKING SYSTEM

Posted by Momen 16/11/2017 0 Comment(s)

 

In some area in Dubai it's very hard to find a free parking also in other congested cities there is a same problem , anyone when he wants to go outside from home with his personal car the first thing come in his mind is how he will find a parking there , and some people late for his job because of the parking problem so problem spend more time to find a free parking , so it is a big hassle and many people when he gets out with his personal car, he keep in fear about parking of his car .

So we need to find a solution for this problem and solve it finally they succeeded to make a cloud based smart and we hope implementing this system can solve and remove the parking problem of our city also . ARTIK cloud can be used because it is really a nice and appropriate platform for such job.

Using this system wiil be able you to find an available parking lot easily using mobile or web app from anywhere.  This system updates the parking data every 30 seconds.

This project will show you how you can easily build such smart system , you can use ARTIK cloud platform , which is the coolest IOT cloud platform.

 

Hardware component:

 

Software apps and online services:

  • Samsung IOT ARTIK Cloud for IOT
  • Google Maps
  • Arduino IDE

 

For this parking system you have to create only one new device in Samsung ARTIK Cloud platform. In this tutorial will learn you how to create it. Name your devices as raibow-parking , then store the parking data such as free slots in a parking lot .then the raspberry pi sends parking information according to the preset rules.

 

Steps involved in this project :

  • Making one device in Artik cloud platform
  • Making one application in Artik cloud
  • Making one rule in Artik cloud
  • Preparing Arduino
  • Connecting the sensors
  • Preparing the raspberry pi 3
  • Developing web application

 

So now we will start one by one. First thing , let's start with making a new device in Artik cloud platform .

Step one: making a new device in Artik cloud platform :

A) Log in to your Samsung account and click DEVOLOPER option from top right corner.

 

 

B) From the DASHBOARD select DEVICE TYPES and CLICK on NEW.


 

 

 

 

 

 

C)  in the DEVICE DISPLY NAME  write a name for the device and give a UNIQUE NAME , then click CREATE DEVICE TYPE .

 

 

D) Click on + NEW MANIFEST.

 

 

 

 

 

 

 

E ) WRITE A FIELD NAME for your sensor data you will upload and store here and write with it the DATA TYPE  as integer . Then click to SAVE.

 

 

F) Then click NEXT: DEVICE ACTIONS

 

G) Make an action or select from STANDARD ACTIONS. CLICK SAVE. For our project action will be not required. Then click NEXT :ACTIVE MAINIFEST.

 

H) Take a look to device field and again click NEXT :ACTIVE MANIFSET.

 

I) NOW YOU SUCCESFULLY CREATED YOU FIRST DEVICE NOW YOU HAVE TO CONNECT THIS DEVICE TO ARTIK CLOUD.

J) Go to MY ARTIK CLOUD from right top corner.

 

 

 

 

k) Click on my artik cloud and select devices.

 

L) Click to + Add another device.

 

M) Write the name of the device which you create it and click on it.

 

N) Click on CONNECT DEVICE.

 

O) Your device is connected successfully to the cloud.

 

 

 

 

P) Click on it. Generate Token, Note down your DEVICE ID and DEVICE TOKEN. These will be required latter every time you want to connect to your device from physical devices, web apps or android apps

 

Step two : Making in artik cloud.

  1. Select RULES from MY ARTIK CLOUD  and CLICK ON NEW RULE .

 

 

 

  1. From IF selection box select a data field for a device and set a value and condition. Then select an action for a specific device from THEN field. You can set a constant parameter value or can select a value from another device data. To define a data field from another device click on download icon shown in red box.

 

  1. Select a device from the drop down then select data field.

 

d) Then click on save rule.

 

Step three. Preparing arduino :

The main purpose of using arduino in this project is to collect data from sensors which connected to parking station and send this data to raspberry pi by using serial port.

 

 

 

 

Step four. Connecting the sensors:

 

Step five. Preparing raspberry pi :

Raspberry pi working as a WEBSOCKET client and also it can communicates with Artik cloud by using Websocket protocol . in this tutorial Node.js is used for programming unlike python node .

  1.  Install node and npm on your raspberry pi by using the following commands :

sudo apt-get update

sudo apt-get install nodejs npm

 

  1.  Install Websocket client to ypur raspberry pi :

npm install --save ws

  1.  Install serial port to raspberry pi :

npm install serialport

 

If you successfully completed all three steps then your pi is ready to communicate with Arduino using serial port and Artik cloud using websocket. As I said earlier two raspberry pis are required for two parking station. So, download the source code for the this from the Github . You can use ftp client like FileZilla to upload the code on raspberry pi. Don't forget to change DEVICE ID & DEVICE TOKEN in the source code.

  1. After uploading the code on rpi run it by the following command into terminal window.

sudo node staff-parking.js

 

 

 

 

Parking lot using ultrasonic sensor

 

 

 

 

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);

 

Code for 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.

 

Leave a Comment