Mdot control and communication for the exosonde
Dependencies: libmDot-mbed5 mbed
Diff: main.cpp
- Revision:
- 2:e7e272186bf2
- Parent:
- 1:3bb9d88c2646
- Child:
- 3:805c0c2f82d3
--- a/main.cpp Mon Aug 28 22:46:55 2017 +0000 +++ b/main.cpp Thu Aug 31 23:08:23 2017 +0000 @@ -1,11 +1,27 @@ #include "mbed.h" #include "mDot.h" -#include "MTSLog.h" #include <string> #include <vector> #include <algorithm> -char sonde_buffer[256]; +#define PARAMSNUM 10 +#define PARAMLENGTH 15 +#define SONDEBUFFERLEN 256 +char sonde_buffer[SONDEBUFFERLEN]; +char parameters[PARAMSNUM][PARAMLENGTH]; + +static std::string config_network_name = "UQ_St_Lucia"; +static std::string config_network_pass = "L0raStLucia"; +static uint8_t config_frequency_sub_band = 7; + +int32_t ret; +mDot* dot; + +Serial device(USBTX, USBRX); //will need to change this to the appropriate pins once connected ot the exosonde +Serial debugger(USBTX,USBRX); + +//update this depending on the desired readings' identifier characters +const char identifiers[PARAMSNUM] = {'a','b','c','d','e','f','g'}; void flushRXbuffer(Serial *serial){ while(serial -> readable()) serial -> getc(); } @@ -36,16 +52,98 @@ void setcommadelim(Serial *device){ device -> printf("delim 2\r\n"); } + +void parsesondedata(void){ + int parametercount = 0; + int charcount; + char currentcheck; + for (int i = 0; i < strlen(sonde_buffer); i++){ + currentcheck = sonde_buffer[i]; + if(currentcheck == ',') { + parameters[parametercount][charcount] = '\0'; + parametercount++; + charcount = 0; + continue; + } + parameters[parametercount][charcount] = sonde_buffer[i]; + charcount++; + } + parameters[parametercount][charcount] = '\0'; //end the final string +} + +void sendpacket(){ + std::vector<uint8_t> payload; + for (int i = 0; i < PARAMSNUM; i++){ + int j = 0; + payload.push_back((uint8_t)identifiers[i]); + payload.push_back((uint8_t)':'); + while(parameters[i][j] != '\0'){ + payload.push_back(parameters[i][j]); + } + if(i != PARAMSNUM-1){ + payload.push_back((uint8_t)','); + } + } + // join the network if not joined + if (!dot->getNetworkJoinStatus()) { + debugger.printf("joining network\n"); + if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { + debugger.printf("\n---------\nJoin Failed, code: %s,\n---------\n",mDot::getReturnCodeString(ret).c_str()); + } + } + if (dot->getNetworkJoinStatus()) { + // send the data + // ACKs are enabled by default, so we're expecting to get one back + if ((ret = dot->send(payload)) != mDot::MDOT_OK) { + debugger.printf("\nFailed send, code: %s\n",mDot::getReturnCodeString(ret).c_str()); + } else { + debugger.printf("\ndata sent\n"); + } + } + +} + +void Loraconfig(void){ + if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { + debugger.printf("Could not set FSB\n"); + } + if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { + debugger.printf("Could not set network name\n"); + } + if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { + debugger.printf("Could not set network passcode\n"); + } + // in the 915 (US) frequency band, spreading factors 7 - 10 are available + if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { + debugger.printf("Could not set spread factor\n"); + } + //set the number of retries for each sub band before giving up + if ((ret = dot->setJoinRetries(50)) != mDot::MDOT_OK) { + debugger.printf("Could not set retries\n"); + } + // request receive confirmation of packets from the gateway + if ((ret = dot->setAck(8)) != mDot::MDOT_OK) { + debugger.printf("Could not set ACK\n"); + } + // set join mode + if ((ret = dot->setJoinMode(mDot::OTA)) != mDot::MDOT_OK) { + debugger.printf("Could not set join mode\n"); + } +} int main() { - Serial device(USBTX, USBRX); //will need to change this to the appropriate pins once connected ot the exosonde - Serial debugger(USBTX,USBRX); + // get an mDot handle + dot = mDot::getInstance(); + Loraconfig(); while(1){ + wait(0.5); getsondedata(&device, &debugger); debugger.printf("sonde: %s\r\n",sonde_buffer); if(!checkforcomma(&debugger)){ setcommadelim(&device); continue; } + parsesondedata(); + sendpacket(); } }