![](/media/cache/profiles/2dd26bee3d25f4e22c3db125e415203a.jpg.50x50_q85.jpg)
This program connects to the The Things Network backend in OTAA Mode. It logs sensor values from a BME 280 to the backend. Tried adding support for Grove GPS using SerialGPS library but it is not working - conflicting with mbed-rtos, so it commented. Deep Sleep for mDot implemented BUT avoiding reprogramming of the mDot config is NOT working.
Dependencies: BME280 SerialGPS libmDot mbed-rtos mbed
Diff: main.cpp
- Revision:
- 10:8071e1ae92ac
- Parent:
- 8:c17b68b03791
- Child:
- 11:3481e24747e2
--- a/main.cpp Fri Jul 08 03:27:36 2016 +0000 +++ b/main.cpp Sat Jul 09 16:38:21 2016 +0000 @@ -3,6 +3,8 @@ * to the mDot UDK. * Additionally sample code to compress the data * for use with LPWANs such as LoRa + * Uses Standard Firmware from Multitech + * *****************************************************/ #include "mbed.h" @@ -10,44 +12,15 @@ #include "MTSLog.h" #include "MTSText.h" #include <string> - + #include "LoRa.h" #include "BME280.h" - //#include "SerialGPS.h" +//#include "SerialGPS.h" -using namespace mts; +//using namespace mts; #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) -// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> - -// Values as used by The Things Network -// Application session key -uint8_t AppSKey[16]= { 0x91, 0x5F, 0xCD, 0x2A, 0xED, 0x8E, 0x0C, 0x2B, 0x30, 0xEF, 0x35, 0x8D, 0xF7, 0xE7, 0x89, 0x0A }; -// Network session key -uint8_t NwkSKey[16]= { 0x60, 0xBF, 0x44, 0xA9, 0x56, 0x0A, 0x4C, 0xB4, 0xF2, 0xEB, 0xB1, 0x6B, 0x9A, 0x2C, 0x57, 0x32 }; - -// App Key 1DD7BB3D3E43ED13029996BEC25BF190 -uint8_t AppKey[16] = {0x1D, 0xD7, 0xBB, 0x3D, 0x3E, 0x43, 0xED, 0x13, 0x02, 0x99, 0x96, 0xBE, 0xC2, 0x5B, 0xF1, 0x90}; -// App EUI 70B3D57ED00005D5 -uint8_t AppEUI[8] = {0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x00, 0x05, 0xD5}; - - -// Network Address - Get your own address range at http://thethingsnetwork.org/wiki/AddressSpace -//uint8_t NetworkAddr[4]= {0x02,0x01,0x6C,0x02}; // Our Network address or Node ID -uint8_t NetworkAddr[4] = { 0x08, 0xBE, 0xAB, 0x8A }; - -// Some defines for the LoRa configuration -#define LORA_ACK 0 -#define LORA_TXPOWER 20 - -//Ignoring sub band for EU modules. -static uint8_t config_frequency_sub_band = 7; - -// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< - - - // mDot UDK Specific // MDot Pinout: https://developer.mbed.org/platforms/MTS-mDot-F411/#pinout-diagram // Uncomment this line if using a full sized UDK2.0 instead of a Micro UDK @@ -65,8 +38,6 @@ BME280 b280(PC_9, PA_8); AnalogIn light(PB_0); // This corresponds to A1 Connector on the Grove Shield -// Serial via USB for debugging only -//Serial pc(USBTX,USBRX); // Function Declarations void endLessTestLoop(); @@ -75,7 +46,7 @@ void readandprintBME280(); float readLightSensor(); void mDotConfig(); -void mDotGotoDeepSleep(int seconds); +void mDotGotoDeepSleep(int seconds, bool sleepState); void mDotConfigPrint(); void initSerialGPS(); void setupNetwork(); @@ -93,13 +64,13 @@ int main(){ // Simple Test Functions, "Hello World on UDK - setUpLEDBlink(); + //setUpLEDBlink(); + mDotConfig(); - setupNetwork(); - //wait(15); + // setupNetwork(); // Moved to mDotConfig joinNetwork(); sendData(); - // endLessTestLoop(); + //endLessTestLoop(); return 0; } @@ -115,9 +86,6 @@ float humidity; int32_t ret; - logInfo("Joined Network"); - - while (true) { data.clear(); @@ -230,8 +198,11 @@ logInfo("successfully sent data to gateway"); } + // Goto Sleep, commenting out the osDelay since next Tx would be after waking up + mDotGotoDeepSleep(5, true); + // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again - osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); + //osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); } @@ -246,19 +217,30 @@ void mDotConfig() { // get a mDot handle dot = mDot::getInstance(); - //dot->setLogLevel(mts::MTSLog::INFO_LEVEL); - dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); + // Test if we've already saved the config + logInfo("Checking Config"); + std::string configNetworkName = dot->getNetworkName(); + printf("Network Name is %s: \n", (char*)configNetworkName.c_str()); + printf("Network Name is %s: \n", (char*)(config_network_name.c_str())); + + if (configNetworkName.compare(config_network_name) != 0) { + logInfo("Setting Up Config"); + setupNetwork(); + + } else { + logInfo("Config is good, skipping setting up... "); + } } -void mDotGotoDeepSleep(int seconds) { - // logInfo("input to sleep routine %d", seconds); +void mDotGotoDeepSleep(int seconds, bool sleepState) { + // Should sleep here and wakeup after a set interval. uint32_t sleep_time = MAX((dot->getNextTxMs() / 1000), seconds); - logInfo("going to sleep for %d seconds", sleep_time); + logInfo("Going to sleep for %d seconds", sleep_time); + // go to sleep and wake up automatically sleep_time seconds later - //dot->sleep(sleep_time, mDot::RTC_ALARM, false); - dot->sleep(sleep_time, mDot::RTC_ALARM); + dot->sleep(sleep_time, mDot::RTC_ALARM, sleepState); } void setupNetwork(){ @@ -273,20 +255,11 @@ std::vector<uint8_t> appEUI; std::vector<uint8_t> appKey; - // get a mDot handle - // dot = mDot::getInstance(); - + //******************************************* // configuration //******************************************* - //dot->setLogLevel(mts::MTSLog::INFO_LEVEL); - //dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); - //logInfo("Checking Config"); - - // Test if we've already saved the config - std::string configNetworkName = dot->getNetworkName(); - uint8_t *it = NwkSKey; for (uint8_t i = 0; i<16; i++) nwkSKey.push_back((uint8_t) *it++); @@ -310,15 +283,28 @@ logInfo("Resetting Config"); // reset to default config so we know what state we're in dot->resetConfig(); + + //dot->setLogLevel(mts::MTSLog::INFO_LEVEL); + dot->setLogLevel(mts::MTSLog::TRACE_LEVEL); - // Set byte order - AEP less than 1.0.30 - //dot->setJoinByteOrder(mDot::MSB); // This is default for > 1.0.30 Conduit + logInfo("Setting Network name"); + if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { + logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); + } + + logInfo("Setting Network password"); + if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { + logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); + } + + // Set byte order + dot->setJoinByteOrder(mDot::LSB); // Set Spreading Factor, higher is lower data rate, smaller packets but longer range // Lower is higher data rate, larger packets and shorter range. logInfo("Set SF"); - //if((ret = dot->setTxDataRate( mDot::SF_10 )) != mDot::MDOT_OK) { - if((ret = dot->setTxDataRate( mDot::SF_8 )) != mDot::MDOT_OK) { + if((ret = dot->setTxDataRate( mDot::SF_10 )) != mDot::MDOT_OK) { + //if((ret = dot->setTxDataRate( mDot::SF_8 )) != mDot::MDOT_OK) { logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); } @@ -388,30 +374,23 @@ //******************************************* mDotConfigPrint(); - - //char dataBuf[50]; - - - } void joinNetwork() { int32_t ret; + 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); osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); } + logInfo("Joined Network"); - wait(5); } - - void mDotConfigPrint() { // Display what is set @@ -452,6 +431,9 @@ // Display LoRa parameters // Display label and values in different colours, show pretty values not numeric values where applicable + + printf("Network Name: %s\n", (char *)(dot->getNetworkName()).c_str()); + printf("Network Name: %s\n", (char *)(dot->getNetworkPassphrase()).c_str()); printf("Public Network: %s\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") ); printf("Frequency: %s\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() ); printf("Sub Band: %s\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() ); @@ -466,12 +448,8 @@ printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") ); printf("Ack: %s\n", (dot->getAck() ? "Y" : "N") ); - - } - - /***************************************************** * Sensor Functions ****************************************************/ @@ -530,9 +508,8 @@ //printf("BME280 Sensor: \n"); readandprintBME280(); - wait(5); - //mDotGotoDeepSleep(60); - //wait(5); + mDotGotoDeepSleep(60, true); + //wait(10); } }