Project in progress

Smart Parking System © GPL3+

ARTIK Cloud based Smart Parking System.

  • 22,909 views
  • 27 comments
  • 73 respects

Components and supplies

Apps and online services

About this project

Finding a free parking lot in a congested city like Dhaka is very hard. Here, if anyone wants to go outside from home with personal car first thing comes in his mind is about parking, where he will park his car. Most of the cases, people go to a parking station and find that all parking slot are full and then he have to search for another parking lot. So, it is a big hassle and many people keep in fear about parking of his car when he gets out with his car.

I realized that, to enjoy a better transport a better parking system is necessary especially in a congested city like Dhaka.

So, I was thinking, how the problem can be solved and finally I succeeded to make a cloud based smart parking system and I hope implementing the system can remove the parking problem of my city. ARTIK Cloud is really a nice and appropriate platform for such job.

Using this system a user will be will able to find an available parking lot easily using mobile or web app from anywhere. I also used Intel Edison with a display which may be placed several important locations of the city or road from where it will be possible to find free parking lot. The system updates parking data every 30 seconds.

In this project I will show you how you can easily build such smart system. Definitely, I will use ARTIK Cloud platform, the coolest IoT cloud platform. Before going to the details, enjoy the demo video of my demo project.

Smart Parking System

For this demo system you have to create three new devices in Samsung Artik Cloud platform. I will show it for one device. The steps are same for all devices. I named three devices as rainbow-parking, indigo-parking & edison respectively. Rainbow-parking & indigo-parking receive and store parking data such as free slots in a parking lot. Edison sends parking information according to the preset rules.

Steps involved in this project:

1. Making three devices in Artik Cloud Platform

2. Making one application in Artik Cloud

3. Making one rule in Artik Cloud

4. Preparing Arduino

5. Connecting the Sensors

6. Preparing Raspberry Pi

7. Preparing Intel Edison

8. Developing Web Application

9. Developing Android Application

10. Completing the Project.

So, Let's start one by one. First thing first. Let's start with making a new device in Artik cloud platform.

Step 1: Making a new device in Artik Cloud Platform

A) Log in to your Samsung account and Click on DEVELOPERS option from top right corner.

B) From the DASHBOARD select DEVICE TYPES.

C) Click on + NEW DEVICE TYPE

D) On the DEVICE DISPLAY NAME type a name for the device and give a UNIQUE NAME, then click to CREATE DEVICE TYPE.

E) Click on + NEW MANIFEST

F) Type a FIELD NAME for your sensor data you will upload and store here and mention the DATA TYPE as integer. Then click to SAVE.

G) Then click NEXT: DEVICE ACTIONS

H) Make an action or select from the STANDARD ACTIONS. Click to SAVE. For our project actions will not be required. Then click to NEXT:ACTIVE MANIFEST.

I) Take a look to Device Fields and again click to NEXT:ACTIVE MANIFEST.

J) Congratulation! you successfully created your first device. Now you have to connect the device to Artik cloud. So, let's do it.

K) Go to ARTIK CLOUD from right top corner.

L) Click on MY ARTIK CLOUD and select DEVICES.

M) Click to +Connect another device.

N) Type the name of the device you just created and click on it.

O) Click on CONNECT DEVICE... button.

P) Your device is connected to the CLOUD.

Q) Click on settings icon.

R) Click on GENERATE DEVICE TOKEN...

S) 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.

Click on SAVE CHANGES.

Follow the same procedures to make an connect other two devices. I connected three devices named as rainbow-parking, indigo-parking and edison. Rainbow-parking and indigo-parking represent two parking stations and edison represents message board. I connected rainbow-parking to one raspberry pi and indigo-parking from another pi and placed in two different parking lot. Again, raspberry pis are connected with the sensors placed in parking lots to identify free and engaged parking slot through Arduino board.

I will jump to 3rd step now, I will explain second step later.

Step 2: Making rule in Artik Cloud

In this step we will make a rule where we set some conditions according to which Artik cloud will generate an action when the condition meets and the devices and applications subscribed to the action receive a message instantly.

A) Select RULES from the MY ARTIK CLOUD menu

B) Click on + NEW RULE

C) 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.

D) Select a device from the drop-down and then select data field.

E) Then click on SAVE RULE.

F) The complete rule for our project should look like as following screenshot.

Step 3. Preparing Arduino

The main work of the Arduino in my project is to collect data from sensors connected to parking stations and to send the data to Raspberry pi using serial port. Two Arduino board is required as I implemented the demo project for two parking stations. For the two parking stations I used two different types of sensors. For one I used IR sensors can be used in the place where direct sun light is not available and for another I used ultrasonic sensors and can be used in any location.

