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 20 08:31:14 2016 +0000
Revision:
13:c966b64b227f
Parent:
12:b31e43c9fb15
Demo of send/receive sync

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
wang1tao 13:c966b64b227f 8 const char TURNON[] = "turnon";
wang1tao 13:c966b64b227f 9 const char TURNOFF[] = "turnoff";
wang1tao 9:cf45820af9b9 10
wang1tao 13:c966b64b227f 11 const char TURN_ON_ALARM[] = "TurnOnAlarm";
wang1tao 13:c966b64b227f 12 const char TURN_OFF_ALARM[] = "TurnOffAlarm";
wang1tao 9:cf45820af9b9 13
wang1tao 13:c966b64b227f 14 const char _header[] = "PH Alarm";
mfiore 0:09250cd371d2 15
mfiore 2:6e2c378339d9 16 // these options must match the settings on your Conduit
mfiore 2:6e2c378339d9 17 // uncomment the following lines and edit their values to match your configuration
JCheng 5:6c7d2f4d7377 18 static std::string config_network_name = "chinaiot";
JCheng 5:6c7d2f4d7377 19 static std::string config_network_pass = "password";
wang1tao 10:79124c0a5952 20 static uint8_t config_frequency_sub_band = 2;
mfiore 0:09250cd371d2 21
wang1tao 13:c966b64b227f 22 bool strCmp(char* str1, const char* str2, int len)
wang1tao 13:c966b64b227f 23 {
wang1tao 13:c966b64b227f 24 int i;
wang1tao 13:c966b64b227f 25 for (i=0; i<len; i++)
wang1tao 13:c966b64b227f 26 {
wang1tao 13:c966b64b227f 27 if(str1[i]!=str2[i])return false;
wang1tao 13:c966b64b227f 28 }
wang1tao 13:c966b64b227f 29 return true;
wang1tao 13:c966b64b227f 30 }
wang1tao 13:c966b64b227f 31
wang1tao 13:c966b64b227f 32
mfiore 0:09250cd371d2 33 int main() {
wang1tao 10:79124c0a5952 34 int32_t ret;
mfiore 0:09250cd371d2 35 mDot* dot;
wang1tao 13:c966b64b227f 36 std::vector<uint8_t> sendData, recvData;
wang1tao 13:c966b64b227f 37 std::vector<uint8_t> turnonData,turnoffData;
wang1tao 13:c966b64b227f 38 char sendBuf[11],recvBuf[30];
wang1tao 13:c966b64b227f 39 int i;
wang1tao 9:cf45820af9b9 40
mfiore 0:09250cd371d2 41 // get a mDot handle
mfiore 0:09250cd371d2 42 dot = mDot::getInstance();
JCheng 5:6c7d2f4d7377 43
mfiore 2:6e2c378339d9 44 // print library version information
mfiore 2:6e2c378339d9 45 logInfo("version: %s", dot->getId().c_str());
mfiore 0:09250cd371d2 46
mfiore 2:6e2c378339d9 47 //*******************************************
mfiore 2:6e2c378339d9 48 // configuration
mfiore 2:6e2c378339d9 49 //*******************************************
mfiore 0:09250cd371d2 50 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 51 dot->resetConfig();
JCheng 5:6c7d2f4d7377 52
mfiore 4:36e214ebfa56 53 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
wang1tao 13:c966b64b227f 54
mfiore 2:6e2c378339d9 55 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 56 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
JCheng 5:6c7d2f4d7377 57
mfiore 4:36e214ebfa56 58 // frequency sub band is only applicable in the 915 (US) frequency band
mfiore 4:36e214ebfa56 59 // 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 60 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
mfiore 2:6e2c378339d9 61 logInfo("setting frequency sub band");
mfiore 0:09250cd371d2 62 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 63 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 2:6e2c378339d9 64 }
JCheng 5:6c7d2f4d7377 65
mfiore 2:6e2c378339d9 66 logInfo("setting network name");
mfiore 2:6e2c378339d9 67 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 68 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 69 }
JCheng 5:6c7d2f4d7377 70
mfiore 2:6e2c378339d9 71 logInfo("setting network password");
mfiore 2:6e2c378339d9 72 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 73 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 74 }
JCheng 5:6c7d2f4d7377 75
mfiore 4:36e214ebfa56 76 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 77 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 78 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
mfiore 4:36e214ebfa56 79 logInfo("setting TX spreading factor");
mfiore 4:36e214ebfa56 80 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 81 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 82 }
JCheng 5:6c7d2f4d7377 83
mfiore 4:36e214ebfa56 84 // request receive confirmation of packets from the gateway
mfiore 4:36e214ebfa56 85 logInfo("enabling ACKs");
mfiore 4:36e214ebfa56 86 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 87 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 88 }
JCheng 5:6c7d2f4d7377 89
mfiore 4:36e214ebfa56 90 // save this configuration to the mDot's NVM
mfiore 2:6e2c378339d9 91 logInfo("saving config");
mfiore 2:6e2c378339d9 92 if (! dot->saveConfig()) {
mfiore 2:6e2c378339d9 93 logError("failed to save configuration");
mfiore 0:09250cd371d2 94 }
mfiore 2:6e2c378339d9 95 //*******************************************
mfiore 2:6e2c378339d9 96 // end of configuration
mfiore 2:6e2c378339d9 97 //*******************************************
mfiore 0:09250cd371d2 98
mfiore 0:09250cd371d2 99 // attempt to join the network
mfiore 2:6e2c378339d9 100 logInfo("joining network");
mfiore 0:09250cd371d2 101 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 102 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 103 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 104 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 105 }
mfiore 0:09250cd371d2 106 // format data for sending to the gateway
wang1tao 13:c966b64b227f 107 for(i=0; i< strlen(TURNON); i++ )
wang1tao 13:c966b64b227f 108 turnonData.push_back( TURNON[i] );
wang1tao 13:c966b64b227f 109
wang1tao 13:c966b64b227f 110 for(i=0; i< strlen(TURNOFF); i++ )
wang1tao 13:c966b64b227f 111 turnoffData.push_back( TURNOFF[i] );
wang1tao 13:c966b64b227f 112 // format data for sending to the gateway
wang1tao 13:c966b64b227f 113 for(i=0; i< strlen(_header); i++ )
wang1tao 13:c966b64b227f 114 sendData.push_back( _header[i] );
wang1tao 9:cf45820af9b9 115
wang1tao 9:cf45820af9b9 116 // send the data to the gateway
wang1tao 13:c966b64b227f 117 if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
wang1tao 9:cf45820af9b9 118 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 9:cf45820af9b9 119 } else {
wang1tao 9:cf45820af9b9 120 logInfo("successfully sent data to gateway");
wang1tao 9:cf45820af9b9 121 }
mfiore 0:09250cd371d2 122 while (true) {
wang1tao 13:c966b64b227f 123 for(i=0;i<30;i++)recvBuf[i]=0;
wang1tao 13:c966b64b227f 124 if ((ret = dot->send(turnonData)) != mDot::MDOT_OK) {
wang1tao 13:c966b64b227f 125 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 13:c966b64b227f 126 } else {
wang1tao 13:c966b64b227f 127 logInfo("successfully sent data to gateway");
wang1tao 13:c966b64b227f 128 recvData.clear();
wang1tao 13:c966b64b227f 129 if ((ret = dot->recv(recvData)) != mDot::MDOT_OK) {
wang1tao 13:c966b64b227f 130 logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 13:c966b64b227f 131 } else {
wang1tao 13:c966b64b227f 132 logInfo("datasize = %d", recvData.size());
wang1tao 13:c966b64b227f 133 for( int i=0; i< recvData.size(); i++ )
wang1tao 13:c966b64b227f 134 recvBuf[i] = recvData[i];
wang1tao 13:c966b64b227f 135 logInfo("%s", recvBuf);
wang1tao 13:c966b64b227f 136 if(strCmp(recvBuf, TURN_ON_ALARM, recvData.size()))logInfo("Turn on the alarm");
wang1tao 13:c966b64b227f 137 if(strCmp(recvBuf, TURN_OFF_ALARM, recvData.size()))logInfo("Turn off the alarm");
wang1tao 13:c966b64b227f 138 }
wang1tao 13:c966b64b227f 139 }
wang1tao 13:c966b64b227f 140 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
wang1tao 13:c966b64b227f 141 for(i=0;i<30;i++)recvBuf[i]=0;
wang1tao 13:c966b64b227f 142 if ((ret = dot->send(turnoffData)) != mDot::MDOT_OK) {
wang1tao 13:c966b64b227f 143 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 13:c966b64b227f 144 } else {
wang1tao 13:c966b64b227f 145 logInfo("successfully sent data to gateway");
wang1tao 13:c966b64b227f 146 recvData.clear();
wang1tao 13:c966b64b227f 147 if ((ret = dot->recv(recvData)) != mDot::MDOT_OK) {
wang1tao 13:c966b64b227f 148 logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 13:c966b64b227f 149 } else {
wang1tao 13:c966b64b227f 150 logInfo("datasize = %d", recvData.size());
wang1tao 13:c966b64b227f 151 for( int i=0; i< recvData.size(); i++ )
wang1tao 13:c966b64b227f 152 recvBuf[i] = recvData[i];
wang1tao 13:c966b64b227f 153 logInfo("%s", recvBuf);
wang1tao 13:c966b64b227f 154 if(strCmp(recvBuf, TURN_ON_ALARM, recvData.size()))logInfo("Turn on the alarm");
wang1tao 13:c966b64b227f 155 if(strCmp(recvBuf, TURN_OFF_ALARM, recvData.size()))logInfo("Turn off the alarm");
wang1tao 13:c966b64b227f 156 }
wang1tao 13:c966b64b227f 157 }
mfiore 4:36e214ebfa56 158 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 159 }
mfiore 0:09250cd371d2 160
mfiore 0:09250cd371d2 161 }