Mdot control and communication for the exosonde
Dependencies: libmDot-mbed5 mbed
Diff: main.cpp
- Revision:
- 4:a241bf8f70bb
- Parent:
- 3:805c0c2f82d3
- Child:
- 5:950cbea63a63
--- a/main.cpp Mon Sep 04 02:58:01 2017 +0000 +++ b/main.cpp Mon Sep 11 13:23:50 2017 +0000 @@ -14,10 +14,16 @@ lora::ChannelPlan* plan = NULL; mDot* dot = NULL; -static std::string config_network_name = "UQ_St_Lucia"; -static std::string config_network_pass = "L0raStLucia"; +//static std::string config_network_name = "UQ_St_Lucia"; +//static std::string config_network_pass = "L0raStLucia"; +static uint8_t devAddr[] = {0x26,0x04,0x1E,0xDB}; +static uint8_t appSKey[] = {0x2B, 0x2A, 0xDC, 0x26, 0xE2, 0x6F, 0x99, 0x75, + 0xCF, 0xBC, 0xA6, 0x20, 0x68, 0xFF, 0xCF, 0xCE}; +static uint8_t nwkSKey[] = {0x17, 0x01, 0x0D, 0x95, 0x9C, 0x72, 0x9F, 0x84, 0x5C, + 0x03, 0x1A, 0xBB,0xAC, 0xF1, 0x33, 0x11}; static uint8_t config_frequency_sub_band = 7; + int32_t ret; Serial device(USBTX, USBRX); //will need to change this to the appropriate pins once connected ot the exosonde @@ -74,8 +80,20 @@ parameters[parametercount][charcount] = '\0'; //end the final string } +void joinnetwork(void){ + debugger.printf("joining network\r\n"); + if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { + debugger.printf("\r\n---------\r\nJoin Failed, code: %s\r\n---------\r\n",mDot::getReturnCodeString(ret).c_str()); + } + else{ + debugger.printf("joined successfully\r\n"); + } +} + void sendpacket(){ - std::vector<uint8_t> payload; + std::vector<uint8_t> payload; + std::vector<uint8_t> dummypayload; + std::string dummystring("hello"); for (int i = 0; i < PARAMSNUM; i++){ int j = 0; payload.push_back((uint8_t)identifiers[i]); @@ -90,54 +108,69 @@ } payload.push_back((uint8_t)'\0'); debugger.printf("made packet %s\r\n", payload); - // join the network if not joined - if (!dot->getNetworkJoinStatus()) { - debugger.printf("joining network\r\n"); - if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { - debugger.printf("\r\n---------\r\nJoin Failed, code: %s\r\n---------\r\n",mDot::getReturnCodeString(ret).c_str()); - } - else{ - debugger.printf("joined successfully\r\n"); - } + std::copy(dummystring.begin(),dummystring.end(),std::back_inserter(dummypayload)); + // send the data + if ((ret = dot->send(dummypayload)) != mDot::MDOT_OK) { + debugger.printf("\r\nFailed send, code: %s\r\n",mDot::getReturnCodeString(ret).c_str()); + } else { + debugger.printf("\r\ndata sent\r\n"); } - if (dot->getNetworkJoinStatus()) { - // send the data - // ACKs are enabled by default, so we're expecting to get one back - if ((ret = dot->send(payload)) != mDot::MDOT_OK) { - debugger.printf("\r\nFailed send, code: %s\r\n",mDot::getReturnCodeString(ret).c_str()); - } else { - debugger.printf("\r\n data sent\r\n"); - } - } + } void Loraconfig(void){ + debugger.printf("default frequency band %d\n\r",dot -> getDefaultFrequencyBand()); if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { debugger.printf("Could not set FSB\r\n"); } - if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { - debugger.printf("Could not set network name\r\n"); + + std::vector<uint8_t> temp; + for (int i = 0; i < 4; i++) { + temp.push_back(devAddr[i]); } - if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { - debugger.printf("Could not set network passcode\r\n"); + // set join mode + if ((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) { + debugger.printf("Could not set join mode\r\n"); + } + //set network address + if ((ret = dot->setNetworkAddress(temp)) != mDot::MDOT_OK) { + debugger.printf("Could not set network address\r\n"); + } + //ser public netowkr to true + if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { + debugger.printf("Could not set public network\r\n"); } - // in the 915 (US) frequency band, spreading factors 7 - 10 are available - if ((ret = dot->setTxDataRate(mDot::SF_7)) != mDot::MDOT_OK) { - debugger.printf("Could not set spread factor\r\n"); + //make a string to be input later + temp.clear(); + for (int i = 0; i < 16; i++) { + temp.push_back(nwkSKey[i]); + } + //set the netowrk session key + if ((ret = dot->setNetworkSessionKey(temp)) != mDot::MDOT_OK) { + debugger.printf("Could not set network session key\r\n"); } - //set the number of retries for each sub band before giving up - if ((ret = dot->setJoinRetries(100)) != mDot::MDOT_OK) { - debugger.printf("Could not set retries\r\n"); + + //make a string to be input later + temp.clear(); + for (int i = 0; i < 16; i++) { + temp.push_back(appSKey[i]); } + + if ((ret = dot->setDataSessionKey(temp)) != mDot::MDOT_OK) { + debugger.printf("Could not set data session key (app session key)\n\r"); + } + // request receive confirmation of packets from the gateway - if ((ret = dot->setAck(8)) != mDot::MDOT_OK) { + if ((ret = dot->setAck(0)) != mDot::MDOT_OK) { debugger.printf("Could not set ACK\r\n"); } - // set join mode - if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) { - debugger.printf("Could not set join mode\r\n"); - } + + //set data rate to dr2, this gives us 126 bytes of payload space + if ((ret = dot->setTxDataRate(mDot::DR1)) != mDot::MDOT_OK) { + debugger.printf("Could not set data rate\r\n"); + } + } int main() { @@ -146,14 +179,18 @@ dot = mDot::getInstance(plan); assert(dot); Loraconfig(); + debugger.printf("data rate= %d\r\n", dot -> getTxDataRate()); + debugger.printf("max packet length = %d\r\n", dot -> getMaxPacketLength()); while(1){ wait(0.5); + /* getsondedata(&device, &debugger); if(!checkforcomma(&debugger)){ setcommadelim(&device); continue; } parsesondedata(); + */ sendpacket(); } }