first_commit
Dependencies: SLAVE_LIB libmDot-mbed5 mbed-rtos mbed
Fork of mDot_LoRa_Connect_Example by
main.cpp@7:93000504469c, 2017-04-21 (annotated)
- Committer:
- AMNoll
- Date:
- Fri Apr 21 16:00:02 2017 +0000
- Revision:
- 7:93000504469c
- Parent:
- 6:f0b6fea36e28
dsaas
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:09250cd371d2 | 1 | #include "mbed.h" |
mfiore | 0:09250cd371d2 | 2 | #include "mDot.h" |
mfiore | 4:36e214ebfa56 | 3 | #include "MTSLog.h" |
AMNoll | 7:93000504469c | 4 | #include "SPISlave.h" |
mfiore | 0:09250cd371d2 | 5 | #include <string> |
mfiore | 0:09250cd371d2 | 6 | #include <vector> |
mfiore | 4:36e214ebfa56 | 7 | #include <algorithm> |
mfiore | 0:09250cd371d2 | 8 | |
mfiore | 2:6e2c378339d9 | 9 | // these options must match the settings on your Conduit |
mfiore | 2:6e2c378339d9 | 10 | // uncomment the following lines and edit their values to match your configuration |
AMNoll | 7:93000504469c | 11 | static std::string config_network_name = "gobucks1"; |
AMNoll | 7:93000504469c | 12 | static std::string config_network_pass = "gobucks1"; |
jreiss | 5:e9d78a9bafc5 | 13 | static uint8_t config_frequency_sub_band = 1; |
mfiore | 0:09250cd371d2 | 14 | |
AMNoll | 7:93000504469c | 15 | int main() |
AMNoll | 7:93000504469c | 16 | { |
mfiore | 0:09250cd371d2 | 17 | int32_t ret; |
AMNoll | 7:93000504469c | 18 | int i = 0; |
mfiore | 0:09250cd371d2 | 19 | mDot* dot; |
mfiore | 0:09250cd371d2 | 20 | std::vector<uint8_t> data; |
AMNoll | 7:93000504469c | 21 | //uint8_t Q = 1; |
AMNoll | 7:93000504469c | 22 | //Instantiate uint8_t for spi read data |
AMNoll | 7:93000504469c | 23 | uint8_t spi_data = 0; |
AMNoll | 7:93000504469c | 24 | uint8_t spi_true = 0; |
AMNoll | 7:93000504469c | 25 | |
AMNoll | 7:93000504469c | 26 | //Instantiate mDot as SPI Slave |
AMNoll | 7:93000504469c | 27 | //FOR mDot: MOSI is PA_7, MISO is PA_6, SCLK is PA_5, NSS is PA_4 |
AMNoll | 7:93000504469c | 28 | SPISlave mdot_slave(PA_7, PA_6, PA_5, PA_4); // mosi, miso, sclk, ssel |
AMNoll | 7:93000504469c | 29 | //Set up spi format with 8 bit data format, mode 0 for mDot slave |
AMNoll | 7:93000504469c | 30 | mdot_slave.format(8,0); |
AMNoll | 7:93000504469c | 31 | //Set up 1 MHz frequency for spi transfer |
AMNoll | 7:93000504469c | 32 | mdot_slave.frequency(1000000); |
AMNoll | 7:93000504469c | 33 | |
mfiore | 0:09250cd371d2 | 34 | // get a mDot handle |
mfiore | 0:09250cd371d2 | 35 | dot = mDot::getInstance(); |
AMNoll | 7:93000504469c | 36 | |
mfiore | 2:6e2c378339d9 | 37 | // print library version information |
mfiore | 2:6e2c378339d9 | 38 | logInfo("version: %s", dot->getId().c_str()); |
mfiore | 0:09250cd371d2 | 39 | |
mfiore | 2:6e2c378339d9 | 40 | //******************************************* |
mfiore | 2:6e2c378339d9 | 41 | // configuration |
mfiore | 2:6e2c378339d9 | 42 | //******************************************* |
mfiore | 0:09250cd371d2 | 43 | // reset to default config so we know what state we're in |
mfiore | 0:09250cd371d2 | 44 | dot->resetConfig(); |
AMNoll | 7:93000504469c | 45 | |
mfiore | 4:36e214ebfa56 | 46 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
mfiore | 0:09250cd371d2 | 47 | |
mfiore | 2:6e2c378339d9 | 48 | // set up the mDot with our network information: frequency sub band, network name, and network password |
mfiore | 2:6e2c378339d9 | 49 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
AMNoll | 7:93000504469c | 50 | |
mfiore | 4:36e214ebfa56 | 51 | // frequency sub band is only applicable in the 915 (US) frequency band |
mfiore | 4:36e214ebfa56 | 52 | // 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 | 53 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
AMNoll | 7:93000504469c | 54 | |
jreiss | 6:f0b6fea36e28 | 55 | // logInfo("setting to public network"); |
jreiss | 6:f0b6fea36e28 | 56 | // if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { |
jreiss | 6:f0b6fea36e28 | 57 | // logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
jreiss | 6:f0b6fea36e28 | 58 | // } |
jreiss | 5:e9d78a9bafc5 | 59 | |
AMNoll | 7:93000504469c | 60 | logInfo("setting frequency sub band"); |
mfiore | 0:09250cd371d2 | 61 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 62 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 2:6e2c378339d9 | 63 | } |
AMNoll | 7:93000504469c | 64 | |
mfiore | 2:6e2c378339d9 | 65 | logInfo("setting network name"); |
mfiore | 2:6e2c378339d9 | 66 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 67 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 68 | } |
AMNoll | 7:93000504469c | 69 | |
mfiore | 2:6e2c378339d9 | 70 | logInfo("setting network password"); |
mfiore | 2:6e2c378339d9 | 71 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 72 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 73 | } |
AMNoll | 7:93000504469c | 74 | |
mfiore | 4:36e214ebfa56 | 75 | // a higher spreading factor allows for longer range but lower throughput |
mfiore | 4:36e214ebfa56 | 76 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
mfiore | 4:36e214ebfa56 | 77 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
mfiore | 4:36e214ebfa56 | 78 | logInfo("setting TX spreading factor"); |
mfiore | 4:36e214ebfa56 | 79 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
mfiore | 4:36e214ebfa56 | 80 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 81 | } |
AMNoll | 7:93000504469c | 82 | |
mfiore | 4:36e214ebfa56 | 83 | // request receive confirmation of packets from the gateway |
mfiore | 4:36e214ebfa56 | 84 | logInfo("enabling ACKs"); |
mfiore | 4:36e214ebfa56 | 85 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
mfiore | 4:36e214ebfa56 | 86 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 87 | } |
AMNoll | 7:93000504469c | 88 | |
mfiore | 4:36e214ebfa56 | 89 | // save this configuration to the mDot's NVM |
mfiore | 2:6e2c378339d9 | 90 | logInfo("saving config"); |
mfiore | 2:6e2c378339d9 | 91 | if (! dot->saveConfig()) { |
mfiore | 2:6e2c378339d9 | 92 | logError("failed to save configuration"); |
mfiore | 0:09250cd371d2 | 93 | } |
mfiore | 2:6e2c378339d9 | 94 | //******************************************* |
mfiore | 2:6e2c378339d9 | 95 | // end of configuration |
mfiore | 2:6e2c378339d9 | 96 | //******************************************* |
mfiore | 0:09250cd371d2 | 97 | |
mfiore | 0:09250cd371d2 | 98 | // attempt to join the network |
mfiore | 2:6e2c378339d9 | 99 | logInfo("joining network"); |
mfiore | 0:09250cd371d2 | 100 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 101 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 102 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
mfiore | 4:36e214ebfa56 | 103 | osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
mfiore | 0:09250cd371d2 | 104 | } |
mfiore | 0:09250cd371d2 | 105 | |
AMNoll | 7:93000504469c | 106 | //data.push_back(Q); |
AMNoll | 7:93000504469c | 107 | |
AMNoll | 7:93000504469c | 108 | // Prime SPI with first reply |
AMNoll | 7:93000504469c | 109 | //mdot_slave.reply(0); |
mfiore | 0:09250cd371d2 | 110 | |
mfiore | 0:09250cd371d2 | 111 | while (true) { |
AMNoll | 7:93000504469c | 112 | |
AMNoll | 7:93000504469c | 113 | while(i<10000){ |
AMNoll | 7:93000504469c | 114 | if(mdot_slave.receive()){ |
AMNoll | 7:93000504469c | 115 | spi_data=11; |
AMNoll | 7:93000504469c | 116 | spi_true=1; |
AMNoll | 7:93000504469c | 117 | } |
AMNoll | 7:93000504469c | 118 | else{ |
AMNoll | 7:93000504469c | 119 | spi_data=10; |
AMNoll | 7:93000504469c | 120 | } |
AMNoll | 7:93000504469c | 121 | i++; |
mfiore | 0:09250cd371d2 | 122 | } |
AMNoll | 7:93000504469c | 123 | /* |
AMNoll | 7:93000504469c | 124 | if(mdot_slave.receive()) { |
AMNoll | 7:93000504469c | 125 | //Read byte from master |
AMNoll | 7:93000504469c | 126 | spi_data = mdot_slave.read(); |
AMNoll | 7:93000504469c | 127 | //Reply spi after slave reading |
AMNoll | 7:93000504469c | 128 | mdot_slave.reply(0); |
AMNoll | 7:93000504469c | 129 | |
AMNoll | 7:93000504469c | 130 | //push received spi data to data packet vector |
AMNoll | 7:93000504469c | 131 | data.push_back(spi_data); |
AMNoll | 7:93000504469c | 132 | |
AMNoll | 7:93000504469c | 133 | // send the data to the gateway |
AMNoll | 7:93000504469c | 134 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
AMNoll | 7:93000504469c | 135 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
AMNoll | 7:93000504469c | 136 | } else |
AMNoll | 7:93000504469c | 137 | logInfo("successfully sent data to gateway"); |
mfiore | 0:09250cd371d2 | 138 | |
AMNoll | 7:93000504469c | 139 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
AMNoll | 7:93000504469c | 140 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
AMNoll | 7:93000504469c | 141 | |
AMNoll | 7:93000504469c | 142 | //Q++; |
AMNoll | 7:93000504469c | 143 | //data.clear(); |
AMNoll | 7:93000504469c | 144 | //data.push_back(Q); |
AMNoll | 7:93000504469c | 145 | //printf("\nQ = %u\n",Q); |
AMNoll | 7:93000504469c | 146 | //printf("Q vector = %u\n",data[0]); |
AMNoll | 7:93000504469c | 147 | |
AMNoll | 7:93000504469c | 148 | //end slave receive if statement |
AMNoll | 7:93000504469c | 149 | } |
AMNoll | 7:93000504469c | 150 | else{ |
AMNoll | 7:93000504469c | 151 | spi_data = 10; |
AMNoll | 7:93000504469c | 152 | */ |
AMNoll | 7:93000504469c | 153 | //push received spi data to data packet vector |
AMNoll | 7:93000504469c | 154 | data.push_back(spi_data); |
AMNoll | 7:93000504469c | 155 | data.push_back(spi_true); |
AMNoll | 7:93000504469c | 156 | // send the data to the gateway |
AMNoll | 7:93000504469c | 157 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
AMNoll | 7:93000504469c | 158 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
AMNoll | 7:93000504469c | 159 | } else |
AMNoll | 7:93000504469c | 160 | logInfo("successfully sent data to gateway"); |
AMNoll | 7:93000504469c | 161 | |
AMNoll | 7:93000504469c | 162 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
AMNoll | 7:93000504469c | 163 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
AMNoll | 7:93000504469c | 164 | //} |
AMNoll | 7:93000504469c | 165 | data.clear(); |
AMNoll | 7:93000504469c | 166 | spi_true = 0; |
AMNoll | 7:93000504469c | 167 | i = 0; |
AMNoll | 7:93000504469c | 168 | //end while(true) loop |
mfiore | 0:09250cd371d2 | 169 | } |
mfiore | 0:09250cd371d2 | 170 | return 0; |
mfiore | 0:09250cd371d2 | 171 | } |
AMNoll | 7:93000504469c | 172 |