Bluetooth Packet Encoding
This is the c code used to construct the packets on the Arduino:
1
/** ================================== */
2
/** BLUETOOTH DATA PACKETING FUNCTIONS */
3
/** ================================== */
4
/** The two functions in this section handle packeting the data and sending it over USART to the bluetooth module. The two functions are
5
* identically named so are called the in the same way, however the first is run if the value passed to it is a float and the second is
6
* run if the value passed into it is an integer (an override function). For all intents and purposes you can ignore this and simply call
7
* 'sendData(identifier, value);' using one of the defined identifiers and either a float or integer value to send informaion over BT.
8
*
9
* identifier: see definitions at start of code
10
* value: the value to send (typically some caluclated value from a sensor)
11
*/
12
13
void sendData(char identifier, float value)
14
{
15
if (!DEBUG_MODE) // Only runs if debug mode is LOW (0)
16
{
17
byte dataByte1;
18
byte dataByte2;
19
20
if (value == 0)
21
{
22
// It is impossible to send null bytes over Serial connection
23
// so instead we define zero as 0xFF or 11111111 i.e. 255
24
dataByte1 = 0xFF;
25
dataByte2 = 0xFF;
26
27
}
28
else if (value <= 127)
29
{
30
// Values under 128 are sent as a float
31
// i.e. value = dataByte1 + dataByte2 / 100
32
int integer;
33
int decimal;
34
float tempDecimal;
35
36
integer = (int) value;
37
tempDecimal = (value - (float) integer) * 100;
38
decimal = (int) tempDecimal;
39
40
dataByte1 = (byte) integer;
41
dataByte2 = (byte) decimal;
42
43
if (decimal == 0)
44
{
45
dataByte2 = 0xFF;
46
}
47
48
if (integer == 0)
49
{
50
dataByte1 = 0xFF;
51
}
52
53
}
54
else
55
{
56
// Values above 127 are sent as integer
57
// i.e. value = dataByte1 * 100 + dataByte2
58
int tens;
59
int hundreds;
60
61
hundreds = (int)(value / 100);
62
tens = value - hundreds * 100;
63
64
dataByte1 = (byte)hundreds;
65
//dataByte1 = dataByte1 || 0x10000000; //flag for integer send value
66
dataByte1 += 128;
67
dataByte2 = (byte) tens;
68
69
if (tens == 0)
70
{
71
dataByte2 = 0xFF;
72
}
73
74
if (hundreds == 0)
75
{
76
dataByte1 = 0xFF;
77
}
78
}
79
80
// Send the data in the format { [id] [1] [2] }
81
Serial.write(123);
82
Serial.write(identifier);
83
Serial.write(dataByte1);
84
Serial.write(dataByte2);
85
Serial.write(125);
86
87
}
88
89
}
90
91
/** override for integer values*/
92
void sendData(char identifier, int value)
93
{
94
if (!DEBUG_MODE)
95
{
96
byte dataByte1;
97
byte dataByte2;
98
99
if (value == 0)
100
{
101
dataByte1 = 0xFF;
102
dataByte2 = 0xFF;
103
104
}
105
else if (value <= 127)
106
{
107
108
dataByte1 = (byte)value;
109
dataByte2 = 0xFF; //we know there's no decimal component as an int was passed in
110
}
111
else
112
{
113
int tens;
114
int hundreds;
115
116
hundreds = (int)(value / 100);
117
tens = value - hundreds * 100;
118
119
dataByte1 = (byte)hundreds;
120
dataByte1 += 128; //sets MSB High to indicate Integer value
121
dataByte2 = (byte) tens;
122
123
if (tens == 0)
124
{
125
dataByte2 = 0xFF;
126
}
127
128
if (hundreds == 0)
129
{
130
dataByte1 = 0xFF;
131
}
132
}
133
134
Serial.write(123);
135
Serial.write(identifier);
136
Serial.write(dataByte1);
137
Serial.write(dataByte2);
138
Serial.write(125);
139
}
140
141
}
Copied!
If you adapt it to any other languages we would be keen to show the code here!
Last modified 3yr ago
Copy link