
Mdot control and communication for the exosonde
Dependencies: libmDot-mbed5 mbed
main.cpp@4:a241bf8f70bb, 2017-09-11 (annotated)
- Committer:
- brettsawyers
- Date:
- Mon Sep 11 13:23:50 2017 +0000
- Revision:
- 4:a241bf8f70bb
- Parent:
- 3:805c0c2f82d3
- Child:
- 5:950cbea63a63
trying with manual connect
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:a241bf8f70bb | 17 | //static std::string config_network_name = "UQ_St_Lucia"; |
brettsawyers | 4:a241bf8f70bb | 18 | //static std::string config_network_pass = "L0raStLucia"; |
brettsawyers | 4:a241bf8f70bb | 19 | static uint8_t devAddr[] = {0x26,0x04,0x1E,0xDB}; |
brettsawyers | 4:a241bf8f70bb | 20 | static uint8_t appSKey[] = {0x2B, 0x2A, 0xDC, 0x26, 0xE2, 0x6F, 0x99, 0x75, |
brettsawyers | 4:a241bf8f70bb | 21 | 0xCF, 0xBC, 0xA6, 0x20, 0x68, 0xFF, 0xCF, 0xCE}; |
brettsawyers | 4:a241bf8f70bb | 22 | static uint8_t nwkSKey[] = {0x17, 0x01, 0x0D, 0x95, 0x9C, 0x72, 0x9F, 0x84, 0x5C, |
brettsawyers | 4:a241bf8f70bb | 23 | 0x03, 0x1A, 0xBB,0xAC, 0xF1, 0x33, 0x11}; |
brettsawyers | 2:e7e272186bf2 | 24 | static uint8_t config_frequency_sub_band = 7; |
brettsawyers | 2:e7e272186bf2 | 25 | |
brettsawyers | 4:a241bf8f70bb | 26 | |
brettsawyers | 2:e7e272186bf2 | 27 | int32_t ret; |
brettsawyers | 2:e7e272186bf2 | 28 | |
brettsawyers | 2:e7e272186bf2 | 29 | Serial device(USBTX, USBRX); //will need to change this to the appropriate pins once connected ot the exosonde |
brettsawyers | 2:e7e272186bf2 | 30 | Serial debugger(USBTX,USBRX); |
brettsawyers | 2:e7e272186bf2 | 31 | |
brettsawyers | 2:e7e272186bf2 | 32 | //update this depending on the desired readings' identifier characters |
brettsawyers | 3:805c0c2f82d3 | 33 | const char identifiers[PARAMSNUM] = {'a','b','c','d','e','f','g','h','i','j'}; |
brettsawyers | 1:3bb9d88c2646 | 34 | void flushRXbuffer(Serial *serial){ |
brettsawyers | 1:3bb9d88c2646 | 35 | while(serial -> readable()) serial -> getc(); |
brettsawyers | 1:3bb9d88c2646 | 36 | } |
brettsawyers | 1:3bb9d88c2646 | 37 | |
brettsawyers | 1:3bb9d88c2646 | 38 | bool checkforcomma(Serial *debugger){ |
brettsawyers | 1:3bb9d88c2646 | 39 | debugger -> printf("received: %s\r\n",sonde_buffer); |
brettsawyers | 1:3bb9d88c2646 | 40 | for(int i = 0; i < strlen(sonde_buffer); i++) { |
brettsawyers | 1:3bb9d88c2646 | 41 | if(sonde_buffer[i] == ','){ |
brettsawyers | 1:3bb9d88c2646 | 42 | return true; |
brettsawyers | 1:3bb9d88c2646 | 43 | } |
brettsawyers | 1:3bb9d88c2646 | 44 | } |
brettsawyers | 1:3bb9d88c2646 | 45 | return false; |
brettsawyers | 1:3bb9d88c2646 | 46 | } |
brettsawyers | 1:3bb9d88c2646 | 47 | |
brettsawyers | 1:3bb9d88c2646 | 48 | void getsondedata(Serial *device, Serial *debugger){ |
brettsawyers | 1:3bb9d88c2646 | 49 | char datachar; |
brettsawyers | 1:3bb9d88c2646 | 50 | device -> printf("data\r\n"); |
brettsawyers | 1:3bb9d88c2646 | 51 | int charcount = 0; |
brettsawyers | 1:3bb9d88c2646 | 52 | while(datachar = device -> getc(),datachar != '\r' && datachar != '\n'){ |
brettsawyers | 1:3bb9d88c2646 | 53 | sonde_buffer[charcount] = datachar; |
brettsawyers | 1:3bb9d88c2646 | 54 | charcount++; |
brettsawyers | 1:3bb9d88c2646 | 55 | } |
brettsawyers | 1:3bb9d88c2646 | 56 | //flush the remaining '\n' character from the buffer |
brettsawyers | 1:3bb9d88c2646 | 57 | flushRXbuffer(device); |
brettsawyers | 1:3bb9d88c2646 | 58 | sonde_buffer[charcount] = '\0'; |
brettsawyers | 1:3bb9d88c2646 | 59 | } |
brettsawyers | 1:3bb9d88c2646 | 60 | |
brettsawyers | 1:3bb9d88c2646 | 61 | void setcommadelim(Serial *device){ |
brettsawyers | 1:3bb9d88c2646 | 62 | device -> printf("delim 2\r\n"); |
brettsawyers | 1:3bb9d88c2646 | 63 | } |
brettsawyers | 2:e7e272186bf2 | 64 | |
brettsawyers | 2:e7e272186bf2 | 65 | void parsesondedata(void){ |
brettsawyers | 2:e7e272186bf2 | 66 | int parametercount = 0; |
brettsawyers | 2:e7e272186bf2 | 67 | int charcount; |
brettsawyers | 2:e7e272186bf2 | 68 | char currentcheck; |
brettsawyers | 2:e7e272186bf2 | 69 | for (int i = 0; i < strlen(sonde_buffer); i++){ |
brettsawyers | 2:e7e272186bf2 | 70 | currentcheck = sonde_buffer[i]; |
brettsawyers | 2:e7e272186bf2 | 71 | if(currentcheck == ',') { |
brettsawyers | 2:e7e272186bf2 | 72 | parameters[parametercount][charcount] = '\0'; |
brettsawyers | 2:e7e272186bf2 | 73 | parametercount++; |
brettsawyers | 2:e7e272186bf2 | 74 | charcount = 0; |
brettsawyers | 2:e7e272186bf2 | 75 | continue; |
brettsawyers | 2:e7e272186bf2 | 76 | } |
brettsawyers | 2:e7e272186bf2 | 77 | parameters[parametercount][charcount] = sonde_buffer[i]; |
brettsawyers | 2:e7e272186bf2 | 78 | charcount++; |
brettsawyers | 2:e7e272186bf2 | 79 | } |
brettsawyers | 2:e7e272186bf2 | 80 | parameters[parametercount][charcount] = '\0'; //end the final string |
brettsawyers | 2:e7e272186bf2 | 81 | } |
brettsawyers | 2:e7e272186bf2 | 82 | |
brettsawyers | 4:a241bf8f70bb | 83 | void joinnetwork(void){ |
brettsawyers | 4:a241bf8f70bb | 84 | debugger.printf("joining network\r\n"); |
brettsawyers | 4:a241bf8f70bb | 85 | if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
brettsawyers | 4:a241bf8f70bb | 86 | debugger.printf("\r\n---------\r\nJoin Failed, code: %s\r\n---------\r\n",mDot::getReturnCodeString(ret).c_str()); |
brettsawyers | 4:a241bf8f70bb | 87 | } |
brettsawyers | 4:a241bf8f70bb | 88 | else{ |
brettsawyers | 4:a241bf8f70bb | 89 | debugger.printf("joined successfully\r\n"); |
brettsawyers | 4:a241bf8f70bb | 90 | } |
brettsawyers | 4:a241bf8f70bb | 91 | } |
brettsawyers | 4:a241bf8f70bb | 92 | |
brettsawyers | 2:e7e272186bf2 | 93 | void sendpacket(){ |
brettsawyers | 4:a241bf8f70bb | 94 | std::vector<uint8_t> payload; |
brettsawyers | 4:a241bf8f70bb | 95 | std::vector<uint8_t> dummypayload; |
brettsawyers | 4:a241bf8f70bb | 96 | std::string dummystring("hello"); |
brettsawyers | 2:e7e272186bf2 | 97 | for (int i = 0; i < PARAMSNUM; i++){ |
brettsawyers | 2:e7e272186bf2 | 98 | int j = 0; |
brettsawyers | 2:e7e272186bf2 | 99 | payload.push_back((uint8_t)identifiers[i]); |
brettsawyers | 2:e7e272186bf2 | 100 | payload.push_back((uint8_t)':'); |
brettsawyers | 2:e7e272186bf2 | 101 | while(parameters[i][j] != '\0'){ |
brettsawyers | 3:805c0c2f82d3 | 102 | payload.push_back(parameters[i][j]); |
brettsawyers | 3:805c0c2f82d3 | 103 | j++; |
brettsawyers | 2:e7e272186bf2 | 104 | } |
brettsawyers | 2:e7e272186bf2 | 105 | if(i != PARAMSNUM-1){ |
brettsawyers | 2:e7e272186bf2 | 106 | payload.push_back((uint8_t)','); |
brettsawyers | 2:e7e272186bf2 | 107 | } |
brettsawyers | 2:e7e272186bf2 | 108 | } |
brettsawyers | 3:805c0c2f82d3 | 109 | payload.push_back((uint8_t)'\0'); |
brettsawyers | 3:805c0c2f82d3 | 110 | debugger.printf("made packet %s\r\n", payload); |
brettsawyers | 4:a241bf8f70bb | 111 | std::copy(dummystring.begin(),dummystring.end(),std::back_inserter(dummypayload)); |
brettsawyers | 4:a241bf8f70bb | 112 | // send the data |
brettsawyers | 4:a241bf8f70bb | 113 | if ((ret = dot->send(dummypayload)) != mDot::MDOT_OK) { |
brettsawyers | 4:a241bf8f70bb | 114 | debugger.printf("\r\nFailed send, code: %s\r\n",mDot::getReturnCodeString(ret).c_str()); |
brettsawyers | 4:a241bf8f70bb | 115 | } else { |
brettsawyers | 4:a241bf8f70bb | 116 | debugger.printf("\r\ndata sent\r\n"); |
brettsawyers | 2:e7e272186bf2 | 117 | } |
brettsawyers | 4:a241bf8f70bb | 118 | |
brettsawyers | 2:e7e272186bf2 | 119 | |
brettsawyers | 2:e7e272186bf2 | 120 | } |
brettsawyers | 2:e7e272186bf2 | 121 | |
brettsawyers | 2:e7e272186bf2 | 122 | void Loraconfig(void){ |
brettsawyers | 4:a241bf8f70bb | 123 | debugger.printf("default frequency band %d\n\r",dot -> getDefaultFrequencyBand()); |
brettsawyers | 2:e7e272186bf2 | 124 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
brettsawyers | 3:805c0c2f82d3 | 125 | debugger.printf("Could not set FSB\r\n"); |
brettsawyers | 2:e7e272186bf2 | 126 | } |
brettsawyers | 4:a241bf8f70bb | 127 | |
brettsawyers | 4:a241bf8f70bb | 128 | std::vector<uint8_t> temp; |
brettsawyers | 4:a241bf8f70bb | 129 | for (int i = 0; i < 4; i++) { |
brettsawyers | 4:a241bf8f70bb | 130 | temp.push_back(devAddr[i]); |
brettsawyers | 2:e7e272186bf2 | 131 | } |
brettsawyers | 4:a241bf8f70bb | 132 | // set join mode |
brettsawyers | 4:a241bf8f70bb | 133 | if ((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) { |
brettsawyers | 4:a241bf8f70bb | 134 | debugger.printf("Could not set join mode\r\n"); |
brettsawyers | 4:a241bf8f70bb | 135 | } |
brettsawyers | 4:a241bf8f70bb | 136 | //set network address |
brettsawyers | 4:a241bf8f70bb | 137 | if ((ret = dot->setNetworkAddress(temp)) != mDot::MDOT_OK) { |
brettsawyers | 4:a241bf8f70bb | 138 | debugger.printf("Could not set network address\r\n"); |
brettsawyers | 4:a241bf8f70bb | 139 | } |
brettsawyers | 4:a241bf8f70bb | 140 | //ser public netowkr to true |
brettsawyers | 4:a241bf8f70bb | 141 | if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { |
brettsawyers | 4:a241bf8f70bb | 142 | debugger.printf("Could not set public network\r\n"); |
brettsawyers | 2:e7e272186bf2 | 143 | } |
brettsawyers | 4:a241bf8f70bb | 144 | //make a string to be input later |
brettsawyers | 4:a241bf8f70bb | 145 | temp.clear(); |
brettsawyers | 4:a241bf8f70bb | 146 | for (int i = 0; i < 16; i++) { |
brettsawyers | 4:a241bf8f70bb | 147 | temp.push_back(nwkSKey[i]); |
brettsawyers | 4:a241bf8f70bb | 148 | } |
brettsawyers | 4:a241bf8f70bb | 149 | //set the netowrk session key |
brettsawyers | 4:a241bf8f70bb | 150 | if ((ret = dot->setNetworkSessionKey(temp)) != mDot::MDOT_OK) { |
brettsawyers | 4:a241bf8f70bb | 151 | debugger.printf("Could not set network session key\r\n"); |
brettsawyers | 2:e7e272186bf2 | 152 | } |
brettsawyers | 4:a241bf8f70bb | 153 | |
brettsawyers | 4:a241bf8f70bb | 154 | //make a string to be input later |
brettsawyers | 4:a241bf8f70bb | 155 | temp.clear(); |
brettsawyers | 4:a241bf8f70bb | 156 | for (int i = 0; i < 16; i++) { |
brettsawyers | 4:a241bf8f70bb | 157 | temp.push_back(appSKey[i]); |
brettsawyers | 2:e7e272186bf2 | 158 | } |
brettsawyers | 4:a241bf8f70bb | 159 | |
brettsawyers | 4:a241bf8f70bb | 160 | if ((ret = dot->setDataSessionKey(temp)) != mDot::MDOT_OK) { |
brettsawyers | 4:a241bf8f70bb | 161 | debugger.printf("Could not set data session key (app session key)\n\r"); |
brettsawyers | 4:a241bf8f70bb | 162 | } |
brettsawyers | 4:a241bf8f70bb | 163 | |
brettsawyers | 2:e7e272186bf2 | 164 | // request receive confirmation of packets from the gateway |
brettsawyers | 4:a241bf8f70bb | 165 | if ((ret = dot->setAck(0)) != mDot::MDOT_OK) { |
brettsawyers | 3:805c0c2f82d3 | 166 | debugger.printf("Could not set ACK\r\n"); |
brettsawyers | 2:e7e272186bf2 | 167 | } |
brettsawyers | 4:a241bf8f70bb | 168 | |
brettsawyers | 4:a241bf8f70bb | 169 | //set data rate to dr2, this gives us 126 bytes of payload space |
brettsawyers | 4:a241bf8f70bb | 170 | if ((ret = dot->setTxDataRate(mDot::DR1)) != mDot::MDOT_OK) { |
brettsawyers | 4:a241bf8f70bb | 171 | debugger.printf("Could not set data rate\r\n"); |
brettsawyers | 4:a241bf8f70bb | 172 | } |
brettsawyers | 4:a241bf8f70bb | 173 | |
brettsawyers | 2:e7e272186bf2 | 174 | } |
brettsawyers | 3:805c0c2f82d3 | 175 | |
brettsawyers | 0:d8d49519a14a | 176 | int main() { |
brettsawyers | 2:e7e272186bf2 | 177 | // get an mDot handle |
brettsawyers | 3:805c0c2f82d3 | 178 | plan = new lora::ChannelPlan_AU915(); |
brettsawyers | 3:805c0c2f82d3 | 179 | dot = mDot::getInstance(plan); |
brettsawyers | 3:805c0c2f82d3 | 180 | assert(dot); |
brettsawyers | 2:e7e272186bf2 | 181 | Loraconfig(); |
brettsawyers | 4:a241bf8f70bb | 182 | debugger.printf("data rate= %d\r\n", dot -> getTxDataRate()); |
brettsawyers | 4:a241bf8f70bb | 183 | debugger.printf("max packet length = %d\r\n", dot -> getMaxPacketLength()); |
brettsawyers | 1:3bb9d88c2646 | 184 | while(1){ |
brettsawyers | 2:e7e272186bf2 | 185 | wait(0.5); |
brettsawyers | 4:a241bf8f70bb | 186 | /* |
brettsawyers | 1:3bb9d88c2646 | 187 | getsondedata(&device, &debugger); |
brettsawyers | 1:3bb9d88c2646 | 188 | if(!checkforcomma(&debugger)){ |
brettsawyers | 1:3bb9d88c2646 | 189 | setcommadelim(&device); |
brettsawyers | 1:3bb9d88c2646 | 190 | continue; |
brettsawyers | 0:d8d49519a14a | 191 | } |
brettsawyers | 2:e7e272186bf2 | 192 | parsesondedata(); |
brettsawyers | 4:a241bf8f70bb | 193 | */ |
brettsawyers | 2:e7e272186bf2 | 194 | sendpacket(); |
brettsawyers | 1:3bb9d88c2646 | 195 | } |
brettsawyers | 1:3bb9d88c2646 | 196 | } |
brettsawyers | 1:3bb9d88c2646 | 197 |