├── CubeCellTTNMapper.ino ├── CubeCellTTNMapper_decoder.js └── LICENSE /CubeCellTTNMapper.ino: -------------------------------------------------------------------------------- 1 | #include "LoRaWan_APP.h" 2 | #include "Arduino.h" 3 | #include "GPS_Air530.h" 4 | 5 | #define POI_BUTTON (GPIO7) 6 | 7 | uint8_t devEui[] = { 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 8 | uint8_t appEui[] = { 0x00, 0x00, 0x00, 0x00, 0xD0, 0x00, 0x00, 0x00 }; 9 | uint8_t appKey[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 10 | 11 | uint32_t devAddr = ( uint32_t)0x26000000; 12 | uint8_t nwkSKey[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 13 | uint8_t appSKey[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 14 | 15 | extern bool IsLoRaMacNetworkJoined; 16 | 17 | /*LoraWan channelsmask*/ 18 | uint16_t userChannelsMask[6] = { 0xFF00, 0x0000, 0x0000, 0x0000, 0x0002, 0x0000 }; // Subchannel 2 for AU915 19 | 20 | /* 21 | set LoraWan_RGB to Active,the RGB active in loraWan 22 | RGB red means sending; 23 | RGB purple means joined done; 24 | RGB blue means RxWindow1; 25 | RGB yellow means RxWindow2; 26 | RGB green means received done; 27 | */ 28 | 29 | /*LoraWan Class*/ 30 | DeviceClass_t loraWanClass = LORAWAN_CLASS; 31 | /*OTAA or ABP*/ 32 | bool overTheAirActivation = LORAWAN_NETMODE; 33 | /*ADR enable*/ 34 | bool loraWanAdr = LORAWAN_ADR; 35 | /* set LORAWAN_Net_Reserve ON, the node could save the network info to flash, when node reset not need to join again */ 36 | bool keepNet = LORAWAN_NET_RESERVE; 37 | /*LoraWan REGION*/ 38 | LoRaMacRegion_t loraWanRegion = ACTIVE_REGION; 39 | 40 | /* Indicates if the node is sending confirmed or unconfirmed messages */ 41 | bool isTxConfirmed = false; //LORAWAN_UPLINKMODE; 42 | /*! 43 | Number of trials to transmit the frame, if the LoRaMAC layer did not 44 | receive an acknowledgment. The MAC performs a datarate adaptation, 45 | according to the LoRaWAN Specification V1.0.2, chapter 18.4, according 46 | to the following table: 47 | 48 | Transmission nb | Data Rate 49 | ----------------|----------- 50 | 1 (first) | DR 51 | 2 | DR 52 | 3 | max(DR-1,0) 53 | 4 | max(DR-1,0) 54 | 5 | max(DR-2,0) 55 | 6 | max(DR-2,0) 56 | 7 | max(DR-3,0) 57 | 8 | max(DR-3,0) 58 | 59 | Note, that if NbTrials is set to 1 or 2, the MAC will not decrease 60 | the datarate, in case the LoRaMAC layer did not receive an acknowledgment 61 | */ 62 | uint8_t confirmedNbTrials = 8; 63 | 64 | /* Application port */ 65 | uint8_t appPort = 2; 66 | 67 | /*the application data transmission duty cycle. value in [ms].*/ 68 | uint32_t appTxDutyCycle = 1000 * 20; 69 | 70 | uint32_t LatitudeBinary, LongitudeBinary; 71 | uint16_t altitudeGps; 72 | uint8_t hdopGps; 73 | bool poi; // point of interest 74 | bool DRfast; // slow or fast DR. Flip-flip between them. 75 | 76 | #define DR_SLOW DR_2 // SF10 77 | #define DR_FAST DR_5 // SF7 78 | 79 | void poiButtonPress() 80 | { 81 | poi = true; 82 | } 83 | 84 | // Set the Datarate (interrelated to SF. DR_0 = SF12, mostly. DR_2 = SF10 for AU915, DR_5 is SF7) 85 | void setDR(int8_t datarate) { 86 | MibRequestConfirm_t mibSet; 87 | 88 | mibSet.Type = MIB_CHANNELS_DATARATE; 89 | mibSet.Param.ChannelsDatarate = datarate; 90 | LoRaMacMibSetRequestConfirm( &mibSet ); 91 | } 92 | 93 | /* Prepares the payload of the frame */ 94 | static void prepareTxFrame( uint8_t port ) 95 | { 96 | appDataSize = 0;//AppDataSize max value is 64 97 | 98 | Serial.printf("GPS data is %ds old - %d sats\n", Air530.location.age()/1000, Air530.satellites.value()); 99 | 100 | if (Air530.location.age() < 1000) { 101 | 102 | appDataSize = 10; 103 | 104 | LatitudeBinary = ((Air530.location.lat() + 90) / 180) * 16777215; 105 | LongitudeBinary = ((Air530.location.lng() + 180) / 360) * 16777215; 106 | 107 | appData[0] = ( LatitudeBinary >> 16 ) & 0xFF; 108 | appData[1] = ( LatitudeBinary >> 8 ) & 0xFF; 109 | appData[2] = LatitudeBinary & 0xFF; 110 | 111 | appData[3] = ( LongitudeBinary >> 16 ) & 0xFF; 112 | appData[4] = ( LongitudeBinary >> 8 ) & 0xFF; 113 | appData[5] = LongitudeBinary & 0xFF; 114 | 115 | altitudeGps = Air530.altitude.meters(); 116 | appData[6] = ( altitudeGps >> 8 ) & 0xFF; 117 | appData[7] = altitudeGps & 0xFF; 118 | 119 | hdopGps = Air530.hdop.hdop() * 10; 120 | appData[8] = hdopGps & 0xFF; 121 | 122 | appData[9] = Air530.satellites.value(); // < 128 sats. Could probably steal another bit. 123 | appData[9] &= ~(1 << 7); 124 | if (poi) { 125 | appData[9] |= 1 << 7; 126 | poi = false; 127 | Serial.println("POI"); 128 | } 129 | } else { 130 | Serial.print("No GPS data found:"); 131 | Serial.printf("%04d-%02d-%02dT%02d:%02d:%02d\n", Air530.date.year(), Air530.date.month(), Air530.date.day(), 132 | Air530.time.hour(), Air530.time.minute(), Air530.time.second()); 133 | 134 | } 135 | } 136 | 137 | void setup() { 138 | pinMode(POI_BUTTON,INPUT_PULLUP); 139 | attachInterrupt(POI_BUTTON, poiButtonPress, FALLING); 140 | 141 | boardInitMcu(); 142 | 143 | Serial.begin(115200); 144 | #if(AT_SUPPORT) 145 | enableAt(); 146 | #endif 147 | //LoRaWAN.displayMcuInit(); 148 | deviceState = DEVICE_STATE_INIT; 149 | LoRaWAN.ifskipjoin(); 150 | Air530.setmode(MODE_GPS_GLONASS_BEIDOU); 151 | Air530.setNMEA(NMEA_RMC); 152 | Air530.begin(); 153 | poi = false; 154 | } 155 | 156 | void get_gps() { 157 | if (Air530.available() > 0) 158 | { 159 | Air530.encode(Air530.read()); 160 | } 161 | } 162 | 163 | void loop() 164 | { 165 | get_gps(); // get something useful 166 | switch ( deviceState ) 167 | { 168 | case DEVICE_STATE_INIT: 169 | { 170 | #if(AT_SUPPORT) 171 | getDevParam(); 172 | #endif 173 | printDevParam(); 174 | Serial.printf("LoRaWan Class%X start! \r\n", loraWanClass + 10); 175 | LoRaWAN.init(loraWanClass, loraWanRegion); 176 | deviceState = DEVICE_STATE_JOIN; 177 | break; 178 | } 179 | case DEVICE_STATE_JOIN: 180 | { 181 | //setTTNBand(); 182 | LoRaWAN.join(); 183 | break; 184 | } 185 | case DEVICE_STATE_SEND: 186 | { 187 | prepareTxFrame( appPort ); 188 | if (appDataSize > 0) { 189 | if(DRfast) { 190 | Serial.println("Setting DRfast"); 191 | setDR(DR_FAST); 192 | } else { 193 | Serial.println("Setting DRslow"); 194 | setDR(DR_SLOW); 195 | } 196 | DRfast = !DRfast; 197 | LoRaWAN.send(); 198 | } 199 | deviceState = DEVICE_STATE_CYCLE; 200 | break; 201 | } 202 | case DEVICE_STATE_CYCLE: 203 | { 204 | // Schedule next packet transmission 205 | txDutyCycleTime = appTxDutyCycle + randr( 0, APP_TX_DUTYCYCLE_RND ); 206 | LoRaWAN.cycle(txDutyCycleTime); 207 | deviceState = DEVICE_STATE_SLEEP; 208 | break; 209 | } 210 | case DEVICE_STATE_SLEEP: 211 | { 212 | //LoRaWAN.sleep(); 213 | Radio.IrqProcess( ); 214 | break; 215 | } 216 | default: 217 | { 218 | deviceState = DEVICE_STATE_INIT; 219 | break; 220 | } 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /CubeCellTTNMapper_decoder.js: -------------------------------------------------------------------------------- 1 | function Decoder(bytes, port) { 2 | var decoded = {}; 3 | 4 | // Location reports are sent on appPort = 2 5 | if (port === 2) { 6 | decoded.latitude = ((bytes[0]<<16)>>>0) + ((bytes[1]<<8)>>>0) + bytes[2]; 7 | decoded.latitude = (decoded.latitude / 16777215.0 * 180) - 90; 8 | decoded.longitude = ((bytes[3]<<16)>>>0) + ((bytes[4]<<8)>>>0) + bytes[5]; 9 | decoded.longitude = (decoded.longitude / 16777215.0 * 360) - 180; 10 | var altValue = ((bytes[6]<<8)>>>0) + bytes[7]; 11 | var sign = bytes[6] & (1 << 7); 12 | if(sign) 13 | { 14 | decoded.altitude = 0xFFFF0000 | altValue; 15 | } 16 | else 17 | { 18 | decoded.altitude = altValue; 19 | } 20 | decoded.hdop = bytes[8] / 10.0; 21 | decoded.satellites = bytes[9] & 0x7F; 22 | if(bytes[9] & (1 << 7)) 23 | decoded.poi = true; 24 | decoded.heading = -1; 25 | } 26 | return decoded; 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Bruce Fitzsimons 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------