Mdot control and communication for the exosonde

Dependencies:   libmDot-mbed5 mbed

Committer:
brettsawyers
Date:
Thu Aug 31 23:08:23 2017 +0000
Revision:
2:e7e272186bf2
Parent:
1:3bb9d88c2646
Child:
3:805c0c2f82d3
changed revisions of libraries to ensure stability and interoperability

Who changed what in which revision?

UserRevisionLine numberNew contents of line
brettsawyers 0:d8d49519a14a 1 #include "mbed.h"
brettsawyers 0:d8d49519a14a 2 #include "mDot.h"
brettsawyers 0:d8d49519a14a 3 #include <string>
brettsawyers 0:d8d49519a14a 4 #include <vector>
brettsawyers 0:d8d49519a14a 5 #include <algorithm>
brettsawyers 0:d8d49519a14a 6
brettsawyers 2:e7e272186bf2 7 #define PARAMSNUM 10
brettsawyers 2:e7e272186bf2 8 #define PARAMLENGTH 15
brettsawyers 2:e7e272186bf2 9 #define SONDEBUFFERLEN 256
brettsawyers 2:e7e272186bf2 10 char sonde_buffer[SONDEBUFFERLEN];
brettsawyers 2:e7e272186bf2 11 char parameters[PARAMSNUM][PARAMLENGTH];
brettsawyers 2:e7e272186bf2 12
brettsawyers 2:e7e272186bf2 13 static std::string config_network_name = "UQ_St_Lucia";
brettsawyers 2:e7e272186bf2 14 static std::string config_network_pass = "L0raStLucia";
brettsawyers 2:e7e272186bf2 15 static uint8_t config_frequency_sub_band = 7;
brettsawyers 2:e7e272186bf2 16
brettsawyers 2:e7e272186bf2 17 int32_t ret;
brettsawyers 2:e7e272186bf2 18 mDot* dot;
brettsawyers 2:e7e272186bf2 19
brettsawyers 2:e7e272186bf2 20 Serial device(USBTX, USBRX); //will need to change this to the appropriate pins once connected ot the exosonde
brettsawyers 2:e7e272186bf2 21 Serial debugger(USBTX,USBRX);
brettsawyers 2:e7e272186bf2 22
brettsawyers 2:e7e272186bf2 23 //update this depending on the desired readings' identifier characters
brettsawyers 2:e7e272186bf2 24 const char identifiers[PARAMSNUM] = {'a','b','c','d','e','f','g'};
brettsawyers 1:3bb9d88c2646 25 void flushRXbuffer(Serial *serial){
brettsawyers 1:3bb9d88c2646 26 while(serial -> readable()) serial -> getc();
brettsawyers 1:3bb9d88c2646 27 }
brettsawyers 1:3bb9d88c2646 28
brettsawyers 1:3bb9d88c2646 29 bool checkforcomma(Serial *debugger){
brettsawyers 1:3bb9d88c2646 30 debugger -> printf("received: %s\r\n",sonde_buffer);
brettsawyers 1:3bb9d88c2646 31 for(int i = 0; i < strlen(sonde_buffer); i++) {
brettsawyers 1:3bb9d88c2646 32 if(sonde_buffer[i] == ','){
brettsawyers 1:3bb9d88c2646 33 return true;
brettsawyers 1:3bb9d88c2646 34 }
brettsawyers 1:3bb9d88c2646 35 }
brettsawyers 1:3bb9d88c2646 36 return false;
brettsawyers 1:3bb9d88c2646 37 }
brettsawyers 1:3bb9d88c2646 38
brettsawyers 1:3bb9d88c2646 39 void getsondedata(Serial *device, Serial *debugger){
brettsawyers 1:3bb9d88c2646 40 char datachar;
brettsawyers 1:3bb9d88c2646 41 device -> printf("data\r\n");
brettsawyers 1:3bb9d88c2646 42 int charcount = 0;
brettsawyers 1:3bb9d88c2646 43 while(datachar = device -> getc(),datachar != '\r' && datachar != '\n'){
brettsawyers 1:3bb9d88c2646 44 sonde_buffer[charcount] = datachar;
brettsawyers 1:3bb9d88c2646 45 charcount++;
brettsawyers 1:3bb9d88c2646 46 }
brettsawyers 1:3bb9d88c2646 47 //flush the remaining '\n' character from the buffer
brettsawyers 1:3bb9d88c2646 48 flushRXbuffer(device);
brettsawyers 1:3bb9d88c2646 49 sonde_buffer[charcount] = '\0';
brettsawyers 1:3bb9d88c2646 50 }
brettsawyers 1:3bb9d88c2646 51
brettsawyers 1:3bb9d88c2646 52 void setcommadelim(Serial *device){
brettsawyers 1:3bb9d88c2646 53 device -> printf("delim 2\r\n");
brettsawyers 1:3bb9d88c2646 54 }
brettsawyers 2:e7e272186bf2 55
brettsawyers 2:e7e272186bf2 56 void parsesondedata(void){
brettsawyers 2:e7e272186bf2 57 int parametercount = 0;
brettsawyers 2:e7e272186bf2 58 int charcount;
brettsawyers 2:e7e272186bf2 59 char currentcheck;
brettsawyers 2:e7e272186bf2 60 for (int i = 0; i < strlen(sonde_buffer); i++){
brettsawyers 2:e7e272186bf2 61 currentcheck = sonde_buffer[i];
brettsawyers 2:e7e272186bf2 62 if(currentcheck == ',') {
brettsawyers 2:e7e272186bf2 63 parameters[parametercount][charcount] = '\0';
brettsawyers 2:e7e272186bf2 64 parametercount++;
brettsawyers 2:e7e272186bf2 65 charcount = 0;
brettsawyers 2:e7e272186bf2 66 continue;
brettsawyers 2:e7e272186bf2 67 }
brettsawyers 2:e7e272186bf2 68 parameters[parametercount][charcount] = sonde_buffer[i];
brettsawyers 2:e7e272186bf2 69 charcount++;
brettsawyers 2:e7e272186bf2 70 }
brettsawyers 2:e7e272186bf2 71 parameters[parametercount][charcount] = '\0'; //end the final string
brettsawyers 2:e7e272186bf2 72 }
brettsawyers 2:e7e272186bf2 73
brettsawyers 2:e7e272186bf2 74 void sendpacket(){
brettsawyers 2:e7e272186bf2 75 std::vector<uint8_t> payload;
brettsawyers 2:e7e272186bf2 76 for (int i = 0; i < PARAMSNUM; i++){
brettsawyers 2:e7e272186bf2 77 int j = 0;
brettsawyers 2:e7e272186bf2 78 payload.push_back((uint8_t)identifiers[i]);
brettsawyers 2:e7e272186bf2 79 payload.push_back((uint8_t)':');
brettsawyers 2:e7e272186bf2 80 while(parameters[i][j] != '\0'){
brettsawyers 2:e7e272186bf2 81 payload.push_back(parameters[i][j]);
brettsawyers 2:e7e272186bf2 82 }
brettsawyers 2:e7e272186bf2 83 if(i != PARAMSNUM-1){
brettsawyers 2:e7e272186bf2 84 payload.push_back((uint8_t)',');
brettsawyers 2:e7e272186bf2 85 }
brettsawyers 2:e7e272186bf2 86 }
brettsawyers 2:e7e272186bf2 87 // join the network if not joined
brettsawyers 2:e7e272186bf2 88 if (!dot->getNetworkJoinStatus()) {
brettsawyers 2:e7e272186bf2 89 debugger.printf("joining network\n");
brettsawyers 2:e7e272186bf2 90 if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
brettsawyers 2:e7e272186bf2 91 debugger.printf("\n---------\nJoin Failed, code: %s,\n---------\n",mDot::getReturnCodeString(ret).c_str());
brettsawyers 2:e7e272186bf2 92 }
brettsawyers 2:e7e272186bf2 93 }
brettsawyers 2:e7e272186bf2 94 if (dot->getNetworkJoinStatus()) {
brettsawyers 2:e7e272186bf2 95 // send the data
brettsawyers 2:e7e272186bf2 96 // ACKs are enabled by default, so we're expecting to get one back
brettsawyers 2:e7e272186bf2 97 if ((ret = dot->send(payload)) != mDot::MDOT_OK) {
brettsawyers 2:e7e272186bf2 98 debugger.printf("\nFailed send, code: %s\n",mDot::getReturnCodeString(ret).c_str());
brettsawyers 2:e7e272186bf2 99 } else {
brettsawyers 2:e7e272186bf2 100 debugger.printf("\ndata sent\n");
brettsawyers 2:e7e272186bf2 101 }
brettsawyers 2:e7e272186bf2 102 }
brettsawyers 2:e7e272186bf2 103
brettsawyers 2:e7e272186bf2 104 }
brettsawyers 2:e7e272186bf2 105
brettsawyers 2:e7e272186bf2 106 void Loraconfig(void){
brettsawyers 2:e7e272186bf2 107 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
brettsawyers 2:e7e272186bf2 108 debugger.printf("Could not set FSB\n");
brettsawyers 2:e7e272186bf2 109 }
brettsawyers 2:e7e272186bf2 110 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
brettsawyers 2:e7e272186bf2 111 debugger.printf("Could not set network name\n");
brettsawyers 2:e7e272186bf2 112 }
brettsawyers 2:e7e272186bf2 113 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
brettsawyers 2:e7e272186bf2 114 debugger.printf("Could not set network passcode\n");
brettsawyers 2:e7e272186bf2 115 }
brettsawyers 2:e7e272186bf2 116 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
brettsawyers 2:e7e272186bf2 117 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
brettsawyers 2:e7e272186bf2 118 debugger.printf("Could not set spread factor\n");
brettsawyers 2:e7e272186bf2 119 }
brettsawyers 2:e7e272186bf2 120 //set the number of retries for each sub band before giving up
brettsawyers 2:e7e272186bf2 121 if ((ret = dot->setJoinRetries(50)) != mDot::MDOT_OK) {
brettsawyers 2:e7e272186bf2 122 debugger.printf("Could not set retries\n");
brettsawyers 2:e7e272186bf2 123 }
brettsawyers 2:e7e272186bf2 124 // request receive confirmation of packets from the gateway
brettsawyers 2:e7e272186bf2 125 if ((ret = dot->setAck(8)) != mDot::MDOT_OK) {
brettsawyers 2:e7e272186bf2 126 debugger.printf("Could not set ACK\n");
brettsawyers 2:e7e272186bf2 127 }
brettsawyers 2:e7e272186bf2 128 // set join mode
brettsawyers 2:e7e272186bf2 129 if ((ret = dot->setJoinMode(mDot::OTA)) != mDot::MDOT_OK) {
brettsawyers 2:e7e272186bf2 130 debugger.printf("Could not set join mode\n");
brettsawyers 2:e7e272186bf2 131 }
brettsawyers 2:e7e272186bf2 132 }
brettsawyers 0:d8d49519a14a 133 int main() {
brettsawyers 2:e7e272186bf2 134 // get an mDot handle
brettsawyers 2:e7e272186bf2 135 dot = mDot::getInstance();
brettsawyers 2:e7e272186bf2 136 Loraconfig();
brettsawyers 1:3bb9d88c2646 137 while(1){
brettsawyers 2:e7e272186bf2 138 wait(0.5);
brettsawyers 1:3bb9d88c2646 139 getsondedata(&device, &debugger);
brettsawyers 1:3bb9d88c2646 140 debugger.printf("sonde: %s\r\n",sonde_buffer);
brettsawyers 1:3bb9d88c2646 141 if(!checkforcomma(&debugger)){
brettsawyers 1:3bb9d88c2646 142 setcommadelim(&device);
brettsawyers 1:3bb9d88c2646 143 continue;
brettsawyers 0:d8d49519a14a 144 }
brettsawyers 2:e7e272186bf2 145 parsesondedata();
brettsawyers 2:e7e272186bf2 146 sendpacket();
brettsawyers 1:3bb9d88c2646 147 }
brettsawyers 1:3bb9d88c2646 148 }
brettsawyers 1:3bb9d88c2646 149