Added example sketch for HM-10 module
This commit is contained in:
parent
5a010bfb1b
commit
1cfde0e232
9 changed files with 11717 additions and 0 deletions
59
BluetoothGUI/BluetoothGUI.js
Normal file
59
BluetoothGUI/BluetoothGUI.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* Bluetooth Test - Processing Side (In Chrome)
|
||||
* Arduino to HM10 module to Google Chrome
|
||||
* https://www.amazon.com/gp/product/B06WGZB2N4/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1
|
||||
*
|
||||
* p5.ble.js
|
||||
https://yining1023.github.io/p5ble-website/
|
||||
|
||||
* kevin darrah
|
||||
*
|
||||
* Twitter: https://twitter.com/KDcircuits
|
||||
* For inquiries or design services:
|
||||
* https://www.kdcircuits.com
|
||||
*
|
||||
* License? Do whatever you want with this code - it's just a sample
|
||||
*/
|
||||
|
||||
//globals
|
||||
let blueToothCharacteristic;//this is a blu
|
||||
let receivedValue = "";
|
||||
|
||||
let blueTooth;
|
||||
let isConnected = false;
|
||||
|
||||
|
||||
var millisecondTimerStart;
|
||||
var oldColorPickerValue;
|
||||
|
||||
|
||||
function setup() {
|
||||
|
||||
createCanvas(windowWidth, windowHeight);
|
||||
|
||||
|
||||
// Create a p5ble class
|
||||
console.log("setting up");
|
||||
blueTooth = new p5ble();
|
||||
|
||||
const connectButton = createButton('Connect');
|
||||
connectButton.mousePressed(connectToBle);
|
||||
connectButton.position(15, 15);
|
||||
|
||||
const LEDonButton = createButton('LED ON');
|
||||
LEDonButton.mousePressed(LEDon);
|
||||
LEDonButton.position(15, 60);
|
||||
|
||||
const LEDoffButton = createButton('LED OFF');
|
||||
LEDoffButton.mousePressed(LEDoff);
|
||||
LEDoffButton.position(LEDonButton.x+LEDonButton.width+10, 60);
|
||||
|
||||
ledColorPicker = createColorPicker('#ff0000');
|
||||
ledColorPicker.position(LEDoffButton.x+LEDoffButton.width+10, 60);
|
||||
millisecondTimerStart = millis();
|
||||
}
|
||||
|
||||
|
||||
function draw() {
|
||||
drawScreen();
|
||||
}
|
7
BluetoothGUI/README.md
Normal file
7
BluetoothGUI/README.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
# Another example
|
||||
|
||||
Sketch with HM-10 Bluetooth Module
|
||||
|
||||
Resources:
|
||||
- [Video](https://youtu.be/w_mRj5IlVpg) on Youtube
|
||||
- Download [Link](https://www.kevindarrah.com/download/arduino_code/BluetoothGUI.zip) to this source files
|
48
BluetoothGUI/bluetooth.js
Normal file
48
BluetoothGUI/bluetooth.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
function connectToBle() {
|
||||
// Connect to a device by passing the service UUID
|
||||
blueTooth.connect(0xFFE0, gotCharacteristics);
|
||||
}
|
||||
|
||||
|
||||
// A function that will be called once got characteristics
|
||||
function gotCharacteristics(error, characteristics) {
|
||||
if (error) {
|
||||
console.log('error: ', error);
|
||||
}
|
||||
console.log('characteristics: ', characteristics);
|
||||
blueToothCharacteristic = characteristics[0];
|
||||
|
||||
blueTooth.startNotifications(blueToothCharacteristic, gotValue, 'string');
|
||||
|
||||
|
||||
isConnected = blueTooth.isConnected();
|
||||
// Add a event handler when the device is disconnected
|
||||
blueTooth.onDisconnected(onDisconnected);
|
||||
}
|
||||
|
||||
|
||||
// A function that will be called once got values
|
||||
function gotValue(value) {
|
||||
console.log('value: ', value);
|
||||
if (value == 'Push Button') {
|
||||
receivedValue = "Push Button Pressed";
|
||||
} else {
|
||||
receivedValue = " ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function onDisconnected() {
|
||||
console.log('Device got disconnected.');
|
||||
isConnected = false;
|
||||
}
|
||||
|
||||
|
||||
function sendData(command) {
|
||||
const inputValue = command;
|
||||
if (!("TextEncoder" in window)) {
|
||||
console.log("Sorry, this browser does not support TextEncoder...");
|
||||
}
|
||||
var enc = new TextEncoder(); // always utf-8
|
||||
blueToothCharacteristic.writeValue(enc.encode(inputValue));
|
||||
}
|
6
BluetoothGUI/commands.js
Normal file
6
BluetoothGUI/commands.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
function LEDon() {
|
||||
sendData("LED Color" + ledColorPicker.value()+ "\n");
|
||||
}
|
||||
function LEDoff() {
|
||||
sendData("LED OFF\n");
|
||||
}
|
20
BluetoothGUI/drawScreen.js
Normal file
20
BluetoothGUI/drawScreen.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
function drawScreen() {
|
||||
textSize(18);
|
||||
if (isConnected) {
|
||||
background(0, 255, 0);
|
||||
text('Connected :)', 80, 15);
|
||||
} else {
|
||||
background(255, 0, 0);
|
||||
textAlign(LEFT, TOP);
|
||||
text('Disconnected :/', 80, 15);
|
||||
}
|
||||
|
||||
text(receivedValue, 15, 40);
|
||||
|
||||
if(oldColorPickerValue != ledColorPicker.value() && millis()-millisecondTimerStart>50 && isConnected){
|
||||
oldColorPickerValue = ledColorPicker.value();
|
||||
sendData("LED Color" + ledColorPicker.value()+ "\n");
|
||||
millisecondTimerStart = millis();
|
||||
}
|
||||
|
||||
}
|
23
BluetoothGUI/index.html
Normal file
23
BluetoothGUI/index.html
Normal file
|
@ -0,0 +1,23 @@
|
|||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
|
||||
<!-- PLEASE NO CHANGES BELOW THIS LINE (UNTIL I SAY SO) -->
|
||||
<script language="javascript" type="text/javascript" src="libraries/p5.min.js"></script>
|
||||
<script language="javascript" type="text/javascript" src="libraries/p5.ble.js"></script>
|
||||
<script language="javascript" type="text/javascript" src="bluetooth.js"></script>
|
||||
<script language="javascript" type="text/javascript" src="commands.js"></script>
|
||||
<script language="javascript" type="text/javascript" src="drawScreen.js"></script>
|
||||
<script language="javascript" type="text/javascript" src="BluetoothGUI.js"></script>
|
||||
<!-- OK, YOU CAN MAKE CHANGES BELOW THIS LINE AGAIN -->
|
||||
<script src="p5.ble.min.js" type="text/javascript"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.min.js"></script>
|
||||
<!-- This line removes any default padding and style.
|
||||
You might only need one of these values set. -->
|
||||
<style> body { padding: 0; margin: 0; } </style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
11549
BluetoothGUI/libraries/p5.ble.js
Normal file
11549
BluetoothGUI/libraries/p5.ble.js
Normal file
File diff suppressed because it is too large
Load diff
3
BluetoothGUI/libraries/p5.min.js
vendored
Normal file
3
BluetoothGUI/libraries/p5.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
2
BluetoothGUI/sketch.properties
Normal file
2
BluetoothGUI/sketch.properties
Normal file
|
@ -0,0 +1,2 @@
|
|||
mode=p5.js
|
||||
mode.id=processing.mode.p5js.p5jsMode
|
Loading…
Add table
Add a link
Reference in a new issue