1/*jslint node:true, vars:true, bitwise:true, unparam:true */ 2/*jshint unused:true */ 3 4/* 5* Author: Zion Orent <zorent@ics.com> 6* Copyright (c) 2015 Intel Corporation. 7* 8* Permission is hereby granted, free of charge, to any person obtaining 9* a copy of this software and associated documentation files (the 10* "Software"), to deal in the Software without restriction, including 11* without limitation the rights to use, copy, modify, merge, publish, 12* distribute, sublicense, and/or sell copies of the Software, and to 13* permit persons to whom the Software is furnished to do so, subject to 14* the following conditions: 15* 16* The above copyright notice and this permission notice shall be 17* included in all copies or substantial portions of the Software. 18* 19* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26*/ 27 28 29/************** Variables **************/ 30// normal read/write mode 31var bufferLength = 256; 32 33var ble = require('jsupm_hm11'); 34 35 36/************** Functions **************/ 37function printUsage(progname) 38{ 39 var outputStr = "Usage: " + progname + " [AT command]\n\n" + 40 "If an argument is supplied on the command line, that argument is\n" + 41 "sent to the module and the response is printed out.\n\n" + 42 "If no argument is used, then the address and PIN of the module\n" + 43 "are queried and the results printed out.\n\n" 44 console.log(outputStr); 45} 46 47// simple helper function to send a command and wait for a response 48function sendCommand(bleObj, cmd, callback) 49{ 50 var bleBuffer = new ble.charArray(bufferLength); 51 bleObj.writeData(cmd, cmd.length); 52 53 // wait up to 1 second 54 if (bleObj.dataAvailable(1000)) 55 { 56 bleObj.readData(bleBuffer, bufferLength); 57 var bleData = ""; 58 // read only the number of characters 59 // specified by myGPSSensor.readData 60 for (var x = 0; x < bufferLength; x++) 61 { 62 if (bleBuffer.getitem(x) == '\0') 63 break; 64 else 65 bleData += bleBuffer.getitem(x); 66 } 67 console.log(bleData); 68 } 69 else 70 console.log("Timed out waiting for response"); 71 if (callback) 72 callback(); 73} 74 75/************** Main code **************/ 76// Instantiate a HM11 BLE Module on UART 0 77var my_ble_obj = new ble.HM11(0); 78 79// make sure port is initialized properly. 9600 baud is the default. 80if (!my_ble_obj.setupTty(ble.int_B9600)) 81{ 82 console.log("Failed to setup tty port parameters"); 83 process.exit(0); 84} 85 86printUsage(process.argv[1]); 87 88// Note: in nodeJS, command-line argument 0 is "node". 89// Command-line argument 1 is "hm11.js" 90// If you have a third argument, then it's a command for BLE 91if (process.argv.length > 2) 92{ 93 console.log("Sending command line argument (" + process.argv[2] + ")..."); 94 sendCommand(my_ble_obj, process.argv[2]); 95} 96else 97{ 98 // query the module address 99 var addr = "AT+ADDR?"; 100 console.log("Querying module address (" + addr + ")..."); 101 102 // sending this command as a synchronous callback ensures better timing 103 var callbackFunc = function() 104 { 105 setTimeout(function() 106 { 107 // query the module address 108 var pin = "AT+PASS?"; 109 console.log("Querying module PIN (" + pin + ")..."); 110 sendCommand(my_ble_obj, pin); 111 112 // Other potentially useful commands are: 113 // 114 // AT+VERS? - query module version 115 // AT+ROLE0 - set as slave 116 // AT+ROLE1 - set as master 117 // AT+CLEAR - clear all previous settings 118 // AT+RESET - restart the device 119 // 120 // A comprehensive list is available from the datasheet at: 121 // http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf 122 }, 1000); 123 }; 124 sendCommand(my_ble_obj, addr, callbackFunc); 125} 126 127 128/************** Exit code **************/ 129process.on('SIGINT', function() 130{ 131 my_ble_obj = null; 132 ble.cleanUp(); 133 ble = null; 134 console.log("Exiting..."); 135 process.exit(0); 136}); 137