Facebook
From Unique Bat, 2 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 125
  1. #include "LoRaWan_APP.h"
  2. #include "Arduino.h"
  3.  
  4. /*
  5.  * set LoraWan_RGB to Active,the RGB active in loraWan
  6.  * RGB red means sending;
  7.  * RGB purple means joined done;
  8.  * RGB blue means RxWindow1;
  9.  * RGB yellow means RxWindow2;
  10.  * RGB green means received done;
  11.  */
  12.  
  13. /* OTAA para*/
  14. uint8_t devEui[] = { 0x22, 0x32, 0x33, 0x00, 0x00, 0x88, 0x88, 0x02 };
  15. uint8_t appEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16. uint8_t appKey[] = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0x01 };
  17.  
  18. /* ABP para*/
  19. uint8_t nwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda,0x85 };
  20. uint8_t appSKey[] = { 0xd7, 0x2c, 0x78, 0x75, 0x8c, 0xdc, 0xca, 0xbf, 0x55, 0xee, 0x4a, 0x77, 0x8d, 0x16, 0xef,0x67 };
  21. uint32_t devAddr =  ( uint32_t )0x007e6ae1;
  22.  
  23. /*LoraWan channelsmask, default channels 0-7*/
  24. uint16_t userChannelsMask[6]={ 0xFF00,0x0000,0x0000,0x0000,0x0000,0x0000 };
  25.  
  26. /*LoraWan region, select in arduino IDE tools*/
  27. LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;
  28.  
  29. /*LoraWan Class, Class A and Class C are supported*/
  30. DeviceClass_t  loraWanClass = LORAWAN_CLASS;
  31.  
  32. /*the application data transmission duty cycle.  value in [ms].*/
  33. uint32_t appTxDutyCycle = 15000;
  34.  
  35. /*OTAA or ABP*/
  36. bool overTheAirActivation = LORAWAN_NETMODE;
  37.  
  38. /*ADR enable*/
  39. bool loraWanAdr = LORAWAN_ADR;
  40.  
  41. /* set LORAWAN_Net_Reserve ON, the node could save the network info to flash, when node reset not need to join again */
  42. bool keepNet = LORAWAN_NET_RESERVE;
  43.  
  44. /* Indicates if the node is sending confirmed or unconfirmed messages */
  45. bool isTxConfirmed = LORAWAN_UPLINKMODE;
  46.  
  47. /* Application port */
  48. uint8_t appPort = 2;
  49. /*!
  50. * Number of trials to transmit the frame, if the LoRaMAC layer did not
  51. * receive an acknowledgment. The MAC performs a datarate adaptation,
  52. * according to the LoRaWAN Specification V1.0.2, chapter 18.4, according
  53. * to the following table:
  54. *
  55. * Transmission nb | Data Rate
  56. * ----------------|-----------
  57. * 1 (first)       | DR
  58. * 2               | DR
  59. * 3               | max(DR-1,0)
  60. * 4               | max(DR-1,0)
  61. * 5               | max(DR-2,0)
  62. * 6               | max(DR-2,0)
  63. * 7               | max(DR-3,0)
  64. * 8               | max(DR-3,0)
  65. *
  66. * Note, that if NbTrials is set to 1 or 2, the MAC will not decrease
  67. * the datarate, in case the LoRaMAC layer did not receive an acknowledgment
  68. */
  69. uint8_t confirmedNbTrials = 4;
  70.  
  71. /* Prepares the payload of the frame */
  72. static void prepareTxFrame( uint8_t port )
  73. {
  74.         /*appData size is LORAWAN_APP_DATA_MAX_SIZE which is defined in "commissioning.h".
  75.         *appDataSize max value is LORAWAN_APP_DATA_MAX_SIZE.
  76.         *if enabled AT, don't modify LORAWAN_APP_DATA_MAX_SIZE, it may cause system hanging or failure.
  77.         *if disabled AT, LORAWAN_APP_DATA_MAX_SIZE can be modified, the max value is reference to lorawan region and SF.
  78.         *for example, if use REGION_CN470,
  79.         *the max value for different DR can be found in MaxPayloadOfDatarateCN470 refer to DataratesCN470 and BandwidthsCN470 in "RegionCN470.h".
  80.         */
  81.     appDataSize = 4;
  82.     appData[0] = 0x00;
  83.     appData[1] = 0x01;
  84.     appData[2] = 0x02;
  85.     appData[3] = 0x03;
  86. }
  87.  
  88. //downlink data handle function example
  89. void downLinkDataHandle(McpsIndication_t *mcpsIndication)
  90. {
  91.   Serial.printf("+REV DATA:%s,RXSIZE %d,PORT %d\r\n",mcpsIndication->RxSlot?"RXWIN2":"RXWIN1",mcpsIndication->BufferSize,mcpsIndication->Port);
  92.   Serial.print("+REV DATA:");
  93.   for(uint8_t i=0;i<mcpsIndication->BufferSize;i++)
  94.   {
  95.     Serial.printf("%02X",mcpsIndication->Buffer[i]);
  96.   }
  97.   Serial.println();
  98.   uint32_t color=mcpsIndication->Buffer[0]<<16|mcpsIndication->Buffer[1]<<8|mcpsIndication->Buffer[2];
  99. #if(LoraWan_RGB==1)
  100.   turnOnRGB(color,5000);
  101.   turnOffRGB();
  102. #endif
  103. }
  104.  
  105. void setup() {
  106.         Serial.begin(115200);
  107. #if(AT_SUPPORT)
  108.         enableAt();
  109. #endif
  110.         deviceState = DEVICE_STATE_INIT;
  111.         LoRaWAN.ifskipjoin();
  112. }
  113.  
  114. void loop()
  115. {
  116.         switch( deviceState )
  117.         {
  118.                 case DEVICE_STATE_INIT:
  119.                 {
  120. #if(LORAWAN_DEVEUI_AUTO)
  121.                         LoRaWAN.generateDeveuiByChipID();
  122. #endif
  123. #if(AT_SUPPORT)
  124.                         getDevParam();
  125. #endif
  126.                         printDevParam();
  127.                         LoRaWAN.init(loraWanClass,loraWanRegion);
  128.                         deviceState = DEVICE_STATE_JOIN;
  129.                         break;
  130.                 }
  131.                 case DEVICE_STATE_JOIN:
  132.                 {
  133.                         LoRaWAN.join();
  134.                         break;
  135.                 }
  136.                 case DEVICE_STATE_SEND:
  137.                 {
  138.                         prepareTxFrame( appPort );
  139.                         LoRaWAN.send();
  140.                         deviceState = DEVICE_STATE_CYCLE;
  141.                         break;
  142.                 }
  143.                 case DEVICE_STATE_CYCLE:
  144.                 {
  145.                         // Schedule next packet transmission
  146.                         txDutyCycleTime = appTxDutyCycle + randr( 0, APP_TX_DUTYCYCLE_RND );
  147.                         LoRaWAN.cycle(txDutyCycleTime);
  148.                         deviceState = DEVICE_STATE_SLEEP;
  149.                         break;
  150.                 }
  151.                 case DEVICE_STATE_SLEEP:
  152.                 {
  153.                         LoRaWAN.sleep();
  154.                         break;
  155.                 }
  156.                 default:
  157.                 {
  158.                         deviceState = DEVICE_STATE_INIT;
  159.                         break;
  160.                 }
  161.         }
  162. }
  163.