-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced_bluetooth.ino
109 lines (95 loc) · 2.86 KB
/
advanced_bluetooth.ino
1
2
3
4
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/********************************************************
* #######################################################
*
* Bluetooth Serial - bt-serial.h
*
* #######################################################
********************************************************/
// Included Libraries:..
#include "bt-serial.h"
/**
* use TOSTRING() function to ensure string value for
* sending to the bluetooth client.
*/
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
/**
* Various preprocessor definitions
* Used as example data for sending to client
*/
#define FIRMWARE_VERSION beta
/**
* setup();
* Default Arduino setup function
*/
void setup() {
// Setup builtin led for toggling
pinMode(LED_BUILTIN, OUTPUT);
// setup BTSerial module
BTSerial::begin(9600,true);
}
/**
* loop();
* Default Arduino loop function
*/
void loop() {
// Call update to update bluetooth serial
// update will return true if a command is
// ready to be used. This saves having to Call
// BTSerial::hasCommand(); after BTSerial::update();
if (BTSerial::update()) {
// process the available command
processBTSCommand();
// Flush the serial buffer
BTSerial::flush();
}
}
/**
* processBTSCommand();
* Processes data stored in the most recent bt serial command
*/
void processBTSCommand() {
// sorts the command data from the buffer string
// this is required before reading the commands
BTSCommand comm = BTSerial::getCommand();
// If command matches disconnection then
// return as its not a valid command
if (strcmp(comm.cn, "+DISC") == 0) {
Serial.println(""); return;
}
// print current command data to serial monitor:..
Serial.print(comm.cn); Serial.print(" : "); Serial.println(comm.cv);
// begin checking commands:..
if (strcmp(comm.cn, "system") == 0) {
sendStringPairToDevice("version", TOSTRING(FIRMWARE_VERSION));
} else if (strcmp(comm.cn, "on") == 0) {
digitalWrite(LED_BUILTIN, HIGH);
sendStringPairToDevice("led", "on");
} else if (strcmp(comm.cn, "off") == 0) {
digitalWrite(LED_BUILTIN, LOW);
sendStringPairToDevice("led", "off");
} else if (strcmp(comm.cn, "value") == 0) {
sendStringPairToDevice("value", comm.cv);
// convert value to integer:..
//int value = atoi(comm.cv);
}
}
/**
* sendStringPairToDevice(k,v);
* Sends k and v strings to device formatted as below
* k:v;
*/
void sendStringPairToDevice(char* k, char* v) {
BTSerial::print(k);
BTSerial::print(":");
BTSerial::print(v);
BTSerial::println(";");
}
/********************************************************
* #######################################################
*
* End of Code
*
* #######################################################
********************************************************/