first_commit
Dependencies: SLAVE_LIB libmDot-mbed5 mbed-rtos mbed
Fork of mDot_LoRa_Connect_Example by
main.cpp
00001 #include "mbed.h" 00002 #include "mDot.h" 00003 #include "MTSLog.h" 00004 #include "SPISlave.h" 00005 #include <string> 00006 #include <vector> 00007 #include <algorithm> 00008 00009 // these options must match the settings on your Conduit 00010 // uncomment the following lines and edit their values to match your configuration 00011 static std::string config_network_name = "gobucks1"; 00012 static std::string config_network_pass = "gobucks1"; 00013 static uint8_t config_frequency_sub_band = 1; 00014 00015 int main() 00016 { 00017 int32_t ret; 00018 int i = 0; 00019 mDot* dot; 00020 std::vector<uint8_t> data; 00021 //uint8_t Q = 1; 00022 //Instantiate uint8_t for spi read data 00023 uint8_t spi_data = 0; 00024 uint8_t spi_true = 0; 00025 00026 //Instantiate mDot as SPI Slave 00027 //FOR mDot: MOSI is PA_7, MISO is PA_6, SCLK is PA_5, NSS is PA_4 00028 SPISlave mdot_slave(PA_7, PA_6, PA_5, PA_4); // mosi, miso, sclk, ssel 00029 //Set up spi format with 8 bit data format, mode 0 for mDot slave 00030 mdot_slave.format(8,0); 00031 //Set up 1 MHz frequency for spi transfer 00032 mdot_slave.frequency(1000000); 00033 00034 // get a mDot handle 00035 dot = mDot::getInstance(); 00036 00037 // print library version information 00038 logInfo("version: %s", dot->getId().c_str()); 00039 00040 //******************************************* 00041 // configuration 00042 //******************************************* 00043 // reset to default config so we know what state we're in 00044 dot->resetConfig(); 00045 00046 dot->setLogLevel(mts::MTSLog::INFO_LEVEL); 00047 00048 // set up the mDot with our network information: frequency sub band, network name, and network password 00049 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() 00050 00051 // frequency sub band is only applicable in the 915 (US) frequency band 00052 // 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 00053 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels 00054 00055 // logInfo("setting to public network"); 00056 // if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { 00057 // logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00058 // } 00059 00060 logInfo("setting frequency sub band"); 00061 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { 00062 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00063 } 00064 00065 logInfo("setting network name"); 00066 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { 00067 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00068 } 00069 00070 logInfo("setting network password"); 00071 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { 00072 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00073 } 00074 00075 // a higher spreading factor allows for longer range but lower throughput 00076 // in the 915 (US) frequency band, spreading factors 7 - 10 are available 00077 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available 00078 logInfo("setting TX spreading factor"); 00079 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { 00080 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00081 } 00082 00083 // request receive confirmation of packets from the gateway 00084 logInfo("enabling ACKs"); 00085 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { 00086 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00087 } 00088 00089 // save this configuration to the mDot's NVM 00090 logInfo("saving config"); 00091 if (! dot->saveConfig()) { 00092 logError("failed to save configuration"); 00093 } 00094 //******************************************* 00095 // end of configuration 00096 //******************************************* 00097 00098 // attempt to join the network 00099 logInfo("joining network"); 00100 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { 00101 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); 00102 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again 00103 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); 00104 } 00105 00106 //data.push_back(Q); 00107 00108 // Prime SPI with first reply 00109 //mdot_slave.reply(0); 00110 00111 while (true) { 00112 00113 while(i<10000){ 00114 if(mdot_slave.receive()){ 00115 spi_data=11; 00116 spi_true=1; 00117 } 00118 else{ 00119 spi_data=10; 00120 } 00121 i++; 00122 } 00123 /* 00124 if(mdot_slave.receive()) { 00125 //Read byte from master 00126 spi_data = mdot_slave.read(); 00127 //Reply spi after slave reading 00128 mdot_slave.reply(0); 00129 00130 //push received spi data to data packet vector 00131 data.push_back(spi_data); 00132 00133 // send the data to the gateway 00134 if ((ret = dot->send(data)) != mDot::MDOT_OK) { 00135 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); 00136 } else 00137 logInfo("successfully sent data to gateway"); 00138 00139 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again 00140 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); 00141 00142 //Q++; 00143 //data.clear(); 00144 //data.push_back(Q); 00145 //printf("\nQ = %u\n",Q); 00146 //printf("Q vector = %u\n",data[0]); 00147 00148 //end slave receive if statement 00149 } 00150 else{ 00151 spi_data = 10; 00152 */ 00153 //push received spi data to data packet vector 00154 data.push_back(spi_data); 00155 data.push_back(spi_true); 00156 // send the data to the gateway 00157 if ((ret = dot->send(data)) != mDot::MDOT_OK) { 00158 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); 00159 } else 00160 logInfo("successfully sent data to gateway"); 00161 00162 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again 00163 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); 00164 //} 00165 data.clear(); 00166 spi_true = 0; 00167 i = 0; 00168 //end while(true) loop 00169 } 00170 return 0; 00171 } 00172
Generated on Fri Aug 12 2022 09:05:46 by 1.7.2