This is an IOT project which helps the Bus travelers to know the rush in the bus in-order to take proper decision based on it. For example if someone heavy and breakable things in his/her hands then it is not safe to travel in a crowded bus but in-order to know the status of the bus one should wait until the bus reaches the stop. But if you know the number of passengers the bus in the before the bus reach the stop then we could save time and take a taxi.
Now lets start with the project for that there are certain steps :
- Setup an Device in Artik Cloud
- Code and assemble Intel Edison
- Create a Web Page and an App
What I love about artik cloud is that it can handle multiple requests at a time without any downtime and it is simple to use.
First create a free Artik Cloud account by clicking here
or by going to https://artik.cloud/account/signup. If you already have an account then just simply login here : https://artik.cloud/account/login.After that Open Developers portal by clicking on Developer menu from the menu bar or simply goto https://developer.artik.cloud.
Click on Device type from the Dashboard menu or click here
Then give it a Display device name and unique name (It should unique and spaces are not allowed) and click Create Device type. Here I give my device Display name as Artik Bus and unique name as artik.bus
After that on Manifest menu click NEW MANIFEST
Then type count as Field name and Data type as long (It should select automatically if you select count as field name)
And click Save
Click NEXT : DEVICE ACTIONS and then click NEXT : ACTIVATE MANIFEST and finally click ACTIVATE MANIFEST
Now you were successfully created your first device manifest for storing the number of passengers.
Now lets create an virtual device using the manifest that we were created. For that goto https://artik.cloud and click on Devices from My Artik Cloud menu or goto : https://artik.cloud/my/devices
If you not yet created any Device it directly ask to search for your device. Other ways click on Connect another device and search your device name that you created before and select it and give it a name and then click on Connect device:
Now you created your device.
Next you should connect your edison board with Artik cloud before that you should copy the Device ID and Device Token. For that click on the small gear (Settings) icon on the near the device name and a pop-up will come. Click on generate device token and you will get a token. Copy the Device ID and the Device Token for later use.
If you never used intel Edison before there are many getting stated projects in hackster.io. Feel free to check that out : Intel Edison Getting Started Projects
This project is using Node.js language and Intel XDK as the IDE. This project is not going too deeper into now you can use XDK but you will get the basics to use
- Get the drivers and setup tools for intel Edison can be
find here
- Download
Intel XDK from here
After downloaded the setup tool you could setup the by installing drivers, flashing firmware, setting up wifi connection ( If already done no need to setup again ). Now download install Intel XDK and connecting the edison to computer or any other power source. Find the local IP address of edison by going to router settings or by connecting edsion to pc via usb and get IP from the Edison setup tools.
Wire up the board as following:
Here 12 and 8 pins are connected to 2 push buttons and the other end of push button to 5v.
Open Intel XDK and on the bottom of the IDE click the dope down menu named IOT devices and click Add manual connection:
Then on the pop-up enter the local IP of edison and thes username and password rest is same. In my case IP address is 192.168.1.4. And click connect:
It will show connected message.
Now click on top left arrow and click New project. Click on template and select blank template.
After that click on Continue and give it a name. Use the following code there:
var mraa = require('mraa'); // require mraa
var request = require("request"); //req
var auth = "Bearer xxxxx"; // Device token
var dev = "xxx"; //device ID
var count = 0; // Setting the inital value of count as 0
var pinIn = new mraa.Gpio(12); // Pin for in
var pinOut = new mraa.Gpio(8); // pin for OUt
pinIn.dir(mraa.DIR_IN); // setting pin IN as INPUT mode
pinOut.dir(mraa.DIR_IN); // setting pin OUT as INPUT mode
// Setting default value as LOW
pinIn.write(0);
pinOut.write(0);
//Function which listen for the the push buttons and make it act as a switch
function check() {
var ch = 0; // help to use pushbutton as switch ( one can hold the push button as much they need but only one request is considered)
var inner = pinIn.read();
if (inner == 1) {
while (pinIn.read() == 1) {}
count++;
ch = 1;
}
var outer = pinOut.read();
if (outer == 1) {
while (pinOut.read() == 1) {}
count--;
ch = 1;
}
if (ch == 1) {
console.log(count) // printing the current count
post(count);
ch = 0;
}
setTimeout(check, 300);
}
console.log("Running"); // Print running when program starts
post(0); //uploading 0 to artik when starts
check(); // starting the fucntion check() to listen pushbuttons
// Function which upload to artik
function post(c) {
// seting the properties to upload
var options = {
method: 'POST', // reqest type
url: 'https://api.artik.cloud/v1.1/messages', // api request url
headers: {
'Content-Type': 'application/json', // setting conect type as json
'cache-control': 'no-cache', // don't store any cache'
'authorization': auth // passing the token as the header
},
//passing data as json though body of the post request
body: '{\r\n\t"sdid": "' + dev + '",\r\n\t"type": "message",\r\n\t"data": {\r\n\t\t\t"count": "' + c + '"\r\n\t\t}\r\n}'
};
//seting the the request with the properties above mentioned
request(options, function(error, response, body) {
if (error) throw new Error(error);
console.log(body); // printing the response
});
}
Here replace:
var auth = "Bearer xxxxx";
xxxxx with your device token
var dev = "xxxxx"; //device ID
xxxxx with devic
e ID
Code could find at Github. Now click on upload button from the bottom:
And then after shows Upload Complete click run button:
And it will show Running.
If in any case it shows request not found then goto SSH terminal tab at bottom and fill up the server details such as ip, username, password and connect. Then type
npm install request
It will solve the problem.
Now try to press the pushbutton you can see a JSON responding back with an ID. It shows it working. For confirming it goto artik.cloud and login. Then select charts from My Artik Cloud.
And click on +/- CHARTS at left corner and tick the option count:
Now you can see a chart:
For the web app I am using azure with dream-spark subscription. If you are conferrable to use any other host feel free to use that (The host should support node.js because in this project i'm using node js as the server side language).
First you should install Node js in your computer for that goto : nodejs.org and download & install it in your system. After that open comment prompt(windows) or terminal (Mac, Linux) and type:
npm install -g express-generator
Then when installation completed open comment prompt in any directory where you want to create your project and type following comment:
express <projectname>
Here <projectname> is the project name as you wish. Now open the folder that automatically created and open app.js file and replace it with following code:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'faviscon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
And then routes/index.js with following:
var express = require('express');
var router = express.Router();
var request = require("request");
var sdids = "xxxxx"; //replace with device id
var token = "xxxxx"; //replace with device token
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index');
});
router.get('/count', function(req, res, next) {
var options = {
method: 'GET',
url: 'https://api.artik.cloud/v1.1/messages/last',
qs: { sdids: sdids, count: '1' },
headers: {
'cache-control': 'no-cache',
'authorization': 'Bearer ' + token
}
};
var data, k = 0;
request(options, function(error, response, body) {
if (error) throw new Error(error);
data = JSON.parse(body).data[0].data;
res.send(data);
});
});
module.exports = router;
Here edit following:
var sdids = "xxxxx"; //replace with device id
var token = "xxxxx"; //replace with device token
Replace views/index.jade:
extends layout
block content
#container
#data
script.
function getData(){
$.ajax({url:'/count', dataType: "json", success:function(result){
console.log(result);
// Get the result and show it
var message = result.count;
document.getElementById("data").innerHTML = JSON.stringify(message);
}});
}
$( document ).ready(function() {
document.getElementById("data").innerHTML = "0"
window.setInterval(getData, 2000);
});
Replace views/layout.jade:
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
script(src='/javascripts/jquery.js')
body
block content
and also replace public/stylesheets/style.css:
body{
margin: 0;
padding: 0;
background: #eee;
}
#container {
display: table;
width: 100%;
height: 100vh;
}
#data {
font-size: 20em;
color: #202020;
display: table-cell;
vertical-align: middle;
text-align: center;
}
Create a file named jquery.js in public/javascripts. Code for that will get from : https://code.jquery.com/jquery-3.1.1.js
Or your could find the complete code on Github. Now upload this project to Github by using git / Github desktop or even your could upload it directly.
Next thing we need is to host this project in azure. For that goto: portal.azure.com and create an account if you don't already have.
- Click on new from the side bar
- Then select Web + mobile
- From that select Web App
- Give it a name and resource group as you wish
Now open that app from dashboard ( if you pinned it to dashboard ) or just select it from All resources.
- On the left side you can see Deployment option click on it
- Then click Choose resource settings
- And select Github (If it is first time it will ask for authentication)
- Now click on chose project and then select your project
- and click OK
Wait for some time until fetch all data and install packages
After the fetching completed you could just your azure web app by the url. If you don't know you could find it in your dashboard.
Open it and you could find the number of passengers. When you press the push button it will also upload. It almost instant update (about 3 - 5 seconds).
You could easily create an mobile app from that by going to appsgeyser and create an account (Login if you already have)
- Now goto : http://www.appsgeyser.com/create-url-app/
- Enter your azure url and click next
- Give it a name and click next
- Give it a DESCRIPTION and click next
- And use default icon or upload one
- Finally click Create
- Next click on Download Button
- When it shows a pop-up just click or test your app
- and click download button
- It will start downloading
- Install it in your phone.
Now you just also created your mobile app. It is simple as making a coffee with a coffee machine which integrated with IOT :p
Demo Video
Comments