-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol-test.ino
101 lines (85 loc) · 2.09 KB
/
protocol-test.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
#include <SoftwareSerial.h>
#define BT_RXD 8
#define BT_TXD 7
SoftwareSerial bluetooth(BT_RXD, BT_TXD);
void setup(){
Serial.begin(9600);
bluetooth.begin(9600);
}
byte id = 0x04;
byte b = 0x00;
byte temp = 0x16;
byte hum = 0x32;
void loop(){
for (int i=0; i<10; i++) {
byte message[6];
while(!bluetooth.available());
int incomingByte = bluetooth.read();
// Set ID
if (i == 1 && incomingByte == 0x49) {
message[0] = 0x02;
message[1] = 0x04;
b = 0x49;
}
if (i == 2 && b == 0x49) {
id = incomingByte;
message[2] = id;
message[3] = 0x00;
message[4] = (message[1] + message[2] + message[3]) % 100;
message[5] = 0x03;
}
// write ID on protocol
if (i == 1) {
message[0] = 0x02;
message[1] = id;
b = incomingByte;
}
// Route by CMD
if (i == 2) {
if (b == 0x53 || b == 0x54) {
if (b == 0x53) {
temp = incomingByte;
}
message[2] = incomingByte;
}
if (b == 0x44) {
message[2] = temp;
message[3] = hum;
message[4] = (message[1] + message[2] + message[3]) % 100;
message[5] = 0x03;
}
if (b == 0x51) {
message[2] = 0x01;
message[3] = 0x00;
message[4] = (message[1] + message[2] + message[3]) % 100;
message[5] = 0x03;
}
if (b == 0x4D) {
message[2] = 0x01;
message[3] = 0x01;
message[4] = (message[1] + message[2] + message[3]) % 100;
message[5] = 0x03; }
}
if (i == 3) {
if (b == 0x53 || b == 0x54) {
if (b = 0x53) {
hum = incomingByte;
}
message[3] = incomingByte;
message[4] = (message[1] + message[2] + message[3]) % 100;
message[5] = 0x03;
}
}
Serial.print(incomingByte, HEX);
Serial.print(' ');
if (incomingByte == 0x03) {
Serial.print('\n\nSend message: ');
for (i = 0; i < 6; i++) {
Serial.print(message[i] + ' ');
}
Serial.println();
bluetooth.write(message, sizeof(message));
break;
}
}
}