Advertisement
Guest User

test_code

a guest
Feb 6th, 2020
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #include "LoRaWan_APP.h"
  2. #include "Arduino.h"
  3.  
  4.  
  5. const char myDevEui[] = { deleted };
  6. const char myAppEui[] = { deleted };
  7. const char myAppKey[] = { deleted };
  8.  
  9. // for OTAA
  10. extern uint8_t DevEui[];
  11. extern uint8_t AppEui[];
  12. extern uint8_t AppKey[];
  13.  
  14. /*
  15. * set LoraWan_RGB to Active,the RGB active in loraWan
  16. * RGB red means sending;
  17. * RGB purple means joined done;
  18. * RGB blue means RxWindow1;
  19. * RGB yellow means RxWindow2;
  20. * RGB green means received done;
  21. */
  22.  
  23. /*LoraWan Class*/
  24. DeviceClass_t CLASS=LORAWAN_CLASS;
  25. /*OTAA or ABP*/
  26. bool OVER_THE_AIR_ACTIVATION = LORAWAN_NETMODE;
  27. /*ADR enable*/
  28. bool LORAWAN_ADR_ON = LORAWAN_ADR;
  29. /* set LORAWAN_Net_Reserve ON, the node could save the network info to flash, when node reset not need to join again */
  30. bool KeepNet = LORAWAN_Net_Reserve;
  31. /*LoraWan REGION*/
  32. LoRaMacRegion_t REGION = ACTIVE_REGION;
  33.  
  34. /* Indicates if the node is sending confirmed or unconfirmed messages */
  35. bool IsTxConfirmed = false;
  36. /*!
  37. * Number of trials to transmit the frame, if the LoRaMAC layer did not
  38. * receive an acknowledgment. The MAC performs a datarate adaptation,
  39. * according to the LoRaWAN Specification V1.0.2, chapter 18.4, according
  40. * to the following table:
  41. *
  42. * Transmission nb | Data Rate
  43. * ----------------|-----------
  44. * 1 (first) | DR
  45. * 2 | DR
  46. * 3 | max(DR-1,0)
  47. * 4 | max(DR-1,0)
  48. * 5 | max(DR-2,0)
  49. * 6 | max(DR-2,0)
  50. * 7 | max(DR-3,0)
  51. * 8 | max(DR-3,0)
  52. *
  53. * Note, that if NbTrials is set to 1 or 2, the MAC will not decrease
  54. * the datarate, in case the LoRaMAC layer did not receive an acknowledgment
  55. */
  56. uint8_t ConfirmedNbTrials = 8;
  57.  
  58. /* Application port */
  59. uint8_t AppPort = 2;
  60.  
  61. /*the application data transmission duty cycle. value in [ms].*/
  62. uint32_t APP_TX_DUTYCYCLE = 20000;
  63.  
  64. /* Prepares the payload of the frame */
  65. static void PrepareTxFrame( uint8_t port )
  66. {
  67. AppDataSize = 4;//AppDataSize max value is 64
  68. AppData[0] = 0x00;
  69. AppData[1] = 0x01;
  70. AppData[2] = 0x02;
  71. AppData[3] = 0x03;
  72. }
  73.  
  74. //downlink data handle function example
  75. void DownLinkDataHandle(McpsIndication_t *mcpsIndication)
  76. {
  77. Serial.println("DATA RECEIVED");
  78. Serial.printf("+REV DATA:%s,RXSIZE %d,PORT %d\r\n",mcpsIndication->RxSlot?"RXWIN2":"RXWIN1",mcpsIndication->BufferSize,mcpsIndication->Port);
  79. Serial.print("+REV DATA:");
  80. for(uint8_t i=0;i<mcpsIndication->BufferSize;i++)
  81. {
  82. Serial.printf("%02X",mcpsIndication->Buffer[i]);
  83. }
  84. Serial.println();
  85. uint32_t color=mcpsIndication->Buffer[0]<<16|mcpsIndication->Buffer[1]<<8|mcpsIndication->Buffer[2];
  86. #if(LoraWan_RGB==1)
  87. RGB_ON(color,5000);
  88. RGB_OFF();
  89. #endif
  90. }
  91.  
  92.  
  93. void setup() {
  94.  
  95. // for OTAA
  96. memcpy(DevEui, myDevEui, sizeof(myDevEui));
  97. memcpy(AppEui, myAppEui, sizeof(myAppEui));
  98. memcpy(AppKey, myAppKey, sizeof(myAppKey));
  99.  
  100.  
  101. BoardInitMcu();
  102. Serial.begin(115200);
  103. #if(AT_SUPPORT)
  104. Enable_AT();
  105. #endif
  106. DeviceState = DEVICE_STATE_INIT;
  107. LoRaWAN.Ifskipjoin();
  108. }
  109.  
  110. void loop()
  111. {
  112. switch( DeviceState )
  113. {
  114. case DEVICE_STATE_INIT:
  115. {
  116. #if(AT_SUPPORT)
  117. getDevParam();
  118. #endif
  119. printDevParam();
  120. Serial.printf("LoRaWan Class%X start! \r\n",CLASS+10);
  121. LoRaWAN.Init(CLASS,REGION);
  122. DeviceState = DEVICE_STATE_JOIN;
  123. break;
  124. }
  125. case DEVICE_STATE_JOIN:
  126. {
  127. Serial.println("DEVICE_STATE_JOIN");
  128. LoRaWAN.Join();
  129. break;
  130. }
  131. case DEVICE_STATE_SEND:
  132. {
  133. Serial.println("DEVICE_STATE_SEND");
  134. PrepareTxFrame( AppPort );
  135. LoRaWAN.Send();
  136. DeviceState = DEVICE_STATE_CYCLE;
  137. break;
  138. }
  139. case DEVICE_STATE_CYCLE:
  140. {
  141. Serial.println("DEVICE_STATE_CYCLE");
  142. // Schedule next packet transmission
  143. TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( 0, APP_TX_DUTYCYCLE_RND );
  144. LoRaWAN.Cycle(TxDutyCycleTime);
  145. DeviceState = DEVICE_STATE_SLEEP;
  146. break;
  147. }
  148. case DEVICE_STATE_SLEEP:
  149. {
  150. //Serial.println("DEVICE_STATE_CYCLE");
  151. LoRaWAN.Sleep();
  152. break;
  153. }
  154. default:
  155. {
  156. DeviceState = DEVICE_STATE_INIT;
  157. break;
  158. }
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement