DOT_Example_16_10_2018

Dependencies:   libmDot-mbed5 ISL29011

Committer:
nguyenhoang9x5555
Date:
Tue Oct 16 08:05:27 2018 +0000
Revision:
0:6a6c127cf398
DOT_EXample_16_10_2018

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nguyenhoang9x5555 0:6a6c127cf398 1 #include "dot_util.h"
nguyenhoang9x5555 0:6a6c127cf398 2 #include "RadioEvent.h"
nguyenhoang9x5555 0:6a6c127cf398 3 #include "string.h"
nguyenhoang9x5555 0:6a6c127cf398 4 #if ACTIVE_EXAMPLE == OTA_EXAMPLE
nguyenhoang9x5555 0:6a6c127cf398 5
nguyenhoang9x5555 0:6a6c127cf398 6 static std::string network_name = "MultiTech";
nguyenhoang9x5555 0:6a6c127cf398 7 static std::string network_passphrase = "MultiTech";
nguyenhoang9x5555 0:6a6c127cf398 8 static uint8_t network_id[] = { 0x6C, 0x4E, 0xEF, 0x66, 0xF4, 0x79, 0x86, 0xA6 };
nguyenhoang9x5555 0:6a6c127cf398 9 static uint8_t network_key[] = { 0x1F, 0x33, 0xA1, 0x70, 0xA5, 0xF1, 0xFD, 0xA0, 0xAB, 0x69, 0x7A, 0xAE, 0x2B, 0x95, 0x91, 0x6B };
nguyenhoang9x5555 0:6a6c127cf398 10 static uint8_t frequency_sub_band = 0;
nguyenhoang9x5555 0:6a6c127cf398 11 static bool public_network = true;
nguyenhoang9x5555 0:6a6c127cf398 12 static uint8_t join_delay = 5;
nguyenhoang9x5555 0:6a6c127cf398 13 static uint8_t ack = 1;
nguyenhoang9x5555 0:6a6c127cf398 14 static bool adr = true;
nguyenhoang9x5555 0:6a6c127cf398 15
nguyenhoang9x5555 0:6a6c127cf398 16 static bool deep_sleep = false;
nguyenhoang9x5555 0:6a6c127cf398 17
nguyenhoang9x5555 0:6a6c127cf398 18 mDot* dot = NULL;
nguyenhoang9x5555 0:6a6c127cf398 19 lora::ChannelPlan* plan = NULL;
nguyenhoang9x5555 0:6a6c127cf398 20
nguyenhoang9x5555 0:6a6c127cf398 21 Serial pc(USBTX, USBRX);
nguyenhoang9x5555 0:6a6c127cf398 22
nguyenhoang9x5555 0:6a6c127cf398 23 char buffer_data[20];
nguyenhoang9x5555 0:6a6c127cf398 24
nguyenhoang9x5555 0:6a6c127cf398 25 int main() {
nguyenhoang9x5555 0:6a6c127cf398 26 // Custom event handler for automatically displaying RX data
nguyenhoang9x5555 0:6a6c127cf398 27 RadioEvent events;
nguyenhoang9x5555 0:6a6c127cf398 28 pc.baud(115200);
nguyenhoang9x5555 0:6a6c127cf398 29
nguyenhoang9x5555 0:6a6c127cf398 30 int _array_Tx[7] = {2,5,8,11,14,17,20};
nguyenhoang9x5555 0:6a6c127cf398 31 // Caution : at here convert position follow position of _array_Tx
nguyenhoang9x5555 0:6a6c127cf398 32 int _min_Tx = 3; // ~ Tx = 11 dBm = _array_Tx[3]
nguyenhoang9x5555 0:6a6c127cf398 33 int _max_Tx = 3; // ~ Tx = 11 dBm
nguyenhoang9x5555 0:6a6c127cf398 34
nguyenhoang9x5555 0:6a6c127cf398 35 int _array_DR[8] = {0,1,2,3,4,5,6,7};
nguyenhoang9x5555 0:6a6c127cf398 36 int _min_DR = 2;
nguyenhoang9x5555 0:6a6c127cf398 37 int _max_DR = 2;
nguyenhoang9x5555 0:6a6c127cf398 38 wait(2);
nguyenhoang9x5555 0:6a6c127cf398 39
nguyenhoang9x5555 0:6a6c127cf398 40 /////////////////////////////// function
nguyenhoang9x5555 0:6a6c127cf398 41
nguyenhoang9x5555 0:6a6c127cf398 42 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
nguyenhoang9x5555 0:6a6c127cf398 43
nguyenhoang9x5555 0:6a6c127cf398 44 #if CHANNEL_PLAN == CP_US915
nguyenhoang9x5555 0:6a6c127cf398 45 plan = new lora::ChannelPlan_US915();
nguyenhoang9x5555 0:6a6c127cf398 46 #elif CHANNEL_PLAN == CP_AS923
nguyenhoang9x5555 0:6a6c127cf398 47 plan = new lora::ChannelPlan_AS923();
nguyenhoang9x5555 0:6a6c127cf398 48 #endif
nguyenhoang9x5555 0:6a6c127cf398 49 assert(plan);
nguyenhoang9x5555 0:6a6c127cf398 50
nguyenhoang9x5555 0:6a6c127cf398 51 dot = mDot::getInstance(plan);
nguyenhoang9x5555 0:6a6c127cf398 52 assert(dot);
nguyenhoang9x5555 0:6a6c127cf398 53
nguyenhoang9x5555 0:6a6c127cf398 54 // attach the custom events handler
nguyenhoang9x5555 0:6a6c127cf398 55 dot->setEvents(&events);
nguyenhoang9x5555 0:6a6c127cf398 56
nguyenhoang9x5555 0:6a6c127cf398 57 if (!dot->getStandbyFlag())
nguyenhoang9x5555 0:6a6c127cf398 58 {
nguyenhoang9x5555 0:6a6c127cf398 59 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
nguyenhoang9x5555 0:6a6c127cf398 60
nguyenhoang9x5555 0:6a6c127cf398 61 // start from a well-known state
nguyenhoang9x5555 0:6a6c127cf398 62 logInfo("defaulting Dot configuration");
nguyenhoang9x5555 0:6a6c127cf398 63 dot->resetConfig();
nguyenhoang9x5555 0:6a6c127cf398 64 dot->resetNetworkSession();
nguyenhoang9x5555 0:6a6c127cf398 65
nguyenhoang9x5555 0:6a6c127cf398 66 // make sure library logging is turned on
nguyenhoang9x5555 0:6a6c127cf398 67 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
nguyenhoang9x5555 0:6a6c127cf398 68
nguyenhoang9x5555 0:6a6c127cf398 69 // update configuration if necessary
nguyenhoang9x5555 0:6a6c127cf398 70 if (dot->getJoinMode() != mDot::OTA)
nguyenhoang9x5555 0:6a6c127cf398 71 {
nguyenhoang9x5555 0:6a6c127cf398 72 logInfo("changing network join mode to OTA");
nguyenhoang9x5555 0:6a6c127cf398 73 if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK)
nguyenhoang9x5555 0:6a6c127cf398 74 {
nguyenhoang9x5555 0:6a6c127cf398 75 logError("failed to set network join mode to OTA");
nguyenhoang9x5555 0:6a6c127cf398 76 }
nguyenhoang9x5555 0:6a6c127cf398 77 }
nguyenhoang9x5555 0:6a6c127cf398 78 // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY
nguyenhoang9x5555 0:6a6c127cf398 79 // only one method or the other should be used!
nguyenhoang9x5555 0:6a6c127cf398 80 // network ID = crc64(network name)
nguyenhoang9x5555 0:6a6c127cf398 81 // network KEY = cmac(network passphrase)
nguyenhoang9x5555 0:6a6c127cf398 82 update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
nguyenhoang9x5555 0:6a6c127cf398 83 //update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
nguyenhoang9x5555 0:6a6c127cf398 84
nguyenhoang9x5555 0:6a6c127cf398 85 // configure network link checks
nguyenhoang9x5555 0:6a6c127cf398 86 // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots
nguyenhoang9x5555 0:6a6c127cf398 87 // check the link every count packets
nguyenhoang9x5555 0:6a6c127cf398 88 // declare the Dot disconnected after threshold failed link checks
nguyenhoang9x5555 0:6a6c127cf398 89 // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row
nguyenhoang9x5555 0:6a6c127cf398 90 update_network_link_check_config(3, 5);
nguyenhoang9x5555 0:6a6c127cf398 91
nguyenhoang9x5555 0:6a6c127cf398 92 // enable or disable Adaptive Data Rate
nguyenhoang9x5555 0:6a6c127cf398 93 // dot->setAdr(adr);
nguyenhoang9x5555 0:6a6c127cf398 94 // dot->setTxDataRate(2);
nguyenhoang9x5555 0:6a6c127cf398 95
nguyenhoang9x5555 0:6a6c127cf398 96 // dot->setTxPower(5);
nguyenhoang9x5555 0:6a6c127cf398 97 // logInfo(" du lieu TxPower %u",dot->getTxPower());
nguyenhoang9x5555 0:6a6c127cf398 98 // Configure the join delay
nguyenhoang9x5555 0:6a6c127cf398 99 dot->setJoinDelay(join_delay);
nguyenhoang9x5555 0:6a6c127cf398 100
nguyenhoang9x5555 0:6a6c127cf398 101 // save changes to configuration
nguyenhoang9x5555 0:6a6c127cf398 102 logInfo("saving configuration");
nguyenhoang9x5555 0:6a6c127cf398 103 if (!dot->saveConfig()) {
nguyenhoang9x5555 0:6a6c127cf398 104 logError("failed to save configuration");
nguyenhoang9x5555 0:6a6c127cf398 105 }
nguyenhoang9x5555 0:6a6c127cf398 106 // display configuration
nguyenhoang9x5555 0:6a6c127cf398 107 display_config();
nguyenhoang9x5555 0:6a6c127cf398 108 }
nguyenhoang9x5555 0:6a6c127cf398 109 else
nguyenhoang9x5555 0:6a6c127cf398 110 {
nguyenhoang9x5555 0:6a6c127cf398 111 // restore the saved session if the dot woke from deepsleep mode
nguyenhoang9x5555 0:6a6c127cf398 112 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
nguyenhoang9x5555 0:6a6c127cf398 113 logInfo("restoring network session from NVM");
nguyenhoang9x5555 0:6a6c127cf398 114 dot->restoreNetworkSession();
nguyenhoang9x5555 0:6a6c127cf398 115 }
nguyenhoang9x5555 0:6a6c127cf398 116 /////////////////////////////////////////// end function
nguyenhoang9x5555 0:6a6c127cf398 117
nguyenhoang9x5555 0:6a6c127cf398 118 while(true) // intial infinite loop from here
nguyenhoang9x5555 0:6a6c127cf398 119 {
nguyenhoang9x5555 0:6a6c127cf398 120 int _value_loop = 10;
nguyenhoang9x5555 0:6a6c127cf398 121
nguyenhoang9x5555 0:6a6c127cf398 122 pc.printf(" START begin now - please press button OPEN on APP SERIAL to BEGIN --- ");
nguyenhoang9x5555 0:6a6c127cf398 123 // fix at here
nguyenhoang9x5555 0:6a6c127cf398 124 while (_value_loop)
nguyenhoang9x5555 0:6a6c127cf398 125 {
nguyenhoang9x5555 0:6a6c127cf398 126
nguyenhoang9x5555 0:6a6c127cf398 127 pc.scanf("%s",&buffer_data);
nguyenhoang9x5555 0:6a6c127cf398 128 pc.printf(" DATA received %s \n ", buffer_data);
nguyenhoang9x5555 0:6a6c127cf398 129 if(buffer_data[0] == 'T' && buffer_data[1] =='x' && buffer_data[6] =='P')
nguyenhoang9x5555 0:6a6c127cf398 130 {
nguyenhoang9x5555 0:6a6c127cf398 131 _value_loop = 0;
nguyenhoang9x5555 0:6a6c127cf398 132 break;
nguyenhoang9x5555 0:6a6c127cf398 133 }
nguyenhoang9x5555 0:6a6c127cf398 134 }
nguyenhoang9x5555 0:6a6c127cf398 135
nguyenhoang9x5555 0:6a6c127cf398 136 // check string config TxDR00PWR11TO17OK
nguyenhoang9x5555 0:6a6c127cf398 137 if(buffer_data[0] != 'T' && buffer_data[1] !='x' && buffer_data[6] !='P')
nguyenhoang9x5555 0:6a6c127cf398 138 return 0;
nguyenhoang9x5555 0:6a6c127cf398 139
nguyenhoang9x5555 0:6a6c127cf398 140 // process string config
nguyenhoang9x5555 0:6a6c127cf398 141 int _array_get_config[6] ={0,0,0,0,0,0};
nguyenhoang9x5555 0:6a6c127cf398 142 int _count_config = 0;
nguyenhoang9x5555 0:6a6c127cf398 143 for(int run = 0; run < sizeof(buffer_data); run++)
nguyenhoang9x5555 0:6a6c127cf398 144 {
nguyenhoang9x5555 0:6a6c127cf398 145 if( buffer_data[run] >='0' && buffer_data[run] <= '9')
nguyenhoang9x5555 0:6a6c127cf398 146 {
nguyenhoang9x5555 0:6a6c127cf398 147 _array_get_config[_count_config] = int (buffer_data[run] -48);
nguyenhoang9x5555 0:6a6c127cf398 148 _count_config ++;
nguyenhoang9x5555 0:6a6c127cf398 149 }
nguyenhoang9x5555 0:6a6c127cf398 150 }
nguyenhoang9x5555 0:6a6c127cf398 151
nguyenhoang9x5555 0:6a6c127cf398 152 wait(2);
nguyenhoang9x5555 0:6a6c127cf398 153 // set DR and Tx
nguyenhoang9x5555 0:6a6c127cf398 154 pc.printf(" Get value Done \n ");
nguyenhoang9x5555 0:6a6c127cf398 155 _min_DR = _array_get_config[0];
nguyenhoang9x5555 0:6a6c127cf398 156 _max_DR = _array_get_config[1];
nguyenhoang9x5555 0:6a6c127cf398 157 _min_Tx = _array_get_config[2];
nguyenhoang9x5555 0:6a6c127cf398 158 _max_Tx = _array_get_config[3];
nguyenhoang9x5555 0:6a6c127cf398 159 // wait for stable
nguyenhoang9x5555 0:6a6c127cf398 160 wait(5);
nguyenhoang9x5555 0:6a6c127cf398 161
nguyenhoang9x5555 0:6a6c127cf398 162 // function mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); ===>> dot->restoreNetworkSession();
nguyenhoang9x5555 0:6a6c127cf398 163
nguyenhoang9x5555 0:6a6c127cf398 164 int _count_while = 1;
nguyenhoang9x5555 0:6a6c127cf398 165 while (_count_while-- >0)
nguyenhoang9x5555 0:6a6c127cf398 166 {
nguyenhoang9x5555 0:6a6c127cf398 167 std::vector<uint8_t> tx_data;
nguyenhoang9x5555 0:6a6c127cf398 168 tx_data.clear();
nguyenhoang9x5555 0:6a6c127cf398 169 tx_data.push_back(0x1D);
nguyenhoang9x5555 0:6a6c127cf398 170 tx_data.push_back(0x1A);
nguyenhoang9x5555 0:6a6c127cf398 171 tx_data.push_back(0x1B);
nguyenhoang9x5555 0:6a6c127cf398 172 tx_data.push_back(0xBB);
nguyenhoang9x5555 0:6a6c127cf398 173 tx_data.push_back(0xCC);
nguyenhoang9x5555 0:6a6c127cf398 174 tx_data.push_back(0xDD);
nguyenhoang9x5555 0:6a6c127cf398 175 tx_data.push_back(0xEE);
nguyenhoang9x5555 0:6a6c127cf398 176 tx_data.push_back(0xFF);
nguyenhoang9x5555 0:6a6c127cf398 177 tx_data.push_back(0x1D);
nguyenhoang9x5555 0:6a6c127cf398 178
nguyenhoang9x5555 0:6a6c127cf398 179 // join network if not joined
nguyenhoang9x5555 0:6a6c127cf398 180
nguyenhoang9x5555 0:6a6c127cf398 181 for(int x = _min_Tx ; x <= _max_Tx ; x++)
nguyenhoang9x5555 0:6a6c127cf398 182 {
nguyenhoang9x5555 0:6a6c127cf398 183
nguyenhoang9x5555 0:6a6c127cf398 184 //dot->setTxPower(_array_Tx[x]);
nguyenhoang9x5555 0:6a6c127cf398 185 if (dot->setTxPower(_array_Tx[x]) != mDot::MDOT_OK) {
nguyenhoang9x5555 0:6a6c127cf398 186 logError("failed to set TX power to %u", _array_Tx[x]);
nguyenhoang9x5555 0:6a6c127cf398 187 }
nguyenhoang9x5555 0:6a6c127cf398 188 else{
nguyenhoang9x5555 0:6a6c127cf398 189 logInfo("\n Data with POWER TRANSMITION %d ============= TESTING ==== \n",_array_Tx[x]);
nguyenhoang9x5555 0:6a6c127cf398 190 }
nguyenhoang9x5555 0:6a6c127cf398 191 wait(3);
nguyenhoang9x5555 0:6a6c127cf398 192
nguyenhoang9x5555 0:6a6c127cf398 193 for(int i = _min_DR; i <= _max_DR ; i++)
nguyenhoang9x5555 0:6a6c127cf398 194 {
nguyenhoang9x5555 0:6a6c127cf398 195
nguyenhoang9x5555 0:6a6c127cf398 196 //dot->setTxDataRate(_array_DR[i]);
nguyenhoang9x5555 0:6a6c127cf398 197 if (dot->setTxDataRate(_array_DR[i]) != mDot::MDOT_OK) {
nguyenhoang9x5555 0:6a6c127cf398 198 logError("failed to set TX datarate to %u", _array_DR[i]);
nguyenhoang9x5555 0:6a6c127cf398 199 }
nguyenhoang9x5555 0:6a6c127cf398 200 else{
nguyenhoang9x5555 0:6a6c127cf398 201 logInfo(" === Data with DR %d ============= TESTING == \n",i);
nguyenhoang9x5555 0:6a6c127cf398 202 }
nguyenhoang9x5555 0:6a6c127cf398 203
nguyenhoang9x5555 0:6a6c127cf398 204 if (!dot->getNetworkJoinStatus())
nguyenhoang9x5555 0:6a6c127cf398 205 {
nguyenhoang9x5555 0:6a6c127cf398 206 join_network();
nguyenhoang9x5555 0:6a6c127cf398 207 }
nguyenhoang9x5555 0:6a6c127cf398 208
nguyenhoang9x5555 0:6a6c127cf398 209 for(int i = 0; i <3; i++ )
nguyenhoang9x5555 0:6a6c127cf398 210 {
nguyenhoang9x5555 0:6a6c127cf398 211
nguyenhoang9x5555 0:6a6c127cf398 212 send_data(tx_data);
nguyenhoang9x5555 0:6a6c127cf398 213 wait(3);
nguyenhoang9x5555 0:6a6c127cf398 214 pc.printf(" SENDING data test ========== \n");
nguyenhoang9x5555 0:6a6c127cf398 215 pc.printf(" RSSI =%d and SNR =%d >>>> OK Done \n",(dot->getRssiStats()).last,(dot->getSnrStats()).last);
nguyenhoang9x5555 0:6a6c127cf398 216 }
nguyenhoang9x5555 0:6a6c127cf398 217
nguyenhoang9x5555 0:6a6c127cf398 218 }
nguyenhoang9x5555 0:6a6c127cf398 219 }//for
nguyenhoang9x5555 0:6a6c127cf398 220
nguyenhoang9x5555 0:6a6c127cf398 221 }
nguyenhoang9x5555 0:6a6c127cf398 222
nguyenhoang9x5555 0:6a6c127cf398 223 logInfo("\n Data with POWER TRANSMITION %u DR ; va %u PWR \n",dot->getTxDataRate(),dot->getTxPower());
nguyenhoang9x5555 0:6a6c127cf398 224 pc.printf("@@@@@@@@@@ THE END test signal @@@@@@@@@@@@@@@@@@@@@@@@ \n \n \n ");
nguyenhoang9x5555 0:6a6c127cf398 225
nguyenhoang9x5555 0:6a6c127cf398 226 buffer_data[0] ='N';
nguyenhoang9x5555 0:6a6c127cf398 227 buffer_data[1] ='A';
nguyenhoang9x5555 0:6a6c127cf398 228 _value_loop = 10;
nguyenhoang9x5555 0:6a6c127cf398 229
nguyenhoang9x5555 0:6a6c127cf398 230 } // end from infinite loop
nguyenhoang9x5555 0:6a6c127cf398 231 //return 0;
nguyenhoang9x5555 0:6a6c127cf398 232 }
nguyenhoang9x5555 0:6a6c127cf398 233
nguyenhoang9x5555 0:6a6c127cf398 234 #endif