|
| 1 | +/* |
| 2 | + * HelTec Automation(TM) LoRaWAN 1.0.2 OTAA example use OTAA, CLASS A |
| 3 | + * |
| 4 | + * Function summary: |
| 5 | + * |
| 6 | + * - use internal RTC(150KHz); |
| 7 | + * |
| 8 | + * - Include stop mode and deep sleep mode; |
| 9 | + * |
| 10 | + * - 15S data send cycle; |
| 11 | + * |
| 12 | + * - Informations output via serial(115200); |
| 13 | + * |
| 14 | + * - Only ESP32 + LoRa series boards can use this library, need a license |
| 15 | + * to make the code run(check you license here: http://www.heltec.cn/search ); |
| 16 | + * |
| 17 | + * You can change some definition in "Commissioning.h" and "LoRaMac-definitions.h" |
| 18 | + * |
| 19 | + * HelTec AutoMation, Chengdu, China. |
| 20 | + * 成都惠利特自动化科技有限公司 |
| 21 | + * https://heltec.org |
| 22 | + |
| 23 | + * |
| 24 | + *this project also release in GitHub: |
| 25 | + *https://github.com/HelTecAutomation/ESP32_LoRaWAN |
| 26 | +*/ |
| 27 | + |
| 28 | +#include <ESP32_LoRaWAN.h> |
| 29 | +#include "Arduino.h" |
| 30 | + |
| 31 | +#define INT_PIN 12 |
| 32 | +/*license for Heltec ESP32 LoRaWan, quary your ChipID relevant license: http://resource.heltec.cn/search */ |
| 33 | +uint32_t license[4] = {0x0C6EE449, 0x446722FE, 0x6758DEB6, 0x0C4A735E}; |
| 34 | +/* OTAA para*/ |
| 35 | +uint8_t DevEui[] = { 0x22, 0x32, 0x33, 0x00, 0x00, 0x88, 0x88, 0x02 }; |
| 36 | +uint8_t AppEui[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x02, 0xB1, 0x8A }; |
| 37 | +uint8_t AppKey[] = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0x01 }; |
| 38 | + |
| 39 | +/* ABP para*/ |
| 40 | +uint8_t NwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda,0x85 }; |
| 41 | +uint8_t AppSKey[] = { 0xd7, 0x2c, 0x78, 0x75, 0x8c, 0xdc, 0xca, 0xbf, 0x55, 0xee, 0x4a, 0x77, 0x8d, 0x16, 0xef,0x67 }; |
| 42 | +uint32_t DevAddr = ( uint32_t )0x007e6ae1; |
| 43 | + |
| 44 | +/*LoraWan channelsmask, default channels 0-7*/ |
| 45 | +uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 }; |
| 46 | + |
| 47 | +/*LoraWan Class, Class A and Class C are supported*/ |
| 48 | +DeviceClass_t loraWanClass = CLASS_C; |
| 49 | + |
| 50 | +/*the application data transmission duty cycle. value in [ms].*/ |
| 51 | +uint32_t appTxDutyCycle = 3600*24*1000; |
| 52 | + |
| 53 | +/*OTAA or ABP*/ |
| 54 | +bool overTheAirActivation = true; |
| 55 | + |
| 56 | +/*ADR enable*/ |
| 57 | +bool loraWanAdr = true; |
| 58 | + |
| 59 | +/* Indicates if the node is sending confirmed or unconfirmed messages */ |
| 60 | +bool isTxConfirmed = true; |
| 61 | + |
| 62 | +/* Application port */ |
| 63 | +uint8_t appPort = 2; |
| 64 | + |
| 65 | +/*! |
| 66 | +* Number of trials to transmit the frame, if the LoRaMAC layer did not |
| 67 | +* receive an acknowledgment. The MAC performs a datarate adaptation, |
| 68 | +* according to the LoRaWAN Specification V1.0.2, chapter 18.4, according |
| 69 | +* to the following table: |
| 70 | +* |
| 71 | +* Transmission nb | Data Rate |
| 72 | +* ----------------|----------- |
| 73 | +* 1 (first) | DR |
| 74 | +* 2 | DR |
| 75 | +* 3 | max(DR-1,0) |
| 76 | +* 4 | max(DR-1,0) |
| 77 | +* 5 | max(DR-2,0) |
| 78 | +* 6 | max(DR-2,0) |
| 79 | +* 7 | max(DR-3,0) |
| 80 | +* 8 | max(DR-3,0) |
| 81 | +* |
| 82 | +* Note, that if NbTrials is set to 1 or 2, the MAC will not decrease |
| 83 | +* the datarate, in case the LoRaMAC layer did not receive an acknowledgment |
| 84 | +*/ |
| 85 | +uint8_t confirmedNbTrials = 1; |
| 86 | + |
| 87 | +/*LoraWan debug level, select in arduino IDE tools. |
| 88 | +* None : print basic info. |
| 89 | +* Freq : print Tx and Rx freq, DR info. |
| 90 | +* Freq && DIO : print Tx and Rx freq, DR, DIO0 interrupt and DIO1 interrupt info. |
| 91 | +* Freq && DIO && PW: print Tx and Rx freq, DR, DIO0 interrupt, DIO1 interrupt, MCU sleep and MCU wake info. |
| 92 | +*/ |
| 93 | +uint8_t debugLevel = LoRaWAN_DEBUG_LEVEL; |
| 94 | + |
| 95 | +/*LoraWan region, select in arduino IDE tools*/ |
| 96 | +LoRaMacRegion_t loraWanRegion = ACTIVE_REGION; |
| 97 | + |
| 98 | + |
| 99 | +static void prepareTxFrame( uint8_t port ) |
| 100 | +{ |
| 101 | + appDataSize = 4; |
| 102 | + appData[0] = 0x00; |
| 103 | + appData[1] = 0x01; |
| 104 | + appData[2] = 0x02; |
| 105 | + appData[3] = 0x03; |
| 106 | +} |
| 107 | + |
| 108 | +void keyDown() |
| 109 | +{ |
| 110 | + delay(10); |
| 111 | + if(digitalRead(INT_PIN)==1 && IsLoRaMacNetworkJoined) |
| 112 | + { |
| 113 | + deviceState = DEVICE_STATE_SEND; |
| 114 | + } |
| 115 | +} |
| 116 | +// Add your initialization code here |
| 117 | +void setup() |
| 118 | +{ |
| 119 | + if(mcuStarted==0) |
| 120 | + { |
| 121 | + LoRaWAN.displayMcuInit(); |
| 122 | + } |
| 123 | + Serial.begin(115200); |
| 124 | + while (!Serial); |
| 125 | + SPI.begin(SCK,MISO,MOSI,SS); |
| 126 | + Mcu.init(SS,RST_LoRa,DIO0,DIO1,license); |
| 127 | + delay(100); |
| 128 | + pinMode(INT_PIN,INPUT_PULLUP); |
| 129 | + attachInterrupt(INT_PIN,keyDown,RISING); |
| 130 | + deviceState = DEVICE_STATE_INIT; |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +// The loop function is called in an endless loop |
| 135 | +void loop() |
| 136 | +{ |
| 137 | + switch( deviceState ) |
| 138 | + { |
| 139 | + case DEVICE_STATE_INIT: |
| 140 | + { |
| 141 | + LoRaWAN.init(loraWanClass,loraWanRegion); |
| 142 | + break; |
| 143 | + } |
| 144 | + case DEVICE_STATE_JOIN: |
| 145 | + { |
| 146 | + LoRaWAN.displayJoining(); |
| 147 | + LoRaWAN.join(); |
| 148 | + break; |
| 149 | + } |
| 150 | + case DEVICE_STATE_SEND: |
| 151 | + { |
| 152 | + LoRaWAN.displaySending(); |
| 153 | + prepareTxFrame( appPort ); |
| 154 | + LoRaWAN.send(loraWanClass); |
| 155 | + deviceState = DEVICE_STATE_CYCLE; |
| 156 | + break; |
| 157 | + } |
| 158 | + case DEVICE_STATE_CYCLE: |
| 159 | + { |
| 160 | + // Schedule next packet transmission |
| 161 | + txDutyCycleTime = appTxDutyCycle + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); |
| 162 | + LoRaWAN.cycle(txDutyCycleTime); |
| 163 | + deviceState = DEVICE_STATE_SLEEP; |
| 164 | + break; |
| 165 | + } |
| 166 | + case DEVICE_STATE_SLEEP: |
| 167 | + { |
| 168 | + LoRaWAN.displayAck(); |
| 169 | + LoRaWAN.sleep(loraWanClass,debugLevel); |
| 170 | + break; |
| 171 | + } |
| 172 | + default: |
| 173 | + { |
| 174 | + deviceState = DEVICE_STATE_INIT; |
| 175 | + break; |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments