Mdot control and communication for the exosonde

Dependencies:   libmDot-mbed5 mbed

Committer:
brettsawyers
Date:
Mon Sep 04 02:58:01 2017 +0000
Revision:
3:805c0c2f82d3
Parent:
2:e7e272186bf2
Child:
4:a241bf8f70bb
Child:
6:f3407f58f521
can now connect to network, but cannot send correctly

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