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