Mdot control and communication for the exosonde
Dependencies: libmDot-mbed5 mbed
main.cpp@7:42e738547cb2, 2018-05-01 (annotated)
- Committer:
- brettsawyers
- Date:
- Tue May 01 01:38:16 2018 +0000
- Revision:
- 7:42e738547cb2
- Parent:
- 6:f3407f58f521
upload to team
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 | 7:42e738547cb2 | 8 | #define PARAMSNUM 9 |
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 | 7:42e738547cb2 | 17 | static uint8_t devAddr[] = { 0x26, 0x01, 0x12, 0xB7 }; |
brettsawyers | 7:42e738547cb2 | 18 | static uint8_t appSKey[] = { 0x10, 0xD8, 0x7A, 0xCC, 0x0E, 0x39, 0x7F, 0x6D, 0x7A, 0xD9, 0x9F, 0xD5, 0x68, 0xFF, 0xF0, 0x11 }; |
brettsawyers | 7:42e738547cb2 | 19 | static uint8_t nwkSKey[] = { 0x42, 0xCC, 0x3A, 0x3A, 0xBC, 0x17, 0x50, 0xC7, 0xDE, 0x0D, 0x16, 0x55, 0x37, 0x3C, 0xE0, 0x8B }; |
brettsawyers | 7:42e738547cb2 | 20 | |
brettsawyers | 6:f3407f58f521 | 21 | static uint8_t config_frequency_sub_band = 2; |
brettsawyers | 2:e7e272186bf2 | 22 | |
brettsawyers | 7:42e738547cb2 | 23 | const char firstidentifier = 'h'; |
brettsawyers | 7:42e738547cb2 | 24 | |
brettsawyers | 2:e7e272186bf2 | 25 | int32_t ret; |
brettsawyers | 2:e7e272186bf2 | 26 | |
brettsawyers | 7:42e738547cb2 | 27 | Serial debugger(USBTX, USBRX); //will need to change this to the appropriate pins once connected ot the exosonde |
brettsawyers | 7:42e738547cb2 | 28 | Serial device(UART_TX,UART_RX); |
brettsawyers | 2:e7e272186bf2 | 29 | |
brettsawyers | 2:e7e272186bf2 | 30 | //update this depending on the desired readings' identifier characters |
brettsawyers | 7:42e738547cb2 | 31 | char identifiers[PARAMSNUM]; |
brettsawyers | 1:3bb9d88c2646 | 32 | void flushRXbuffer(Serial *serial){ |
brettsawyers | 1:3bb9d88c2646 | 33 | while(serial -> readable()) serial -> getc(); |
brettsawyers | 1:3bb9d88c2646 | 34 | } |
brettsawyers | 1:3bb9d88c2646 | 35 | |
brettsawyers | 1:3bb9d88c2646 | 36 | bool checkforcomma(Serial *debugger){ |
brettsawyers | 1:3bb9d88c2646 | 37 | debugger -> printf("received: %s\r\n",sonde_buffer); |
brettsawyers | 1:3bb9d88c2646 | 38 | for(int i = 0; i < strlen(sonde_buffer); i++) { |
brettsawyers | 1:3bb9d88c2646 | 39 | if(sonde_buffer[i] == ','){ |
brettsawyers | 1:3bb9d88c2646 | 40 | return true; |
brettsawyers | 1:3bb9d88c2646 | 41 | } |
brettsawyers | 1:3bb9d88c2646 | 42 | } |
brettsawyers | 1:3bb9d88c2646 | 43 | return false; |
brettsawyers | 1:3bb9d88c2646 | 44 | } |
brettsawyers | 1:3bb9d88c2646 | 45 | |
brettsawyers | 1:3bb9d88c2646 | 46 | void getsondedata(Serial *device, Serial *debugger){ |
brettsawyers | 1:3bb9d88c2646 | 47 | char datachar; |
brettsawyers | 1:3bb9d88c2646 | 48 | device -> printf("data\r\n"); |
brettsawyers | 1:3bb9d88c2646 | 49 | int charcount = 0; |
brettsawyers | 7:42e738547cb2 | 50 | int commarecvd = 0; |
brettsawyers | 7:42e738547cb2 | 51 | while(datachar = device -> getc()){ |
brettsawyers | 7:42e738547cb2 | 52 | if(datachar == ',') commarecvd++; |
brettsawyers | 1:3bb9d88c2646 | 53 | sonde_buffer[charcount] = datachar; |
brettsawyers | 7:42e738547cb2 | 54 | charcount++; |
brettsawyers | 7:42e738547cb2 | 55 | if(commarecvd >= 9) break; |
brettsawyers | 1:3bb9d88c2646 | 56 | } |
brettsawyers | 1:3bb9d88c2646 | 57 | //flush the remaining '\n' character from the buffer |
brettsawyers | 1:3bb9d88c2646 | 58 | flushRXbuffer(device); |
brettsawyers | 1:3bb9d88c2646 | 59 | sonde_buffer[charcount] = '\0'; |
brettsawyers | 1:3bb9d88c2646 | 60 | } |
brettsawyers | 1:3bb9d88c2646 | 61 | |
brettsawyers | 1:3bb9d88c2646 | 62 | void setcommadelim(Serial *device){ |
brettsawyers | 1:3bb9d88c2646 | 63 | device -> printf("delim 2\r\n"); |
brettsawyers | 1:3bb9d88c2646 | 64 | } |
brettsawyers | 2:e7e272186bf2 | 65 | |
brettsawyers | 2:e7e272186bf2 | 66 | void parsesondedata(void){ |
brettsawyers | 2:e7e272186bf2 | 67 | int parametercount = 0; |
brettsawyers | 2:e7e272186bf2 | 68 | int charcount; |
brettsawyers | 2:e7e272186bf2 | 69 | char currentcheck; |
brettsawyers | 2:e7e272186bf2 | 70 | for (int i = 0; i < strlen(sonde_buffer); i++){ |
brettsawyers | 2:e7e272186bf2 | 71 | currentcheck = sonde_buffer[i]; |
brettsawyers | 2:e7e272186bf2 | 72 | if(currentcheck == ',') { |
brettsawyers | 2:e7e272186bf2 | 73 | parameters[parametercount][charcount] = '\0'; |
brettsawyers | 2:e7e272186bf2 | 74 | parametercount++; |
brettsawyers | 2:e7e272186bf2 | 75 | charcount = 0; |
brettsawyers | 2:e7e272186bf2 | 76 | continue; |
brettsawyers | 2:e7e272186bf2 | 77 | } |
brettsawyers | 2:e7e272186bf2 | 78 | parameters[parametercount][charcount] = sonde_buffer[i]; |
brettsawyers | 2:e7e272186bf2 | 79 | charcount++; |
brettsawyers | 2:e7e272186bf2 | 80 | } |
brettsawyers | 2:e7e272186bf2 | 81 | parameters[parametercount][charcount] = '\0'; //end the final string |
brettsawyers | 2:e7e272186bf2 | 82 | } |
brettsawyers | 2:e7e272186bf2 | 83 | |
brettsawyers | 7:42e738547cb2 | 84 | void joinnetwork(void){ |
brettsawyers | 7:42e738547cb2 | 85 | debugger.printf("joining network\r\n"); |
brettsawyers | 7:42e738547cb2 | 86 | if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
brettsawyers | 7:42e738547cb2 | 87 | debugger.printf("\r\n---------\r\nJoin Failed, code: %s\r\n---------\r\n",mDot::getReturnCodeString(ret).c_str()); |
brettsawyers | 7:42e738547cb2 | 88 | } |
brettsawyers | 7:42e738547cb2 | 89 | else{ |
brettsawyers | 7:42e738547cb2 | 90 | debugger.printf("joined successfully\r\n"); |
brettsawyers | 7:42e738547cb2 | 91 | } |
brettsawyers | 7:42e738547cb2 | 92 | } |
brettsawyers | 7:42e738547cb2 | 93 | |
brettsawyers | 2:e7e272186bf2 | 94 | void sendpacket(){ |
brettsawyers | 7:42e738547cb2 | 95 | std::vector<uint8_t> payload; |
brettsawyers | 7:42e738547cb2 | 96 | std::vector<uint8_t> dummypayload; |
brettsawyers | 7:42e738547cb2 | 97 | std::string dummystring("hello"); |
brettsawyers | 2:e7e272186bf2 | 98 | for (int i = 0; i < PARAMSNUM; i++){ |
brettsawyers | 2:e7e272186bf2 | 99 | int j = 0; |
brettsawyers | 2:e7e272186bf2 | 100 | payload.push_back((uint8_t)identifiers[i]); |
brettsawyers | 2:e7e272186bf2 | 101 | payload.push_back((uint8_t)':'); |
brettsawyers | 2:e7e272186bf2 | 102 | while(parameters[i][j] != '\0'){ |
brettsawyers | 3:805c0c2f82d3 | 103 | payload.push_back(parameters[i][j]); |
brettsawyers | 3:805c0c2f82d3 | 104 | j++; |
brettsawyers | 2:e7e272186bf2 | 105 | } |
brettsawyers | 2:e7e272186bf2 | 106 | if(i != PARAMSNUM-1){ |
brettsawyers | 2:e7e272186bf2 | 107 | payload.push_back((uint8_t)','); |
brettsawyers | 2:e7e272186bf2 | 108 | } |
brettsawyers | 2:e7e272186bf2 | 109 | } |
brettsawyers | 3:805c0c2f82d3 | 110 | payload.push_back((uint8_t)'\0'); |
brettsawyers | 3:805c0c2f82d3 | 111 | debugger.printf("made packet %s\r\n", payload); |
brettsawyers | 7:42e738547cb2 | 112 | std::copy(dummystring.begin(),dummystring.end(),std::back_inserter(dummypayload)); |
brettsawyers | 7:42e738547cb2 | 113 | debugger.printf("%d\r\n", dummypayload.size()); |
brettsawyers | 7:42e738547cb2 | 114 | // send the data |
brettsawyers | 7:42e738547cb2 | 115 | for(int i = 0; i < 12; i++) { |
brettsawyers | 7:42e738547cb2 | 116 | wait(0.05); |
brettsawyers | 7:42e738547cb2 | 117 | //send eight times to ensure a message goes out on channel 917.4 Mhz |
brettsawyers | 7:42e738547cb2 | 118 | if ((ret = dot->send(payload)) != mDot::MDOT_OK) { |
brettsawyers | 7:42e738547cb2 | 119 | debugger.printf("\r\nFailed send, code: %s\r\n",mDot::getReturnCodeString(ret).c_str()); |
brettsawyers | 7:42e738547cb2 | 120 | }else { |
brettsawyers | 7:42e738547cb2 | 121 | debugger.printf("\r\ndata sent\r\n"); |
brettsawyers | 7:42e738547cb2 | 122 | } |
brettsawyers | 2:e7e272186bf2 | 123 | } |
brettsawyers | 7:42e738547cb2 | 124 | |
brettsawyers | 7:42e738547cb2 | 125 | |
brettsawyers | 2:e7e272186bf2 | 126 | |
brettsawyers | 2:e7e272186bf2 | 127 | } |
brettsawyers | 2:e7e272186bf2 | 128 | |
brettsawyers | 2:e7e272186bf2 | 129 | void Loraconfig(void){ |
brettsawyers | 7:42e738547cb2 | 130 | debugger.printf("default frequency band %d\n\r",dot -> getDefaultFrequencyBand()); |
brettsawyers | 2:e7e272186bf2 | 131 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
brettsawyers | 3:805c0c2f82d3 | 132 | debugger.printf("Could not set FSB\r\n"); |
brettsawyers | 2:e7e272186bf2 | 133 | } |
brettsawyers | 7:42e738547cb2 | 134 | |
brettsawyers | 7:42e738547cb2 | 135 | std::vector<uint8_t> temp; |
brettsawyers | 7:42e738547cb2 | 136 | for (int i = 0; i < 4; i++) { |
brettsawyers | 7:42e738547cb2 | 137 | temp.push_back(devAddr[i]); |
brettsawyers | 2:e7e272186bf2 | 138 | } |
brettsawyers | 7:42e738547cb2 | 139 | // set join mode |
brettsawyers | 7:42e738547cb2 | 140 | if ((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) { |
brettsawyers | 7:42e738547cb2 | 141 | debugger.printf("Could not set join mode\r\n"); |
brettsawyers | 7:42e738547cb2 | 142 | } |
brettsawyers | 7:42e738547cb2 | 143 | //set network address |
brettsawyers | 7:42e738547cb2 | 144 | if ((ret = dot->setNetworkAddress(temp)) != mDot::MDOT_OK) { |
brettsawyers | 7:42e738547cb2 | 145 | debugger.printf("Could not set network address\r\n"); |
brettsawyers | 7:42e738547cb2 | 146 | } |
brettsawyers | 7:42e738547cb2 | 147 | //ser public netowkr to true |
brettsawyers | 7:42e738547cb2 | 148 | if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { |
brettsawyers | 7:42e738547cb2 | 149 | debugger.printf("Could not set public network\r\n"); |
brettsawyers | 2:e7e272186bf2 | 150 | } |
brettsawyers | 7:42e738547cb2 | 151 | //make a string to be input later |
brettsawyers | 7:42e738547cb2 | 152 | temp.clear(); |
brettsawyers | 7:42e738547cb2 | 153 | for (int i = 0; i < 16; i++) { |
brettsawyers | 7:42e738547cb2 | 154 | temp.push_back(nwkSKey[i]); |
brettsawyers | 7:42e738547cb2 | 155 | } |
brettsawyers | 7:42e738547cb2 | 156 | //set the netowrk session key |
brettsawyers | 7:42e738547cb2 | 157 | if ((ret = dot->setNetworkSessionKey(temp)) != mDot::MDOT_OK) { |
brettsawyers | 7:42e738547cb2 | 158 | debugger.printf("Could not set network session key\r\n"); |
brettsawyers | 2:e7e272186bf2 | 159 | } |
brettsawyers | 7:42e738547cb2 | 160 | |
brettsawyers | 7:42e738547cb2 | 161 | //make a string to be input later |
brettsawyers | 7:42e738547cb2 | 162 | temp.clear(); |
brettsawyers | 7:42e738547cb2 | 163 | for (int i = 0; i < 16; i++) { |
brettsawyers | 7:42e738547cb2 | 164 | temp.push_back(appSKey[i]); |
brettsawyers | 2:e7e272186bf2 | 165 | } |
brettsawyers | 7:42e738547cb2 | 166 | |
brettsawyers | 7:42e738547cb2 | 167 | if ((ret = dot->setDataSessionKey(temp)) != mDot::MDOT_OK) { |
brettsawyers | 7:42e738547cb2 | 168 | debugger.printf("Could not set data session key (app session key)\n\r"); |
brettsawyers | 7:42e738547cb2 | 169 | } |
brettsawyers | 7:42e738547cb2 | 170 | |
brettsawyers | 2:e7e272186bf2 | 171 | // request receive confirmation of packets from the gateway |
brettsawyers | 6:f3407f58f521 | 172 | if ((ret = dot->setAck(0)) != mDot::MDOT_OK) { |
brettsawyers | 3:805c0c2f82d3 | 173 | debugger.printf("Could not set ACK\r\n"); |
brettsawyers | 2:e7e272186bf2 | 174 | } |
brettsawyers | 7:42e738547cb2 | 175 | |
brettsawyers | 7:42e738547cb2 | 176 | //set data rate to dr2, this gives us 126 bytes of payload space |
brettsawyers | 7:42e738547cb2 | 177 | if ((ret = dot->setTxDataRate(mDot::DR3)) != mDot::MDOT_OK) { |
brettsawyers | 7:42e738547cb2 | 178 | debugger.printf("Could not set data rate\r\n"); |
brettsawyers | 7:42e738547cb2 | 179 | } |
brettsawyers | 7:42e738547cb2 | 180 | |
brettsawyers | 2:e7e272186bf2 | 181 | } |
brettsawyers | 3:805c0c2f82d3 | 182 | |
brettsawyers | 7:42e738547cb2 | 183 | /* |
brettsawyers | 7:42e738547cb2 | 184 | * initilaise the identifier array used when sending data |
brettsawyers | 7:42e738547cb2 | 185 | */ |
brettsawyers | 7:42e738547cb2 | 186 | void setup_identifiers() { |
brettsawyers | 7:42e738547cb2 | 187 | for(int i = 0; i < PARAMSNUM; i++) { |
brettsawyers | 7:42e738547cb2 | 188 | identifiers[i] = firstidentifier + i; |
brettsawyers | 7:42e738547cb2 | 189 | } |
brettsawyers | 7:42e738547cb2 | 190 | } |
brettsawyers | 0:d8d49519a14a | 191 | int main() { |
brettsawyers | 2:e7e272186bf2 | 192 | // get an mDot handle |
brettsawyers | 7:42e738547cb2 | 193 | wait(2); //stabilise |
brettsawyers | 7:42e738547cb2 | 194 | device.printf("\r\n");//flush any buffer built up during start up |
brettsawyers | 7:42e738547cb2 | 195 | //plan = new lora::ChannelPlan_AU915(); |
brettsawyers | 7:42e738547cb2 | 196 | plan = new lora::ChannelPlan_AS923(); |
brettsawyers | 3:805c0c2f82d3 | 197 | dot = mDot::getInstance(plan); |
brettsawyers | 3:805c0c2f82d3 | 198 | assert(dot); |
brettsawyers | 7:42e738547cb2 | 199 | setup_identifiers(); |
brettsawyers | 2:e7e272186bf2 | 200 | Loraconfig(); |
brettsawyers | 7:42e738547cb2 | 201 | debugger.printf("data rate = %d\r\n", dot -> getTxDataRate()); |
brettsawyers | 7:42e738547cb2 | 202 | debugger.printf("max packet length = %d\r\n", dot -> getMaxPacketLength()); |
brettsawyers | 1:3bb9d88c2646 | 203 | while(1){ |
brettsawyers | 7:42e738547cb2 | 204 | wait(2); |
brettsawyers | 7:42e738547cb2 | 205 | getsondedata(&device, &debugger); |
brettsawyers | 7:42e738547cb2 | 206 | if(!checkforcomma(&debugger)){ |
brettsawyers | 7:42e738547cb2 | 207 | setcommadelim(&device); |
brettsawyers | 7:42e738547cb2 | 208 | continue; |
brettsawyers | 7:42e738547cb2 | 209 | } |
brettsawyers | 7:42e738547cb2 | 210 | parsesondedata(); |
brettsawyers | 2:e7e272186bf2 | 211 | sendpacket(); |
brettsawyers | 1:3bb9d88c2646 | 212 | } |
brettsawyers | 1:3bb9d88c2646 | 213 | } |
brettsawyers | 1:3bb9d88c2646 | 214 |