├── public ├── favicon.ico ├── apple-icon.png ├── images │ ├── logo.png │ ├── connecting.svg │ ├── connected.svg │ └── disconnected.svg ├── favicon-16x16.png ├── favicon-32x32.png ├── favicon-96x96.png ├── ms-icon-70x70.png ├── apple-icon-57x57.png ├── apple-icon-60x60.png ├── apple-icon-72x72.png ├── apple-icon-76x76.png ├── ms-icon-144x144.png ├── ms-icon-150x150.png ├── ms-icon-310x310.png ├── android-icon-36x36.png ├── android-icon-48x48.png ├── android-icon-72x72.png ├── android-icon-96x96.png ├── apple-icon-114x114.png ├── apple-icon-120x120.png ├── apple-icon-144x144.png ├── apple-icon-152x152.png ├── apple-icon-180x180.png ├── android-icon-144x144.png ├── android-icon-192x192.png ├── apple-icon-precomposed.png ├── browserconfig.xml ├── manifest.json ├── js │ ├── object-keys-polyfill.js │ └── conveyor-belt.js └── stylesheets │ └── style.css ├── views ├── error.hbs └── index.hbs ├── images ├── architecture.png ├── raspi_circuit.png └── app_conveyor_belt.png ├── .gitignore ├── repository.yaml ├── manifest.yml ├── package.json ├── CONTRIBUTING.md ├── routes ├── index.js └── ibm-iot.js ├── bin └── www ├── CLA.md ├── app.js ├── MAINTAINERS.md ├── LICENSE └── README.md /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /views/error.hbs: -------------------------------------------------------------------------------- 1 |

{{message}}

2 |

{{error.status}}

