-
Notifications
You must be signed in to change notification settings - Fork 8
/
IO_WSSFM10.cpp
297 lines (231 loc) · 5.23 KB
/
IO_WSSFM10.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/***************************************************************************
This is a library for the Wisol 10 module
Designed to work with all kinds of WSSFM10R Breakout Boards
This module use UART, 2 pins are required to interface.
Written by Adrien Chapelet for IoThings
***************************************************************************/
#include "IO_WSSFM10.h"
//IO_WSSFM10 WSSFM10;
//Message buffer
uint8_t msg[12];
/*IO_WSSFM10::IO_WSSFM10 () : Sigfox(), debug(1) {
//Sigfox(rxPin, txPin, 3);
}*/
/*IO_WSSFM10::IO_WSSFM10 (uint8_t rxPin, uint8_t txPin) : rx(rxPin), tx(txPin), debug(0) {
Sigfox(rxPin, txPin, 3);
}*/
IO_WSSFM10::IO_WSSFM10 (uint8_t rxPin=10 , uint8_t txPin=11, bool debugEn=true) : Sigfox(rxPin, txPin), debug(debugEn) {
//Sigfox(rxPin, txPin);
}
//IO_WSSFM10::IO_WSSFM10 (uint8_t rxPin, uint8_t txPin, bool debugEn) : Sigfox(rxPin, txPin, 3), debug(debugEn) {
//}
void IO_WSSFM10::blink(){
digitalWrite(LED_BUILTIN, 1); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(LED_BUILTIN, 0); // turn the LED off by making the voltage LOW
delay(50);
}
String IO_WSSFM10::getData(){
String data = "";
char output;
while (!Sigfox.available()){
blink();
}
while(Sigfox.available()){
output = Sigfox.read();
if ((output != 0x0A) && (output != 0x0D)){//0x0A Line feed | 0x0D Carriage return
data += output;
}
delay(10);
}
if(debug){
Serial.print("Data: ");
Serial.println(data);
}
return data;
}
String IO_WSSFM10::test(){
Sigfox.print("AT\r");
String status = getData();
if(debug){
Serial.print("Status: ");
Serial.print(status);
}
return status;
}
void IO_WSSFM10::begin(){
//pinMode(rxPin, INPUT);
//pinMode(txPin, OUTPUT);
Sigfox.begin(9600);
if (debug) {
Serial.begin(9600);
delay(100);
getID();
delay(100);
getPAC();
}
}
//Get Sigfox ID
String IO_WSSFM10::getID(){
Sigfox.print("AT$I=10\r");
String id = getData();
if(debug){
Serial.print("ID: ");
Serial.println(id);
}
return id;
}
//Get PAC number
String IO_WSSFM10::getPAC(){
Sigfox.print("AT$I=11\r");
String pac = getData();
if(debug){
Serial.print("PAC: ");
Serial.println(pac);
}
return pac;
}
uint16_t IO_WSSFM10::getTemp(void){
Sigfox.print("AT$T?\r");
uint16_t tempVal = getData().toInt();
if(debug){
Serial.println("Module temperature: ");
Serial.println(tempVal);
}
return tempVal;
}
String IO_WSSFM10::setPowerMode(uint8_t mode){
//0: Software reset
//1: sleep, send a break to wake up
//2: deep_sleep, toggle GPIO0 or Reset_N to wake up
Sigfox.print("AT$P=");
Sigfox.print(mode);
Sigfox.print("\r");
String res = getData();
if(debug){
Serial.println("Power Mode response: ");
Serial.println(res);
}
return res;
}
String IO_WSSFM10::setOutputPower(uint8_t power){
Sigfox.print("ATS302=");
Sigfox.print(power);
Sigfox.print("\r");
String res = getData();
if(debug){
Serial.println("Output Power response: ");
Serial.println(res);
}
return res;
}
void IO_WSSFM10::wakeUp(void){
Sigfox.print("\r");
}
//Send Sigfox Message
bool IO_WSSFM10::send(const void* data, uint8_t size){//const void* data
uint8_t* bytes = (uint8_t*)data;
Sigfox.print("AT$SF=");
char tmp[3] = {0}; // 2 hex characters + null terminator
if(debug){
Serial.print("Bytes:");
}
for(uint8_t i= 0; i<size; ++i){
sprintf(tmp, "%02X", bytes[i]);
Sigfox.print(tmp);
if(debug){
Serial.print(tmp);
}
}
if(debug){
Serial.println();
}
Sigfox.print("\r");
while (!Sigfox.available()){
blink();
}
String res = getData();
if(res.indexOf("OK") >= 0) {
Serial.println("Message successfully sent");
return true;
}
if(debug){
Serial.print("Status: ");
Serial.println(res);
}
return false;
}
bool IO_WSSFM10::sendString(String str) {
if(debug){
Serial.print("Message:");
Serial.println(str);
}
Sigfox.print("AT$SF=");
Sigfox.print(str);
Sigfox.print("\r");
while (!Sigfox.available()){
blink();
}
String res = getData();
if(res.indexOf("OK") >= 0) {
Serial.println("Message successfully sent");
return true;
}
if(debug){
Serial.print("Status: ");
Serial.println(res);
}
return false;
}
bool IO_WSSFM10::sendReceive(const void* data, uint8_t size, String response){
uint8_t* bytes = (uint8_t*)data;
Sigfox.print("AT$SF=");
char tmp[3] = {0}; // 2 hex characters + null terminator
if(debug){
Serial.print("Bytes:");
}
for(uint8_t i= 0; i<size; ++i){
sprintf(tmp, "%02X", bytes[i]);
Sigfox.print(tmp);
if(debug){
Serial.print(tmp);
}
}
if(debug){
Serial.println();
}
Sigfox.print(",1\r");
while (!Sigfox.available()){
blink();
}
String res = getData();
if(res.indexOf("OK") >= 0) {
Serial.println("Message successfully sent");
response = getData();
return true;
}
if(debug){
Serial.print("Status: ");
Serial.println(res);
}
return false;
}
bool IO_WSSFM10::sendReceiveString(String str, String response) {
Sigfox.print("AT$SF=");
Sigfox.print(str);
Sigfox.print(",1\r");
while (!Sigfox.available()){
blink();
}
String res = getData();
if(res.indexOf("OK") >= 0) {
Serial.println("Message successfully sent");
response = getData();
return true;
}
if(debug){
Serial.print("Status: ");
Serial.println(res);
}
return false;
}