PageRenderTime 87ms CodeModel.GetById 3ms app.highlight 76ms RepoModel.GetById 2ms app.codeStats 0ms

/WaspXBeeXSC.cpp

https://github.com/mencey/waspmote-api
C++ | 1789 lines | 1177 code | 132 blank | 480 comment | 34 complexity | e2351ccf89e15684af10cec2d43bba28 MD5 | raw file
   1/*
   2 *  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
   3 *  http://www.libelium.com
   4 *
   5 *  This program is free software: you can redistribute it and/or modify
   6 *  it under the terms of the GNU Lesser General Public License as published by
   7 *  the Free Software Foundation, either version 2.1 of the License, or
   8 *  (at your option) any later version.
   9   
  10 *  This program is distributed in the hope that it will be useful,
  11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 *  GNU Lesser General Public License for more details.
  14  
  15 *  You should have received a copy of the GNU Lesser General Public License
  16 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17 *
  18 *  Version:		0.11
  19 *  Design:		David Gascè´¸n
  20 *  Implementation:	Alberto Bielsa
  21 */
  22 
  23
  24#ifndef __WPROGRAM_H__
  25	#include "WaspClasses.h"
  26#endif
  27
  28void	WaspXBeeXSC::init(uint8_t protocol_used, uint8_t frequency, uint8_t model_used)
  29{
  30	protocol=protocol_used;
  31	freq=frequency;
  32	model=model_used;
  33}
  34
  35/*
  36 Function: Set the module into Command Mode
  37 Returns: Returns: Integer that determines if there has been any error 
  38   error=2 --> The command has not been executed
  39   error=1 --> There has been an error while executing the command
  40   error=0 --> The command has been executed with no errors
  41*/
  42uint8_t WaspXBeeXSC::setCommandMode()
  43{
  44  uint8_t error=0;
  45
  46	delay(1500);
  47  	XBee.print("+++");
  48  	delay(1000);
  49	clean();
  50	error=0;
  51	return error;
  52}
  53
  54/*
  55 Function: Exits the module from Command Mode
  56 Returns: Returns: Integer that determines if there has been any error 
  57   error=2 --> The command has not been executed
  58   error=1 --> There has been an error while executing the command
  59   error=0 --> The command has been executed with no errors
  60*/
  61uint8_t WaspXBeeXSC::exitCommandMode()
  62{
  63  	clean();
  64  	XBee.println("atcn");
  65  	delay(1000);
  66  	return check();
  67}
  68
  69/*
  70 Function: Set the Module Vendor ID
  71 Returns: Returns: Integer that determines if there has been any error 
  72   error=2 --> The command has not been executed
  73   error=1 --> There has been an error while executing the command
  74   error=0 --> The command has been executed with no errors
  75 Values: Stores in global "vendorID" variable the module Vendor ID
  76*/
  77uint8_t WaspXBeeXSC::setVendorID(uint8_t VID_H, uint8_t VID_L)
  78{
  79  clean();
  80  XBee.print("atid");
  81  XBee.print(VID_H,HEX);
  82  XBee.println(VID_L,HEX);
  83  delay(1000);
  84  if(!check())
  85  {
  86	vendorID[0]=VID_H;
  87	vendorID[1]=VID_L;
  88	return 0;
  89  }
  90  return 1;
  91}
  92
  93/*
  94 Function: Get the Module Vendor ID
  95 Returns: Returns: Integer that determines if there has been any error 
  96   error=2 --> The command has not been executed
  97   error=1 --> There has been an error while executing the command
  98   error=0 --> The command has been executed with no errors
  99 Values: Stores in global "vendorID" variable the module Vendor ID
 100*/
 101uint8_t WaspXBeeXSC::getVendorID()
 102{
 103  uint8_t ByteIN[10];
 104  uint8_t error=2;
 105  uint8_t i=0;
 106
 107  clean();
 108  XBee.println("atid");
 109  delay(1000);
 110  while(XBee.available() > 0) 
 111  {
 112	ByteIN[i]=XBee.read();
 113	i++;
 114	error=0;
 115  }
 116  vendorID[0]=converter(ByteIN[0],ByteIN[1]);
 117  vendorID[1]=converter(ByteIN[2],ByteIN[3]);
 118  return error;
 119}
 120
 121
 122/*
 123 Function: Set the Baud Rate used in the module
 124 Returns: Returns: Integer that determines if there has been any error 
 125   error=2 --> The command has not been executed
 126   error=1 --> There has been an error while executing the command
 127   error=0 --> The command has been executed with no errors
 128 Values: Stores in global "baudRate" variable the Baud Rate used in the module
 129*/
 130uint8_t WaspXBeeXSC::setBaudRate(uint8_t brate)
 131{
 132  clean();
 133  XBee.print("atbd");
 134  XBee.println(brate,DEC);
 135  delay(1000);
 136  if(!check())
 137  {
 138	baudRate=brate;
 139	return 0;
 140  }
 141  return 1;
 142}
 143
 144
 145/*
 146 Function: Get the Baud Rate used in the module
 147 Returns: Returns: Integer that determines if there has been any error 
 148   error=2 --> The command has not been executed
 149   error=1 --> There has been an error while executing the command
 150   error=0 --> The command has been executed with no errors
 151 Values: Stores in global "baudRate" variable the Baud Rate used in the module
 152*/
 153uint8_t WaspXBeeXSC::getBaudRate()
 154{
 155  uint8_t ByteIN[10];
 156  uint8_t error=1;
 157  uint8_t i=0;
 158
 159  clean();
 160  XBee.println("atbd");
 161  delay(1000);
 162  while(XBee.available() > 0) 
 163  {
 164	ByteIN[i]=XBee.read();
 165	i++;
 166	error=0;
 167  }
 168  baudRate=converter(ByteIN[0]);
 169  return error;
 170}
 171
 172/*
 173 Function: Get the dBm level in the most recent packet
 174 Returns: Returns: Integer that determines if there has been any error 
 175   error=2 --> The command has not been executed
 176   error=1 --> There has been an error while executing the command
 177   error=0 --> The command has been executed with no errors
 178 Values: Stores in global "valueRSSI" variable the dBm level in the most recent packet
 179*/
 180uint8_t WaspXBeeXSC::getRSSI()
 181{
 182  uint8_t ByteIN[10];
 183  uint8_t error=1;
 184  uint8_t i=0;
 185
 186  clean();
 187  XBee.println("atdb");
 188  delay(1000);
 189  while(XBee.available() > 0) 
 190  {
 191	ByteIN[i]=XBee.read();
 192	i++;
 193	error=0;
 194  }
 195  valueRSSI=converter(ByteIN[0],ByteIN[1]);
 196  return error;
 197}
 198
 199/*
 200 Function: Set the networking address of a module
 201 Returns: Returns: Integer that determines if there has been any error 
 202   error=2 --> The command has not been executed
 203   error=1 --> There has been an error while executing the command
 204   error=0 --> The command has been executed with no errors
 205 Values: Stores in global "destAddress" variable the networking address of the module
 206*/
 207uint8_t WaspXBeeXSC::setDestAddress(uint8_t destAD_H, uint8_t destAD_L)
 208{
 209  clean();
 210  XBee.print("atdt");
 211  XBee.print(destAD_H,HEX);
 212  XBee.println(destAD_L,HEX);
 213  delay(1000);
 214  
 215  if(!check())
 216  {
 217	destAddress[0]=destAD_H;
 218	destAddress[1]=destAD_L;
 219	return 0;
 220  }
 221  return 1;
 222}
 223
 224/*
 225 Function: Get the networking address of a module
 226 Returns: Returns: Integer that determines if there has been any error 
 227   error=2 --> The command has not been executed
 228   error=1 --> There has been an error while executing the command
 229   error=0 --> The command has been executed with no errors
 230 Values: Stores in global "destAddress" variable the Baud Rate used in the module
 231*/
 232uint8_t WaspXBeeXSC::getDestAddress()
 233{
 234  uint8_t ByteIN[10];
 235  uint8_t error=1;
 236  uint8_t i=0;
 237
 238  clean();
 239  XBee.println("atdt");
 240  delay(1000);
 241  while(XBee.available() > 0) 
 242  {
 243	ByteIN[i]=XBee.read();
 244	i++;
 245	error=0;
 246  }
 247  destAddress[0]=converter(ByteIN[0],ByteIN[1]);
 248  destAddress[1]=converter(ByteIN[2],ByteIN[3]);
 249  return error;
 250}
 251
 252/*
 253 Function: Set the receive error count
 254 Returns: Returns: Integer that determines if there has been any error 
 255   error=2 --> The command has not been executed
 256   error=1 --> There has been an error while executing the command
 257   error=0 --> The command has been executed with no errors
 258 Values: Stores in global "receiveErrorCount" variable the receive error count
 259*/
 260uint8_t WaspXBeeXSC::setReceiveErrorCount(uint8_t recerror_H, uint8_t recerror_L)
 261{
 262  clean();
 263  XBee.print("ater");
 264  XBee.print(recerror_H,HEX);
 265  XBee.println(recerror_L,HEX);
 266  delay(1000);
 267  
 268  if(!check())
 269  {
 270	receiveErrorCount[0]=recerror_H;
 271	receiveErrorCount[1]=recerror_L;
 272	return 0;
 273  }
 274  return 1;
 275}
 276
 277/*
 278 Function: Set the receive error count
 279 Returns: Returns: Integer that determines if there has been any error 
 280   error=2 --> The command has not been executed
 281   error=1 --> There has been an error while executing the command
 282   error=0 --> The command has been executed with no errors
 283 Values: Stores in global "receiveErrorCount" variable the receive error count
 284*/
 285uint8_t WaspXBeeXSC::getReceiveErrorCount()
 286{
 287  uint8_t ByteIN[10];
 288  uint8_t error=1;
 289  uint8_t i=0;
 290
 291  clean();
 292  XBee.println("ater");
 293  delay(1000);
 294  while(XBee.available() > 0) 
 295  {
 296	ByteIN[i]=XBee.read();
 297	i++;
 298	error=0;
 299  }
 300  receiveErrorCount[0]=converter(ByteIN[0],ByteIN[1]);
 301  receiveErrorCount[1]=converter(ByteIN[2],ByteIN[3]);
 302  return error;
 303}
 304
 305/*
 306 Function: Force a Wake-up initializer to be sent on the next transmit
 307 Returns: Returns: Integer that determines if there has been any error 
 308   error=2 --> The command has not been executed
 309   error=1 --> There has been an error while executing the command
 310   error=0 --> The command has been executed with no errors
 311*/
 312uint8_t WaspXBeeXSC::forceWakeUP()
 313{
 314  clean();
 315  XBee.println("atfh");
 316  delay(1000);
 317  return check();
 318}
 319
 320/*
 321 Function: Resets the module through the UART
 322 Returns: Returns: Integer that determines if there has been any error 
 323   error=2 --> The command has not been executed
 324   error=1 --> There has been an error while executing the command
 325   error=0 --> The command has been executed with no errors
 326*/
 327uint8_t WaspXBeeXSC::forceReset()
 328{
 329  clean();
 330  XBee.println("atfr");
 331  delay(1000);
 332  return check();
 333}
 334
 335/*
 336 Function: Set the receive good count
 337 Returns: Returns: Integer that determines if there has been any error 
 338   error=2 --> The command has not been executed
 339   error=1 --> There has been an error while executing the command
 340   error=0 --> The command has been executed with no errors
 341 Values: Stores in global "receiveGoodCount" variable the receive good count
 342*/
 343uint8_t WaspXBeeXSC::setReceiveGoodCount(uint8_t recgood_H, uint8_t recgood_L)
 344{
 345  clean();
 346  XBee.print("atgd");
 347  XBee.print(recgood_H,HEX);
 348  XBee.println(recgood_L,HEX);
 349  delay(1000);
 350  
 351  if(!check())
 352  {
 353	receiveGoodCount[0]=recgood_H;
 354	receiveGoodCount[1]=recgood_L;
 355	return 0;
 356  }
 357  return 1;
 358}
 359
 360/*
 361 Function: Set the receive good count
 362 Returns: Returns: Integer that determines if there has been any error 
 363   error=2 --> The command has not been executed
 364   error=1 --> There has been an error while executing the command
 365   error=0 --> The command has been executed with no errors
 366 Values: Stores in global "receiveGoodCount" variable the receive good count
 367*/
 368uint8_t WaspXBeeXSC::getReceiveGoodCount()
 369{
 370  uint8_t ByteIN[10];
 371  uint8_t error=1;
 372  uint8_t i=0;
 373
 374  clean();
 375  XBee.println("atgd");
 376  delay(1000);
 377  while(XBee.available() > 0) 
 378  {
 379	ByteIN[i]=XBee.read();
 380	i++;
 381	error=0;
 382  }
 383  receiveGoodCount[0]=converter(ByteIN[0],ByteIN[1]);
 384  receiveGoodCount[1]=converter(ByteIN[2],ByteIN[3]);
 385  return error;
 386}
 387
 388/*
 389 Function: Set the Hopping Channel used in the communication
 390 Returns: Returns: Integer that determines if there has been any error 
 391   error=2 --> The command has not been executed
 392   error=1 --> There has been an error while executing the command
 393   error=0 --> The command has been executed with no errors
 394 Values: Stores in global "channel" variable the Hopping Channel used in the communication
 395*/
 396uint8_t WaspXBeeXSC::setHoppingChannel(uint8_t hchannel)
 397{
 398  clean();
 399  XBee.print("athp");
 400  XBee.println(hchannel,DEC);
 401  delay(1000);
 402  
 403  if(!check())
 404  {
 405	channel=hchannel;
 406	return 0;
 407  }
 408  return 1;
 409}
 410
 411/*
 412 Function: Get the Hopping Channel used in the communication
 413 Returns: Returns: Integer that determines if there has been any error 
 414   error=2 --> The command has not been executed
 415   error=1 --> There has been an error while executing the command
 416   error=0 --> The command has been executed with no errors
 417 Values: Stores in global "channel" variable the Hopping Channel used in the communication
 418*/
 419uint8_t WaspXBeeXSC::getHoppingChannel()
 420{
 421  uint8_t ByteIN[10];
 422  uint8_t error=1;
 423  uint8_t i=0;
 424
 425  clean();
 426  XBee.println("athp");
 427  delay(1000);
 428  while(XBee.available() > 0) 
 429  {
 430	ByteIN[i]=XBee.read();
 431	i++;
 432	error=0;
 433  }
 434  channel=converter(ByteIN[0]);
 435  return error;
 436}
 437
 438/*
 439 Function: Set the time before Wake-UP initializer
 440 Returns: Returns: Integer that determines if there has been any error 
 441   error=2 --> The command has not been executed
 442   error=1 --> There has been an error while executing the command
 443   error=0 --> The command has been executed with no errors
 444 Values: Stores in global "timeBeforeWakeUP" variable the time before Wake-UP initializer
 445*/
 446uint8_t WaspXBeeXSC::setTimeBeforeWakeUP(uint8_t timeHT_H, uint8_t timeHT_L)
 447{
 448  clean();
 449  XBee.print("atht");
 450  XBee.print(timeHT_H,HEX);
 451  XBee.println(timeHT_L,HEX);
 452  delay(1000);
 453  
 454  if(!check())
 455  {
 456	timeBeforeWakeUP[0]=timeHT_H;
 457	timeBeforeWakeUP[1]=timeHT_L;
 458	return 0;
 459  }
 460  return 1;
 461}
 462
 463/*
 464 Function: Get the time before Wake-UP initializer
 465 Returns: Returns: Integer that determines if there has been any error 
 466   error=2 --> The command has not been executed
 467   error=1 --> There has been an error while executing the command
 468   error=0 --> The command has been executed with no errors
 469 Values: Stores in global "timeBeforeWakeUP" variable the time before Wake-UP initializer
 470*/
 471uint8_t WaspXBeeXSC::getTimeBeforeWakeUP()
 472{
 473  uint8_t ByteIN[10];
 474  uint8_t error=1;
 475  uint8_t i=0;
 476
 477  clean();
 478  XBee.println("atht");
 479  delay(1000);
 480  while(XBee.available() > 0) 
 481  {
 482	ByteIN[i]=XBee.read();
 483	i++;
 484	error=0;
 485  }
 486  timeBeforeWakeUP[0]=converter(ByteIN[0],ByteIN[1]);
 487  timeBeforeWakeUP[1]=converter(ByteIN[2],ByteIN[3]);
 488  return error;
 489}
 490
 491/*
 492 Function: Set the amount of time Wake-UP initializer is sent
 493 Returns: Returns: Integer that determines if there has been any error 
 494   error=2 --> The command has not been executed
 495   error=1 --> There has been an error while executing the command
 496   error=0 --> The command has been executed with no errors
 497 Values: Stores in global "timeWakeUpInit" variable the amount of time Wake-UP initializer is sent
 498*/
 499uint8_t WaspXBeeXSC::setTimeWakeUpInit(uint8_t timeLH)
 500{
 501  clean();
 502  XBee.print("atlh");
 503  XBee.println(timeLH,HEX);
 504  delay(1000);
 505  
 506  if(!check())
 507  {
 508	timeWakeUpInit=timeLH;
 509	return 0;
 510  }
 511  return 1;
 512}
 513
 514/*
 515 Function: Get the amount of time Wake-UP initializer is sent
 516 Returns: Returns: Integer that determines if there has been any error 
 517   error=2 --> The command has not been executed
 518   error=1 --> There has been an error while executing the command
 519   error=0 --> The command has been executed with no errors
 520 Values: Stores in global "timeWakeUpInit" variable the amount of time Wake-UP initializer is sent
 521*/
 522uint8_t WaspXBeeXSC::getTimeWakeUpInit()
 523{
 524  uint8_t ByteIN[10];
 525  uint8_t error=1;
 526  uint8_t i=0;
 527
 528  clean();
 529  XBee.println("atlh");
 530  delay(1000);
 531  while(XBee.available() > 0) 
 532  {
 533	ByteIN[i]=XBee.read();
 534	i++;
 535	error=0;
 536  }
 537  timeWakeUpInit=converter(ByteIN[0],ByteIN[1]);
 538  return error;
 539}
 540
 541/*
 542 Function: Set the address mask
 543 Returns: Returns: Integer that determines if there has been any error 
 544   error=2 --> The command has not been executed
 545   error=1 --> There has been an error while executing the command
 546   error=0 --> The command has been executed with no errors
 547 Values: Stores in global "addressMask" variable the address mask
 548*/
 549uint8_t WaspXBeeXSC::setAddressMask(uint8_t mask_H, uint8_t mask_L)
 550{
 551  clean();
 552  XBee.print("atmk");
 553  XBee.print(mask_H,HEX);
 554  XBee.println(mask_L,HEX);
 555  delay(1000);
 556  
 557  if(!check())
 558  {
 559	addressMask[0]=mask_H;
 560	addressMask[1]=mask_L;
 561	return 0;
 562  }
 563  return 1;
 564}
 565
 566/*
 567 Function: Get the address mask
 568 Returns: Returns: Integer that determines if there has been any error 
 569   error=2 --> The command has not been executed
 570   error=1 --> There has been an error while executing the command
 571   error=0 --> The command has been executed with no errors
 572 Values: Stores in global "addressMask" variable the address mask
 573*/
 574uint8_t WaspXBeeXSC::getAddressMask()
 575{
 576  uint8_t ByteIN[10];
 577  uint8_t error=1;
 578  uint8_t i=0;
 579
 580  clean();
 581  XBee.println("atmk");
 582  delay(1000);
 583  while(XBee.available() > 0) 
 584  {
 585	ByteIN[i]=XBee.read();
 586	i++;
 587	error=0;
 588  }
 589  addressMask[0]=converter(ByteIN[0],ByteIN[1]);
 590  addressMask[1]=converter(ByteIN[2],ByteIN[3]);
 591  return error;
 592}
 593
 594/*
 595 Function: Set Pin Wake-UP ON/OFF
 596 Returns: Returns: Integer that determines if there has been any error 
 597   error=2 --> The command has not been executed
 598   error=1 --> There has been an error while executing the command
 599   error=0 --> The command has been executed with no errors
 600 Values: Stores in global "pinWakeUP" variable Pin Wake-UP ON/OFF
 601*/
 602uint8_t WaspXBeeXSC::setPinWakeUP(uint8_t pin)
 603{
 604  clean();
 605  XBee.print("atpw");
 606  XBee.println(pin,DEC);
 607  delay(1000);
 608  
 609  if(!check())
 610  {
 611	pinWakeUP=pin;
 612	return 0;
 613  }
 614  return 1;
 615}
 616
 617/*
 618 Function: Get Pin Wake-UP ON/OFF
 619 Returns: Returns: Integer that determines if there has been any error 
 620   error=2 --> The command has not been executed
 621   error=1 --> There has been an error while executing the command
 622   error=0 --> The command has been executed with no errors
 623 Values: Stores in global "pin" variable Pin Wake-UP ON/OFF
 624*/
 625uint8_t WaspXBeeXSC::getPinWakeUP()
 626{
 627  uint8_t ByteIN[10];
 628  uint8_t error=1;
 629  uint8_t i=0;
 630
 631  clean();
 632  XBee.println("atpw");
 633  delay(1000);
 634  while(XBee.available() > 0) 
 635  {
 636	ByteIN[i]=XBee.read();
 637	i++;
 638	error=0;
 639  }
 640  pinWakeUP=converter(ByteIN[0]);
 641  return error;
 642}
 643
 644/*
 645 Function: Restore all configurable parameters to defaults values
 646 Returns: Returns: Integer that determines if there has been any error 
 647   error=2 --> The command has not been executed
 648   error=1 --> There has been an error while executing the command
 649   error=0 --> The command has been executed with no errors
 650*/
 651uint8_t WaspXBeeXSC::restoreDefaults()
 652{
 653  clean();
 654  XBee.println("atre");
 655  delay(1000);
 656  return check();
 657}
 658
 659/*
 660 Function: Set Random delay slots
 661 Returns: Returns: Integer that determines if there has been any error 
 662   error=2 --> The command has not been executed
 663   error=1 --> There has been an error while executing the command
 664   error=0 --> The command has been executed with no errors
 665 Values: Stores in global "delaySlots" variable the Random delay slots
 666*/
 667uint8_t WaspXBeeXSC::setDelaySlots(uint8_t slot)
 668{
 669  clean();
 670  XBee.print("atrn");
 671  XBee.println(slot,HEX);
 672  delay(1000);
 673  
 674  if(!check())
 675  {
 676	delaySlots=slot;
 677	return 0;
 678  }
 679  return 1;
 680}
 681
 682/*
 683 Function: Get Random delay slots
 684 Returns: Returns: Integer that determines if there has been any error 
 685   error=2 --> The command has not been executed
 686   error=1 --> There has been an error while executing the command
 687   error=0 --> The command has been executed with no errors
 688 Values: Stores in global "delaySlots" variable the Random delay slots
 689*/
 690uint8_t WaspXBeeXSC::getDelaySlots()
 691{
 692  uint8_t ByteIN[10];
 693  uint8_t error=1;
 694  uint8_t i=0;
 695
 696  clean();
 697  XBee.println("atrn");
 698  delay(1000);
 699  while(XBee.available() > 0) 
 700  {
 701	ByteIN[i]=XBee.read();
 702	i++;
 703	error=0;
 704  }
 705  delaySlots=converter(ByteIN[0],ByteIN[1]);
 706  return error;
 707}
 708
 709/*
 710 Function: Set the packetization timeout
 711 Returns: Returns: Integer that determines if there has been any error 
 712   error=2 --> The command has not been executed
 713   error=1 --> There has been an error while executing the command
 714   error=0 --> The command has been executed with no errors
 715 Values: Stores in global "packetTimeout" variable the packetization timeout
 716*/
 717uint8_t WaspXBeeXSC::setPacketTimeout(uint8_t pack_H, uint8_t pack_L)
 718{
 719  clean();
 720  XBee.print("atro");
 721  XBee.print(pack_H,HEX);
 722  XBee.println(pack_L,HEX);
 723  delay(1000);
 724  
 725  if(!check())
 726  {
 727	packetTimeout[0]=pack_H;
 728	packetTimeout[1]=pack_L;
 729	return 0;
 730  }
 731  return 1;
 732}
 733
 734/*
 735 Function: Get the packetization timeout
 736 Returns: Returns: Integer that determines if there has been any error 
 737   error=2 --> The command has not been executed
 738   error=1 --> There has been an error while executing the command
 739   error=0 --> The command has been executed with no errors
 740 Values: Stores in global "packetTimeout" variable the packetization timeout
 741*/
 742uint8_t WaspXBeeXSC::getPacketTimeout()
 743{
 744  uint8_t ByteIN[10];
 745  uint8_t error=1;
 746  uint8_t i=0;
 747
 748  clean();
 749  XBee.println("atro");
 750  delay(1000);
 751  while(XBee.available() > 0) 
 752  {
 753	ByteIN[i]=XBee.read();
 754	i++;
 755	error=0;
 756  }
 757  packetTimeout[0]=converter(ByteIN[0],ByteIN[1]);
 758  packetTimeout[1]=converter(ByteIN[2],ByteIN[3]);
 759  return error;
 760}
 761
 762/*
 763 Function: Set the time the output pin is active with the RSSI value
 764 Returns: Returns: Integer that determines if there has been any error 
 765   error=2 --> The command has not been executed
 766   error=1 --> There has been an error while executing the command
 767   error=0 --> The command has been executed with no errors
 768 Values: Stores in global "timeRSSI" variable the time the output pin is active with the RSSI value
 769*/
 770uint8_t WaspXBeeXSC::setRSSItime(uint8_t rssiTime)
 771{
 772  clean();
 773  XBee.print("atrp");
 774  XBee.println(rssiTime,HEX);
 775  delay(1000);
 776  
 777  if(!check())
 778  {
 779	timeRSSI=rssiTime;
 780	return 0;
 781  }
 782  return 1;
 783}
 784
 785/*
 786 Function: Get the time the output pin is active with the RSSI value
 787 Returns: Returns: Integer that determines if there has been any error 
 788   error=2 --> The command has not been executed
 789   error=1 --> There has been an error while executing the command
 790   error=0 --> The command has been executed with no errors
 791 Values: Stores in global "timeRSSI" variable the time the output pin is active with the RSSI value
 792*/
 793uint8_t WaspXBeeXSC::getRSSItime()
 794{
 795  uint8_t ByteIN[10];
 796  uint8_t error=1;
 797  uint8_t i=0;
 798
 799  clean();
 800  XBee.println("atrp");
 801  delay(1000);
 802  while(XBee.available() > 0) 
 803  {
 804	ByteIN[i]=XBee.read();
 805	i++;
 806	error=0;
 807  }
 808  timeRSSI=converter(ByteIN[0],ByteIN[1]);
 809  return error;
 810}
 811
 812/*
 813 Function: Set the number of retries that can be sent for a given packet RF
 814 Returns: Returns: Integer that determines if there has been any error 
 815   error=2 --> The command has not been executed
 816   error=1 --> There has been an error while executing the command
 817   error=0 --> The command has been executed with no errors
 818 Values: Stores in global "retries" variable the number of retries that can be sent for a given packet RF
 819*/
 820uint8_t WaspXBeeXSC::setRetries(uint8_t retry)
 821{
 822  clean();
 823  XBee.print("atrr");
 824  XBee.println(retry,HEX);
 825  delay(1000);
 826  
 827  if(!check())
 828  {
 829	retries=retry;
 830	return 0;
 831  }
 832  return 1;
 833}
 834
 835/*
 836 Function: Get the number of retries that can be sent for a given packet RF
 837 Returns: Returns: Integer that determines if there has been any error 
 838   error=2 --> The command has not been executed
 839   error=1 --> There has been an error while executing the command
 840   error=0 --> The command has been executed with no errors
 841 Values: Stores in global "retries" variable the number of retries that can be sent for a given packet RF
 842*/
 843uint8_t WaspXBeeXSC::getRetries()
 844{
 845  uint8_t ByteIN[10];
 846  uint8_t error=1;
 847  uint8_t i=0;
 848
 849  clean();
 850  XBee.println("atrr");
 851  delay(1000);
 852  while(XBee.available() > 0) 
 853  {
 854	ByteIN[i]=XBee.read();
 855	i++;
 856	error=0;
 857  }
 858  retries=converter(ByteIN[0],ByteIN[1]);
 859  return error;
 860}
 861
 862/*
 863 Function: Get RSSI of the last packet received
 864 Returns: Returns: Integer that determines if there has been any error 
 865   error=2 --> The command has not been executed
 866   error=1 --> There has been an error while executing the command
 867   error=0 --> The command has been executed with no errors
 868 Values: Stores in global "RSSI" variable the RSSI of the last packet received
 869*/
 870uint8_t WaspXBeeXSC::getRSSIvalue()
 871{
 872  uint8_t ByteIN[10];
 873  uint8_t error=1;
 874  uint8_t i=0;
 875
 876  clean();
 877  XBee.println("atrs");
 878  delay(1000);
 879  while(XBee.available() > 0) 
 880  {
 881	ByteIN[i]=XBee.read();
 882	i++;
 883	error=0;
 884  }
 885  RSSI=converter(ByteIN[0],ByteIN[1]);
 886  return error;
 887}
 888
 889/*
 890 Function: Set the number of bits in the data packet
 891 Returns: Returns: Integer that determines if there has been any error 
 892   error=2 --> The command has not been executed
 893   error=1 --> There has been an error while executing the command
 894   error=0 --> The command has been executed with no errors
 895 Values: Stores in global "stopBits" variable the number of bits in the data packet
 896*/
 897uint8_t WaspXBeeXSC::setStopBits(uint8_t stop)
 898{
 899  clean();
 900  XBee.print("atsb");
 901  XBee.println(stop,HEX);
 902  delay(1000);
 903  
 904  if(!check())
 905  {
 906	stopBits=stop;
 907	return 0;
 908  }
 909  return 1;
 910}
 911
 912/*
 913 Function: Get the number of bits in the data packet
 914 Returns: Returns: Integer that determines if there has been any error 
 915   error=2 --> The command has not been executed
 916   error=1 --> There has been an error while executing the command
 917   error=0 --> The command has been executed with no errors
 918 Values: Stores in global "stopBits" variable the number of bits in the data packet
 919*/
 920uint8_t WaspXBeeXSC::getStopBits()
 921{
 922  uint8_t ByteIN[10];
 923  uint8_t error=1;
 924  uint8_t i=0;
 925
 926  clean();
 927  XBee.println("atsb");
 928  delay(1000);
 929  while(XBee.available() > 0) 
 930  {
 931	ByteIN[i]=XBee.read();
 932	i++;
 933	error=0;
 934  }
 935  stopBits=converter(ByteIN[0],ByteIN[1]);
 936  return error;
 937}
 938
 939/*
 940 Function: Get the 32b higher Source Mac 
 941 Returns: Returns: Integer that determines if there has been any error 
 942   error=2 --> The command has not been executed
 943   error=1 --> There has been an error while executing the command
 944   error=0 --> The command has been executed with no errors
 945 Values: Stores in global "sourceMacHigh" variable the 32b higher Source Mac 
 946*/
 947uint8_t WaspXBeeXSC::getSourceMacHigh()
 948{
 949  uint8_t ByteIN[10];
 950  uint8_t error=1;
 951  uint8_t i=0;
 952
 953  clean();
 954  XBee.println("atsh");
 955  delay(1000);
 956  while(XBee.available() > 0) 
 957  {
 958	ByteIN[i]=XBee.read();
 959	i++;
 960	error=0;
 961  }
 962  if(i<5)
 963  {
 964	  ByteIN[3]=ByteIN[2];
 965	  ByteIN[2]=ByteIN[1];
 966	  ByteIN[1]=ByteIN[0];
 967	  ByteIN[0]=0;
 968  }
 969  it=0; 
 970  i=0;
 971  while(i<4)
 972  {
 973	sourceMacHigh[it]=converter(ByteIN[i],ByteIN[i+1]);
 974	i++;
 975	i++;
 976	it++;
 977  }
 978  return error;
 979}
 980
 981/*
 982 Function: Get the 32b lowe Source Mac 
 983 Returns: Returns: Integer that determines if there has been any error 
 984   error=2 --> The command has not been executed
 985   error=1 --> There has been an error while executing the command
 986   error=0 --> The command has been executed with no errors
 987 Values: Stores in global "sourceMacLow" variable the 32b lower Source Mac 
 988*/
 989uint8_t WaspXBeeXSC::getSourceMacLow()
 990{
 991  uint8_t ByteIN[10];
 992  uint8_t error=1;
 993  uint8_t i=0;
 994
 995  clean();
 996  XBee.println("atsl");
 997  delay(1000);
 998  while(XBee.available() > 0) 
 999  {
1000	ByteIN[i]=XBee.read();
1001	i++;
1002	error=0;
1003  }
1004  if(i<5)
1005  {
1006	  ByteIN[3]=ByteIN[2];
1007	  ByteIN[2]=ByteIN[1];
1008	  ByteIN[1]=ByteIN[0];
1009	  ByteIN[0]=0;
1010  }
1011  i=0;
1012  it=0;
1013  while(i<4)
1014  {
1015	sourceMacLow[it]=converter(ByteIN[i],ByteIN[i+1]);
1016	i++;
1017	i++;
1018	it++;
1019  }
1020  return error;
1021}
1022
1023/*
1024 Function: Set the Sleep Mode
1025 Returns: Returns: Integer that determines if there has been any error 
1026   error=2 --> The command has not been executed
1027   error=1 --> There has been an error while executing the command
1028   error=0 --> The command has been executed with no errors
1029 Values: Stores in global "sleepMode" variable the Sleep Mode
1030*/
1031uint8_t WaspXBeeXSC::setSleepMode(uint8_t smode)
1032{
1033  clean();
1034  XBee.print("atsm");
1035  XBee.println(smode,DEC);
1036  delay(1000);
1037  
1038  if(!check())
1039  {
1040	sleepMode=smode;
1041	return 0;
1042  }
1043  return 1;
1044}
1045
1046/*
1047 Function: Get the Sleep Mode
1048 Returns: Returns: Integer that determines if there has been any error 
1049   error=2 --> The command has not been executed
1050   error=1 --> There has been an error while executing the command
1051   error=0 --> The command has been executed with no errors
1052 Values: Stores in global "sleepMode" variable the Sleep Mode
1053*/
1054uint8_t WaspXBeeXSC::getSleepMode()
1055{
1056  uint8_t ByteIN[10];
1057  uint8_t error=1;
1058  uint8_t i=0;
1059
1060  clean();
1061  XBee.println("atsm");
1062  delay(1000);
1063  while(XBee.available() > 0) 
1064  {
1065	ByteIN[i]=XBee.read();
1066	i++;
1067	error=0;
1068  }
1069  sleepMode=converter(ByteIN[0]);
1070  return error;
1071}
1072
1073
1074/*
1075 Function: Set the amount of time the module stays inactive before entering into Sleep Mode
1076 Returns: Returns: Integer that determines if there has been any error 
1077   error=2 --> The command has not been executed
1078   error=1 --> There has been an error while executing the command
1079   error=0 --> The command has been executed with no errors
1080 Values: Stores in global "awakeTime" variable the amount of time the module stays inactive before entering into Sleep Mode
1081*/
1082uint8_t WaspXBeeXSC::setAwakeTime(uint8_t awake_H, uint8_t awake_L)
1083{
1084  clean();
1085  XBee.print("atst");
1086  XBee.print(awake_H,HEX);
1087  XBee.println(awake_L,HEX);
1088  delay(1000);
1089  
1090  if(!check())
1091  {
1092	awakeTime[0]=awake_H;
1093	awakeTime[1]=awake_L;
1094	return 0;
1095  }
1096  return 1;
1097}
1098
1099
1100/*
1101 Function: Get the amount of time the module stays inactive before entering into Sleep Mode
1102 Returns: Returns: Integer that determines if there has been any error 
1103   error=2 --> The command has not been executed
1104   error=1 --> There has been an error while executing the command
1105   error=0 --> The command has been executed with no errors
1106 Values: Stores in global "awakeTime" variable the amount of time the module stays inactive before entering into Sleep Mode
1107*/
1108uint8_t WaspXBeeXSC::getAwakeTime()
1109{
1110  uint8_t ByteIN[10];
1111  uint8_t error=1;
1112  uint8_t i=0;
1113
1114  clean();
1115  XBee.println("atst");
1116  delay(1000);
1117  while(XBee.available() > 0) 
1118  {
1119	ByteIN[i]=XBee.read();
1120	i++;
1121	error=0;
1122  }
1123  awakeTime[0]=converter(ByteIN[0],ByteIN[1]);
1124  awakeTime[1]=converter(ByteIN[2],ByteIN[3]);
1125  return error;
1126}
1127
1128/*
1129 Function: Set the Time before sending again Initializer
1130 Returns: Returns: Integer that determines if there has been any error 
1131   error=2 --> The command has not been executed
1132   error=1 --> There has been an error while executing the command
1133   error=0 --> The command has been executed with no errors
1134 Values: Stores in global "timeBeforeInit" variable the Time before sending again Initializer
1135*/
1136uint8_t WaspXBeeXSC::setTimeBeforeInit(uint8_t timeInit)
1137{
1138  clean();
1139  XBee.print("atsy");
1140  XBee.println(timeInit,HEX);
1141  delay(1000);
1142  
1143  if(!check())
1144  {
1145	timeBeforeInit=timeInit;
1146	return 0;
1147  }
1148  return 1;
1149}
1150
1151/*
1152 Function: Get the Time before sending again Initializer
1153 Returns: Returns: Integer that determines if there has been any error 
1154   error=2 --> The command has not been executed
1155   error=1 --> There has been an error while executing the command
1156   error=0 --> The command has been executed with no errors
1157 Values: Stores in global "timeBeforeInit" variable the Time before sending again Initializer
1158*/
1159uint8_t WaspXBeeXSC::getTimeBeforeInit()
1160{
1161  uint8_t ByteIN[10];
1162  uint8_t error=1;
1163  uint8_t i=0;
1164
1165  clean();
1166  XBee.println("atsy");
1167  delay(1000);
1168  while(XBee.available() > 0) 
1169  {
1170	ByteIN[i]=XBee.read();
1171	i++;
1172	error=0;
1173  }
1174  timeBeforeInit=converter(ByteIN[0],ByteIN[1]);
1175  return error;
1176}
1177
1178/*
1179 Function: Set the trasmit error count
1180 Returns: Returns: Integer that determines if there has been any error 
1181   error=2 --> The command has not been executed
1182   error=1 --> There has been an error while executing the command
1183   error=0 --> The command has been executed with no errors
1184 Values: Stores in global "transmitErrorCount" variable the transmit error count
1185*/
1186uint8_t WaspXBeeXSC::setTransmitErrorCount(uint8_t txerror_H, uint8_t txerror_L)
1187{
1188  clean();
1189  XBee.print("attr");
1190  XBee.print(txerror_H,HEX);
1191  XBee.println(txerror_L,HEX);
1192  delay(1000);
1193  
1194  if(!check())
1195  {
1196	transmitErrorCount[0]=txerror_H;
1197	transmitErrorCount[1]=txerror_L;
1198	return 0;
1199  }
1200  return 1;
1201}
1202
1203/*
1204 Function: Set the transmit error count
1205 Returns: Returns: Integer that determines if there has been any error 
1206   error=2 --> The command has not been executed
1207   error=1 --> There has been an error while executing the command
1208   error=0 --> The command has been executed with no errors
1209 Values: Stores in global "transmitErrorCount" variable the transmit error count
1210*/
1211uint8_t WaspXBeeXSC::getTransmitErrorCount()
1212{
1213  uint8_t ByteIN[10];
1214  uint8_t error=1;
1215  uint8_t i=0;
1216
1217  clean();
1218  XBee.println("attr");
1219  delay(1000);
1220  while(XBee.available() > 0) 
1221  {
1222	ByteIN[i]=XBee.read();
1223	i++;
1224	error=0;
1225  }
1226  transmitErrorCount[0]=converter(ByteIN[0],ByteIN[1]);
1227  transmitErrorCount[1]=converter(ByteIN[2],ByteIN[3]);
1228  return error;
1229}
1230
1231/*
1232 Function: Set the max number of bytes a module can send before waiting some random time
1233 Returns: Returns: Integer that determines if there has been any error 
1234   error=2 --> The command has not been executed
1235   error=1 --> There has been an error while executing the command
1236   error=0 --> The command has been executed with no errors
1237 Values: Stores in global "transmitLimit" variable the max number of bytes a module can send before waiting some random time
1238*/
1239uint8_t WaspXBeeXSC::setTransmitLimit(uint8_t txlim_H, uint8_t txlim_L)
1240{
1241  clean();
1242  XBee.print("attt");
1243  XBee.print(txlim_H,HEX);
1244  XBee.println(txlim_L,HEX);
1245  delay(1000);
1246  
1247  if(!check())
1248  {
1249	transmitLimit[0]=txlim_H;
1250	transmitLimit[1]=txlim_L;
1251	return 0;
1252  }
1253  return 1;
1254}
1255
1256/*
1257 Function: Get the max number of bytes a module can send before waiting some random time
1258 Returns: Returns: Integer that determines if there has been any error 
1259   error=2 --> The command has not been executed
1260   error=1 --> There has been an error while executing the command
1261   error=0 --> The command has been executed with no errors
1262 Values: Stores in global "transmitLimit" variable the max number of bytes a module can send before waiting some random time
1263*/
1264uint8_t WaspXBeeXSC::getTransmitLimit()
1265{
1266  uint8_t ByteIN[10];
1267  uint8_t error=1;
1268  uint8_t i=0;
1269
1270  clean();
1271  XBee.println("attt");
1272  delay(1000);
1273  while(XBee.available() > 0) 
1274  {
1275	ByteIN[i]=XBee.read();
1276	i++;
1277	error=0;
1278  }
1279  transmitLimit[0]=converter(ByteIN[0],ByteIN[1]);
1280  transmitLimit[1]=converter(ByteIN[2],ByteIN[3]);
1281  return error;
1282}
1283
1284/*
1285 Function: Get the Firmware Version of the module
1286 Returns: Returns: Integer that determines if there has been any error 
1287   error=2 --> The command has not been executed
1288   error=1 --> There has been an error while executing the command
1289   error=0 --> The command has been executed with no errors
1290 Values: Stores in global "firmwareVersion" variable the Firmware Version of the module
1291*/
1292uint8_t WaspXBeeXSC::getSoftVersion()
1293{
1294  uint8_t ByteIN[10];
1295  uint8_t error=1;
1296  uint8_t i=0;
1297
1298  clean();
1299  XBee.println("atvr");
1300  delay(1000);
1301  while(XBee.available() > 0) 
1302  {
1303	ByteIN[i]=XBee.read();
1304	i++;
1305	error=0;
1306  }
1307  firmwareVersion[0]=converter(ByteIN[0],ByteIN[1]);
1308  firmwareVersion[1]=converter(ByteIN[2],ByteIN[3]);
1309  return error;
1310}
1311
1312/*
1313 Function: Write the values into a non-volatil memory
1314 Returns: Returns: Integer that determines if there has been any error 
1315   error=2 --> The command has not been executed
1316   error=1 --> There has been an error while executing the command
1317   error=0 --> The command has been executed with no errors
1318*/
1319uint8_t WaspXBeeXSC::writeValues()
1320{
1321  uint8_t ByteIN[10];
1322  uint8_t error=1;
1323  uint8_t i=0;
1324
1325  clean();
1326  XBee.println("atwr");
1327  delay(1000);
1328  return check();
1329}
1330
1331/*
1332 Function: Send bytes to other modules in the same network
1333 Returns: Returns: Integer that determines if there has been any error 
1334   error=2 --> The command has not been executed
1335   error=1 --> There has been an error while executing the command
1336   error=0 --> The command has been executed with no errors
1337*/
1338uint8_t WaspXBeeXSC::sendData(struct packetXSC* packet)
1339{
1340  uint8_t error=2;
1341
1342	error=setCommandMode();
1343	error=getSourceMacHigh();
1344	error=getSourceMacLow();
1345	error=exitCommandMode();
1346	packet->sourceMacHigh[0]=sourceMacHigh[0];
1347	packet->sourceMacHigh[1]=sourceMacHigh[1];
1348	packet->sourceMacLow[0]=sourceMacLow[0];
1349	packet->sourceMacLow[1]=sourceMacLow[1];
1350
1351	// Enviamos los datos
1352	XBee.print(packet->sourceMacHigh[0],HEX);
1353	XBee.print(packet->sourceMacHigh[1],HEX);
1354	XBee.print(packet->sourceMacLow[0],HEX);
1355	XBee.print(packet->sourceMacLow[1],HEX);	
1356	XBee.print("-");
1357	XBee.print(packet->data_length,DEC);
1358	XBee.print("-");
1359	for(it=0;it<packet->data_length;it++)
1360	{
1361		XBee.print(packet->data[it],BYTE);
1362	}
1363	error=0;
1364	return error;
1365}
1366
1367/*
1368 Function: Read incoming data via RF
1369 Returns: Returns: Integer that determines if there has been any error 
1370   error=2 --> The command has not been executed
1371   error=1 --> There has been an error while executing the command
1372   error=0 --> The command has been executed with no errors
1373*/
1374uint8_t WaspXBeeXSC::readData(struct packetXSC* packet)
1375{
1376  uint8_t error=2;
1377  uint8_t end=0;  
1378  uint16_t interval=2000;
1379  long previous=millis();
1380  uint8_t counter3=0;
1381  uint8_t trama=0;
1382  uint8_t length=0;
1383  uint8_t dataIN[110];
1384
1385  	for(int i=0;i<110;i++) dataIN[i]=0;
1386	previous=millis();
1387	while(end==0)
1388	{
1389		if(XBee.available()>0)
1390		{
1391			previous=millis();
1392			dataIN[counter3]=XBee.read();
1393			counter3++;
1394		}
1395		
1396		if( (millis()-previous) > interval )
1397		{
1398			end=1;
1399			XBee.flush();
1400		} 
1401		
1402	}
1403	end=0;
1404	if(dataIN[0]==10) end=1;
1405	if( dataIN[8+end]!='-' ) return 1;
1406	packet->sourceMacHigh[0]=converter(dataIN[0+end],dataIN[1+end]);
1407	packet->sourceMacHigh[1]=converter(dataIN[2+end],dataIN[3+end]);
1408	packet->sourceMacLow[0]=converter(dataIN[4+end],dataIN[5+end]);
1409	packet->sourceMacLow[1]=converter(dataIN[6+end],dataIN[7+end]);
1410	it=9;
1411	while(dataIN[it]!='-')
1412	{
1413		length++;
1414		it++;
1415	}
1416	switch(length)
1417	{
1418		case 1: packet->data_length=converter(dataIN[9]);
1419			break;
1420		case 2:	packet->data_length=converter(dataIN[9],dataIN[10]);
1421			break;
1422		case 3:	packet->data_length=converter(dataIN[9],dataIN[10],dataIN[11]);
1423			break;
1424	}
1425	packet->data_length=Utils.dec2hex(packet->data_length);
1426	it=0;
1427	for(it=0;it<packet->data_length;it++)
1428	{
1429		packet->data[it]=char(dataIN[it+10+length]);
1430	}
1431	return 0;
1432}
1433
1434
1435/*
1436 Function: Transparent function. The user uint8_troduces an AT command within a string and the function executes it without knowing its meaning
1437 Returns: Integer that determines if there has been any error 
1438   error=2 --> The command has not been executed
1439   error=1 --> There has been an error while executing the command
1440   error=0 --> The command has been executed with no errors
1441 Parameters:
1442  atcommand : String to specify the AT command to execute
1443*/
1444uint8_t WaspXBeeXSC::sendCommandAT(char* atcommand)
1445{
1446	uint8_t error=1;
1447	uint8_t i=0;
1448
1449	clean();
1450	XBee.print("at");
1451	while( *atcommand!='#' )
1452	{
1453		XBee.print(*atcommand++,BYTE);
1454	}
1455	XBee.println("");
1456	delay(1000);
1457	while(XBee.available() > 0) 
1458	{
1459		commandAT[i]=XBee.read();
1460		i++;
1461		error=0;
1462	}
1463	return error;
1464}
1465
1466
1467/*
1468 Function: Connect XBee, activating switch in Waspmote
1469 Returns: Integer that determines if there has been any error 
1470   error=2 --> The command has not been executed
1471   error=1 --> There has been an error while executing the command
1472   error=0 --> The command has been executed with no errors
1473*/
1474uint8_t WaspXBeeXSC::ON()
1475{
1476	uint8_t error=2;
1477	XBee.begin();
1478	XBee.setMode(XBEE_ON);
1479	delay(50);
1480	clean();
1481	error=0;
1482	return error;
1483}
1484
1485
1486/*
1487 Function: Set XBee to sleep, asserting PIN 9
1488 Returns: Integer that determines if there has been any error 
1489   error=2 --> The command has not been executed
1490   error=1 --> There has been an error while executing the command
1491   error=0 --> The command has been executed with no errors
1492*/
1493uint8_t WaspXBeeXSC::sleep()
1494{
1495	uint8_t error=2;
1496	pinMode(XBEE_SLEEP, OUTPUT);
1497	digitalWrite(XBEE_SLEEP,HIGH); 
1498	XBee.close();
1499	error=0;
1500	return error;
1501}
1502
1503
1504/*
1505 Function: Wake up XBee, de-asserting PIN 9
1506 Returns: Integer that determines if there has been any error 
1507   error=2 --> The command has not been executed
1508   error=1 --> There has been an error while executing the command
1509   error=0 --> The command has been executed with no errors
1510*/
1511uint8_t WaspXBeeXSC::wake()
1512{
1513	uint8_t error=2;
1514	pinMode(XBEE_SLEEP, OUTPUT);
1515	digitalWrite(XBEE_SLEEP,LOW); 
1516	XBee.begin();
1517	delay(50);
1518	clean();
1519	error=0;
1520	return error;
1521}
1522
1523
1524/*
1525 Function: Generates a decimal number from an ASCII character which was a number
1526 Returns: The generated number
1527*/
1528uint8_t WaspXBeeXSC::converter(uint8_t conv1)
1529{
1530  uint8_t aux=0;
1531  uint8_t resul=0;
1532
1533	switch(conv1)
1534	{
1535		case 48: aux=0;
1536			 break;
1537		case 49: aux=1;
1538			 break;
1539		case 50: aux=2;
1540			 break;
1541		case 51: aux=3;
1542			 break;
1543		case 52: aux=4;
1544			 break;
1545		case 53: aux=5;
1546			 break;
1547		case 54: aux=6;
1548			 break;
1549		case 55: aux=7;
1550			 break;
1551		case 56: aux=8;
1552			 break;
1553		case 57: aux=9;
1554			 break;
1555	}
1556	resul=aux;
1557	return resul;
1558}
1559
1560
1561/*
1562 Function: Generates a decimal number from two ASCII characters which were numbers
1563 Returns: The generated number
1564*/
1565uint8_t WaspXBeeXSC::converter(uint8_t conv1, uint8_t conv2)
1566{
1567  uint8_t aux=0;
1568  uint8_t aux2=0;
1569  uint8_t resul=0;
1570
1571	switch(conv1)
1572	{
1573		case 48: aux=0;
1574			 break;
1575		case 49: aux=1;
1576			 break;
1577		case 50: aux=2;
1578			 break;
1579		case 51: aux=3;
1580			 break;
1581		case 52: aux=4;
1582			 break;
1583		case 53: aux=5;
1584			 break;
1585		case 54: aux=6;
1586			 break;
1587		case 55: aux=7;
1588			 break;
1589		case 56: aux=8;
1590			 break;
1591		case 57: aux=9;
1592			 break;
1593		case 65: aux=10;
1594			 break;
1595		case 66: aux=11;
1596			 break;
1597		case 67: aux=12;
1598			 break;
1599		case 68: aux=13;
1600			 break;
1601		case 69: aux=14;
1602			 break;
1603		case 70: aux=15;
1604			 break;
1605	}
1606	switch(conv2)
1607	{
1608		case 48: aux2=0;
1609			 break;
1610		case 49: aux2=1;
1611			 break;
1612		case 50: aux2=2;
1613			 break;
1614		case 51: aux2=3;
1615			 break;
1616		case 52: aux2=4;
1617			 break;
1618		case 53: aux2=5;
1619			 break;
1620		case 54: aux2=6;
1621			 break;
1622		case 55: aux2=7;
1623			 break;
1624		case 56: aux2=8;
1625			 break;
1626		case 57: aux2=9;
1627			 break;
1628		case 65: aux2=10;
1629			 break;
1630		case 66: aux2=11;
1631			 break;
1632		case 67: aux2=12;
1633			 break;
1634		case 68: aux2=13;
1635			 break;
1636		case 69: aux2=14;
1637			 break;
1638		case 70: aux2=15;
1639			 break;
1640		default: aux2=100;
1641			 break;
1642	}
1643	if(aux2==100) // Only one character but we have treated two, so We have to fix it
1644	{
1645		resul=aux;
1646	}
1647	else
1648	{
1649		resul=(aux*16)+aux2;
1650	}
1651	return resul;
1652}
1653
1654/*
1655 Function: Generates a decimal number from three ASCII characters which were numbers
1656 Returns: The generated number
1657*/
1658uint8_t WaspXBeeXSC::converter(uint8_t conv1, uint8_t conv2, uint8_t conv3)
1659{
1660  uint8_t aux=0;
1661  uint8_t aux2=0;
1662  uint8_t aux3=0;
1663  uint8_t resul=0;
1664
1665	switch(conv1)
1666	{
1667		case 48: aux=0;
1668			 break;
1669		case 49: aux=1;
1670			 break;
1671		case 50: aux=2;
1672			 break;
1673		case 51: aux=3;
1674			 break;
1675		case 52: aux=4;
1676			 break;
1677		case 53: aux=5;
1678			 break;
1679		case 54: aux=6;
1680			 break;
1681		case 55: aux=7;
1682			 break;
1683		case 56: aux=8;
1684			 break;
1685		case 57: aux=9;
1686			 break;
1687	}
1688	switch(conv2)
1689	{
1690		case 48: aux2=0;
1691			 break;
1692		case 49: aux2=1;
1693			 break;
1694		case 50: aux2=2;
1695			 break;
1696		case 51: aux2=3;
1697			 break;
1698		case 52: aux2=4;
1699			 break;
1700		case 53: aux2=5;
1701			 break;
1702		case 54: aux2=6;
1703			 break;
1704		case 55: aux2=7;
1705			 break;
1706		case 56: aux2=8;
1707			 break;
1708		case 57: aux2=9;
1709			 break;
1710	}
1711	switch(conv3)
1712	{
1713		case 48: aux3=0;
1714			 break;
1715		case 49: aux3=1;
1716			 break;
1717		case 50: aux3=2;
1718			 break;
1719		case 51: aux3=3;
1720			 break;
1721		case 52: aux3=4;
1722			 break;
1723		case 53: aux3=5;
1724			 break;
1725		case 54: aux3=6;
1726			 break;
1727		case 55: aux3=7;
1728			 break;
1729		case 56: aux3=8;
1730			 break;
1731		case 57: aux3=9;
1732			 break;
1733	}
1734	resul=aux3*100+aux2*10+aux;
1735	return resul;
1736}
1737
1738void WaspXBeeXSC::clean()
1739{
1740	uint8_t ByteIN=0;
1741	
1742	while(XBee.available() > 0) 
1743	{
1744		ByteIN = XBee.read();
1745	}
1746}
1747
1748uint8_t WaspXBeeXSC::check()
1749{
1750	uint8_t ByteIN[10];
1751	uint8_t error=0;
1752	uint8_t i=0;
1753	uint8_t	counter=0;
1754	long previous=0;
1755	uint8_t end=0;
1756	long interval=1000;
1757	
1758	previous=millis();
1759	while(end==0)
1760	{
1761		if(XBee.available()>0)
1762		{
1763			ByteIN[i]=XBee.read();
1764			i++;
1765			previous=millis();
1766		}
1767		if( (millis()-previous) > interval )
1768		{
1769			end=1;
1770			XBee.flush();
1771		} 
1772	}
1773	counter=i;
1774	i=0;
1775	while(counter>i)
1776	{
1777		if(ByteIN[i]=='O')
1778		{
1779			if(ByteIN[i+1]=='K')
1780			{
1781				return 0;
1782			}
1783		}
1784		i++;		
1785	}
1786	return 1;
1787}
1788
1789WaspXBeeXSC	xbeeXSC = WaspXBeeXSC();