3 |
{{error.stack}}
4 | -------------------------------------------------------------------------------- /public/apple-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/apple-icon.png -------------------------------------------------------------------------------- /public/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/images/logo.png -------------------------------------------------------------------------------- /images/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/images/architecture.png -------------------------------------------------------------------------------- /images/raspi_circuit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/images/raspi_circuit.png -------------------------------------------------------------------------------- /public/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/favicon-16x16.png -------------------------------------------------------------------------------- /public/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/favicon-32x32.png -------------------------------------------------------------------------------- /public/favicon-96x96.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/favicon-96x96.png -------------------------------------------------------------------------------- /public/ms-icon-70x70.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/ms-icon-70x70.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | VCAP_SERVICES.json 3 | # Logs 4 | logs 5 | *.log 6 | npm-debug.log* 7 | *.DS_Store 8 | -------------------------------------------------------------------------------- /public/apple-icon-57x57.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/apple-icon-57x57.png -------------------------------------------------------------------------------- /public/apple-icon-60x60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/apple-icon-60x60.png -------------------------------------------------------------------------------- /public/apple-icon-72x72.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/apple-icon-72x72.png -------------------------------------------------------------------------------- /public/apple-icon-76x76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/apple-icon-76x76.png -------------------------------------------------------------------------------- /public/ms-icon-144x144.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/ms-icon-144x144.png -------------------------------------------------------------------------------- /public/ms-icon-150x150.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/ms-icon-150x150.png -------------------------------------------------------------------------------- /public/ms-icon-310x310.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/ms-icon-310x310.png -------------------------------------------------------------------------------- /images/app_conveyor_belt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/images/app_conveyor_belt.png -------------------------------------------------------------------------------- /public/android-icon-36x36.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/android-icon-36x36.png -------------------------------------------------------------------------------- /public/android-icon-48x48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/android-icon-48x48.png -------------------------------------------------------------------------------- /public/android-icon-72x72.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/android-icon-72x72.png -------------------------------------------------------------------------------- /public/android-icon-96x96.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/android-icon-96x96.png -------------------------------------------------------------------------------- /public/apple-icon-114x114.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/apple-icon-114x114.png -------------------------------------------------------------------------------- /public/apple-icon-120x120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/apple-icon-120x120.png -------------------------------------------------------------------------------- /public/apple-icon-144x144.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/apple-icon-144x144.png -------------------------------------------------------------------------------- /public/apple-icon-152x152.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/apple-icon-152x152.png -------------------------------------------------------------------------------- /public/apple-icon-180x180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/apple-icon-180x180.png -------------------------------------------------------------------------------- /public/android-icon-144x144.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/android-icon-144x144.png -------------------------------------------------------------------------------- /public/android-icon-192x192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/android-icon-192x192.png -------------------------------------------------------------------------------- /public/apple-icon-precomposed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/guide-conveyor-simulator/HEAD/public/apple-icon-precomposed.png -------------------------------------------------------------------------------- /repository.yaml: -------------------------------------------------------------------------------- 1 | id: guide-conveyor-simulator 2 | event_id: web 3 | event_organizer: dev-journeys 4 | runtimes: 5 | - Cloud Foundry 6 | services: 7 | - Internet of Things Platform 8 | language: nodejs 9 | -------------------------------------------------------------------------------- /public/browserconfig.xml: -------------------------------------------------------------------------------- 1 | 2 | #ffffff -------------------------------------------------------------------------------- /manifest.yml: -------------------------------------------------------------------------------- 1 | declared-services: 2 | gcs-iot-service: 3 | label: iotf-service 4 | plan: iotf-service-free 5 | applications: 6 | - name: guide-conveyor-simulator 7 | command: npm start 8 | path: . 9 | memory: 256M 10 | disk_quota: 1024M 11 | buildpack: nodejs_buildpack 12 | instances: 1 13 | random-route: false 14 | services: 15 | - gcs-iot-service 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "conveyor-belt", 3 | "version": "0.1.0", 4 | "scripts": { 5 | "start": "node ./bin/www" 6 | }, 7 | "dependencies": { 8 | "body-parser": "~1.15.2", 9 | "cookie-parser": "~1.4.3", 10 | "debug": "~4.1.1", 11 | "express": "~4.14.0", 12 | "hbs": "~4.0.1", 13 | "ibmiotf": "^0.2.34", 14 | "morgan": "~1.9.1", 15 | "serve-favicon": "~2.3.0" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/IBM/guide-conveyor-simulator" 20 | }, 21 | "license": "EPL-1.0" 22 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | This is an open source project, and we appreciate your help! 4 | 5 | We use the GitHub issue tracker to discuss new features and non-trivial bugs. 6 | 7 | In addition to the issue tracker, [#journeys on 8 | Slack](https://dwopen.slack.com) is the best way to get into contact with the 9 | project's maintainers. 10 | 11 | To contribute code, documentation, or tests, please submit a pull request to 12 | the GitHub repository. Generally, we expect two maintainers to review your pull 13 | request before it is approved for merging. For more details, see the 14 | [MAINTAINERS](MAINTAINERS.md) page. 15 | -------------------------------------------------------------------------------- /public/images/connecting.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /public/images/connected.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /public/images/disconnected.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 9 | 11 | 12 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "App", 3 | "icons": [ 4 | { 5 | "src": "\/android-icon-36x36.png", 6 | "sizes": "36x36", 7 | "type": "image\/png", 8 | "density": "0.75" 9 | }, 10 | { 11 | "src": "\/android-icon-48x48.png", 12 | "sizes": "48x48", 13 | "type": "image\/png", 14 | "density": "1.0" 15 | }, 16 | { 17 | "src": "\/android-icon-72x72.png", 18 | "sizes": "72x72", 19 | "type": "image\/png", 20 | "density": "1.5" 21 | }, 22 | { 23 | "src": "\/android-icon-96x96.png", 24 | "sizes": "96x96", 25 | "type": "image\/png", 26 | "density": "2.0" 27 | }, 28 | { 29 | "src": "\/android-icon-144x144.png", 30 | "sizes": "144x144", 31 | "type": "image\/png", 32 | "density": "3.0" 33 | }, 34 | { 35 | "src": "\/android-icon-192x192.png", 36 | "sizes": "192x192", 37 | "type": "image\/png", 38 | "density": "4.0" 39 | } 40 | ] 41 | } -------------------------------------------------------------------------------- /public/js/object-keys-polyfill.js: -------------------------------------------------------------------------------- 1 | // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys 2 | if (!Object.keys) { 3 | Object.keys = (function() { 4 | 'use strict'; 5 | var hasOwnProperty = Object.prototype.hasOwnProperty, 6 | hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'), 7 | dontEnums = [ 8 | 'toString', 9 | 'toLocaleString', 10 | 'valueOf', 11 | 'hasOwnProperty', 12 | 'isPrototypeOf', 13 | 'propertyIsEnumerable', 14 | 'constructor' 15 | ], 16 | dontEnumsLength = dontEnums.length; 17 | 18 | return function(obj) { 19 | if (typeof obj !== 'function' && (typeof obj !== 'object' || obj === null)) { 20 | throw new TypeError('Object.keys called on non-object'); 21 | } 22 | 23 | var result = [], prop, i; 24 | 25 | for (prop in obj) { 26 | if (hasOwnProperty.call(obj, prop)) { 27 | result.push(prop); 28 | } 29 | } 30 | 31 | if (hasDontEnumBug) { 32 | for (i = 0; i < dontEnumsLength; i++) { 33 | if (hasOwnProperty.call(obj, dontEnums[i])) { 34 | result.push(dontEnums[i]); 35 | } 36 | } 37 | } 38 | return result; 39 | }; 40 | }()); 41 | } -------------------------------------------------------------------------------- /routes/index.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | var https = require('https'); 4 | 5 | /* GET home page. */ 6 | router.get('/', function(req, res, next) { 7 | res.render('index'); 8 | }); 9 | 10 | router.get('/credentials', function(req, res) { 11 | var basicConfig = req.app.get('iot_credentials'); 12 | 13 | res.json({ 14 | org: basicConfig.org, 15 | serviceName: basicConfig.serviceName 16 | }); 17 | }); 18 | 19 | router.get('/iotServiceLink', function(req, res) { 20 | var basicConfig = req.app.get('iot_credentials'); 21 | 22 | console.log("BASIC CONFIG", basicConfig); 23 | 24 | var options = { 25 | host: basicConfig.org + '.internetofthings.ibmcloud.com', 26 | port: 443, 27 | headers: { 28 | 'Content-Type': 'application/json' 29 | }, 30 | auth: basicConfig.apiKey + ':' + basicConfig.apiToken, 31 | method: 'GET', 32 | path: 'api/v0002/' 33 | }; 34 | 35 | var org_req = https.request(options, function(org_res) { 36 | var str = ''; 37 | org_res.on('data', function(chunk) { 38 | str += chunk; 39 | }); 40 | org_res.on('end', function() { 41 | try { 42 | console.log("STRING", str); 43 | var org = JSON.parse(str); 44 | 45 | var url = "https://bluemix.net/services/" + org.bluemix.serviceInstanceGuid + "?ace_config=%7B%22orgGuid%22%3A%22" + org.bluemix.organizationGuid + "%22%2C%22spaceGuid%22%3A%22" + org.bluemix.spaceGuid; 46 | 47 | res.json({ url: url, serviceName: basicConfig.serviceName }); 48 | } catch (e) { 49 | console.log("Something went wrong...", e); 50 | console.log("RESPONSE", str); 51 | 52 | res.send(500); 53 | } 54 | 55 | console.log("iotServiceLink end: ", str.toString()); 56 | }); 57 | }).on('error', function(e) { console.log("ERROR", e); }); 58 | org_req.end(); 59 | }); 60 | 61 | module.exports = router; -------------------------------------------------------------------------------- /bin/www: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /** 4 | * Module dependencies. 5 | */ 6 | 7 | var app = require('../app'); 8 | var debug = require('debug')('conveyor-belt:server'); 9 | var http = require('http'); 10 | 11 | /** 12 | * Get port from environment and store in Express. 13 | */ 14 | 15 | var port = normalizePort(process.env.PORT || '3000'); 16 | app.set('port', port); 17 | 18 | /** 19 | * Create HTTP server. 20 | */ 21 | 22 | var server = http.createServer(app); 23 | 24 | /** 25 | * Listen on provided port, on all network interfaces. 26 | */ 27 | 28 | server.listen(port); 29 | server.on('error', onError); 30 | server.on('listening', onListening); 31 | 32 | 33 | /** 34 | * Normalize a port into a number, string, or false. 35 | */ 36 | 37 | function normalizePort(val) { 38 | var port = parseInt(val, 10); 39 | 40 | if (isNaN(port)) { 41 | // named pipe 42 | return val; 43 | } 44 | 45 | if (port >= 0) { 46 | // port number 47 | return port; 48 | } 49 | 50 | return false; 51 | } 52 | 53 | /** 54 | * Event listener for HTTP server "error" event. 55 | */ 56 | 57 | function onError(error) { 58 | if (error.syscall !== 'listen') { 59 | throw error; 60 | } 61 | 62 | var bind = typeof port === 'string' 63 | ? 'Pipe ' + port 64 | : 'Port ' + port; 65 | 66 | // handle specific listen errors with friendly messages 67 | switch (error.code) { 68 | case 'EACCES': 69 | console.error(bind + ' requires elevated privileges'); 70 | process.exit(1); 71 | break; 72 | case 'EADDRINUSE': 73 | console.error(bind + ' is already in use'); 74 | process.exit(1); 75 | break; 76 | default: 77 | throw error; 78 | } 79 | } 80 | 81 | /** 82 | * Event listener for HTTP server "listening" event. 83 | */ 84 | 85 | function onListening() { 86 | var addr = server.address(); 87 | var bind = typeof addr === 'string' 88 | ? 'pipe ' + addr 89 | : 'port ' + addr.port; 90 | debug('Listening on ' + bind); 91 | } 92 | -------------------------------------------------------------------------------- /routes/ibm-iot.js: -------------------------------------------------------------------------------- 1 | var express = require('express') 2 | , router = express.Router() 3 | , https = require('https'); 4 | 5 | router.post('/registerDevice', function(req, res) { 6 | var basicConfig = req.app.get('iot_credentials'); 7 | var options = req.app.get('iot_options'); 8 | 9 | options['method'] = 'POST'; 10 | options['path'] = 'api/v0002/device/types'; 11 | 12 | var deviceId = null, typeId = null, password = null; 13 | if (req.body.deviceId) { deviceId = req.body.deviceId; } 14 | if (req.body.typeId) { typeId = req.body.typeId; } 15 | if (req.body.password) { password = req.body.password; } 16 | 17 | var deviceTypeDetails = { 18 | id: typeId 19 | }; 20 | console.log(deviceTypeDetails); 21 | 22 | var type_req = https.request(options, function(type_res) { 23 | var str = ''; 24 | 25 | type_res.on('data', function(chunk) { 26 | str += chunk; 27 | }); 28 | 29 | type_res.on('end', function() { 30 | console.log("createDeviceType end: ", str.toString()); 31 | console.log(basicConfig); 32 | 33 | var dev_options = { 34 | host: basicConfig.org + '.internetofthings.ibmcloud.com', 35 | port: 443, 36 | headers: { 37 | 'Content-Type': 'application/json' 38 | }, 39 | auth: basicConfig.apiKey + ':' + basicConfig.apiToken, 40 | method: 'POST', 41 | path: 'api/v0002/device/types/'+typeId+'/devices' 42 | } 43 | 44 | var deviceDetails = { 45 | deviceId: deviceId, 46 | authToken: password 47 | }; 48 | console.log(deviceDetails); 49 | 50 | var dev_req = https.request(dev_options, function(dev_res) { 51 | var str = ''; 52 | dev_res.on('data', function(chunk) { 53 | str += chunk; 54 | }); 55 | dev_res.on('end', function() { 56 | console.log("createDevice end: ", str.toString()); 57 | res.send({ result: "Success!" }); 58 | }); 59 | }).on('error', function(e) { console.log("ERROR", e); }); 60 | dev_req.write(JSON.stringify(deviceDetails)); 61 | dev_req.end(); 62 | }); 63 | }).on('error', function(e) { console.log("ERROR", e); }); 64 | 65 | type_req.write(JSON.stringify(deviceTypeDetails)); 66 | type_req.end(); 67 | }); 68 | 69 | module.exports = router; -------------------------------------------------------------------------------- /CLA.md: -------------------------------------------------------------------------------- 1 | IBM Contributor License Agreement 2 | ================================= 3 | 4 | Version 1.0.0 January 14, 2014 5 | 6 | In order for You (as defined below) to make intellectual property Contributions (as defined below) now or in the future to IBM GitHub repositories, 7 | You must agree to this Contributor License Agreement ("CLA"). 8 | 9 | Please read this CLA carefully before accepting its terms. By accepting the CLA, You are agreeing to be bound by its terms. 10 | If You submit a Pull Request against an IBM repository on GitHub You must include in the Pull Request a statement of Your acceptance of this CLA. 11 | 12 | As used in this CLA: 13 | (i) "You" (or "Your") shall mean the entity that is making this Agreement with IBM; 14 | (ii)"Contribution" shall mean any original work of authorship, including any modifications or additions to an existing work, that is submitted by You to IBM for inclusion in, 15 | or documentation of, any of the IBM GitHub repositories; 16 | (iii) "Submit" (or "Submitted") means any form of communication sent to IBM (e.g. the content You post in a GitHub Issue or submit as part of a GitHub Pull Request). 17 | 18 | This agreement applies to all Contributions You Submit. 19 | 20 | This CLA, and the license(s) associated with the particular IBM GitHub repositories You are contributing to, provides a license to Your Contributions to IBM and downstream consumers, 21 | but You still own Your Contributions, and except for the licenses provided for in this CLA, You reserve all right, title and interest in Your Contributions. 22 | 23 | IBM requires that each Contribution You Submit now or in the future comply with the following four commitments. 24 | 25 | 1) You will only Submit Contributions where You have authored 100% of the content. 26 | 2) You will only Submit Contributions to which You have the necessary rights. This means that if You are employed You have received the necessary permissions from Your employer to make the 27 | Contributions. 28 | 3) Whatever content You Contribute will be provided under the license(s) associated with the particular IBM GitHub repository You are contributing to. 29 | 4) You understand and agree that IBM GitHub repositories and Your contributions are public, and that a record of the contribution (including all personal information You submit with it) 30 | is maintained indefinitely and may be redistributed consistent with the license(s) involved. 31 | You will promptly notify the Eclipse Foundation if You become aware of any facts or circumstances that would make these commitments inaccurate in any way. 32 | To do so, please create an Issue in the appropriate GitHub repository. 33 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var path = require('path'); 3 | var favicon = require('serve-favicon'); 4 | var logger = require('morgan'); 5 | var cookieParser = require('cookie-parser'); 6 | var bodyParser = require('body-parser'); 7 | 8 | var indexRoutes = require('./routes/index'); 9 | var ibmIoTRoutes = require('./routes/ibm-iot'); 10 | 11 | var app = express(); 12 | 13 | // view engine setup 14 | app.set('views', path.join(__dirname, 'views')); 15 | app.set('view engine', 'hbs'); 16 | 17 | // uncomment after placing your favicon in /public 18 | //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); 19 | app.use(logger('dev')); 20 | app.use(bodyParser.json()); 21 | app.use(bodyParser.urlencoded({ extended: false })); 22 | app.use(cookieParser()); 23 | app.use(express.static(path.join(__dirname, 'public'))); 24 | app.use(express.static(path.join(__dirname, 'node_modules/ibmiotf/dist'))); 25 | 26 | /* === VCAP_SERVICES - START === */ 27 | /* === Grab VCAP_SERVICES information either from Bluemix, or from a local file called VCAP_SERVICES.json === */ 28 | function configureCredentials(vcap) { 29 | config = vcap; 30 | 31 | var iotService = config['iotf-service']; 32 | credentials = iotService[0].credentials; 33 | credentials["serviceName"] = iotService[0].name; 34 | } 35 | 36 | try { 37 | var VCAP_SERVICES = require(__dirname + '/VCAP_SERVICES.json'); 38 | 39 | if (process.env.VCAP_SERVICES) { 40 | configureCredentials(JSON.parse(process.env.VCAP_SERVICES)); 41 | } else { 42 | configureCredentials(VCAP_SERVICES); 43 | } 44 | } catch (error) { 45 | console.log(error); 46 | console.log("Fallback to Bluemix VCAP_SERVICES"); 47 | 48 | if (process.env.VCAP_SERVICES) { 49 | configureCredentials(JSON.parse(process.env.VCAP_SERVICES)); 50 | } else { 51 | console.log("ERROR: IoT Service was not bound!"); 52 | } 53 | } 54 | 55 | var basicConfig = { 56 | org: credentials.org, 57 | apiKey: credentials.apiKey, 58 | apiToken: credentials.apiToken, 59 | serviceName: credentials.serviceName 60 | }; 61 | 62 | var options = { 63 | host: basicConfig.org + '.internetofthings.ibmcloud.com', 64 | port: 443, 65 | headers: { 66 | 'Content-Type': 'application/json' 67 | }, 68 | auth: basicConfig.apiKey + ':' + basicConfig.apiToken 69 | }; 70 | 71 | app.set('iot_credentials', basicConfig); 72 | app.set('iot_options', options); 73 | /* === VCAP_SERVICES - END === */ 74 | 75 | app.use('/', indexRoutes); 76 | app.use('/api', ibmIoTRoutes); 77 | 78 | // catch 404 and forward to error handler 79 | app.use(function(req, res, next) { 80 | var err = new Error('Not Found'); 81 | err.status = 404; 82 | next(err); 83 | }); 84 | 85 | // error handler 86 | app.use(function(err, req, res, next) { 87 | // set locals, only providing error in development 88 | res.locals.message = err.message; 89 | res.locals.error = req.app.get('env') === 'development' ? err : {}; 90 | 91 | // render the error page 92 | res.status(err.status || 500); 93 | res.render('error'); 94 | }); 95 | 96 | module.exports = app; 97 | -------------------------------------------------------------------------------- /MAINTAINERS.md: -------------------------------------------------------------------------------- 1 | # Maintainers Guide 2 | 3 | This guide is intended for maintainers - anybody with commit access to one or 4 | more Code Pattern repositories. 5 | 6 | ## Methodology 7 | 8 | This repository does not have a traditional release management cycle, but 9 | should instead be maintained as a useful, working, and polished reference at 10 | all times. While all work can therefore be focused on the master branch, the 11 | quality of this branch should never be compromised. 12 | 13 | The remainder of this document details how to merge pull requests to the 14 | repositories. 15 | 16 | ## Merge approval 17 | 18 | The project maintainers use LGTM (Looks Good To Me) in comments on the pull 19 | request to indicate acceptance prior to merging. A change requires LGTMs from 20 | two project maintainers. If the code is written by a maintainer, the change 21 | only requires one additional LGTM. 22 | 23 | ## Reviewing Pull Requests 24 | 25 | We recommend reviewing pull requests directly within GitHub. This allows a 26 | public commentary on changes, providing transparency for all users. When 27 | providing feedback be civil, courteous, and kind. Disagreement is fine, so long 28 | as the discourse is carried out politely. If we see a record of uncivil or 29 | abusive comments, we will revoke your commit privileges and invite you to leave 30 | the project. 31 | 32 | During your review, consider the following points: 33 | 34 | ### Does the change have positive impact? 35 | 36 | Some proposed changes may not represent a positive impact to the project. Ask 37 | whether or not the change will make understanding the code easier, or if it 38 | could simply be a personal preference on the part of the author (see 39 | [bikeshedding](https://en.wiktionary.org/wiki/bikeshedding)). 40 | 41 | Pull requests that do not have a clear positive impact should be closed without 42 | merging. 43 | 44 | ### Do the changes make sense? 45 | 46 | If you do not understand what the changes are or what they accomplish, ask the 47 | author for clarification. Ask the author to add comments and/or clarify test 48 | case names to make the intentions clear. 49 | 50 | At times, such clarification will reveal that the author may not be using the 51 | code correctly, or is unaware of features that accommodate their needs. If you 52 | feel this is the case, work up a code sample that would address the pull 53 | request for them, and feel free to close the pull request once they confirm. 54 | 55 | ### Does the change introduce a new feature? 56 | 57 | For any given pull request, ask yourself "is this a new feature?" If so, does 58 | the pull request (or associated issue) contain narrative indicating the need 59 | for the feature? If not, ask them to provide that information. 60 | 61 | Are new unit tests in place that test all new behaviors introduced? If not, do 62 | not merge the feature until they are! Is documentation in place for the new 63 | feature? (See the documentation guidelines). If not do not merge the feature 64 | until it is! Is the feature necessary for general use cases? Try and keep the 65 | scope of any given component narrow. If a proposed feature does not fit that 66 | scope, recommend to the user that they maintain the feature on their own, and 67 | close the request. You may also recommend that they see if the feature gains 68 | traction among other users, and suggest they re-submit when they can show such 69 | support. 70 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /public/stylesheets/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | background: #f5f7fa; 3 | margin: 0; 4 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; 5 | } 6 | 7 | a { 8 | color: #3d70b2; 9 | } 10 | 11 | label { 12 | display: block; 13 | font-weight: 700; 14 | margin-bottom: 15px; 15 | } 16 | 17 | input { 18 | border: none; 19 | outline: none; 20 | padding: 10px 15px; 21 | color: #5a6872; 22 | background-color: rgba(61,112,178,0.1); 23 | } 24 | 25 | form button { 26 | display: block; 27 | margin: auto; 28 | margin-top: 20px; 29 | padding: 10px 25px; 30 | font-size: 15px; 31 | background: #1BCC49; 32 | color: #fff; 33 | border: none; 34 | outline: none; 35 | } 36 | 37 | form p.message { 38 | background-color: #fff; 39 | border: 1px solid red; 40 | width: 100%; 41 | box-shadow: 0 4px 4px 0 rgba(0,0,0,.1); 42 | } 43 | 44 | form p.message span { 45 | display: table; 46 | padding: 10px 15px; 47 | margin: auto; 48 | } 49 | 50 | .hidden { 51 | display: none; 52 | } 53 | 54 | div.header { 55 | font-family: 'IBM Helvetica', Helvetica Neue, HelveticaNeue, Helvetica, sans-serif; 56 | width: 100%; 57 | height: 50px; 58 | font-size: 17px; 59 | font-weight: 300; 60 | line-height: 48px; 61 | background-color: #376393; 62 | color: #fff; 63 | overflow: hidden; 64 | } 65 | 66 | div.header img { 67 | float: left; 68 | width: 33px; 69 | margin: 15px 15px 0 30px; 70 | } 71 | 72 | div.header b { 73 | font-weight: 700; 74 | } 75 | 76 | /* SVG Styles */ 77 | svg.conveyor-belt { 78 | width: 80%; 79 | max-width: 750px; 80 | } 81 | 82 | .iot-box { 83 | -webkit-transform-origin: 50% 50%; 84 | -moz-transform-origin: 50% 50%; 85 | transform-origin: 50% 50%; 86 | } 87 | 88 | #rotator { 89 | -webkit-transform-origin: 766.454px 502.4215px; 90 | -moz-transform-origin: 766.454px 502.4215px; 91 | transform-origin: 766.454px 502.4215px; 92 | } 93 | 94 | .iot-box { 95 | -webkit-transform: translateY(-252px) scale(0.5); 96 | -ms-transform: translateY(-252px) scale(0.5); 97 | transform: translateY(-252px) scale(0.5); 98 | } 99 | 100 | div.controls { 101 | margin-top: 25px; 102 | } 103 | 104 | div.buttons { 105 | display: table; 106 | } 107 | 108 | a.btn { 109 | display: table; 110 | padding: 10px 25px; 111 | text-decoration: none; 112 | -moz-border-radius: 5px; 113 | -webkit-border-radius: 5px; 114 | border-radius: 5px; 115 | } 116 | 117 | a.btn.stop, 118 | a.btn.start, 119 | a.btn.speed-down, 120 | a.btn.speed-up { 121 | float: left; 122 | color: #fff; 123 | margin-left: 15px; 124 | } 125 | 126 | a.btn.stop, 127 | a.btn.speed-down { 128 | background: #E21717; 129 | margin-left: 0; 130 | } 131 | 132 | a.btn.start, 133 | a.btn.speed-up { 134 | background: #1BCC49; 135 | } 136 | 137 | a.btn.disabled { 138 | pointer-events: none; 139 | cursor: default; 140 | opacity: 0.45; 141 | } 142 | 143 | a.btn.btn-compact { 144 | padding: 3px 10px 6px 10px; 145 | line-height: 24px; 146 | font-size: 21px; 147 | } 148 | 149 | div.buttons span { 150 | float: left; 151 | margin-top: 10px; 152 | margin-left: 17px; 153 | } 154 | 155 | .iot-connected-image { 156 | max-width: 28px; 157 | max-height: 60px; 158 | } 159 | 160 | 161 | 162 | /* === 2. Modal === */ 163 | div.modal { 164 | position: absolute; 165 | top: 0; 166 | left: 0; 167 | width: 100%; 168 | height: 100%; 169 | overflow: auto; 170 | background: rgba(256, 256, 256, 0.9); 171 | } 172 | 173 | div.modal div.modal-content { 174 | position: absolute; 175 | top: 50%; 176 | left: 50%; 177 | transform: translate(-50%, -50%); 178 | } 179 | 180 | div.modal div.modal-content form { 181 | width: 250px; 182 | } 183 | 184 | div.modal div.modal-content form div.form-input { 185 | width: 155px; 186 | margin: auto; 187 | text-align: left; 188 | } 189 | 190 | 191 | 192 | /* === 3. Grid System === */ 193 | 194 | .row { 195 | box-sizing: border-box; 196 | display: -webkit-box; 197 | display: -ms-flexbox; 198 | display: -webkit-flex; 199 | display: flex; 200 | -webkit-box-flex: 0; 201 | -webkit-flex: 0 1 auto; 202 | -ms-flex: 0 1 auto; 203 | flex: 0 1 auto; 204 | -webkit-box-orient: horizontal; 205 | -webkit-box-direction: normal; 206 | -ms-flex-wrap: wrap; 207 | -webkit-flex-wrap: wrap; 208 | flex-wrap: wrap; 209 | -ms-flex-direction: row; 210 | -webkit-flex-direction: row; 211 | flex-direction: row; 212 | } 213 | 214 | .column-2-med, .column-3-med, .column-4-med, .column-5-med, .column-6-med, .column-7-med, .column-8-med, .column-9-med, .column-10-med, .column-11-med, .column-12-med { 215 | box-sizing: border-box; 216 | -webkit-box-flex: 0; 217 | -webkit-flex: 0 0 auto; 218 | -ms-flex: 0 0 auto; 219 | flex: 0 auto auto; 220 | padding: 15px; 221 | } 222 | 223 | .noPaddingRow .column-2-med, .noPaddingRow .column-3-med, .noPaddingRow .column-4-med, .noPaddingRow .column-5-med, 224 | .noPaddingRow .column-6-med, .noPaddingRow .column-7-med, .noPaddingRow .column-9-med, .noPaddingRow .column-10-med { 225 | padding: 0; 226 | } 227 | 228 | .noOffsetRow .column-2-med:first-child, .noOffsetRow .column-3-med:first-child, .noOffsetRow .column-4-med:first-child, 229 | .noOffsetRow .column-5-med:first-child, .noOffsetRow .column-6-med:first-child, .noOffsetRow .column-7-med:first-child, 230 | .noOffsetRow .column-9-med:first-child, .noOffsetRow .column-10-med:first-child { 231 | padding-left: 0; 232 | } 233 | 234 | .noOffsetRow .column-2-med:last-child, .noOffsetRow .column-3-med:last-child, .noOffsetRow .column-4-med:last-child, 235 | .noOffsetRow .column-5-med:last-child, .noOffsetRow .column-6-med:last-child, .noOffsetRow .column-7-med:last-child, 236 | .noOffsetRow .column-9-med:last-child, .noOffsetRow .column-10-med:last-child { 237 | padding-right: 0; 238 | } 239 | 240 | .noBottomPadding .column-2-med, .noBottomPadding .column-3-med, .noBottomPadding .column-4-med, .noBottomPadding .column-5-med, 241 | .noBottomPadding .column-6-med, .noBottomPadding .column-7-med, .noBottomPadding .column-9-med, .noBottomPadding .column-10-med { 242 | padding-bottom: 0; 243 | } 244 | 245 | .noOffsetRow-6 .column-6-med:nth-child(odd) { 246 | padding-left: 0; 247 | } 248 | 249 | .noOffsetRow-6 .column-6-med:last-child { 250 | padding-right: 15px; 251 | } 252 | 253 | .column-2-med { 254 | max-width: 16.6666666667%; 255 | -ms-flex-preferred-size: 16.6666666667%; 256 | -webkit-flex-basis: 16.6666666667%; 257 | flex-basis: 16.6666666667%; 258 | } 259 | 260 | .column-3-med { 261 | max-width: 25%; 262 | -ms-flex-preferred-size: 25%; 263 | -webkit-flex-basis: 25%; 264 | flex-basis: 25%; 265 | } 266 | 267 | .column-4-med { 268 | max-width: 33.3333333%; 269 | -ms-flex-preferred-size: 33.3333333%; 270 | -webkit-flex-basis: 33.3333333%; 271 | flex-basis: 33.3333333%; 272 | } 273 | 274 | .column-5-med { 275 | max-width: 41.6666667%; 276 | -ms-flex-preferred-size: 41.6666667%; 277 | -webkit-flex-basis: 41.6666667%; 278 | flex-basis: 41.6666667%; 279 | } 280 | 281 | .column-6-med { 282 | max-width: 50%; 283 | -ms-flex-preferred-size: 50%; 284 | -webkit-flex-basis: 50%; 285 | flex-basis: 50%; 286 | } 287 | 288 | .column-7-med { 289 | max-width: 58.333333333%; 290 | -ms-flex-preferred-size: 58.333333333%; 291 | -webkit-flex-basis: 58.333333333%; 292 | flex-basis: 58.333333333%; 293 | } 294 | 295 | .column-8-med { 296 | max-width: 66.666666666%; 297 | -ms-flex-preferred-size: 66.666666666%; 298 | -webkit-flex-basis: 66.666666666%; 299 | flex-basis: 66.666666666%; 300 | } 301 | 302 | .column-9-med { 303 | max-width: 75%; 304 | -ms-flex-preferred-size: 75%; 305 | -webkit-flex-basis: 75%; 306 | flex-basis: 75%; 307 | } 308 | 309 | .column-10-med { 310 | max-width: 83.3333333%; 311 | -ms-flex-preferred-size: 83.3333333%; 312 | -webkit-flex-basis: 83.3333333%; 313 | flex-basis: 83.3333333%; 314 | } 315 | 316 | .column-12-med { 317 | max-width: 100%; 318 | -ms-flex-preferred-size: 100%; 319 | -webkit-flex-basis: 100%; 320 | flex-basis: 100%; 321 | } 322 | 323 | @media (max-width:767px) { 324 | .column-2-med, .column-3-med, .column-4-med, .column-5-med, .column-6-med, .column-7-med, .column-8-med, .column-9-med, .column-10-med { 325 | max-width: 100%; 326 | -ms-flex-preferred-size: 100%; 327 | -webkit-flex-basis: 100%; 328 | flex-basis: 100%; 329 | } 330 | 331 | .noOffsetRow .column-2-med, .noOffsetRow .column-3-med, .noOffsetRow .column-4-med, .noOffsetRow .column-5-med, 332 | .noOffsetRow .column-6-med, .noOffsetRow .column-7-med, .noOffsetRow .column-9-med, .noOffsetRow .column-10-med { 333 | padding-right: 0; 334 | padding-left: 0; 335 | } 336 | 337 | .column-2-xs { 338 | max-width: 16.6666666667%; 339 | -ms-flex-preferred-size: 16.6666666667%; 340 | -webkit-flex-basis: 16.6666666667%; 341 | flex-basis: 16.6666666667%; 342 | } 343 | 344 | .column-3-xs { 345 | max-width: 25%; 346 | -ms-flex-preferred-size: 25%; 347 | -webkit-flex-basis: 25%; 348 | flex-basis: 25%; 349 | } 350 | 351 | .column-4-xs { 352 | max-width: 33.3333333%; 353 | -ms-flex-preferred-size: 33.3333333%; 354 | -webkit-flex-basis: 33.3333333%; 355 | flex-basis: 33.3333333%; 356 | } 357 | 358 | .column-5-xs { 359 | max-width: 41.6666667%; 360 | -ms-flex-preferred-size: 41.6666667%; 361 | -webkit-flex-basis: 41.6666667%; 362 | flex-basis: 41.6666667%; 363 | } 364 | 365 | .column-6-xs { 366 | max-width: 50%; 367 | -ms-flex-preferred-size: 50%; 368 | -webkit-flex-basis: 50%; 369 | flex-basis: 50%; 370 | } 371 | 372 | .column-7-xs { 373 | max-width: 58.333333333%; 374 | -ms-flex-preferred-size: 58.333333333%; 375 | -webkit-flex-basis: 58.333333333%; 376 | flex-basis: 58.333333333%; 377 | } 378 | 379 | .column-9-xs { 380 | max-width: 75%; 381 | -ms-flex-preferred-size: 75%; 382 | -webkit-flex-basis: 75%; 383 | flex-basis: 75%; 384 | } 385 | 386 | .column-10-xs { 387 | max-width: 83.3333333%; 388 | -ms-flex-preferred-size: 83.3333333%; 389 | -webkit-flex-basis: 83.3333333%; 390 | flex-basis: 83.3333333%; 391 | } 392 | } 393 | 394 | /* small */ 395 | @media (min-width: 768px and max-width: 991px) { } 396 | 397 | /* med */ 398 | @media (min-width: 992px and max-width: 1199px) { } 399 | 400 | /* large */ 401 | @media (min-width: 1200px) { 402 | .column-2-lg { 403 | max-width: 16.6666666667%; 404 | -ms-flex-preferred-size: 16.6666666667%; 405 | -webkit-flex-basis: 16.6666666667%; 406 | flex-basis: 16.6666666667%; 407 | } 408 | 409 | .column-3-lg { 410 | max-width: 25%; 411 | -ms-flex-preferred-size: 25%; 412 | -webkit-flex-basis: 25%; 413 | flex-basis: 25%; 414 | } 415 | 416 | .column-4-lg { 417 | max-width: 33.3333333%; 418 | -ms-flex-preferred-size: 33.3333333%; 419 | -webkit-flex-basis: 33.3333333%; 420 | flex-basis: 33.3333333%; 421 | } 422 | 423 | .column-5-lg { 424 | max-width: 41.6666667%; 425 | -ms-flex-preferred-size: 41.6666667%; 426 | -webkit-flex-basis: 41.6666667%; 427 | flex-basis: 41.6666667%; 428 | } 429 | 430 | .column-6-lg { 431 | max-width: 50%; 432 | -ms-flex-preferred-size: 50%; 433 | -webkit-flex-basis: 50%; 434 | flex-basis: 50%; 435 | } 436 | 437 | .column-7-lg { 438 | max-width: 58.333333333%; 439 | -ms-flex-preferred-size: 58.333333333%; 440 | -webkit-flex-basis: 58.333333333%; 441 | flex-basis: 58.333333333%; 442 | } 443 | 444 | .column-9-lg { 445 | max-width: 75%; 446 | -ms-flex-preferred-size: 75%; 447 | -webkit-flex-basis: 75%; 448 | flex-basis: 75%; 449 | } 450 | 451 | .column-10-lg { 452 | max-width: 83.3333333%; 453 | -ms-flex-preferred-size: 83.3333333%; 454 | -webkit-flex-basis: 83.3333333%; 455 | flex-basis: 83.3333333%; 456 | } 457 | } -------------------------------------------------------------------------------- /views/index.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Conveyor Belt 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 | 41 | Plant floor - Conveyor belt 42 |
43 | 44 |
45 |
46 | 48 | 49 | 50 | 55 | 56 | 57 | 59 | 62 | 65 | 66 | 68 | 70 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 83 | 86 | 89 | 90 | 92 | 94 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | {{!----}} 150 | 151 |
152 | 153 |
154 |
155 | 164 | 165 |
166 |

