Advertisement
poiou

LORAWAN send packets

Mar 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.19 KB | None | 0 0
  1. #include <multiprotocolShield.h>
  2.  
  3. #include <MCP23008.h>
  4.  
  5.  
  6. /*
  7. * ------ P2P Code Example --------
  8. *
  9. * Explanation: This example shows how to configure the module
  10. * for P2P mode and the corresponding parameters. After this,
  11. * the example shows how to send packets to other radio modules
  12. * which must be set with the same radio settings
  13. *
  14. * Copyright (C) 2015 Libelium Comunicaciones Distribuidas S.L.
  15. * http://www.libelium.com
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. * Version: 1.3
  31. * Design: David Gascon
  32. * Implementation: Luismi Marti
  33. * Ported to Arduino: Ruben Martin
  34. */
  35.  
  36. #include <Wire.h>
  37.  
  38. // Cooking API libraries
  39. #include <arduinoUART.h>
  40. #include <arduinoUtils.h>
  41.  
  42. // LoRaWAN library
  43. #include <arduinoLoRaWAN.h>
  44.  
  45. // Pin definition for LoRaWAN module error LED:
  46. const int error_led = 13;
  47.  
  48. //////////////////////////////////////////////
  49. uint8_t socket = SOCKET1;
  50. //////////////////////////////////////////////
  51.  
  52. // define radio settings
  53. //////////////////////////////////////////////
  54. int8_t power = 15;
  55. uint32_t frequency = 868100000;
  56. char spreading_factor[] = "sf12";
  57. char coding_rate[] = "4/5";
  58. uint16_t bandwidth = 125;
  59. char crc_mode[] = "on";
  60. //////////////////////////////////////////////
  61.  
  62.  
  63. // define data to send
  64. char data[] = "010203040506070809";
  65.  
  66. // variable
  67. uint8_t error;
  68.  
  69.  
  70. void setup()
  71. {
  72. Serial.begin(9600);
  73. Serial.println("---------------------------------------------");
  74. Serial.println("begin setup");
  75. Serial.println("---------------------------------------------");
  76. //Radio P2P example - Sending packets
  77. pinMode(error_led, OUTPUT);
  78. digitalWrite(error_led, LOW);
  79.  
  80. // module setup
  81. Serial.println("begin module setup");
  82.  
  83.  
  84. error = radioModuleSetup();
  85. Serial.println("end module setup");
  86. // Check status
  87. if (error == 0)
  88. {
  89. //Module configured OK
  90. digitalWrite(error_led, LOW);
  91. Serial.println("module ok");
  92. }
  93. else
  94. {
  95. //Module configured ERROR"
  96. digitalWrite(error_led, HIGH);
  97. Serial.println("module error");
  98. }
  99.  
  100. Serial.println("---------------------------------------------");
  101. Serial.println("end setup");
  102. Serial.println("---------------------------------------------");
  103. }
  104.  
  105.  
  106. void loop()
  107. {
  108. // Send packet
  109. error=LoRaWAN.sendRadio("teste");
  110.  
  111. Serial.println("---------------------------------------------");
  112. Serial.println("Sending data");
  113. Serial.println("---------------------------------------------");
  114. //error = LoRaWAN.sendRadio(10101010);
  115. error=LoRaWAN.sendRadio("teste");
  116. Serial.println(data);
  117. Serial.println("---------------------------------------------");
  118. Serial.println("data sent");
  119. Serial.println("---------------------------------------------");
  120. // Check status
  121. if (error == 0)
  122. {
  123. //--> Packet sent OK
  124. digitalWrite(error_led, LOW);
  125. }
  126. else
  127. {
  128. //Error waiting for packets. error
  129. digitalWrite(error_led, LOW);
  130. }
  131.  
  132. delay(5000);
  133. }
  134.  
  135.  
  136. /***********************************************************************************
  137. *
  138. * radioModuleSetup()
  139. *
  140. * This function includes all functions related to the module setup and configuration
  141. * The user must keep in mind that each time the module powers on, all settings are set
  142. * to default values. So it is better to develop a specific function including all steps
  143. * for setup and call it everytime the module powers on.
  144. *
  145. *
  146. ***********************************************************************************/
  147. uint8_t radioModuleSetup()
  148. {
  149. Serial.println(" ");
  150. Serial.println(" radioModuleSetup");
  151.  
  152. uint8_t status = 0;
  153. uint8_t e = 0;
  154.  
  155. //////////////////////////////////////////////
  156. // 1. switch on
  157. //////////////////////////////////////////////
  158. Serial.println(" 1. switch on");
  159.  
  160. e = LoRaWAN.ON(SOCKET1);
  161.  
  162. // Check status
  163. if (e == 0)
  164. {
  165. //1. Switch ON OK
  166. digitalWrite(error_led, LOW);
  167. Serial.println("OK");
  168. }
  169. else
  170. {
  171.  
  172. //1. Switch ON error
  173. digitalWrite(error_led, HIGH);
  174. Serial.println("error");
  175. status = 1;
  176.  
  177. }
  178. //-------------------------------------------------------
  179.  
  180. //////////////////////////////////////////////
  181. // 2. Enable P2P mode
  182. //////////////////////////////////////////////
  183. Serial.println(" 2. Enable P2P mode");
  184. e = LoRaWAN.macPause();
  185.  
  186. // Check status
  187. if (e == 0)
  188. {
  189. //2. P2P mode enabled OK
  190. digitalWrite(error_led, LOW);
  191. }
  192. else
  193. {
  194. //2. Enable P2P mode error
  195. digitalWrite(error_led, HIGH);
  196. status = 1;
  197. }
  198. //-------------------------------------------------------
  199.  
  200. //////////////////////////////////////////////
  201. // 3. Set/Get Radio Power
  202. //////////////////////////////////////////////
  203. Serial.println(" 3. Set/Get Radio Power");
  204. // Set power
  205. e = LoRaWAN.setRadioPower(power);
  206.  
  207. // Check status
  208. if (e == 0)
  209. {
  210. //3.1. Set Radio Power OK
  211. digitalWrite(error_led, LOW);
  212. }
  213. else
  214. {
  215. //3.1. Set Radio Power error
  216. digitalWrite(error_led, HIGH);
  217. status = 1;
  218. }
  219.  
  220. // Get power
  221. e = LoRaWAN.getRadioPower();
  222.  
  223. // Check status
  224. if (e == 0)
  225. {
  226. //3.2. Get Radio Power OK.
  227. //"Power in LoRaWAN._radioPower
  228. digitalWrite(error_led, LOW);
  229. }
  230. else
  231. {
  232. //3.2. Get Radio Power error
  233. digitalWrite(error_led, HIGH);
  234. status = 1;
  235. }
  236. //-------------------------------------------------------
  237.  
  238. //////////////////////////////////////////////
  239. // 4. Set/Get Radio Frequency
  240. //////////////////////////////////////////////
  241. Serial.println(" 4. Set/Get Radio Frequency");
  242. // Set frequency
  243. e = LoRaWAN.setRadioFreq(frequency);
  244.  
  245. // Check status
  246. if (e == 0)
  247. {
  248. //4.1. Set Radio Frequency OK
  249. digitalWrite(error_led, LOW);
  250. }
  251. else
  252. {
  253. //4.1. Set Radio Frequency error
  254. digitalWrite(error_led, LOW);
  255. status = 1;
  256. }
  257.  
  258. // Get frequency
  259. e = LoRaWAN.getRadioFreq();
  260.  
  261. // Check status
  262. if (e == 0)
  263. {
  264. //4.2. Get Radio Frequency OK
  265. //Frequency in LoRaWAN._radioFreq
  266. digitalWrite(error_led, LOW);
  267. }
  268. else
  269. {
  270. //4.2. Get Radio Frequency error
  271. digitalWrite(error_led, HIGH);
  272. status = 1;
  273. }
  274. //-------------------------------------------------------
  275.  
  276. //////////////////////////////////////////////
  277. // 5. Set/Get Radio Spreading Factor (SF)
  278. //////////////////////////////////////////////
  279. Serial.println(" 5. Set/Get Radio Spreading Factor (SF)");
  280. // Set SF
  281. e = LoRaWAN.setRadioSF(spreading_factor);
  282.  
  283. // Check status
  284. if (e == 0)
  285. {
  286. //5.1. Set Radio SF OK
  287. digitalWrite(error_led, LOW);
  288. }
  289. else
  290. {
  291. //5.1. Set Radio SF error
  292. digitalWrite(error_led, HIGH);
  293. status = 1;
  294. }
  295.  
  296. // Get SF
  297. e = LoRaWAN.getRadioSF();
  298.  
  299. // Check status
  300. if (e == 0)
  301. {
  302. //5.2. Get Radio SF OK
  303. //Spreading Factor in LoRaWAN._radioSF
  304. digitalWrite(error_led, LOW);
  305. }
  306. else
  307. {
  308. //5.2. Get Radio SF error
  309. digitalWrite(error_led, HIGH);
  310. status = 1;
  311. }
  312. //-------------------------------------------------------
  313.  
  314. //////////////////////////////////////////////
  315. // 6. Set/Get Radio Coding Rate (CR)
  316. //////////////////////////////////////////////
  317. Serial.println(" 6. Set/Get Radio Coding Rate (CR)");
  318. // Set CR
  319. e = LoRaWAN.setRadioCR(coding_rate);
  320.  
  321. // Check status
  322. if (e == 0)
  323. {
  324. //6.1. Set Radio CR OK
  325. digitalWrite(error_led, LOW);
  326. }
  327. else
  328. {
  329. //6.1. Set Radio CR error
  330. digitalWrite(error_led, HIGH);
  331. status = 1;
  332. }
  333.  
  334. // Get CR
  335. e = LoRaWAN.getRadioCR();
  336.  
  337. // Check status
  338. if (e == 0)
  339. {
  340. //6.2. Get Radio CR OK.
  341. //Coding Rate in LoRaWAN._radioCR
  342. digitalWrite(error_led, LOW);
  343. }
  344. else
  345. {
  346. //6.2. Get Radio CR error
  347. digitalWrite(error_led, HIGH);
  348. status = 1;
  349. }
  350. //-------------------------------------------------------
  351.  
  352. //////////////////////////////////////////////
  353. // 7. Set/Get Radio Bandwidth (BW)
  354. //////////////////////////////////////////////
  355. Serial.println(" 7. Set/Get Radio Bandwidth (BW)");
  356. // Set BW
  357. e = LoRaWAN.setRadioBW(bandwidth);
  358.  
  359. // Check status
  360. if (e == 0)
  361. {
  362. //7.1. Set Radio BW OK
  363. digitalWrite(error_led, LOW);
  364. }
  365. else
  366. {
  367. //7.1. Set Radio BW error
  368. digitalWrite(error_led, HIGH);
  369. }
  370.  
  371. // Get BW
  372. e = LoRaWAN.getRadioBW();
  373.  
  374. // Check status
  375. if (e == 0)
  376. {
  377. //7.2. Get Radio BW OK.
  378. //Bandwidth in LoRaWAN._radioBW
  379. digitalWrite(error_led, LOW);
  380. }
  381. else
  382. {
  383. //7.2. Get Radio BW error
  384. digitalWrite(error_led, HIGH);
  385. status = 1;
  386. }
  387. //-------------------------------------------------------
  388.  
  389. //////////////////////////////////////////////
  390. // 8. Set/Get Radio CRC mode
  391. //////////////////////////////////////////////
  392.  
  393. Serial.println(" 8. Set/Get Radio CRC mode");
  394. // Set CRC
  395. e = LoRaWAN.setRadioCRC(crc_mode);
  396.  
  397. // Check status
  398. if (e == 0)
  399. {
  400. //8.1. Set Radio CRC mode OK
  401. digitalWrite(error_led, LOW);
  402. }
  403. else
  404. {
  405. //8.1. Set Radio CRC mode error
  406. digitalWrite(error_led, HIGH);
  407. status = 1;
  408. }
  409.  
  410. // Get CRC
  411. e = LoRaWAN.getRadioCRC();
  412.  
  413. // Check status
  414. if (e == 0)
  415. {
  416. //8.2. Get Radio CRC mode OK.
  417. //CRC status in LoRaWAN._crcStatus
  418. digitalWrite(error_led, LOW);
  419. }
  420. else
  421. {
  422. //8.2. Get Radio CRC mode error
  423. digitalWrite(error_led, HIGH);
  424. status = 1;
  425. }
  426. //-------------------------------------------------------
  427. Serial.println(" end radioModuleSetup");
  428. return status;
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement