PageRenderTime 222ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 1ms

/WaspXBeeXSC.cpp

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