Mdot control and communication for the exosonde

Dependencies:   libmDot-mbed5 mbed

Revision:
7:42e738547cb2
Parent:
6:f3407f58f521
--- a/main.cpp	Tue Sep 26 13:32:45 2017 +0000
+++ b/main.cpp	Tue May 01 01:38:16 2018 +0000
@@ -5,7 +5,7 @@
 #include <algorithm>
 #include "ChannelPlans.h"
 
-#define PARAMSNUM 10
+#define PARAMSNUM 9
 #define PARAMLENGTH 15
 #define SONDEBUFFERLEN 256
 char sonde_buffer[SONDEBUFFERLEN];
@@ -14,19 +14,21 @@
 lora::ChannelPlan* plan = NULL;
 mDot* dot = NULL;
 
-//static std::string config_network_name = "UQ_St_Lucia";
-//static std::string config_network_pass = "L0raStLucia";
-const std::uint8_t *appEui = "70B3D57ED00074FE";
-const std::uint8_t *appKey = "35AC3CD8615D97F43B82E2B350626ACA";
+static uint8_t devAddr[] = { 0x26, 0x01, 0x12, 0xB7 };
+static uint8_t appSKey[] = { 0x10, 0xD8, 0x7A, 0xCC, 0x0E, 0x39, 0x7F, 0x6D, 0x7A, 0xD9, 0x9F, 0xD5, 0x68, 0xFF, 0xF0, 0x11 };
+static uint8_t nwkSKey[] = { 0x42, 0xCC, 0x3A, 0x3A, 0xBC, 0x17, 0x50, 0xC7, 0xDE, 0x0D, 0x16, 0x55, 0x37, 0x3C, 0xE0, 0x8B };
+
 static uint8_t config_frequency_sub_band = 2;
 
+const char firstidentifier = 'h';
+
 int32_t ret;
 
-Serial device(USBTX, USBRX); //will need to change this to the appropriate pins once connected ot the exosonde
-Serial debugger(USBTX,USBRX);
+Serial debugger(USBTX, USBRX); //will need to change this to the appropriate pins once connected ot the exosonde
+Serial device(UART_TX,UART_RX);
 
 //update this depending on the desired readings' identifier characters
-const char identifiers[PARAMSNUM] = {'a','b','c','d','e','f','g','h','i','j'};
+char identifiers[PARAMSNUM];
 void flushRXbuffer(Serial *serial){
     while(serial -> readable()) serial -> getc();    
 }
@@ -45,9 +47,12 @@
     char datachar;
     device -> printf("data\r\n");
     int charcount = 0;
-    while(datachar = device -> getc(),datachar != '\r' && datachar != '\n'){
+    int commarecvd = 0;
+    while(datachar = device -> getc()){
+        if(datachar == ',') commarecvd++;
         sonde_buffer[charcount] = datachar;
-        charcount++; 
+        charcount++;
+        if(commarecvd >= 9) break;
     }
     //flush the remaining '\n' character from the buffer
     flushRXbuffer(device);
@@ -76,11 +81,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> dummypayload;
-    std::string dummystring = "hello";
-    std::copy(dummystring.begin(),dummystring.end(),std::back_inserter(dummypayload));      
+    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]);
@@ -95,70 +109,105 @@
     }
     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));
+    debugger.printf("%d\r\n", dummypayload.size());
+    // send the data
+    for(int i = 0; i < 12; i++) {
+        wait(0.05);
+        //send eight times to ensure a message goes out on channel 917.4 Mhz
+        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\ndata sent\r\n");
+        }        
     }
-    // (dot->getNetworkJoinStatus()) {
-        // 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\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->setAppEUI(appEui)) != mDot::MDOT_OK) {
-        debugger.printf("Could not set network EUI\r\n");
+    
+    std::vector<uint8_t> temp;
+    for (int i = 0; i < 4; i++) {
+        temp.push_back(devAddr[i]);    
     }
-    if ((ret = dot->setAppKey(appKey)) != mDot::MDOT_OK) {
-        debugger.printf("Could not set network key\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::DR0))!= 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(0)) != mDot::MDOT_OK) {
         debugger.printf("Could not set ACK\r\n");
     }
-    // set join mode 
-    if ((ret = dot->setJoinMode(mDot::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::DR3)) != mDot::MDOT_OK) {
+        debugger.printf("Could not set data rate\r\n");
+    }
+    
 }
 
+/*
+ * initilaise the identifier array used when sending data
+ */
+void setup_identifiers() {
+    for(int i = 0; i < PARAMSNUM; i++) {
+        identifiers[i] = firstidentifier + i;    
+    }
+}
 int main() {
     // get an mDot handle
-    plan = new lora::ChannelPlan_AU915();
+    wait(2); //stabilise
+    device.printf("\r\n");//flush any buffer built up during start up
+    //plan = new lora::ChannelPlan_AU915();
+    plan = new lora::ChannelPlan_AS923();
     dot = mDot::getInstance(plan);
     assert(dot);
+    setup_identifiers();
     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();
+        wait(2);
+        getsondedata(&device, &debugger);
+        if(!checkforcomma(&debugger)){
+            setcommadelim(&device);
+            continue;    
+        }
+        parsesondedata();
         sendpacket();
     }
 }