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.
Diff: main.cpp
- Revision:
- 7:2a704d1a30e1
- Parent:
- 6:0a7760eeaba9
- Child:
- 8:8070e9d660e4
diff -r 0a7760eeaba9 -r 2a704d1a30e1 main.cpp --- a/main.cpp Wed Oct 21 23:12:33 2015 +0000 +++ b/main.cpp Wed Oct 28 11:06:03 2015 +0000 @@ -90,8 +90,6 @@ // Test if we've already saved the config std::string configNetworkName = dot->getNetworkName(); -// config_network_name = std::string(NwkSKey,16); -// config_network_pass = std::string(NwkSKey,16); // pc.printf("Name: %s\r\n",config_network_name.c_str()); uint8_t *it = NwkSKey; @@ -117,18 +115,9 @@ // Set byte order - AEP less than 1.0.30 // dot->setJoinByteOrder(mDot::MSB); -// dot->setJoinByteOrder(mDot::LSB); + dot->setJoinByteOrder(mDot::LSB); - logInfo("Set Public mode"); - if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { - logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); - } - - logInfo("Set MANUAL Join mode"); -// if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) { - if((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) { - logError("Failed to set MANUAL Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); - } +// dot->setCrc(true); // If on developer board then you can enable activity LED // Currently no spare pins that LEDs are connected too. @@ -136,10 +125,10 @@ // dot->setActivityLedEnable(false); // Have a decent nubmer of retries in connecting to LoRaWAN - logInfo("Set Join retries"); - if((ret = dot->setJoinRetries( 3 )) != mDot::MDOT_OK) { - logError("Failed to set Join retries %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); - } +// logInfo("Set Join retries"); +// if((ret = dot->setJoinRetries( 3 )) != mDot::MDOT_OK) { +// logError("Failed to set Join retries %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); +// } // Set Spreading Factor, higher is lower data rate, smaller packets but longer range // Lower is higher data rate, larger packets and shorter range. @@ -154,6 +143,16 @@ logError("Failed to set Tx Power %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); } + logInfo("Set Public mode"); + if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { + logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); + } + + logInfo("Set MANUAL Join mode"); + if((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) { + logError("Failed to set MANUAL Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); + } + logInfo("Set Ack"); // 1 retries on Ack, 0 to disable if((ret = dot->setAck( 0 )) != mDot::MDOT_OK) { @@ -168,7 +167,12 @@ logInfo("Set Network Address"); if ((ret = dot->setNetworkAddress(networkAddr)) != mDot::MDOT_OK) { - logError("Failed to set Network Address %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); + logError("Failed to set Network Address %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); + } + + logInfo("Set Data Session Key"); + if ((ret = dot->setDataSessionKey(nwkSKey)) != mDot::MDOT_OK) { + logError("Failed to set Data Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); } logInfo("Set Network Session Key"); @@ -176,11 +180,6 @@ logError("Failed to set Network Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); } - logInfo("Set Data Session Key"); - if ((ret = dot->setDataSessionKey(nwkSKey)) != mDot::MDOT_OK) { - logError("Failed to set Data Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); - } - // if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { // logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); // } @@ -233,21 +232,21 @@ pc.printf("Join Retries: %d\r\n", dot->getJoinRetries() ); pc.printf("Join Byte Order: %s\r\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") ); pc.printf("Link Check Count: %d\r\n", dot->getLinkCheckCount() ); - pc.printf("Link Check Thold: %s\r\n", dot->getLinkCheckThreshold() ); + pc.printf("Link Check Thold: %d\r\n", dot->getLinkCheckThreshold() ); pc.printf("Tx Data Rate: %s\r\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() ); pc.printf("Tx Power: %d\r\n", dot->getTxPower() ); pc.printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" )); pc.printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") ); pc.printf("Ack: %s\r\n", (dot->getAck() ? "Y" : "N") ); - logInfo("Joining Network"); +// logInfo("Joining Network"); - while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { - logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); - wait_ms(dot->getNextTxMs() + 1); - } +// while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { +// logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); +// wait_ms(dot->getNextTxMs() + 1); +// } - logInfo("Joined Network"); +// logInfo("Joined Network"); probe.setResolution(9); @@ -257,9 +256,9 @@ probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready // printf("It is %3.1fC\r\n", probe.temperature()); - // Output data as JSON e.g. {"tmp":21.3} + // Output data as JSON e.g. {"t":21.3} temperature = probe.temperature(); - sprintf(dataBuf, "{\"tmp\":%3.1f}", temperature ); + sprintf(dataBuf, "{\"t\":%3.1f}", temperature ); send_data.clear(); // probably not the most efficent way to do this for( int i=0; i< strlen(dataBuf); i++ )