For IR sensor interfacing Arduino analog pins were used and for ultrasonic sensors Arduion digital pins were used. Arduino sketch for both types of sensors are attached. Upload appropriate sketch to your Arduino board. I developed the sketch for only four sensors. If you require more modify the sketch accordingly.

If you are new in IR sensors take a look here.

Details about ultrasonic sensor can be found here.

Step 4: Connecting the Sensors

For connecting all the sensors to Arduino please see the schematic section.

Step 5: Preparing Raspberry Pi

I am assuming that, you have some previous experience in working with Raspberry pi. Lots of beginners tutorials are available in the Internet.

Let's start our work with raspberry pi. Raspberry pi is working as a WebSocket client and communicates with Artik cloud using WebSocket protocol. I used Node.js for programming. Unlike Python Node is not preinstalled on Raspberry Pi.

A). Install Node and npm on Raspberry pi using following commands in terminal.

sudo apt-get update
sudo apt-get install nodejs npm

B). Install WebSocket client ws to raspberry pi

npm install --save ws

C). Install serialport 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 pis from the Github and upload rainbow-parking.js for the pi connected to Rainbow parking station and indigo-parking.js on the pi connected to Indigo parking station. You can used ftp client like FileZilla to upload the code on raspberry pi. Don't forget to change DEVICE ID & DEVICE TOKEN in the source code.

D) After uploading the code on raspberry pi run the code by typing the following command into terminal window.

sudo node rainbow-parking.js

Step 6: Preparing Intel Edison

I am assuming you have some previous experience on working with Intel Edison board.

A). Download Intel XDK from the link and install it to your computer.

B). Download edison-lcd folder from my Github and open with Intel XDK.

C) Connect i2c LCD display with Edison board. You may use base shield for easy connection.

D). Connect Edison board with computer using serial cable and upload the code and then run it. Uploading and running code is very easy using Intel XDK.

Step 7. Developing Web Application

For developing web application HTML and JavaScript was used. For communicating with Artik cloud here I again used WebSocket protocol. For integrating Map in my application I used Google Map API.

Download the web-app folder from my Github and modify the DEVICE ID and DEVICE TOKEN with your own device id and token. Use ID of the device which generate the action on new data received (edison in my case). Open the websocket.html using a web browser and enjoy. See the demo video...

Step 8. Creating an application in Artik Cloud

Before going to develop Android application you need to create an application to Artik Cloud. To do so use the following steps:

A). From the developer dashboard select APPLICATION

B). Click on +NEW APPLICATION

C). Put name and description. For authorization select Client credentials, auth code, implicit. Put http://localhost:8000/acdemo/index.php as redirect url and then click to SAVE APPLICATION button.

D). Click on SET PERMISSION FOR A SPECIFIC DEVICE.

E). Select rainbow-parking and click on Read check box. Then click ADD DEVICE TYPE.

F). Do the same for indigo-parking and click on SAVE.

G). From the top right corner click on SHOW CLIENT ID & SECRET

H). Note down Client ID and Secret, these will be required for Android Application.

Step 9. Developing Android Application

A). Download Android Studio and install it to your computer.

B). Download android-app source code from my Github

C). Run Android Studio and import the project by browsing the source.

D). Open MainActivity.java and put your CLIENT ID you got after creating application on Artik cloud.

E). Open MessageActivity.java and put DEVICE ID & ACCESS TOKEN for the devices you added to the application on Artik cloud.

F). Save the changes, Build APK, install it to your mobile phone and enjoy.

Step 10. Setup all the hardware

Connect all the sensors with Arduino board. Then connect Arduino with the Raspberry Pi using serial cable. Access raspberry pi using SSH client and run the code you uploaded before. Open the web application using your favorite browser and enjoy the whole thing. Don't forget to test it using Android application.

Schematics

Android App Screenshot
Android App using Artik Cloud Android SDK
Main
Screenshot of Web Application
Web app screenshot
Block Diagram of Smart Parking Syatem
Block diagram
Parking lot using ultrasonic sensor
Sonar lot bb updated
Parking lot using Photosensor
Parking ir

Code

Raspberry Pi (rainbow-parking)JavaScript
Source code for raspberry pi
//rainbow parking data transfer
var webSocketUrl = "wss://api.artik.cloud/v1.1/websocket?ack=true";
var device_id = "Your Device ID Here"; // Rainbow parking DEVICE ID
var device_token = "Your Device Token Here"; //Rainbow 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() == "parking_state") 
    { 
        // do something here after receiving 'parking_state'
    }
    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){
    try{
        ts = ', "ts": '+getTimeMillis();
        var data = {
            "rainbowlot": parking
            //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
            sendStateToArtikCloud(data);
           
    });
});

