Demonstration TTN OTAA node
Dependencies: BME280 DS1820 libmDot mbed-rtos mbed
This is an example application for the MultiTech mDot and connects to The Things Network using Over The Air Activation (OTAA). It sends data from a Dallas Semiconductors DS18B20 OneWire temperature sensor.
Register a device and generate a random AppKey for the currently used application Id: (You need to use your own device IDs, the ones shown here are examples only)
./ttnctl devices register 0080000000000000 INFO Generating random AppKey... INFO Registered device AppKey=000102030405060708090A0B0C0D0E0F DevEUI=0080000000000000
or to specify the same AppKey for a new device or to reregister the same device again:
./ttnctl devices register 0080000000000000 000102030405060708090A0B0C0D0E0F
./ttnctl devices info 0080000000000000 Dynamic device:
AppEUI: 70B3D50000000000 {0x70, 0xB3, 0xD5, 0x00, 0x00, 0x00, 0x00, 0x00}
DevEUI: 0080000000000000 {0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
AppKey: 000102030405060708090A0B0C0D0E0F {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}
Copy the AppEUI and AppKey values provided in hex array notation above to the AppEUI and AppKey parameters below.
main.cpp@15:8a0ebf59b8bb, 2016-05-11 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Wed May 11 06:26:11 2016 +0000
- Revision:
- 15:8a0ebf59b8bb
- Parent:
- 13:5369ba22389a
updated comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 13:5369ba22389a | 1 | /** mDot_TTN_Node - Simple mDot temperature sensor using Dallas Semiconductors DS18B20 OneWire temperature sensor. |
SomeRandomBloke | 13:5369ba22389a | 2 | * It used the AUTO_OTA join mode with parameters for The Things Network. |
SomeRandomBloke | 11:38ce10209eff | 3 | * |
SomeRandomBloke | 2:9db840d12557 | 4 | * |
SomeRandomBloke | 0:5a0b43f3b143 | 5 | * Uses MultiTech mDot developer board http://www.multitech.com/models/94558010LF |
SomeRandomBloke | 0:5a0b43f3b143 | 6 | * Requires a MultiTech MultiConnect Conduit http://www.multitech.com/models/94557203LF |
SomeRandomBloke | 5:48eb9245a914 | 7 | * http://www.multitech.net/developer/software/lora/conduit-mlinux-convert-to-basic-packet-forwarder/ |
SomeRandomBloke | 5:48eb9245a914 | 8 | * http://forum.thethingsnetwork.org/t/setting-up-multitech-conduit-gateway-for-ttn/216/35 |
SomeRandomBloke | 0:5a0b43f3b143 | 9 | * |
SomeRandomBloke | 13:5369ba22389a | 10 | * Register a device and generate a random AppKey for the currently used application Id: |
SomeRandomBloke | 15:8a0ebf59b8bb | 11 | * (You need to use your own device IDs, the ones shown here are examples only) |
SomeRandomBloke | 13:5369ba22389a | 12 | * |
SomeRandomBloke | 15:8a0ebf59b8bb | 13 | *./ttnctl devices register 0080000000000000 |
SomeRandomBloke | 13:5369ba22389a | 14 | * INFO Generating random AppKey... |
SomeRandomBloke | 15:8a0ebf59b8bb | 15 | * INFO Registered device AppKey=000102030405060708090A0B0C0D0E0F DevEUI=0080000000000000 |
SomeRandomBloke | 13:5369ba22389a | 16 | * |
SomeRandomBloke | 13:5369ba22389a | 17 | * or to specify the same AppKey for a new device or to reregister the same device again: |
SomeRandomBloke | 13:5369ba22389a | 18 | * |
SomeRandomBloke | 15:8a0ebf59b8bb | 19 | * ./ttnctl devices register 0080000000000000 000102030405060708090A0B0C0D0E0F |
SomeRandomBloke | 13:5369ba22389a | 20 | * |
SomeRandomBloke | 15:8a0ebf59b8bb | 21 | * ./ttnctl devices info 0080000000000000 |
SomeRandomBloke | 13:5369ba22389a | 22 | * Dynamic device: |
SomeRandomBloke | 13:5369ba22389a | 23 | * |
SomeRandomBloke | 15:8a0ebf59b8bb | 24 | * AppEUI: 70B3D50000000000 |
SomeRandomBloke | 15:8a0ebf59b8bb | 25 | * {0x70, 0xB3, 0xD5, 0x00, 0x00, 0x00, 0x00, 0x00} |
SomeRandomBloke | 13:5369ba22389a | 26 | * |
SomeRandomBloke | 15:8a0ebf59b8bb | 27 | * DevEUI: 0080000000000000 |
SomeRandomBloke | 15:8a0ebf59b8bb | 28 | * {0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} |
SomeRandomBloke | 13:5369ba22389a | 29 | * |
SomeRandomBloke | 15:8a0ebf59b8bb | 30 | * AppKey: 000102030405060708090A0B0C0D0E0F |
SomeRandomBloke | 15:8a0ebf59b8bb | 31 | * {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F} |
SomeRandomBloke | 13:5369ba22389a | 32 | * |
SomeRandomBloke | 13:5369ba22389a | 33 | * |
SomeRandomBloke | 13:5369ba22389a | 34 | * Copy the AppEUI and AppKey values provided in hex array notation above to the AppEUI and AppKey parameters below. |
SomeRandomBloke | 13:5369ba22389a | 35 | * |
SomeRandomBloke | 0:5a0b43f3b143 | 36 | */ |
SomeRandomBloke | 0:5a0b43f3b143 | 37 | |
SomeRandomBloke | 0:5a0b43f3b143 | 38 | #include "mbed.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 39 | #include "DS1820.h" |
SomeRandomBloke | 13:5369ba22389a | 40 | //#include "BME280.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 41 | #include "mDot.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 42 | #include "MTSLog.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 43 | #include "MTSText.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 44 | #include <string> |
SomeRandomBloke | 0:5a0b43f3b143 | 45 | #include <vector> |
SomeRandomBloke | 0:5a0b43f3b143 | 46 | |
SomeRandomBloke | 0:5a0b43f3b143 | 47 | using namespace mts; |
SomeRandomBloke | 0:5a0b43f3b143 | 48 | |
SomeRandomBloke | 4:f649ab1b61d1 | 49 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
SomeRandomBloke | 4:f649ab1b61d1 | 50 | #define MAX(a,b) (((a)>(b))?(a):(b)) |
SomeRandomBloke | 4:f649ab1b61d1 | 51 | |
SomeRandomBloke | 13:5369ba22389a | 52 | // AppEUI |
SomeRandomBloke | 15:8a0ebf59b8bb | 53 | uint8_t AppEUI[8]={0x70, 0xB3, 0xD5, 0x00, 0x00, 0x00, 0x00, 0x00}; |
SomeRandomBloke | 13:5369ba22389a | 54 | // AppKey |
SomeRandomBloke | 15:8a0ebf59b8bb | 55 | uint8_t AppKey[16]={0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; |
SomeRandomBloke | 8:8070e9d660e4 | 56 | |
SomeRandomBloke | 8:8070e9d660e4 | 57 | // Some defines for the LoRa configuration |
SomeRandomBloke | 13:5369ba22389a | 58 | /* |
SomeRandomBloke | 13:5369ba22389a | 59 | * EU868 Datarates |
SomeRandomBloke | 13:5369ba22389a | 60 | * --------------- |
SomeRandomBloke | 13:5369ba22389a | 61 | * DR0 - SF12BW125 |
SomeRandomBloke | 13:5369ba22389a | 62 | * DR1 - SF11BW125 |
SomeRandomBloke | 13:5369ba22389a | 63 | * DR2 - SF10BW125 |
SomeRandomBloke | 13:5369ba22389a | 64 | * DR3 - SF9BW125 |
SomeRandomBloke | 13:5369ba22389a | 65 | * DR4 - SF8BW125 |
SomeRandomBloke | 13:5369ba22389a | 66 | * DR5 - SF7BW125 |
SomeRandomBloke | 13:5369ba22389a | 67 | * DR6 - SF7BW250 |
SomeRandomBloke | 13:5369ba22389a | 68 | * |
SomeRandomBloke | 13:5369ba22389a | 69 | * US915 Datarates |
SomeRandomBloke | 13:5369ba22389a | 70 | * --------------- |
SomeRandomBloke | 13:5369ba22389a | 71 | * DR0 - SF10BW125 |
SomeRandomBloke | 13:5369ba22389a | 72 | * DR1 - SF9BW125 |
SomeRandomBloke | 13:5369ba22389a | 73 | * DR2 - SF8BW125 |
SomeRandomBloke | 13:5369ba22389a | 74 | * DR3 - SF7BW125 |
SomeRandomBloke | 13:5369ba22389a | 75 | * DR4 - SF8BW500 |
SomeRandomBloke | 13:5369ba22389a | 76 | */ |
SomeRandomBloke | 13:5369ba22389a | 77 | #define LORA_SF mDot::DR5 |
SomeRandomBloke | 8:8070e9d660e4 | 78 | #define LORA_ACK 0 |
SomeRandomBloke | 8:8070e9d660e4 | 79 | #define LORA_TXPOWER 14 |
SomeRandomBloke | 5:48eb9245a914 | 80 | |
SomeRandomBloke | 3:367aa95f9771 | 81 | // Ignoring sub band for EU modules. |
SomeRandomBloke | 13:5369ba22389a | 82 | static uint8_t config_frequency_sub_band = 1; |
SomeRandomBloke | 0:5a0b43f3b143 | 83 | |
SomeRandomBloke | 2:9db840d12557 | 84 | // DS18B20 OneWire pin |
SomeRandomBloke | 8:8070e9d660e4 | 85 | // D13 on Dev Board, pin 18 on mDot, Compatible with Oxford Flood Network PCB temperature sensor. |
SomeRandomBloke | 13:5369ba22389a | 86 | //#define DATA_PIN PA_5 |
SomeRandomBloke | 13:5369ba22389a | 87 | // A1 on Dev Board, pin 19 on mDot |
SomeRandomBloke | 13:5369ba22389a | 88 | #define DATA_PIN PC_13 |
SomeRandomBloke | 0:5a0b43f3b143 | 89 | |
SomeRandomBloke | 3:367aa95f9771 | 90 | // Temperature sensor object |
SomeRandomBloke | 0:5a0b43f3b143 | 91 | DS1820 probe(DATA_PIN); |
SomeRandomBloke | 0:5a0b43f3b143 | 92 | |
SomeRandomBloke | 13:5369ba22389a | 93 | // BME280 Temperature/Humidity/Pressure sensor |
SomeRandomBloke | 13:5369ba22389a | 94 | //BME280 sensor(I2C_SDA, I2C_SCL); |
SomeRandomBloke | 13:5369ba22389a | 95 | |
SomeRandomBloke | 3:367aa95f9771 | 96 | // Serial via USB for debugging only |
SomeRandomBloke | 0:5a0b43f3b143 | 97 | Serial pc(USBTX,USBRX); |
SomeRandomBloke | 0:5a0b43f3b143 | 98 | |
SomeRandomBloke | 0:5a0b43f3b143 | 99 | int main() |
SomeRandomBloke | 0:5a0b43f3b143 | 100 | { |
SomeRandomBloke | 0:5a0b43f3b143 | 101 | int32_t ret; |
SomeRandomBloke | 0:5a0b43f3b143 | 102 | mDot* dot; |
SomeRandomBloke | 0:5a0b43f3b143 | 103 | std::vector<uint8_t> send_data; |
SomeRandomBloke | 0:5a0b43f3b143 | 104 | std::vector<uint8_t> recv_data; |
SomeRandomBloke | 13:5369ba22389a | 105 | std::vector<uint8_t> nwkId; |
SomeRandomBloke | 13:5369ba22389a | 106 | std::vector<uint8_t> nwkKey; |
SomeRandomBloke | 0:5a0b43f3b143 | 107 | |
SomeRandomBloke | 0:5a0b43f3b143 | 108 | float temperature = 0.0; |
SomeRandomBloke | 13:5369ba22389a | 109 | float humidity = 0.0; |
SomeRandomBloke | 13:5369ba22389a | 110 | float pressure = 0.0; |
SomeRandomBloke | 1:45cec6aea002 | 111 | |
SomeRandomBloke | 0:5a0b43f3b143 | 112 | pc.baud(115200); |
SomeRandomBloke | 13:5369ba22389a | 113 | pc.printf("TTN OTAA mDot LoRa Temperature sensor\n\r"); |
SomeRandomBloke | 1:45cec6aea002 | 114 | |
SomeRandomBloke | 0:5a0b43f3b143 | 115 | // get a mDot handle |
SomeRandomBloke | 0:5a0b43f3b143 | 116 | dot = mDot::getInstance(); |
SomeRandomBloke | 0:5a0b43f3b143 | 117 | |
SomeRandomBloke | 13:5369ba22389a | 118 | // dot->setLogLevel(MTSLog::WARNING_LEVEL); |
SomeRandomBloke | 13:5369ba22389a | 119 | dot->setLogLevel(MTSLog::TRACE_LEVEL); |
SomeRandomBloke | 0:5a0b43f3b143 | 120 | |
SomeRandomBloke | 1:45cec6aea002 | 121 | logInfo("Checking Config"); |
SomeRandomBloke | 1:45cec6aea002 | 122 | |
SomeRandomBloke | 13:5369ba22389a | 123 | uint8_t *it = AppEUI; |
SomeRandomBloke | 13:5369ba22389a | 124 | for (uint8_t i = 0; i<8; i++) |
SomeRandomBloke | 13:5369ba22389a | 125 | nwkId.push_back((uint8_t) *it++); |
SomeRandomBloke | 13:5369ba22389a | 126 | |
SomeRandomBloke | 13:5369ba22389a | 127 | it = AppKey; |
SomeRandomBloke | 5:48eb9245a914 | 128 | for (uint8_t i = 0; i<16; i++) |
SomeRandomBloke | 13:5369ba22389a | 129 | nwkKey.push_back((uint8_t) *it++); |
SomeRandomBloke | 1:45cec6aea002 | 130 | |
SomeRandomBloke | 9:086351e54b57 | 131 | logInfo("Resetting Config"); |
SomeRandomBloke | 9:086351e54b57 | 132 | // reset to default config so we know what state we're in |
SomeRandomBloke | 9:086351e54b57 | 133 | dot->resetConfig(); |
SomeRandomBloke | 1:45cec6aea002 | 134 | |
SomeRandomBloke | 5:48eb9245a914 | 135 | // Set Spreading Factor, higher is lower data rate, smaller packets but longer range |
SomeRandomBloke | 5:48eb9245a914 | 136 | // Lower is higher data rate, larger packets and shorter range. |
SomeRandomBloke | 5:48eb9245a914 | 137 | logInfo("Set SF"); |
SomeRandomBloke | 8:8070e9d660e4 | 138 | if((ret = dot->setTxDataRate( LORA_SF )) != mDot::MDOT_OK) { |
SomeRandomBloke | 5:48eb9245a914 | 139 | logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 140 | } |
SomeRandomBloke | 5:48eb9245a914 | 141 | |
SomeRandomBloke | 5:48eb9245a914 | 142 | logInfo("Set TxPower"); |
SomeRandomBloke | 8:8070e9d660e4 | 143 | if((ret = dot->setTxPower( LORA_TXPOWER )) != mDot::MDOT_OK) { |
SomeRandomBloke | 5:48eb9245a914 | 144 | logError("Failed to set Tx Power %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 145 | } |
SomeRandomBloke | 5:48eb9245a914 | 146 | |
SomeRandomBloke | 7:2a704d1a30e1 | 147 | logInfo("Set Public mode"); |
SomeRandomBloke | 7:2a704d1a30e1 | 148 | if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { |
SomeRandomBloke | 7:2a704d1a30e1 | 149 | logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 7:2a704d1a30e1 | 150 | } |
SomeRandomBloke | 7:2a704d1a30e1 | 151 | |
SomeRandomBloke | 13:5369ba22389a | 152 | logInfo("Set AUTO_OTA Join mode"); |
SomeRandomBloke | 13:5369ba22389a | 153 | if((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) { |
SomeRandomBloke | 13:5369ba22389a | 154 | logError("Failed to set AUTO_OTA Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 7:2a704d1a30e1 | 155 | } |
SomeRandomBloke | 7:2a704d1a30e1 | 156 | |
SomeRandomBloke | 5:48eb9245a914 | 157 | logInfo("Set Ack"); |
SomeRandomBloke | 5:48eb9245a914 | 158 | // 1 retries on Ack, 0 to disable |
SomeRandomBloke | 8:8070e9d660e4 | 159 | if((ret = dot->setAck( LORA_ACK)) != mDot::MDOT_OK) { |
SomeRandomBloke | 5:48eb9245a914 | 160 | logError("Failed to set Ack %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 161 | } |
SomeRandomBloke | 3:367aa95f9771 | 162 | |
SomeRandomBloke | 13:5369ba22389a | 163 | // Library ignores the frequency sub band for 868MHz in EU |
SomeRandomBloke | 13:5369ba22389a | 164 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
SomeRandomBloke | 13:5369ba22389a | 165 | logError("Failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 7:2a704d1a30e1 | 166 | } |
SomeRandomBloke | 7:2a704d1a30e1 | 167 | |
SomeRandomBloke | 13:5369ba22389a | 168 | logInfo("Set Network Id"); |
SomeRandomBloke | 13:5369ba22389a | 169 | if ((ret = dot->setNetworkId(nwkId)) != mDot::MDOT_OK) { |
SomeRandomBloke | 13:5369ba22389a | 170 | logError("Failed to set Network Id %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 171 | } |
SomeRandomBloke | 13:5369ba22389a | 172 | logInfo("Set Network Key"); |
SomeRandomBloke | 13:5369ba22389a | 173 | if ((ret = dot->setNetworkKey(nwkKey)) != mDot::MDOT_OK) { |
SomeRandomBloke | 13:5369ba22389a | 174 | logError("Failed to set Network Id %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 175 | } |
SomeRandomBloke | 5:48eb9245a914 | 176 | |
SomeRandomBloke | 5:48eb9245a914 | 177 | logInfo("Saving Config"); |
SomeRandomBloke | 5:48eb9245a914 | 178 | // Save config |
SomeRandomBloke | 5:48eb9245a914 | 179 | if (! dot->saveConfig()) { |
SomeRandomBloke | 5:48eb9245a914 | 180 | logError("failed to save configuration"); |
SomeRandomBloke | 0:5a0b43f3b143 | 181 | } |
SomeRandomBloke | 5:48eb9245a914 | 182 | |
SomeRandomBloke | 13:5369ba22389a | 183 | pc.printf("Device ID {"); |
SomeRandomBloke | 6:0a7760eeaba9 | 184 | std::vector<uint8_t> deviceId; |
SomeRandomBloke | 6:0a7760eeaba9 | 185 | deviceId = dot->getDeviceId(); |
SomeRandomBloke | 13:5369ba22389a | 186 | for (std::vector<uint8_t>::iterator it = deviceId.begin() ; it != deviceId.end(); ++it) { |
SomeRandomBloke | 13:5369ba22389a | 187 | pc.printf("0x%2.2X",*it ); |
SomeRandomBloke | 13:5369ba22389a | 188 | pc.printf("%s", it != (deviceId.end() -1 ) ? ", " : " " ); |
SomeRandomBloke | 13:5369ba22389a | 189 | } |
SomeRandomBloke | 13:5369ba22389a | 190 | pc.printf("}\r\n"); |
SomeRandomBloke | 9:086351e54b57 | 191 | |
SomeRandomBloke | 13:5369ba22389a | 192 | std::vector<uint8_t> netId; |
SomeRandomBloke | 13:5369ba22389a | 193 | pc.printf("Network Id/App EUI {"); |
SomeRandomBloke | 13:5369ba22389a | 194 | netId = dot->getNetworkId(); |
SomeRandomBloke | 13:5369ba22389a | 195 | for (std::vector<uint8_t>::iterator it = netId.begin() ; it != netId.end(); ++it) { |
SomeRandomBloke | 13:5369ba22389a | 196 | pc.printf("0x%2.2X", *it ); |
SomeRandomBloke | 13:5369ba22389a | 197 | pc.printf("%s", it != (netId.end() -1 ) ? ", " : " " ); |
SomeRandomBloke | 13:5369ba22389a | 198 | } |
SomeRandomBloke | 13:5369ba22389a | 199 | pc.printf("}\r\n"); |
SomeRandomBloke | 5:48eb9245a914 | 200 | |
SomeRandomBloke | 13:5369ba22389a | 201 | std::vector<uint8_t> netKey; |
SomeRandomBloke | 13:5369ba22389a | 202 | pc.printf("Network Key/App Key {"); |
SomeRandomBloke | 13:5369ba22389a | 203 | netKey = dot->getNetworkKey(); |
SomeRandomBloke | 13:5369ba22389a | 204 | for (std::vector<uint8_t>::iterator it = netKey.begin() ; it != netKey.end(); ++it) { |
SomeRandomBloke | 13:5369ba22389a | 205 | pc.printf("0x%2.2X", *it ); |
SomeRandomBloke | 13:5369ba22389a | 206 | pc.printf("%s", it != (netKey.end() -1 ) ? ", " : " " ); |
SomeRandomBloke | 13:5369ba22389a | 207 | } |
SomeRandomBloke | 13:5369ba22389a | 208 | pc.printf("}\r\n"); |
SomeRandomBloke | 5:48eb9245a914 | 209 | |
SomeRandomBloke | 5:48eb9245a914 | 210 | // Display LoRa parameters |
SomeRandomBloke | 5:48eb9245a914 | 211 | // Display label and values in different colours, show pretty values not numeric values where applicable |
SomeRandomBloke | 13:5369ba22389a | 212 | /* |
SomeRandomBloke | 5:48eb9245a914 | 213 | pc.printf("Public Network: %s\r\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") ); |
SomeRandomBloke | 5:48eb9245a914 | 214 | pc.printf("Frequency: %s\r\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() ); |
SomeRandomBloke | 5:48eb9245a914 | 215 | pc.printf("Sub Band: %s\r\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() ); |
SomeRandomBloke | 5:48eb9245a914 | 216 | pc.printf("Join Mode: %s\r\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() ); |
SomeRandomBloke | 6:0a7760eeaba9 | 217 | pc.printf("Join Retries: %d\r\n", dot->getJoinRetries() ); |
SomeRandomBloke | 6:0a7760eeaba9 | 218 | pc.printf("Join Byte Order: %s\r\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") ); |
SomeRandomBloke | 5:48eb9245a914 | 219 | pc.printf("Link Check Count: %d\r\n", dot->getLinkCheckCount() ); |
SomeRandomBloke | 7:2a704d1a30e1 | 220 | pc.printf("Link Check Thold: %d\r\n", dot->getLinkCheckThreshold() ); |
SomeRandomBloke | 5:48eb9245a914 | 221 | pc.printf("Tx Data Rate: %s\r\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() ); |
SomeRandomBloke | 5:48eb9245a914 | 222 | pc.printf("Tx Power: %d\r\n", dot->getTxPower() ); |
SomeRandomBloke | 6:0a7760eeaba9 | 223 | pc.printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" )); |
SomeRandomBloke | 5:48eb9245a914 | 224 | pc.printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") ); |
SomeRandomBloke | 5:48eb9245a914 | 225 | pc.printf("Ack: %s\r\n", (dot->getAck() ? "Y" : "N") ); |
SomeRandomBloke | 13:5369ba22389a | 226 | */ |
SomeRandomBloke | 9:086351e54b57 | 227 | logInfo("Joining Network"); |
SomeRandomBloke | 6:0a7760eeaba9 | 228 | |
SomeRandomBloke | 9:086351e54b57 | 229 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
SomeRandomBloke | 9:086351e54b57 | 230 | logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 9:086351e54b57 | 231 | wait_ms(dot->getNextTxMs() + 1); |
SomeRandomBloke | 9:086351e54b57 | 232 | } |
SomeRandomBloke | 6:0a7760eeaba9 | 233 | |
SomeRandomBloke | 9:086351e54b57 | 234 | logInfo("Joined Network"); |
SomeRandomBloke | 6:0a7760eeaba9 | 235 | |
SomeRandomBloke | 13:5369ba22389a | 236 | // Display Network session key and data session key from Join command |
SomeRandomBloke | 13:5369ba22389a | 237 | /* |
SomeRandomBloke | 13:5369ba22389a | 238 | std::vector<uint8_t> tmp = dot->getNetworkSessionKey(); |
SomeRandomBloke | 13:5369ba22389a | 239 | pc.printf("Network Session Key: "); |
SomeRandomBloke | 13:5369ba22389a | 240 | pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str()); |
SomeRandomBloke | 13:5369ba22389a | 241 | |
SomeRandomBloke | 13:5369ba22389a | 242 | tmp = dot->getDataSessionKey(); |
SomeRandomBloke | 13:5369ba22389a | 243 | pc.printf("Data Session Key: "); |
SomeRandomBloke | 13:5369ba22389a | 244 | pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str()); |
SomeRandomBloke | 13:5369ba22389a | 245 | */ |
SomeRandomBloke | 9:086351e54b57 | 246 | // Set the Temperature sesnor resolution, 9 bits is enough and makes it faster to provide a reading. |
SomeRandomBloke | 0:5a0b43f3b143 | 247 | probe.setResolution(9); |
SomeRandomBloke | 0:5a0b43f3b143 | 248 | |
SomeRandomBloke | 0:5a0b43f3b143 | 249 | char dataBuf[50]; |
SomeRandomBloke | 0:5a0b43f3b143 | 250 | while( 1 ) { |
SomeRandomBloke | 13:5369ba22389a | 251 | // Output data as JSON e.g. {"t":21.3} |
SomeRandomBloke | 13:5369ba22389a | 252 | // temperature = sensor.getTemperature(); |
SomeRandomBloke | 13:5369ba22389a | 253 | // humidity = sensor.getHumidity(); |
SomeRandomBloke | 13:5369ba22389a | 254 | // pressure = sensor.getPressure(); |
SomeRandomBloke | 13:5369ba22389a | 255 | |
SomeRandomBloke | 13:5369ba22389a | 256 | //Start temperature conversion, wait until ready |
SomeRandomBloke | 9:086351e54b57 | 257 | probe.convertTemperature(true, DS1820::all_devices); |
SomeRandomBloke | 13:5369ba22389a | 258 | // Output data as JSON e.g. {"t":21.3} |
SomeRandomBloke | 0:5a0b43f3b143 | 259 | temperature = probe.temperature(); |
SomeRandomBloke | 7:2a704d1a30e1 | 260 | sprintf(dataBuf, "{\"t\":%3.1f}", temperature ); |
SomeRandomBloke | 13:5369ba22389a | 261 | // sprintf(dataBuf, "%3.1f,%3.1f,%04.2f", temperature,humidity,pressure ); |
SomeRandomBloke | 13:5369ba22389a | 262 | pc.printf("%s\n",dataBuf); |
SomeRandomBloke | 13:5369ba22389a | 263 | send_data.clear(); |
SomeRandomBloke | 13:5369ba22389a | 264 | // probably not the most efficent way to do this |
SomeRandomBloke | 13:5369ba22389a | 265 | for( int i=0; i< strlen(dataBuf); i++ ) |
SomeRandomBloke | 13:5369ba22389a | 266 | send_data.push_back( dataBuf[i] ); |
SomeRandomBloke | 0:5a0b43f3b143 | 267 | |
SomeRandomBloke | 13:5369ba22389a | 268 | if ((ret = dot->send(send_data)) != mDot::MDOT_OK) { |
SomeRandomBloke | 13:5369ba22389a | 269 | logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 13:5369ba22389a | 270 | } else { |
SomeRandomBloke | 13:5369ba22389a | 271 | logInfo("send data: %s", Text::bin2hexString(send_data).c_str()); |
SomeRandomBloke | 13:5369ba22389a | 272 | } |
SomeRandomBloke | 0:5a0b43f3b143 | 273 | |
SomeRandomBloke | 13:5369ba22389a | 274 | // Should sleep here and wakeup after a set 5 minute interval. |
SomeRandomBloke | 13:5369ba22389a | 275 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
SomeRandomBloke | 13:5369ba22389a | 276 | uint32_t sleep_time = std::max((uint32_t)300000, (uint32_t)dot->getNextTxMs()) / 1000; |
SomeRandomBloke | 13:5369ba22389a | 277 | //wait_ms(2000); |
SomeRandomBloke | 13:5369ba22389a | 278 | // go to sleep and wake up automatically sleep_time seconds later |
SomeRandomBloke | 13:5369ba22389a | 279 | dot->sleep(sleep_time, mDot::RTC_ALARM); |
SomeRandomBloke | 0:5a0b43f3b143 | 280 | } |
SomeRandomBloke | 2:9db840d12557 | 281 | |
SomeRandomBloke | 0:5a0b43f3b143 | 282 | } |