Demo of LoRA send/receive

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_Connect_IoTClub_one_wire by wireless sensor

Committer:
wang1tao
Date:
Sat Aug 13 10:27:50 2016 +0000
Revision:
10:79124c0a5952
Parent:
9:cf45820af9b9
Child:
11:1ecd1cdca9ef
bug fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:09250cd371d2 1 #include "mbed.h"
mfiore 0:09250cd371d2 2 #include "mDot.h"
mfiore 4:36e214ebfa56 3 #include "MTSLog.h"
mfiore 0:09250cd371d2 4 #include <string>
mfiore 0:09250cd371d2 5 #include <vector>
mfiore 4:36e214ebfa56 6 #include <algorithm>
wang1tao 9:cf45820af9b9 7 #include "AnalogPH.h"
wang1tao 9:cf45820af9b9 8
wang1tao 9:cf45820af9b9 9 //defined for mDot SVB debug, comment it if applying for whole system
wang1tao 10:79124c0a5952 10 //#define NO_MULTITECH_GATEWAY
wang1tao 9:cf45820af9b9 11
wang1tao 9:cf45820af9b9 12 //Define the number of channels
wang1tao 9:cf45820af9b9 13 #define NUM_OF_CH 4
JCheng 5:6c7d2f4d7377 14
wang1tao 9:cf45820af9b9 15 AnalogPHSensor CH0_PH(PB_0, 0.0, 0.0);
wang1tao 9:cf45820af9b9 16
wang1tao 9:cf45820af9b9 17 #if (NUM_OF_CH >1)
wang1tao 9:cf45820af9b9 18 AnalogPHSensor CH1_PH(PB_1, 0.0, 0.0);
wang1tao 9:cf45820af9b9 19 #endif
wang1tao 9:cf45820af9b9 20
wang1tao 9:cf45820af9b9 21 #if (NUM_OF_CH >2)
wang1tao 10:79124c0a5952 22 AnalogPHSensor CH2_PH(PA_5, 0.0, 0.0);
wang1tao 9:cf45820af9b9 23 #endif
wang1tao 9:cf45820af9b9 24 #if (NUM_OF_CH >3)
wang1tao 10:79124c0a5952 25 AnalogPHSensor CH3_PH(PA_4, 0.0, 0.0);
wang1tao 9:cf45820af9b9 26 #endif
mfiore 0:09250cd371d2 27
mfiore 2:6e2c378339d9 28 // these options must match the settings on your Conduit
mfiore 2:6e2c378339d9 29 // uncomment the following lines and edit their values to match your configuration
JCheng 5:6c7d2f4d7377 30 static std::string config_network_name = "chinaiot";
JCheng 5:6c7d2f4d7377 31 static std::string config_network_pass = "password";
wang1tao 10:79124c0a5952 32 static uint8_t config_frequency_sub_band = 2;
mfiore 0:09250cd371d2 33
mfiore 0:09250cd371d2 34 int main() {
wang1tao 10:79124c0a5952 35 int32_t ret;
mfiore 0:09250cd371d2 36 mDot* dot;
mfiore 0:09250cd371d2 37 std::vector<uint8_t> data;
wang1tao 9:cf45820af9b9 38 float phValue;
wang1tao 9:cf45820af9b9 39 char _header[] = "PH Sensors";
wang1tao 8:dfcee7a96019 40 char dataBuf[11];
wang1tao 9:cf45820af9b9 41 int i, k;
wang1tao 9:cf45820af9b9 42
mfiore 0:09250cd371d2 43 // get a mDot handle
mfiore 0:09250cd371d2 44 dot = mDot::getInstance();
JCheng 5:6c7d2f4d7377 45
mfiore 2:6e2c378339d9 46 // print library version information
mfiore 2:6e2c378339d9 47 logInfo("version: %s", dot->getId().c_str());
mfiore 0:09250cd371d2 48
mfiore 2:6e2c378339d9 49 //*******************************************
mfiore 2:6e2c378339d9 50 // configuration
mfiore 2:6e2c378339d9 51 //*******************************************
mfiore 0:09250cd371d2 52 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 53 dot->resetConfig();
JCheng 5:6c7d2f4d7377 54
mfiore 4:36e214ebfa56 55 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
wang1tao 9:cf45820af9b9 56 #ifndef NO_MULTITECH_GATEWAY
mfiore 2:6e2c378339d9 57 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 58 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
JCheng 5:6c7d2f4d7377 59
mfiore 4:36e214ebfa56 60 // frequency sub band is only applicable in the 915 (US) frequency band
mfiore 4:36e214ebfa56 61 // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band
mfiore 4:36e214ebfa56 62 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
mfiore 2:6e2c378339d9 63 logInfo("setting frequency sub band");
mfiore 0:09250cd371d2 64 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 65 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 2:6e2c378339d9 66 }
JCheng 5:6c7d2f4d7377 67
mfiore 2:6e2c378339d9 68 logInfo("setting network name");
mfiore 2:6e2c378339d9 69 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 70 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 71 }
JCheng 5:6c7d2f4d7377 72
mfiore 2:6e2c378339d9 73 logInfo("setting network password");
mfiore 2:6e2c378339d9 74 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 75 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 76 }
JCheng 5:6c7d2f4d7377 77
mfiore 4:36e214ebfa56 78 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 79 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 80 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
mfiore 4:36e214ebfa56 81 logInfo("setting TX spreading factor");
mfiore 4:36e214ebfa56 82 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 83 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 84 }
JCheng 5:6c7d2f4d7377 85
mfiore 4:36e214ebfa56 86 // request receive confirmation of packets from the gateway
mfiore 4:36e214ebfa56 87 logInfo("enabling ACKs");
mfiore 4:36e214ebfa56 88 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 89 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 90 }
JCheng 5:6c7d2f4d7377 91
mfiore 4:36e214ebfa56 92 // save this configuration to the mDot's NVM
mfiore 2:6e2c378339d9 93 logInfo("saving config");
mfiore 2:6e2c378339d9 94 if (! dot->saveConfig()) {
mfiore 2:6e2c378339d9 95 logError("failed to save configuration");
mfiore 0:09250cd371d2 96 }
mfiore 2:6e2c378339d9 97 //*******************************************
mfiore 2:6e2c378339d9 98 // end of configuration
mfiore 2:6e2c378339d9 99 //*******************************************
mfiore 0:09250cd371d2 100
mfiore 0:09250cd371d2 101 // attempt to join the network
mfiore 2:6e2c378339d9 102 logInfo("joining network");
mfiore 0:09250cd371d2 103 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 104 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 105 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 106 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 107 }
mfiore 0:09250cd371d2 108
mfiore 0:09250cd371d2 109 // format data for sending to the gateway
wang1tao 9:cf45820af9b9 110 for( int i=0; i< strlen(_header); i++ )
wang1tao 9:cf45820af9b9 111 data.push_back( _header[i] );
wang1tao 9:cf45820af9b9 112
wang1tao 9:cf45820af9b9 113 // send the data to the gateway
wang1tao 9:cf45820af9b9 114 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
wang1tao 9:cf45820af9b9 115 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 9:cf45820af9b9 116 } else {
wang1tao 9:cf45820af9b9 117 logInfo("successfully sent data to gateway");
wang1tao 9:cf45820af9b9 118 }
wang1tao 9:cf45820af9b9 119 #else
wang1tao 9:cf45820af9b9 120 logInfo("%s", _header);
wang1tao 9:cf45820af9b9 121 #endif
mfiore 0:09250cd371d2 122 while (true) {
wang1tao 9:cf45820af9b9 123
wang1tao 9:cf45820af9b9 124 // Read the PH values
wang1tao 9:cf45820af9b9 125 for(k=0; k<NUM_OF_CH; k++){
wang1tao 9:cf45820af9b9 126 switch(k){
wang1tao 9:cf45820af9b9 127 case 0: phValue = CH0_PH; break;
wang1tao 9:cf45820af9b9 128 #if (NUM_OF_CH >1)
wang1tao 9:cf45820af9b9 129 case 1: phValue = CH1_PH; break;
wang1tao 9:cf45820af9b9 130 #endif
wang1tao 9:cf45820af9b9 131 #if (NUM_OF_CH >2)
wang1tao 9:cf45820af9b9 132 case 2: phValue = CH2_PH; break;
wang1tao 9:cf45820af9b9 133 #endif
wang1tao 9:cf45820af9b9 134 #if (NUM_OF_CH >3)
wang1tao 9:cf45820af9b9 135 case 3: phValue = CH3_PH; break;
wang1tao 9:cf45820af9b9 136 #endif
wang1tao 9:cf45820af9b9 137 default:
wang1tao 9:cf45820af9b9 138 break;
wang1tao 10:79124c0a5952 139 }
wang1tao 9:cf45820af9b9 140 sprintf(dataBuf, "CH%d:%5.4f", k, phValue);
wang1tao 9:cf45820af9b9 141 logInfo("%s", dataBuf);
wang1tao 9:cf45820af9b9 142
wang1tao 9:cf45820af9b9 143 #ifndef NO_MULTITECH_GATEWAY
wang1tao 9:cf45820af9b9 144 //Send the data to Gateway
wang1tao 9:cf45820af9b9 145 data.clear();
wang1tao 9:cf45820af9b9 146 // probably not the most efficent way to do this
wang1tao 9:cf45820af9b9 147 for( int i=0; i< strlen(dataBuf); i++ )
wang1tao 9:cf45820af9b9 148 data.push_back( dataBuf[i] );
wang1tao 9:cf45820af9b9 149
wang1tao 9:cf45820af9b9 150 // send the data to the gateway
wang1tao 9:cf45820af9b9 151 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
wang1tao 9:cf45820af9b9 152 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 9:cf45820af9b9 153 } else {
mfiore 2:6e2c378339d9 154 logInfo("successfully sent data to gateway");
wang1tao 9:cf45820af9b9 155 }
wang1tao 9:cf45820af9b9 156 #endif
wang1tao 10:79124c0a5952 157 } //for
wang1tao 10:79124c0a5952 158 //printf("++++++++++++++++++++++\t");
mfiore 4:36e214ebfa56 159 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
wang1tao 9:cf45820af9b9 160 #ifndef NO_MULTITECH_GATEWAY
mfiore 4:36e214ebfa56 161 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
wang1tao 9:cf45820af9b9 162 #else
wang1tao 9:cf45820af9b9 163 osDelay(1000);
wang1tao 9:cf45820af9b9 164 #endif
mfiore 0:09250cd371d2 165 }
mfiore 0:09250cd371d2 166
mfiore 0:09250cd371d2 167 }