process.on('SIGINT', exitClosePins);
Arduino (sonar-sensor)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 + distance4;;

 // free slot = total slot - total car
 freeSlot = 4 - count;
 // number of total slot is sent to raspberry pi using usb
 Serial.println(freeSlot);
 // the status is updated every 30 seconds.
 delay(30000);
 freeSlot = 0;
 distance1 = 0;
 distance2 = 0;
 distance3 = 0;
 distance4 = 0;
}
Intel Edison (main.js)JavaScript
Source code for Intel Edison to display parking information in lcd
// Libmraa is a C/C++ library with bindings to Java, Python and JavaScript
// to interface with the IO on Galileo, Edison & other platforms, 
// with a structured and sane API where port names/numbering matches the board that you are on.
var mraa = require('mraa');
// Hitachi HD44780 LCD driver for Linux boards
var lcd = require('./lcd');

var version = mraa.getVersion();
var display = new lcd.LCD(0);
// mqtt is a client library for the MQTT protocol, written in JavaScript for node.js and the browser.
var mqtt = require('mqtt');
// Node implements File I/O using simple wrappers around standard POSIX functions. 
// The Node File System (fs) module is imported using the following line
var fs = require('fs');
// mqtt connection endpoint is define in ARTIK Cloud documantation page
var PROTOCOL = 'mqtts';
var BROKER ='api.artik.cloud';
var PORT = 8883;

var URL = PROTOCOL + '://' + BROKER;
URL += ':' + PORT;
// device id and device token is used for authentication 
var deviceID = '4368e28eb8db4adfa368e5e0120932fa';
var deviceTOKEN = '655eac98350f42858e7df90cd270550a';
// use device id as username & device token as password
var requireds = { username: deviceID, password: deviceTOKEN };
var mqttConfig = { 'url': URL, 'requireds': requireds };
// Connects to the broker specified by the given url and options and returns a Client.
var client = mqtt.connect(mqttConfig.url, mqttConfig.requireds);
// topic for client
var pubTOPIC = '/v1.1/messages/'+deviceID;
// topic for subscriber
var subTOPIC = '/v1.1/actions/'+deviceID;

if (version >= 'v0.6.1') {
    console.log('mraa version (' + version + ') ok');
}
else {
    console.log('meaa version(' + version + ') is old - this code may not work');
}

// Emitted automatically on successful connection 
client.on('connect', function () {
  console.log('connected');
  // set backlight color for RGB lcd
  display.setColor(255, 255, 255);
  display.setCursor(0,0);
  display.write('Connected');
  // subscribe to the topic
  client.subscribe(subTOPIC);  
});

// Emitted when the client receives a publish packet 
// topic is topic of the received packet
// message payload of the received packet
client.on('message', function (topic, message) {
  // message is Buffer 
  // parses the received string as JSON
  var msgObj = JSON.parse(message);
  var rainbowData = msgObj.actions[0].parameters.text; 
  var indigoData = msgObj.actions[0].parameters.text2; 
  // the values then print to lcd display
  display.setCursor(0,0);
  display.write('RainbowFSlot: '+rainbowData); 
  display.setCursor(1,0);
  display.write('IndigoFrSlot: '+indigoData); 
  console.log(rainbowData);
  console.log(message.toString())
  //client.end()
});
Web ApplicationJavaScript
Source for web application
// 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 HERE"; // Edison parking DEVICE ID
var device_token = "YOUR DEVICE TOKEN HERE"; //Intel Edison parking 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;
   
}
Android AppJava
Source code for android appication
package cloud.artik.example.hellocloud;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cloud.artik.api.MessagesApi;
import cloud.artik.api.UsersApi;
import cloud.artik.client.ApiCallback;
import cloud.artik.client.ApiClient;
import cloud.artik.client.ApiException;
import cloud.artik.model.MessageAction;
import cloud.artik.model.MessageIDEnvelope;
import cloud.artik.model.NormalizedMessagesEnvelope;
import cloud.artik.model.UserEnvelope;

public class MessageActivity extends Activity {
    private static final String TAG = "MessageActivity";

    private static final String RDEVICE_ID = "Your Device ID";
    public static final String RKEY_ACCESS_TOKEN = "Your Device Token";  // r set
    private static final String IDEVICE_ID = "Another Device ID";
    public static final String IKEY_ACCESS_TOKEN = "Another Device Token";  // r set



    private MessagesApi rMessagesApi = null; // r set
    private MessagesApi iMessagesApi = null; // r set
    

    private String rAccessToken;    // r set
    private String iAccessToken;    // r set