167 | 168 | Adjust Speed 169 | 170 |

171 | 172 |
173 | 174 | - 175 | 176 | 177 | 178 | 1.0x 179 | 180 | 181 | 182 | + 183 | 184 |
185 |
186 |
187 | 188 |
189 |

Your Device ID: 190 | 191 |

192 | 193 |

194 | 195 | Disconnected 196 |

​ 197 | 198 | 205 |
206 |
207 |
208 | 209 | 239 | 240 | -------------------------------------------------------------------------------- /public/js/conveyor-belt.js: -------------------------------------------------------------------------------- 1 | // Variables used for the animations 2 | var stop = false; 3 | var animationSpeed = 1.0; 4 | var animatedElements = []; 5 | 6 | // IoT Platform & MQTT Related Variables 7 | var deviceInfo = { 8 | // You can onfigure these to your liking, or automate the generation of them 9 | deviceId: "", 10 | typeId: "iot-conveyor-belt", 11 | password: "" 12 | }; 13 | 14 | // Mobile Sensor Related Variables 15 | var ax = 0, 16 | ay = 0, 17 | az = 0; 18 | 19 | var last_sample = {}; 20 | var shifted_filter = {}; 21 | // High-pass filter to remove gravity offset from the acceleration waveforms 22 | 23 | var client; 24 | var iot_host; 25 | var iot_port; 26 | var iot_clientid; 27 | var iot_username; 28 | var iot_password; 29 | var topic = "sensorData"; 30 | var iot_service_link; 31 | 32 | var isConnected = false; 33 | window.msgCount = 0; 34 | 35 | // ********** TABLE OF CONTENTS ********** 36 | /* 37 | 1. IoT Platform Device 38 | 2. MQTT 39 | 3. Mobile Sensor Data 40 | 4. Animations/Interactions 41 | */ 42 | // ********** TABLE OF CONTENTS - END ********** // 43 | 44 | 45 | 46 | 47 | 48 | 49 | // ********************************** // 50 | // ***** 1. IoT Platform Device ***** // 51 | 52 | // Initialize the application 53 | // Getting the VCAP_SERVICES credentials from the backend 54 | function init() { 55 | $.ajax({ 56 | url: "/credentials", 57 | type: "GET", 58 | contentType: "application/json; charset=utf-8", 59 | dataType: "json", 60 | success: function(response) { 61 | console.log(response); 62 | 63 | // Set necessary fields for the MQTT Connection to the IoT Platform 64 | window.iot_host = response.org + ".messaging.internetofthings.ibmcloud.com"; 65 | window.iot_port = 443; 66 | window.deviceClient = new IBMIoTF.IotfDevice({ 67 | "org": response.org, 68 | "id": deviceInfo.deviceId, 69 | "type": deviceInfo.typeId, 70 | "auth-method": "token", 71 | "auth-token": deviceInfo.password 72 | }); 73 | registerDevice(); 74 | }, 75 | error: function(xhr, status, error) { 76 | console.error("Could not fetch organization information."); 77 | } 78 | }); 79 | 80 | $.ajax({ 81 | url: "/iotServiceLink", 82 | type: "GET", 83 | contentType: "application/json; charset=utf-8", 84 | dataType: "json", 85 | success: function(response) { 86 | iot_service_link = response; 87 | }, 88 | error: function(xhr, status, error) { 89 | console.error("Could not fetch organization information."); 90 | } 91 | }); 92 | } 93 | 94 | // Register the device through the backend to the IoT Platform 95 | // (1st. creates device type, then the actual device, all values configured at the top of this file) 96 | function registerDevice() { 97 | console.log("Attempting to Register the Device"); 98 | 99 | // Make an AJAX call to the backend 100 | $.ajax({ 101 | url: "/api/registerDevice", 102 | type: "POST", 103 | contentType: "application/json; charset=utf-8", 104 | dataType: "json", 105 | data: JSON.stringify(deviceInfo), 106 | success: function(response) { 107 | console.log("Attempting connect"); 108 | 109 | // After registration is successful, attempt connecting to MQTT 110 | connectDevice(); 111 | }, 112 | error: function(xhr, status, error) { 113 | if (xhr.status === 403) { 114 | // Authentication check succeeded and told us we're invalid 115 | console.error("Incorrect code!"); 116 | } else { 117 | // Something else went wrong 118 | console.error("Failed to authenticate! " + error); 119 | } 120 | } 121 | }); 122 | } 123 | 124 | 125 | 126 | // ******************* // 127 | // ***** 2. MQTT ***** // 128 | 129 | // Once connected, this functions is called to publish MQTT events to the IoT Platform 130 | function publish(publishFields) { 131 | console.log(publishFields); 132 | 133 | // We only attempt to publish if we're actually connected, saving CPU and battery 134 | if (isConnected) { 135 | // The payload that will be sent (all fields go in here) 136 | var payload = { 137 | "d": { 138 | "id": deviceInfo.deviceId, 139 | "ts": (new Date()).getTime(), 140 | "ay": Math.max(ax, ay, az).toFixed(2) 141 | } 142 | }; 143 | 144 | if (publishFields.running === false) { 145 | publishFields["rpm"] = 0; 146 | } 147 | 148 | for (var i = 0; i < Object.keys(publishFields).length; i++) { 149 | var index = Object.keys(publishFields)[i]; 150 | payload.d[index] = publishFields[index]; 151 | } 152 | 153 | // Publish message 154 | try { 155 | window.deviceClient.publish(topic, "json", JSON.stringify(payload)); 156 | window.msgCount += 1; 157 | 158 | $("pre code#publishedMessage").html(JSON.stringify(payload, null, "\t")); 159 | $('pre code#publishedMessage').each(function(i, block) { 160 | hljs.highlightBlock(block); 161 | }); 162 | 163 | console.log("[%s] Published", new Date().getTime()); 164 | } catch (err) { 165 | console.error(err); 166 | 167 | // If there is an error, set the "connection" indicator on the screen to "Disconnected" 168 | isConnected = false; 169 | changeConnectionStatusImage("images/disconnected.svg"); 170 | document.getElementById("connection").innerHTML = "Disconnected"; 171 | 172 | setTimeout(connectDevice(), 1000); 173 | } 174 | } 175 | } 176 | 177 | // Once MQTT Connects 178 | function onConnectSuccess() { 179 | // The device connected successfully 180 | console.log("Connected Successfully!"); 181 | 182 | if ($("div#publishedMessage.hidden").hasClass("hidden")) $("div#publishedMessage.hidden").removeClass("hidden"); 183 | 184 | isConnected = true; 185 | changeConnectionStatusImage("images/connected.svg"); 186 | document.getElementById("connection").innerHTML = "Connected" + 187 | (iot_service_link !== undefined ? (" to " + iot_service_link.serviceName + "") : ""); 188 | 189 | var publishFields = { 190 | running: stop ? false : true, 191 | }; 192 | 193 | if (!stop) publishFields["rpm"] = animationSpeed.toFixed(1); 194 | publish(publishFields); 195 | } 196 | 197 | // Once MQTT Fails 198 | function onConnectFailure(error) { 199 | console.error(error); 200 | 201 | // The device failed to connect. Let's try again in one second. 202 | console.log("Could not connect to IBM Watson IoT Platform! Trying again in one second."); 203 | 204 | // Try connecting after 1000 milliseconds 205 | setTimeout(connectDevice(), 1000); 206 | } 207 | 208 | 209 | // Connect to MQTT 210 | function connectDevice() { 211 | $("#deviceId").html(deviceInfo.deviceId); 212 | 213 | // Update connection status on screen to "Connecting" 214 | changeConnectionStatusImage("images/connecting.svg"); 215 | document.getElementById("connection").innerHTML = "Connecting"; 216 | console.log("Connecting device to IBM Watson IoT Platform..."); 217 | 218 | // Initiate the MQTT connection using the password set above in line 8 219 | try { 220 | window.deviceClient.connect(); 221 | } catch(e) { 222 | onConnectFailure(e); 223 | } 224 | 225 | window.deviceClient.on('connect', onConnectSuccess); 226 | } 227 | 228 | 229 | 230 | // ************************************** // 231 | // ***** 3. Mobile Sensor Data ***** // 232 | function filterOffset(sample, channel) { 233 | if(sample === null || sample === undefined) return 0; 234 | if(last_sample[channel] === undefined) last_sample[channel] = sample; 235 | if(shifted_filter[channel] === undefined) shifted_filter[channel] = 0; 236 | var shiftedFCL = shifted_filter[channel] + ((sample-last_sample[channel])*256); 237 | shifted_filter[channel] = shiftedFCL - (shiftedFCL/256); 238 | last_sample[channel] = sample; 239 | return ((shifted_filter[channel]+128)/256); 240 | } 241 | 242 | window.ondevicemotion = function(event) { 243 | ax = parseFloat((event.acceleration.x || filterOffset(event.accelerationIncludingGravity.x, "ax") || 0)); 244 | ay = parseFloat((event.acceleration.y || filterOffset(event.accelerationIncludingGravity.y, "ay") || 0)); 245 | az = parseFloat((event.acceleration.z || filterOffset(event.accelerationIncludingGravity.z, "az") || 0)); 246 | }; 247 | 248 | // ************************************** // 249 | // ***** 4. Animations/Interactions ***** // 250 | //***** to handle multiple clicks*****// 251 | function debounce(func, wait, immediate) { 252 | var timeout; 253 | return function() { 254 | var context = this, args = arguments; 255 | var later = function() { 256 | timeout = null; 257 | if (!immediate) func.apply(context, args); 258 | }; 259 | var callNow = immediate && !timeout; 260 | clearTimeout(timeout); 261 | timeout = setTimeout(later, wait); 262 | if (callNow) func.apply(context, args); 263 | }; 264 | }; 265 | 266 | function dropbox(index) { 267 | var firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1; 268 | 269 | if (!stop) { 270 | var tween = Tweene.line($("g#box-" + index)) 271 | .from({translateY: -252}) 272 | //.to({translateY: 0}, 400, 100, "easeInQuad") 273 | .to({translateY: 0,easing:"easeInQuad"} ) 274 | .to({translateX: 237.5}, 1000, '++=0', 'linear') 275 | .to({translateX: 575}, 1000, '++=0', 'linear', function() { 276 | if (index === "1") { 277 | dropbox("2"); 278 | } else if (index === "2") { 279 | dropbox("1"); 280 | } 281 | }) 282 | .to({translateX: 630, translateY: 15, rotateZ: firefox ? 0 : 30}, 300, '++=0', 'linear') 283 | .to({translateX: 650, translateY: 30, rotateZ: firefox ? 0 : 40}, 100, '++=0', 'linear') 284 | .to({translateX: 715, translateY: 150}, 300, '++=0', 'linear') 285 | .to({translateY: 400, rotateZ: firefox ? 0 : 100}, 300, '++=0', 'linear') 286 | .to({ translateY: -252, translateX: 0, rotateZ: 0 }, 0, '++=0', 'linear', function() { 287 | animatedElements.splice(animatedElements.indexOf(tween), 1); 288 | }) 289 | .speed(animationSpeed) 290 | .loops(0) 291 | .play(); 292 | 293 | animatedElements.push(tween); 294 | } 295 | } 296 | 297 | function rotator() { 298 | // if (animatedElements.indexOf("line#rotator") === -1) animatedElements.push("line#rotator"); 299 | 300 | $("line#rotator") 301 | .delay(0) 302 | .velocity({ rotateZ: 360 }, { 303 | duration: 2000, 304 | easing: "linear", 305 | loop: true 306 | }); 307 | } 308 | 309 | // Update connection status image on screen 310 | function changeConnectionStatusImage(image) { 311 | document.getElementById("connectionImage").src = image; 312 | } 313 | 314 | $(document).ready(function() { 315 | $("#form-login").submit(function( event ) { 316 | event.preventDefault(); 317 | 318 | var usernameEntered = $("input#username").val(); 319 | var passwordEntered = $("input#password").val(); 320 | 321 | function showErrorOnRegister(message) { 322 | if ($("form#form-login p.message").hasClass("hidden")) { 323 | $("form#form-login p.message").removeClass("hidden"); 324 | } 325 | 326 | $("form#form-login p.message b").html(message); 327 | } 328 | 329 | if (usernameEntered.length === 0 && passwordEntered.length === 0) { 330 | showErrorOnRegister("a Device ID & Password"); 331 | } else if (usernameEntered.length === 0) { 332 | showErrorOnRegister("a Device ID"); 333 | } else if (passwordEntered.length === 0) { 334 | showErrorOnRegister("a Password"); 335 | } else { 336 | if (!$("form#form-login p.message").hasClass("hidden")) { 337 | $("form#form-login p.message").addClass("hidden"); 338 | } 339 | 340 | deviceInfo.deviceId = $("input#username").val(); 341 | deviceInfo.password = $("input#password").val(); 342 | 343 | $("div#modal-login").fadeOut( "fast", function() { 344 | init(); 345 | }); 346 | } 347 | }); 348 | 349 | rotator(); 350 | dropbox("1"); 351 | 352 | $("a.btn.start").addClass("disabled"); 353 | 354 | function toggleAnimations(pause) { 355 | stop = pause ? true : false; 356 | 357 | for (var i = 0; i < animatedElements.length; i++) { 358 | if (pause) animatedElements[i].pause(); 359 | else animatedElements[i].resume(); 360 | } 361 | } 362 | 363 | $("a.stop").click(function(event) { 364 | event.preventDefault(); 365 | console.log("STOP Clicked"); 366 | 367 | toggleAnimations(true); 368 | 369 | if ($("a.btn.start").hasClass("disabled")) { 370 | $("a.btn.start").removeClass("disabled"); 371 | } 372 | 373 | if (!$("a.btn.stop").hasClass("disabled")) { 374 | $("a.btn.stop").addClass("disabled"); 375 | } 376 | 377 | if (!$("a.speed-down").hasClass("disabled")) { 378 | $("a.speed-down").addClass("disabled"); 379 | } 380 | 381 | if (!$("a.speed-up").hasClass("disabled")) { 382 | $("a.speed-up").addClass("disabled"); 383 | } 384 | 385 | if (isConnected) { 386 | publish({ 387 | "running": false 388 | }); 389 | } 390 | }); 391 | 392 | $("a.start").click(function(event) { 393 | event.preventDefault(); 394 | console.log("START Clicked"); 395 | 396 | toggleAnimations(false); 397 | 398 | if ($("a.btn.stop").hasClass("disabled")) { 399 | $("a.btn.stop").removeClass("disabled"); 400 | } 401 | 402 | if (!$("a.btn.start").hasClass("disabled")) { 403 | $("a.btn.start").addClass("disabled"); 404 | } 405 | 406 | if ($("a.speed-down").hasClass("disabled")) { 407 | $("a.speed-down").removeClass("disabled"); 408 | } 409 | 410 | if ($("a.speed-up").hasClass("disabled")) { 411 | $("a.speed-up").removeClass("disabled"); 412 | } 413 | 414 | publish({ 415 | "running": true, 416 | "rpm": animationSpeed.toFixed(1) 417 | }); 418 | }); 419 | 420 | function round(value, decimals) { 421 | return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals); 422 | } 423 | 424 | function updateSpeedOnScreen() { 425 | $("span#speed-value").html(round(animationSpeed, 1) + "x"); 426 | } 427 | 428 | var adjustSpeed =debounce(function adjustSpeed() { 429 | console.log("After debounce"); 430 | for (var i = 0; i < animatedElements.length; i++) { 431 | animatedElements[i].speed(animationSpeed); 432 | } 433 | },1000,false); 434 | 435 | $("a.speed-down").click(function(event) { 436 | event.preventDefault(); 437 | console.log("SPEED DOWN Clicked"); 438 | 439 | animationSpeed -= 0.1; 440 | adjustSpeed(); 441 | 442 | if (isConnected) { 443 | publish({ 444 | "running": true, 445 | "rpm": animationSpeed.toFixed(1) 446 | }); 447 | } 448 | 449 | //rotator(); 450 | updateSpeedOnScreen(); 451 | 452 | console.log(animationSpeed); 453 | 454 | if (animationSpeed.toFixed(1) === "0.1") { 455 | $("a.speed-down").addClass("disabled"); 456 | } 457 | }); 458 | 459 | $("a.speed-up").click(function(event) { 460 | event.preventDefault(); 461 | console.log("SPEED UP Clicked"); 462 | 463 | animationSpeed += 0.1; 464 | adjustSpeed(); 465 | 466 | if (isConnected) { 467 | publish({ 468 | "rpm": animationSpeed.toFixed(1), 469 | "running": true 470 | }); 471 | } 472 | 473 | // rotator(); 474 | updateSpeedOnScreen(); 475 | 476 | console.log(animationSpeed); 477 | }); 478 | }); 479 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WARNING: This repository is no longer maintained :warning: 2 | > This content is no longer being updated or maintained. The content is provided “as is.” Given the rapid evolution of technology, some content, steps, or illustrations may have changed. 3 | 4 | 5 | 6 | **__Skill Level__**: Beginner 7 |
**__N.B__**: All services used in this repo are Lite plans. 8 | 9 | # [Connecting a conveyor belt device](https://developer.ibm.com/code/patterns/build-monitor-iot-driven-conveyor-belt/) 10 | Create a basic conveyor belt with an IoT device that sends monitoring data to Watson IoT Platform on IBM Cloud 11 | 12 | ## Overview and goal 13 | 14 | This guide steps you through the process of connecting devices to Watson IoT Platform, monitoring and acting on device data 15 | 16 | We set up a connected conveyor belt and use it to send IoT data to Watson IoT Platform. Depending on your skill level, you can follow one or both of the following paths to set up your conveyor belt: 17 | * Path A 18 | This path gets you started quickly by installing a conveyor belt simulator app on IBM Cloud. The app self-registers a device with Watson IoT Platform and automatically sends well-formatted data to the platform. Instructions for this path are in [Step 2A - Use the simulator sample app from GitHub](#step-2a---deploy-the-sample-conveyor-belt-web-application). 19 | * Path B 20 | This path is technically more challenging and requires additional hardware, Python programming skills, and manual registration of your device with Watson IoT Platform. Instructions for this path are in [Step 2B - Build a physical conveyor belt with a Raspberry Pi and an electric motor](#step-2b---build-a-raspberry-pi-powered-conveyor-belt). 21 | 22 | As part of this guide, you will: 23 | 1. Create and deploy a Watson IoT Platform organization by using Cloud Foundry CLI. 24 | 2. Build and deploy a sample conveyor belt device. 25 | 3. Connect the simulated conveyor belt device to Watson IoT Platform. 26 | 4. Monitor and visualize device data by using the Watson IoT Platform dashboards. 27 | 28 | > To get started with Watson IoT Platform using a different IoT device, see the [Getting started tutorial](https://console.bluemix.net/docs/services/IoT/getting-started.html#getting-started-with-iotp). 29 | 30 | ![Architecture Diagram](images/architecture.png) 31 | 32 | 1. Create & Deploy Watson IoT Platform on IBM Cloud 33 | 2. Build & Deploy Simulated Sample Conveyor 34 | 3. Connect the conveyor belt to the Watson IoT Platform 35 | 4. Monitor & Visualize device data on the Watson IoT dashboard. 36 | 37 | Click here to view the [IBM Pattern](https://developer.ibm.com/code/patterns/build-monitor-iot-driven-conveyor-belt/) for this project. 38 | ## Prerequisites 39 | 40 | You will need the following accounts and tools: 41 | * [IBM Cloud account](https://console.ng.bluemix.net/registration/) 42 | * [Bluemix CLI](https://console.bluemix.net/docs/cli/reference/bluemix_cli/index.html#getting-started) 43 | * Optional: [Git](https://git-scm.com/downloads) 44 | If you choose to use Git to download the code samples you must also have a [GitHub.com account](https://github.com). You can also download the code as a compressed file without a GitHub.com account. 45 | * Optional: A mobile phone on which to run the *Conveyor belt* sample web application to send accelerometer data. 46 | 47 | ## Deploy to IBM Cloud 48 | If you want to deploy directly to IBM Cloud, click on `Deploy to IBM Cloud` button below to create a IBM Cloud DevOps service toolchain and pipeline for deploying basic conveyor belt with an IoT device that sends monitoring data to Watson IoT Platform on IBM Cloud, else jump to [Steps](#steps) 49 | 50 | [![Deploy to IBM Cloud](https://bluemix.net/deploy/button.png)](https://bluemix.net/deploy?repository=https://github.com/IBM/guide-conveyor-simulator.git) 51 | 52 | After deploying the application, please continue with [Step 3 - See raw data in Watson IoT Platform](#step-3---see-raw-data-in-watson-iot-platform). 53 | 54 | ## Steps 55 | 1. [Deploy Watson IoT Platform](#step-1----deploy-watson-iot-platform) 56 | 2. Do one of the following 2 steps: 57 | 58 | 2a. [Deploy the sample conveyor belt web application](#step-2a---deploy-the-sample-conveyor-belt-web-application) 59 | 60 | 2b. [Build a Raspberry Pi-powered conveyor belt](#step-2b---build-a-raspberry-pi-powered-conveyor-belt) 61 | 62 | 3. [See raw data in Watson IoT Platform](#step-3---see-raw-data-in-watson-iot-platform) 63 | 4. [Visualize live data in Watson IoT Platform](#step-4---visualize-live-data-in-watson-iot-platform) 64 | 65 | ## Step 1 - Deploy Watson IoT Platform 66 | 67 | > Watson IoT Platform provides powerful application access to IoT devices and data to help you rapidly compose analytics applications, visualization dashboards, and mobile IoT apps. The steps that follow will deploy an instance of the Watson IoT Platform service with the name `iotp-for-conveyor` in your IBM Cloud environment. If you already have a service instance running, you can use that instance with the guide and skip this first step. Just make sure that you use the correct service name and IBM Cloud space when you proceed through the guides. 68 | 69 | 1. From the command line, set your API endpoint by running the `bx api` command. 70 | Replace the `API-ENDPOINT` value with the API endpoint for your region. 71 | ``` 72 | bx api 73 | ``` 74 | Example: `bx api https://api.ng.bluemix.net` 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 |
RegionAPI Endpoint
US Southhttps://api.ng.bluemix.net
United Kingdomhttps://api.eu-gb.bluemix.net
89 | 90 | 2. Log into your IBM Cloud account. 91 | ``` 92 | bx login 93 | ``` 94 | If prompted, select the organization and space where you want to deploy Watson IoT Platform and the sample app. **Note** example a) org: lbenn@us.ibm.com b) space = dev 95 | 96 | 3. Deploy the Watson IoT Platform service to IBM Cloud. 97 | ``` 98 | bx create-service iotf-service iotf-service-free YOUR_IOT_PLATFORM_NAME 99 | ``` 100 | 101 | For `YOUR_IOT_PLATFORM_NAME`, use *iotp-for-conveyor*. 102 | Example: `bx create-service iotf-service iotf-service-free iotp-for-conveyor` 103 | 104 | Next, you will create a sample conveyor belt device. Choose which path you want to take: 105 | - Path A: [Step 2A - Use the simulator sample app from GitHub](#step-2a---deploy-the-sample-conveyor-belt-web-application). 106 | - Path B: [Step 2B - Build a physical conveyor belt with a Raspberry Pi and an electric motor](#step-2b---build-a-raspberry-pi-powered-conveyor-belt). 107 | 108 | ## Step 2A - Deploy the sample conveyor belt web application 109 | 110 | The sample app lets you simulate a IBM Cloud connected industrial conveyor belt. You can start and stop the belt and adjust the speed of the belt. Every change to the belt is sent to IBM Cloud in the form of an MQTT message that is displayed in the app. You can monitor the belt behavior by using the default dashboard cards. The sample app is built using the Node.js client libraries at: [https://github.com/ibm-watson-iot/iot-nodejs](https://github.com/ibm-watson-iot/iot-nodejs) 111 | 112 | ![Conveyor belt app](images/app_conveyor_belt.png) 113 | 114 | * From the *guide-conveyor-simulator* directory, push your app to IBM Cloud and give it a new name by replacing `YOUR_APP_NAME` in the bx push command. Use the `--no-start` option because you will start the app in the next stage after it is bound to Watson IoT Platform. 115 | > Note: Deploying your application can take a few minutes. 116 | 117 | ```bash 118 | bx app push YOUR_APP_NAME --no-start 119 | ``` 120 | 121 | * In the *guide-conveyor-simulator* directory, bind your app to your instance of the Watson IoT Platform by using the names that you provided for each. 122 | ```bash 123 | bx bind-service YOUR_APP_NAME iotp-for-conveyor 124 | ``` 125 | >For more information about binding applications, see [Connecting applications](https://console.bluemix.net/docs/services/IoT/platform_authorization.html#bluemix-binding). 126 | 127 | * Start your application for the binding to take effect. 128 | ```bash 129 | bx start YOUR_APP_NAME 130 | ``` 131 | At this stage, your sample web application is deployed on IBM Cloud. When deployment is completed, a message is displayed to indicate that your app is running. 132 | Example: 133 | ```bash 134 | name: YOUR_APP_NAME 135 | requested state: started 136 | instances: 1/1 137 | usage: 64M x 1 instances 138 | routes: YOUR_APP_NAM.mybluemix.net 139 | last uploaded: Tue 09 May 09:29:49 EDT 2017 140 | stack: cflinuxfs2 141 | buildpack: SDK for Node.js(TM) (ibm-node.js-4.8.0, 142 | buildpack-v3.11-20170303-1144) 143 | start command: ./vendor/initial_startup.rb 144 | 145 | state since cpu memory disk details 146 | #0 running 2017-05-09T13:35:08Z 0.0% 19.6M of 64M 66.2M of 256M 147 | ``` 148 | 149 | 150 | > Troubleshoot errors in the deployment process by using the `bx app logs YOUR_APP_NAME --recent` command. 151 | 152 | * In a browser, access the app. 153 | Open the following URL: `https://YOUR_APP_NAME.mybluemix.net` 154 | Example: `https://conveyorbelt.mybluemix.net/`. 155 | 156 | * Enter a device ID and token for your device. 157 | The sample app automatically registers a device of type `iot-conveyor-belt` with the device ID (e.g. my-first-device) and token that you provided. For more information about registering devices, see [Connecting devices](https://console.bluemix.net/docs/services/IoT/iotplatform_task.html#iotplatform_subtask1). 158 | 4. Continue with [Step 3 - See raw data in Watson IoT Platform](#step-3---see-raw-data-in-watson-iot-platform). 159 | 160 | ## Step 2B - Build a Raspberry Pi-powered conveyor belt 161 | 162 | The Raspberry Pi solution is built using the Python client libraries at: [https://github.com/ibm-watson-iot/iot-python](https://github.com/ibm-watson-iot/iot-python) 163 | 164 | ### Schematic diagram for the circuit 165 | 166 | ![Circuit Diagram](images/raspi_circuit.png) 167 | 168 | ### Required connections 169 | 170 | The following connections between the Raspberry Pi and the L298N Dual H Bridge Motor Driver Board are required. Connect: 171 | 172 | - Pin 2 of the Raspberry Pi to +5v on the L298N board 173 | - Pin 6 of the Raspberry Pi to GND on the L298N board 174 | - Pin 13 of the Raspberry Pi to the IN1 of the L298N board 175 | - Pin 15 of the Raspberry Pi to the IN2 of the L298N board 176 | - +9v of the battery to +12v on the L298N board 177 | - -9v of the battery to GND on the L298N board 178 | - The +ve node of the motor to OUT1 on the L298N board 179 | - The -ve node of the motor to OUT2 on the L298N board 180 | 181 | ### Hardware requirements 182 | 183 | 1. [Raspberry Pi(2/3)](https://www.raspberrypi.org/) with Raspbian Jessie (8.x). 184 | 2. [L298N Dual H Bridge Motor Driver Board](https://tronixlabs.com.au/robotics/motor-controllers/l298n-dual-motor-controller-module-2a-australia/) 185 | 3. 9V DC motor 186 | 4. 9V Battery 187 | 188 | ### Raspberry Pi software requirements 189 | - Python 2.7.9 and above, installed with Raspbian Jessie (8.x). 190 | - [Watson IoT Python library](https://github.com/ibm-watson-iot/iot-python) 191 | - Optional: Git. 192 | If you do not have git installed on your Raspberry Pi, you can install it by using the following command: 193 | ```bash 194 | $ sudo apt-get install git 195 | ``` 196 | 197 | ### Detailed steps 198 | 199 | 1. Open the terminal or SSH to your Raspberry Pi. 200 | 2. Use your favorite git tool to clone the following repository to your Raspberry Pi: 201 | https://github.com/ibm-watson-iot/guide-conveyor-rasp-pi 202 | In Git Shell, use the following command: 203 | ```bash 204 | $ git clone https://github.com/ibm-watson-iot/guide-conveyor-rasp-pi 205 | ``` 206 | 3. Register the device with Watson IoT Platform. 207 | For more information about registering devices, see [Connecting devices](https://console.bluemix.net/docs/services/IoT/iotplatform_task.html#iotplatform_subtask1). 208 | * In the IBM console, click **Launch** other Watson IoT Platform service details page. The Watson IoT Platform web console opens in a new browser tab at the following URL: 209 | ``` 210 | https://ORG_ID.internetofthings.ibmcloud.com/dashboard/#/overview 211 | ``` 212 | Where `ORG_ID` is the unique six character ID of [your Watson IoT Platform organization](https://console.bluemix.net/docs/services/IoT/iotplatform_overview.html#organizations). 213 | * In the Overview dashboard, from the menu pane, select **Devices** and then click **Add Device**. 214 | * Create a device type for the device that you are adding. 215 | - Click **Create device type**. 216 | - Enter the device type name `iotp-for-conveyor` and a description for the device type. 217 | >**Note:** You can enter any device type name, but the other guides in the series expect devices that use the device type `iotp-for-conveyor`. If you use a different device type, you must modify the settings in the other guides accordingly. 218 | 219 | - Optional: Enter device type attributes and metadata. 220 | * Click **Next** to begin the process of adding your device with the selected device type. 221 | * Enter a device ID, for example, `conveyor_belt`. 222 | * Click **Next** to complete the process. 223 | * Provide an authentication token or accept an automatically generated token. 224 | * Verify the summary information is correct and then click **Add** to add the connection. 225 | * In the device information page, copy and save the following details: 226 | * Organization ID 227 | * Device Type 228 | * Device ID 229 | * Authentication Method 230 | * Authentication Token. You'll need the values for the Organization ID, Device Type, Device ID, and Authentication Token to configure your device to connect to Watson IoT Platform. 231 | 232 | 4. Navigate to the *guide-conveyor-rasp-pi* root of the cloned repository. 233 | 5. Make the shell script executable by using this command `sudo chmod +x setup.sh` 234 | 6. Run the *setup.sh* file and enter the details that you copied from the device information page when prompted. 235 | ```bash 236 | ./setup.sh 237 | ``` 238 | 239 | 7. Run the deviceClient program. 240 | When you run the program, your Raspberry Pi starts the motor and it will run for up to 2 minutes based on parameter settings. 241 | ```bash 242 | python deviceClient.py -t 2 243 | ``` 244 | 245 | While the motor is running, the program publishes events of event type `sensorData` that have the following sample payload structure: 246 | ``` 247 | { 248 | "d": { 249 | "elapsed": 1, 250 | "running": true, 251 | "temperature": 35.00, 252 | "ay": "0.00", 253 | "rpm": "0.6" 254 | } 255 | } 256 | ``` 257 | 258 | 8. Continue with [ Step 3 - See raw data in Watson IoT Platform](#step-3---see-raw-data-in-Watson-iot-platform). 259 | 260 | ## Step 3 - See raw data in Watson IoT Platform 261 | 262 | 1. Verify that the device is registered with Watson IoT Platform. 263 | * Login to your IBM dashboard at: [https://bluemix.net](https://bluemix.net/) 264 | * From [your list of services](https://bluemix.net/dashboard/services), click the *iotp-for-conveyor* Watson IoT Platform service. 265 | * Click *Launch* to open the Watson IoT Platform dashboard in a new browser tab. 266 | You can bookmark the URL for easy access later. 267 | Example: `https://*iot-org-id*.internetofthings.ibmcloud.com`. 268 | * From the menu, select **Devices** and verify that your new device is displayed. 269 | 270 | 2. View raw data 271 | * From the menu, select **Boards**. 272 | * Select the **Device Centric Analytics** board. 273 | * Locate the **Devices I Care About** card and select your device. 274 | The device name is displayed in the Device Properties card. 275 | 276 | 3. Send sensor data to the platform. 277 | The device sends data to Watson IoT Platform when sensor readings change. You can simulate this data sending by stopping, starting or changing the speed of the conveyor belt. 278 | >**Path A:** If you are accessing the app on a mobile browser, try shaking your smart phone to trigger accelerometer data for the conveyor belt. 279 | 280 | 4. Verify that the updated device data points that correspond to the published message are displayed in the Device Properties card. 281 | Message example A: 282 | ``` 283 | { 284 | "d": { 285 | "id": "belt1", 286 | "ts": 1494341288662, 287 | "ay": "0.00", 288 | "running": true, 289 | "rpm": "0.6" 290 | } 291 | } 292 | ``` 293 | Message example B: 294 | ``` 295 | { 296 | "d": { 297 | "elapsed": 1, 298 | "running": true, 299 | "temperature": 35.00, 300 | "ay": "0.00", 301 | "rpm": "0.6" 302 | } 303 | } 304 | ``` 305 | 306 | ## Step 4 - Visualize live data in Watson IoT Platform 307 | 308 | To create a dashboard card to see live conveyor belt data: 309 | 1. On the same Device Centric Analytics board, click **Add New Card** and then select **Line Chart**. 310 | 2. For card source data, click **Cards**. 311 | A list of card names is displayed. 312 | 3. Select **Devices I Care About** and then click **Next**. 313 | 4. Click **Connect new data set** and enter the following values for the data set parameters: 314 | - Event: sensorData 315 | - Property: d.rpm 316 | - Name: Belt RPM 317 | - Type: Float 318 | - Unit: rpm 319 | 5. Click **Next**. 320 | 6. On the card preview page, select **L**, and then click **Next**. 321 | 7. On the card information page, change the name of the title to `Belt data` and then click **Submit**. 322 | 8. Change the speed of your belt to see live data in your new card. 323 | 9. Optional: Add a second data set to add acceleration data for the belt. 324 | 325 | 326 | If you use your phone to connect to the sample app, you can shake the phone to send acceleration data for the belt. 327 | 1. Click the menu icon on your card and select to edit the card. 328 | 2. For card source data, select **Cards**. 329 | 3. Select **Devices I Care About** and click **Next**. 330 | 4. Click **Connect new data set** and enter the following values: 331 | - Event: sensorData 332 | - Property: d.ay 333 | - Name: Accel. Y 334 | - Type: Float 335 | - Unit: gs 336 | 5. Click **Next**. 337 | 6. Path A only: Shake your phone to see the live accelerometer data in your new card. 338 | For more information about creating boards and cards, see [Visualizing real-time data by using boards and cards](https://console.bluemix.net/docs/services/IoT/data_visualization.html#boards_and_cards). 339 | 340 | ## Useful links 341 | 342 | * [IBM Cloud](https://bluemix.net/) 343 | * [IBM Cloud Documentation](https://www.ng.bluemix.net/docs/) 344 | * [IBM Cloud Developers Community](http://developer.ibm.com/bluemix) 345 | * [IBM Watson Internet of Things](http://www.ibm.com/internet-of-things/) 346 | * [IBM Watson IoT Platform](http://www.ibm.com/internet-of-things/iot-solutions/watson-iot-platform/) 347 | * [IBM Watson IoT Platform Developers Community](https://developer.ibm.com/iotplatform/) 348 | 349 | 350 | ##

Learn more

351 | 361 | 362 | 363 | ## License 364 | This code pattern is licensed under the Apache Software License, Version 2. Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the [Developer Certificate of Origin, Version 1.1 (DCO)](https://developercertificate.org/) and the [Apache Software License, Version 2](http://www.apache.org/licenses/LICENSE-2.0.txt). 365 | 366 | [Apache Software License (ASL) FAQ](http://www.apache.org/foundation/license-faq.html#WhatDoesItMEAN) 367 | --------------------------------------------------------------------------------