Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hammadtq committed Aug 12, 2016
1 parent 2b719dd commit 9c77f74
Show file tree
Hide file tree
Showing 34 changed files with 5,434 additions and 0 deletions.
3 changes: 3 additions & 0 deletions hm10-arduino-ble/README.md
@@ -0,0 +1,3 @@
# Control an LED using HM-10 BLE module, Arduino and a Mobile App

This example app code is written to be used with Evothings Studio. Using this example app code one can easily control an LED with the help of HM-10 BLE module and Arduino.
Binary file added hm10-arduino-ble/app-icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions hm10-arduino-ble/app/evothings.json
@@ -0,0 +1,3 @@
{
"uuid": "23387664-eeaa-4705-9c20-c79ae83304e5"
}
118 changes: 118 additions & 0 deletions hm10-arduino-ble/app/index.html
@@ -0,0 +1,118 @@
<!DOCTYPE html>
<html>
<!--
This is an app that demonstrates how to control an Arduio board
using BLE (Bluetooth Low Energy).
Please note that you must use a HM-10 compatible module for this example to work.
-->
<head>

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no,
shrink-to-fit=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />

<title>HM-10 and Arduino LED On/Off BLE</title>

<style>
@import 'ui/css/evothings-app.css';
</style>

<script>
// Redirect console.log to Evothings Workbench.
if (window.hyper && window.hyper.log) { console.log = hyper.log }
window.onerror = function(msg, url, line)
{
console.log(msg + ": " + url + ":" + line);
};
</script>

<script src="cordova.js"></script>
<script src="libs/jquery/jquery.js"></script>
<script src="libs/evothings/evothings.js"></script>
<script src="libs/evothings/ui/ui.js"></script>
<script src="libs/evothings/arduinoble/arduinoble.js"></script>

</head>

<body ontouchstart=""><!-- ontouchstart="" enables low-delay CSS transitions. -->

<header>
<button class="back" onclick="history.back()">
<img src="ui/images/arrow-left.svg" />
</button>

<img class="logotype" src="ui/images/logo.svg" alt="Evothings" />

<!--<button class="menu" onclick=""><img src="ui/images/menu.svg" /></button>-->
</header>

<h1>HM-10 and Arduino LED On/Off BLE</h1>

<p id="info">Initializing...</p>

<button class="yellow wide" onclick="app.connect()">CONNECT</button>

<br />

<button class="green wide big" onclick="app.ledOn()">LED ON</button>

<br />

<button class="red wide big" onclick="app.ledOff()">LED OFF</button>

<script>
// Application object.
var app = {}

// Connected device.
app.device = null;

// Turn on LED.
app.ledOn = function()
{
app.device && app.device.writeDataArray(new Uint8Array([1]), '0000ffe1-0000-1000-8000-00805f9b34fb');
}

// Turn off LED.
app.ledOff = function()
{
app.device && app.device.writeDataArray(new Uint8Array([0]), '0000ffe1-0000-1000-8000-00805f9b34fb');
}

app.showMessage = function(info)
{
document.getElementById('info').innerHTML = info
};

// Called when BLE and other native functions are available.
app.onDeviceReady = function()
{
app.showMessage('Touch the connect button to begin.');
};

app.connect = function()
{
evothings.arduinoble.close();

evothings.arduinoble.connect(
'BT05', // Name of the module.
function(device)
{
app.device = device;
app.showMessage('Connected! Touch buttons to turn LED on/off.');
},
function(errorCode)
{
app.showMessage('Connect error: ' + errorCode + '.');
});
};

document.addEventListener(
'deviceready',
function() { evothings.scriptsLoaded(app.onDeviceReady) },
false);
</script>

</body>

</html>
1 change: 1 addition & 0 deletions hm10-arduino-ble/app/libs/evothings/VERSION
@@ -0,0 +1 @@
Evothings Libraries version 2.1.0
153 changes: 153 additions & 0 deletions hm10-arduino-ble/app/libs/evothings/arduinoble/arduinoble.js
@@ -0,0 +1,153 @@
// File: arduinoble.js

// Load library EasyBLE.
evothings.loadScript('libs/evothings/easyble/easyble.js');

/**
* @namespace
* @author Mikael Kindborg
* @description <p>Functions for communicating with an Arduino BLE shield.</p>
* <p>It is safe practise to call function {@link evothings.scriptsLoaded}
* to ensure dependent libraries are loaded before calling functions
* in this library.</p>
*
* @todo This is a very simple library that has only write capability,
* read and notification functions should be added.
*
* @todo Add function to set the write characteristic UUID to make
* the code more generic.
*/
evothings.arduinoble = {};

;(function()
{
// Internal functions.
var internal = {};

/**
* Stop any ongoing scan and disconnect all devices.
* @public
*/
evothings.arduinoble.close = function()
{
evothings.easyble.stopScan();
evothings.easyble.closeConnectedDevices();
};

/**
* Called when you've connected to an Arduino BLE shield.
* @callback evothings.arduinoble.connectsuccess
* @param {evothings.arduinoble.ArduinoBLEDevice} device -
* The connected BLE shield.
*/

/**
* Connect to a BLE-shield.
* @param deviceName BLE name if the shield.
* @param {evothings.arduinoble.connectsuccess} success -
* Success callback: success(device)
* @param {function} fail - Error callback: fail(errorCode)
* @example
* evothings.arduinoble.connect(
* 'arduinoble', // Name of BLE shield.
* function(device)
* {
* console.log('connected!');
* device.writeDataArray(new Uint8Array([1]));
* evothings.arduinoble.close();
* },
* function(errorCode)
* {
* console.log('Error: ' + errorCode);
* });
* @public
*/
evothings.arduinoble.connect = function(deviceName, success, fail)
{
evothings.easyble.startScan(
function(device)
{
if (device.name == deviceName)
{
evothings.easyble.stopScan();
internal.connectToDevice(device, success, fail);
}
},
function(errorCode)
{
fail(errorCode);
});
};

/**
* Connect to the BLE shield.
* @private
*/
internal.connectToDevice = function(device, success, fail)
{
device.connect(
function(device)
{
// Get services info.
internal.getServices(device, success, fail);
},
function(errorCode)
{
fail(errorCode);
});
};

/**
* Read all services from the device.
* @private
*/
internal.getServices = function(device, success, fail)
{
device.readServices(
null, // null means read info for all services
function(device)
{
internal.addMethodsToDeviceObject(device);
success(device);
},
function(errorCode)
{
fail(errorCode);
});
};

/**
* Add instance methods to the device object.
* @private
*/
internal.addMethodsToDeviceObject = function(device)
{
/**
* Object that holds info about an Arduino BLE shield.
* @namespace evothings.arduinoble.ArduinoBLEDevice
*/

/**
* @function writeDataArray
* @description Write data to an Arduino BLE shield.
* @param {Uint8Array} uint8array - The data to be written.
* @memberof evothings.arduinoble.ArduinoBLEDevice
* @instance
* @public
*/
device.writeDataArray = function(uint8array, uuid)
{
device.writeCharacteristic(
uuid,
uint8array,
function()
{
console.log('writeCharacteristic success');
},
function(errorCode)
{
console.log('writeCharacteristic error: ' + errorCode);
});
};
};
})();

0 comments on commit 9c77f74

Please sign in to comment.