    private TextView rainbowLot;
    private TextView indigoLot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        rAccessToken = getIntent().getStringExtra(RKEY_ACCESS_TOKEN);   // r set
        iAccessToken = getIntent().getStringExtra(IKEY_ACCESS_TOKEN);   // r set

        Button getLatestMsgBtn = (Button)findViewById(R.id.getlatest_btn);

        rainbowLot = (TextView)findViewById(R.id.txtRlot);
        indigoLot = (TextView)findViewById(R.id.txtIlot);
        
        setupArtikCloudApi();

        getLatestMsgBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                // Now get the message
                //getRainbowMsg();
                getIndigoMsg();
            }
        });
    }

    private void setupArtikCloudApi() {
        ApiClient rApiClient = new ApiClient();
        ApiClient iApiClient = new ApiClient();

        rApiClient.setAccessToken(rAccessToken);   // r set
        iApiClient.setAccessToken(iAccessToken);   // r set

        rMessagesApi = new MessagesApi(rApiClient);  // r set
        iMessagesApi = new MessagesApi(iApiClient);  // r set
    }

    private void getRainbowMsg() {
        final String tag = TAG + " getLastNormalizedMessagesAsync";
        try {
            int messageCount = 2;
            rMessagesApi.getLastNormalizedMessagesAsync(messageCount, RDEVICE_ID, null,
                    new ApiCallback<NormalizedMessagesEnvelope>() {
                        @Override
                        public void onFailure(ApiException exc, int i, Map<String, List<String>> stringListMap) {
                            processFailure(tag, exc);
                        }

                        @Override
                        public void onSuccess(NormalizedMessagesEnvelope result, int i, Map<String, List<String>> stringListMap) {
                            Log.v(tag, " onSuccess latestMessage = " + result.getData().toString());
                            updateGetResponseOnUIThread(result.getData().get(0).getMid(), result.getData().get(0).getData().toString());
                        }

                        @Override
                        public void onUploadProgress(long bytes, long contentLen, boolean done) {
                        }

                        @Override
                        public void onDownloadProgress(long bytes, long contentLen, boolean done) {
                        }
                    });

        } catch (ApiException exc) {
            processFailure(tag, exc);
        }
    }

    private void getIndigoMsg() {
        final String tag = TAG + " getLastNormalizedMessagesAsync";
        try {
            int messageCount = 1;
            iMessagesApi.getLastNormalizedMessagesAsync(messageCount, IDEVICE_ID, null,
                    new ApiCallback<NormalizedMessagesEnvelope>() {
                        @Override
                        public void onFailure(ApiException exc, int i, Map<String, List<String>> stringListMap) {
                            processFailure(tag, exc);
                        }

                        @Override
                        public void onSuccess(NormalizedMessagesEnvelope result, int i, Map<String, List<String>> stringListMap) {
                            Log.v(tag, " onSuccess latestMessage = " + result.getData().toString());
                            updateGetResponseOnUIThread(result.getData().get(0).getMid(), result.getData().get(0).getData().toString());
                        }

                        @Override
                        public void onUploadProgress(long bytes, long contentLen, boolean done) {
                        }

                        @Override
                        public void onDownloadProgress(long bytes, long contentLen, boolean done) {
                        }
                    });

        } catch (ApiException exc) {
            processFailure(tag, exc);
        }
    }

    static void showErrorOnUIThread(final String text, final Activity activity) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(activity.getApplicationContext(), text, duration);
                toast.show();
            }
        });
    }


    private void updateGetResponseOnUIThread(final String mid, final String msgData) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String delims = "[=}.]+";
                String[] tokens = msgData.split(delims);
                //rainbowLot.setText(tokens[1]);
                indigoLot.setText(tokens[1]);
            }
        });
    }

    private void processFailure(final String context, ApiException exc) {
        String errorDetail = " onFailure with exception" + exc;
        Log.w(context, errorDetail);
        exc.printStackTrace();
        showErrorOnUIThread(context+errorDetail, MessageActivity.this);
    }

} //MessageActivity
Source Code for Smart Parking System
All the necessary code and files are included in this Github repository.

Comments

Similar projects you might like

Plant Communicator

Project tutorial by Arduino

  • 1,448 views
  • 0 comments
  • 6 respects

The Nerd

Project tutorial by Arduino

  • 617 views
  • 1 comment
  • 1 respect

LED Matrix Display Badge

Project tutorial by FabLabAQ

  • 684 views
  • 0 comments
  • 3 respects

PICASO 4.0 The Infinite X- Axis CNC Wall Engraver

Project tutorial by Arnab Kumar Das

  • 185 views
  • 1 comment
  • 2 respects

Object Tracker and Updater

by Rupin Chheda

  • 264 views
  • 1 comment
  • 3 respects
Add projectSign up / Login