-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinatore.h
More file actions
398 lines (340 loc) · 11.5 KB
/
coordinatore.h
File metadata and controls
398 lines (340 loc) · 11.5 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*-----------------------constants definition -------------------------------*/
#define ARRAY_LENGTH 50
#define COORDINATOR_ADDRESS_LWM 0
#define BUFFER_SIZE 100
#define del 100
/*-----------------------variables definition ------------------------------*/
String content; //contains the whole string: deviceAddr+property+value+...+property+value
char comunication_protocol; //which communications interface to use (and other various control commands)
String deviceAddr; //destination address
String property;
String value;
int numberkey=0; //number of pairs property:value
char sendThis[BUFFER_SIZE];
//declaration concerning LWM
static bool nwkDataReqBusy = false;
//declaration concerning XBEE
XBee xbee = XBee();
ZBRxResponse zbRx = ZBRxResponse(); //for the package in reception
ZBTxRequest zbTx = ZBTxRequest();
ZBTxStatusResponse txStatus = ZBTxStatusResponse(); // for the response of the xbee
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
//does the power of two integers:I need because the 'pow' function had trouble rounding
//(pow(10,3)=999,838...) if I make a cast to int truncates the decimal
uint32_t power(int base, int esp)
{
int i;
uint32_t result = base;
if(esp == 0)
{
return 1;
}
for(i=1; i<esp; i++)
{
result *= base;
}
return result;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
//convert a string made up of only numbers (representing a number in hexadecimal) in a data type uint32_t-----------
uint32_t stringHEX_To_uint32_t(String string)
{
uint32_t num = 0;
char buf[12];
int len = string.length();
string.toCharArray(buf, 12);
int i;
for(i=len-1; i>=0; i--)
{
if(buf[i]>='0' && buf[i]<='9') //if the character is a number(ascii value between 48('0') and 57('9') ) then subtract '0'
{
num += ( (buf[i] - '0')*power(16,len-i-1) );
}
if(buf[i]>='A' && buf[i]<='F') //if the character is an uppercase letter invalid (ASCII value between 65('A') and 70('F') - than subtract 'A'
{ //then I add 10 because A (in hex) is equal to 10 (alternatively I can subtract '7' = 'A'-10)
num += ( (buf[i] - 'A' + 10)*power(16,len-i-1) );
}
if(buf[i]>='a' && buf[i]<='f') //if the character is an lowercase letter invalid (ASCII value between 97('a') and 102('f') - than subtract 'a'
{ //then I add 10 because a (in hex) is equal to 10 (alternatively I can subtract 'W' = 'a'-10)
num += ( (buf[i] - 'a' + 10)*power(16,len-i-1) );
}
}
return num;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
//convert a string made up of only numbers (representing a number in decimal) in a data type uint32_t-----------
uint16_t stringDEC_To_uint16_t(String string) //da cambiare perche per l wm abbiamo valori decimali e non hex
{
uint16_t num = 0;
uint16_t numprec = 0; //need to check if the variable num overflows ( num > 65535 )
char buf[12];
int len = string.length();
string.toCharArray(buf, 12);
int i;
for(i=len-1; i>=0; i--)
{
//numprec=num; to control the overflow
if(buf[i]>='0' && buf[i]<='9') //if the character is a number (ascii value between 48('0') and 57('9') ) than subtract '0'
{
num += ( (buf[i] - '0')*power(10,len-i-1) );
}
return num;
/*if(num<numprec) //if num overflows starts from 0 so it will be less than its value at the previous cycle
{
return NULL;
}*/
}
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void divide_string(String stringToSplit) {
int strlen=stringToSplit.length();
//Serial1.println(stringToSplit); //debug
int i; //counter
deviceAddr="";
//Serial1.println(numberkey);
//-----------deviceAddr----------------
for(i=0; stringToSplit.charAt(i)!=':' && i<strlen ;i++)
{
deviceAddr += String(stringToSplit.charAt(i));
}
for(i++; stringToSplit.charAt(i)!=':' && i<strlen ;i++)
{
property+= String(stringToSplit.charAt(i));
}
for(i++; stringToSplit.charAt(i)!='-' && i<strlen ;i++)
{
value += String(stringToSplit.charAt(i));
}
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
char ReadFromWebServer()
{
//l16:onoff:1-rosso:255-verde:255-blu:255-funzioni:0-
//Serial1.println("readfrom");
int flagAddr=0;
char interface=NULL;
delayMicroseconds(del); // it is needed, otherwise every now and then you lose the first character to be read!!!
interface = Serial.read();
delayMicroseconds(del);
while (Serial.available())
{
char buf = Serial.read();
delayMicroseconds(del);
if(buf==':'){
flagAddr=1;
}
if(flagAddr==0)
{
deviceAddr+=buf;
}
content += buf;
}
//Serial.println(content);
delayMicroseconds(del);
//divide_string(content);
return interface;
}
/*-----------------------------------------------------LWM----------------------------------*/
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
//callback for the management of the confirmation (access the field message->opzioni) and verification of ack
static void appDataConf(NWK_DataReq_t *req)
{
//Serial.print("ACK: "); //debug
char x[100];
switch(req->status)
{
case NWK_SUCCESS_STATUS:
//Serial.print(1,DEC);
{
int message_size=req->size;
Serial.print("ok:");
for(int j=0; j<message_size; j++)
{
x[j] = req->data[j];
Serial.print(x[j]);
//delay(10);
//Serial.write(ind->data[i]);
}
Serial.println();
break;
}
case NWK_ERROR_STATUS:
//Serial.print(2,DEC);
break;
case NWK_OUT_OF_MEMORY_STATUS:
//Serial.print(3,DEC);
break;
case NWK_NO_ACK_STATUS:
//Serial.print(4,DEC);
break;
case NWK_NO_ROUTE_STATUS:
//Serial.print(5,DEC);
break;
case NWK_PHY_CHANNEL_ACCESS_FAILURE_STATUS:
//Serial.print(6,DEC);
break;
case NWK_PHY_NO_ACK_STATUS:
//Serial.print(7,DEC);
break;
// default:
// Serial1.print("no correspondence in ack");
// break;
}
nwkDataReqBusy = false;
//Serial.println("");
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static void LwmOutput_109(String devAddr,String toSend)
{
//Serial1.println("LwmOutput_62");//debug
int len = toSend.length(); //if i use toSend.toCharArray() the lwm packet do not get good
//Serial.print("Lunghezza:");
//Serial.println(len);
sendThis[len];
for(int g=0; g<len ;g++)
{
sendThis[g]=toSend.charAt(g);
// Serial.write(sendThis[g]);
}
//Serial.println("");
//int16_t address = stringDEC_To_uint16_t(devAddr);
nwkDataReqBusy = true;
NWK_DataReq_t *message = (NWK_DataReq_t*)malloc(sizeof(NWK_DataReq_t));
message->dstAddr = devAddr.toInt(); //object address
message->dstEndpoint = 1;
message->srcEndpoint = 1;
message->options = NWK_OPT_ACK_REQUEST; //request an ack
message->size = len;
message->data = (uint8_t*)(sendThis);
message->confirm = appDataConf; //callback for the management of the confirmation (option field)
//and verification of ack required above
NWK_DataReq(message); //send message
//delay(100);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
//to receive a packet with LWM
static bool LwmInput(NWK_DataInd_t *ind)
{
int message_size=ind->size;
char bufferL[62];
for(int i=0; i<message_size; i++)
{
//Buffer[i] = ind->data[i];
bufferL[i] = ind->data[i];
}
Serial.println(String(bufferL));
return true;
}
/***********************************************************************/
/*+++++++++++++++++++++++++++++++++++XBEE++++++++++++++++++++++++++++++*/
/***********************************************************************/
//-----------------------------------------function that sends data to xbee-----------------
bool XBeeOutput_62(String devAddr, String toSend)
{
//Serial.println(toSend);
char toSend_char[100]; //beacause the ZBTxRequest needs a uint8_t*
toSend.toCharArray(toSend_char, 100);
uint32_t indirizzo = stringHEX_To_uint32_t(devAddr);
XBeeAddress64 addr64 = XBeeAddress64(0x0013A200, indirizzo);
zbTx = ZBTxRequest(addr64, (uint8_t*)(toSend_char), strlen(toSend_char));
xbee.send(zbTx);
//
// // after sending a tx request, we expect a status response
// // wait up to half second for the status response
// if (xbee.readPacket(500))
// {
// Serial1.println("Got a tesponse!");
//
// // got a response!
//
// // should be a znet tx status
// if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE)
// {
// Serial1.println("ZB_TX_STATUS_RESPONSE");
//
// xbee.getResponse().getZBTxStatusResponse(txStatus);
// // get the delivery status, the fifth byte
// if (txStatus.getDeliveryStatus() == SUCCESS)
// {
// // success. time to celebrate
// //flashLed(statusLed, 5, 50);
// Serial1.println("SUCCESS");
// return true;
// }
// else
// {
// if(txStatus.getDeliveryStatus() == PAYLOAD_TOO_LARGE)
// {
// Serial1.println("PAYLOAD_TOO_LARGE");
// }
// // the remote XBee did not receive our packet. is it powered on?
// //flashLed(errorLed, 3, 500);
// Serial1.println("ERROR");
//
// return false;
//
// }
// }
// else if (xbee.getResponse().isError())
// {
// Serial1.print("Error reading packet. Error code: ");
// Serial1.println(xbee.getResponse().getErrorCode());
// return false;
// }
// else
// {
// Serial1.print("local XBee did not provide a timely TX Status Response");
//
// // local XBee did not provide a timely TX Status Response -- should not happen
// //flashLed(errorLed, 2, 50);
// return false;
// }
// }
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
bool XBeeInput()
{
// 1. This will read any data that is available:
xbee.readPacket();
// 2. Now, to check if a packet was received:
if (xbee.getResponse().isAvailable()) //se è vero ho ricevuto qualcosa
{
char bufferX[62];
String receivedX;
if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) //if it's true i have a zb-rx packet
{
xbee.getResponse().getZBRxResponse(zbRx); //fill zb-rx class
/*flashLed(led, zbRx.getDataLength(), 500);*/
for (int i = 0; i < zbRx.getDataLength(); i++)
{
/*buffer[i] = char*/
bufferX[i]=zbRx.getData()[i];
receivedX+=bufferX[i];
}
Serial.println(receivedX);
receivedX="";
for (int i = 0; i < zbRx.getDataLength(); i++)
{
/*buffer[i] = char*/
bufferX[i]=NULL;
}
return true;
}
}
}
void ApioCoordinatorSetup()
{
Serial.begin(115200); //for comucicate with the web-server
//setup Xbee
Serial1.begin(9600);//for comunicate with xbee
xbee.setSerial(Serial1);
SYS_Init();//in questa funzione (di libreria LW_Mesh, file sys.c) è stata eliminata sys_Uninit_Arduino() altrimenti non funzionava bene la seriale
NWK_SetAddr(COORDINATOR_ADDRESS_LWM);
NWK_SetPanId(0x01);
PHY_SetChannel(0x1a);
//PHY_SetTxPower(0);
TRX_CTRL_2_REG_s.oqpskDataRate = 2;
PHY_SetRxState(true);
NWK_OpenEndpoint(1, LwmInput);
Serial.println("COORDINATOR ACTIVE");
}