-
Notifications
You must be signed in to change notification settings - Fork 0
/
HC12.cpp
215 lines (185 loc) · 3.94 KB
/
HC12.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "HC12.hpp"
unsigned long AT_DELAY = 100;
/**
* @brief Construct a new HC12 object.
*
* @param RX_Pin RXD pin on HC12 module.
* @param TX_Pin TXD pin on HC12 module.
*/
HC12::HC12(uint8_t RX_Pin, uint8_t TX_Pin): _serial(SoftwareSerial(RX_Pin, TX_Pin))
{
_SET_PIN = 0;
_MODE = FU3;
}
/**
* @brief Construct a new HC12 object.
*
* @param RX_Pin RXD pin on HC12 module.
* @param TX_Pin TXD pin on HC12 module.
* @param SET_Pin SET pin on HC12 module.
*/
HC12::HC12(uint8_t RX_Pin, uint8_t TX_Pin, uint8_t SET_Pin): _serial(SoftwareSerial(RX_Pin, TX_Pin))
{
_SET_PIN = SET_Pin;
_MODE = FU3;
}
/**
* @brief Initialize the communication with the HC12 module.
*/
void HC12::begin(int baudrate)
{
_serial.begin(baudrate);
if (_SET_PIN > 0)
{
digitalWrite(_SET_PIN, HIGH);
}
delay(AT_DELAY);
}
/**
* @brief Sleep mode.
*
*/
void HC12::sleep(void)
{
if (_SET_PIN == 0) return;
digitalWrite(_SET_PIN, LOW);
delay(AT_DELAY);
// Send command
_serial.print("AT+SLEEP");
delay(AT_DELAY);
// Read response
while(_serial.available() > 0)
{
char c = _serial.read();
}
digitalWrite(_SET_PIN, HIGH);
delay(AT_DELAY);
}
/**
* @brief Wake Up.
*
*/
void HC12::wakeup(void)
{
if (_SET_PIN == 0) return;
digitalWrite(_SET_PIN, LOW);
delay(AT_DELAY);
// Send command
_serial.print("AT");
delay(AT_DELAY);
// Read response
while(_serial.available() > 0)
{
char c = _serial.read();
}
digitalWrite(_SET_PIN, HIGH);
delay(AT_DELAY);
}
/**
* @brief Sent AT command to HC-12 module.
*
* @param command Command
*/
void HC12::send_AT(String command)
{
if (_SET_PIN == 0) return;
digitalWrite(_SET_PIN, LOW);
delay(AT_DELAY);
// Send command
_serial.print(command);
delay(AT_DELAY);
// Read response
while(_serial.available() > 0) {
char c = _serial.read();
}
digitalWrite(_SET_PIN, HIGH);
delay(AT_DELAY);
}
/**
* @brief Change serial port transparent transmission mode.
*
* @param mode HC12 mode.
*/
void HC12::set_mode(Mode mode)
{
HC12::send_AT("AT+FU" + mode);
}
/**
* @brief Set transmitting power of module.
*
* @param power Power value from 1 to 8.
*/
void HC12::set_power(short power)
{
HC12::send_AT("AT+P" + String(power));
}
/**
* @brief Set wireless communication channel.
*
* @param channel Channel number from 001 to 127.
*/
void HC12::set_channel(String channel)
{
HC12::send_AT("AT+C" + channel);
}
/**
* @brief Set serial port baud rate.
*
* @param baudrate Serial baudrate.
* The baud rate can be set to be 1,200bps, 2,400bps, 4,800bps,
* 9,600bps, 19,200bps, 38,400bps, 57,600bps, and 115,200bps.
*/
void HC12::set_baudrate(unsigned int baudrate)
{
HC12::send_AT("AT+B" + String(baudrate));
}
/**
* @brief Send message.
*
* @param message Message to be sent.
*/
void HC12::send(String message)
{
_serial.print(message);
// Wait for sending.
unsigned long send_delay = message.length();
switch (_MODE)
{
case FU1: // Transmision data delay is 15-25 ms/byte.
send_delay *= 25;
break;
case FU2: // Transmision data delay is 500 ms/byte.
send_delay *= 500;
break;
case FU3: // Transmision data delay is 4-80 ms/byte.
send_delay *= 80;
break;
}
delay(send_delay);
}
/**
* @brief Receive message.
*
* @param timeout Timeout. Min allowed value: 1000 ms.
* @param end End line character.
* @return Message.
*/
String HC12::receive(unsigned int timeout, char end)
{
if (timeout < 1000) timeout = 1000;
while(!_serial.available() && timeout > 0) {
delay(50);
timeout -= 50;
}
if (!timeout) {
return "";
}
String msg;
char byte;
_serial.setTimeout(1000);
while(_serial.available() && byte != end) {
byte = char(_serial.read());
msg += byte;
}
return msg;
}