fp
Dependencies: libmDot-mbed5 ISL29011
examples/src/ota_example.cpp@40:71d8df33ef7d, 2020-01-06 (annotated)
- Committer:
- harryyyy
- Date:
- Mon Jan 06 08:17:12 2020 +0000
- Revision:
- 40:71d8df33ef7d
- Parent:
- 39:1f8558902454
fp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 0:a151a6350d7f | 1 | #include "dot_util.h" |
Mike Fiore |
14:19fae4509473 | 2 | #include "RadioEvent.h" |
mfiore | 17:d4f82e16de5f | 3 | |
ted12842 | 39:1f8558902454 | 4 | #include <string> |
ted12842 | 39:1f8558902454 | 5 | #include <vector> |
ted12842 | 39:1f8558902454 | 6 | #include <algorithm> |
ted12842 | 39:1f8558902454 | 7 | #include <iostream> |
ted12842 | 39:1f8558902454 | 8 | #include <sstream> |
ted12842 | 39:1f8558902454 | 9 | using namespace std; |
ted12842 | 39:1f8558902454 | 10 | AnalogIn in(PA_5); |
ted12842 | 39:1f8558902454 | 11 | static uint8_t config_app_eui[] = {0x86,0xe4,0xef,0xc7,0x10,0x4f,0x68,0x29 }; |
ted12842 | 39:1f8558902454 | 12 | static uint8_t config_app_key[] = { 0xa3,0x46,0xb6,0xfa,0xef,0x2b,0xd3,0x3c,0x16,0xfe,0x9b,0x1d,0x8d,0x47,0xa1,0x1d}; |
mfiore | 0:a151a6350d7f | 13 | |
ted12842 | 39:1f8558902454 | 14 | // use the same subband as gateway that gateway can listen to you |
ted12842 | 39:1f8558902454 | 15 | static uint8_t config_frequency_sub_band = 1; |
ted12842 | 39:1f8558902454 | 16 | mDot *dot; |
Mike Fiore |
21:09d05faf0e13 | 17 | lora::ChannelPlan* plan = NULL; |
mfiore | 0:a151a6350d7f | 18 | |
ted12842 | 39:1f8558902454 | 19 | #if ACTIVE_EXAMPLE == OTA_EXAMPLE |
ted12842 | 39:1f8558902454 | 20 | void config(mDot *dot,lora::ChannelPlan *plan) |
ted12842 | 39:1f8558902454 | 21 | { |
ted12842 | 39:1f8558902454 | 22 | uint8_t ret; |
ted12842 | 39:1f8558902454 | 23 | // reset to default config so we know what state we're in |
ted12842 | 39:1f8558902454 | 24 | dot->resetConfig(); |
ted12842 | 39:1f8558902454 | 25 | // set how many log info will be show |
ted12842 | 39:1f8558902454 | 26 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
ted12842 | 39:1f8558902454 | 27 | // set subband frequency the same as gateway so gateway can listen to you |
ted12842 | 39:1f8558902454 | 28 | logInfo("setting frequency sub band\r\n"); |
ted12842 | 39:1f8558902454 | 29 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
ted12842 | 39:1f8558902454 | 30 | logError("failed to set frequency sub band %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); |
ted12842 | 39:1f8558902454 | 31 | } |
ted12842 | 39:1f8558902454 | 32 | // lora has private network and public network here we use public network |
ted12842 | 39:1f8558902454 | 33 | logInfo("setting public network mode"); |
ted12842 | 39:1f8558902454 | 34 | if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { |
ted12842 | 39:1f8558902454 | 35 | logError("failed to public network mode"); |
ted12842 | 39:1f8558902454 | 36 | } |
ted12842 | 39:1f8558902454 | 37 | std::vector<uint8_t> temp; |
ted12842 | 39:1f8558902454 | 38 | |
ted12842 | 39:1f8558902454 | 39 | for (int i = 0; i < 8; i++) { |
ted12842 | 39:1f8558902454 | 40 | temp.push_back(config_app_eui[i]); |
ted12842 | 39:1f8558902454 | 41 | } |
ted12842 | 39:1f8558902454 | 42 | // set network id |
ted12842 | 39:1f8558902454 | 43 | logInfo("setting app eui\r\n"); |
ted12842 | 39:1f8558902454 | 44 | if ((ret = dot->setNetworkId(temp)) != mDot::MDOT_OK) { |
ted12842 | 39:1f8558902454 | 45 | logError("failed to set app eui %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); |
ted12842 | 39:1f8558902454 | 46 | } |
ted12842 | 39:1f8558902454 | 47 | temp.clear(); |
ted12842 | 39:1f8558902454 | 48 | for (int i = 0; i < 16; i++) { |
ted12842 | 39:1f8558902454 | 49 | temp.push_back(config_app_key[i]); |
ted12842 | 39:1f8558902454 | 50 | } |
ted12842 | 39:1f8558902454 | 51 | // set network key |
ted12842 | 39:1f8558902454 | 52 | logInfo("setting app key\r\n"); |
ted12842 | 39:1f8558902454 | 53 | if ((ret = dot->setNetworkKey(temp)) != mDot::MDOT_OK) { |
ted12842 | 39:1f8558902454 | 54 | logError("failed to set app key %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); |
ted12842 | 39:1f8558902454 | 55 | } |
ted12842 | 39:1f8558902454 | 56 | // a higher spreading factor allows for longer range but lower throughput |
ted12842 | 39:1f8558902454 | 57 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
ted12842 | 39:1f8558902454 | 58 | logInfo("setting TX spreading factor\r\n"); |
ted12842 | 39:1f8558902454 | 59 | if ((ret = dot->setTxDataRate(mDot::DR10)) != mDot::MDOT_OK) { |
ted12842 | 39:1f8558902454 | 60 | logError("failed to set TX datarate %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); |
ted12842 | 39:1f8558902454 | 61 | } |
ted12842 | 39:1f8558902454 | 62 | // request receive confirmation of packets from the gateway |
ted12842 | 39:1f8558902454 | 63 | logInfo("enabling ACKs\r\n"); |
ted12842 | 39:1f8558902454 | 64 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
ted12842 | 39:1f8558902454 | 65 | logError("failed to enable ACKs %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); |
ted12842 | 39:1f8558902454 | 66 | } |
ted12842 | 39:1f8558902454 | 67 | // Set Tx Power |
ted12842 | 39:1f8558902454 | 68 | logInfo("enabling Tx Power\r\n"); |
ted12842 | 39:1f8558902454 | 69 | if ((ret = dot->setTxPower(20)) != mDot::MDOT_OK) { |
ted12842 | 39:1f8558902454 | 70 | logError("failed to enable Tx Power %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); |
ted12842 | 39:1f8558902454 | 71 | } |
ted12842 | 39:1f8558902454 | 72 | // request receive confirmation of packets from the gateway |
ted12842 | 39:1f8558902454 | 73 | logInfo("enabling Tx Data Rate\r\n"); |
ted12842 | 39:1f8558902454 | 74 | if ((ret = dot->setTxDataRate(3)) != mDot::MDOT_OK) { |
ted12842 | 39:1f8558902454 | 75 | logError("failed to enable Tx Data Rate %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); |
ted12842 | 39:1f8558902454 | 76 | } |
ted12842 | 39:1f8558902454 | 77 | // save this configuration to the mDot's NVM |
ted12842 | 39:1f8558902454 | 78 | logInfo("saving config\r\n"); |
ted12842 | 39:1f8558902454 | 79 | if (! dot->saveConfig()) { |
ted12842 | 39:1f8558902454 | 80 | logError("failed to save configuration\r\n"); |
ted12842 | 39:1f8558902454 | 81 | } |
ted12842 | 39:1f8558902454 | 82 | logInfo("joining network\r\n"); |
ted12842 | 39:1f8558902454 | 83 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
ted12842 | 39:1f8558902454 | 84 | logError("failed to join network %d:%s\r\n", ret, mDot::getReturnCodeString(ret).c_str()); |
ted12842 | 39:1f8558902454 | 85 | osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
ted12842 | 39:1f8558902454 | 86 | } |
ted12842 | 39:1f8558902454 | 87 | return; |
ted12842 | 39:1f8558902454 | 88 | } |
ted12842 | 39:1f8558902454 | 89 | int main() |
ted12842 | 39:1f8558902454 | 90 | { |
ted12842 | 39:1f8558902454 | 91 | // object to control the debug board |
ted12842 | 39:1f8558902454 | 92 | |
ted12842 | 39:1f8558902454 | 93 | plan= new lora::ChannelPlan_AS923(); |
ted12842 | 39:1f8558902454 | 94 | |
ted12842 | 39:1f8558902454 | 95 | dot = mDot::getInstance(plan); |
ted12842 | 39:1f8558902454 | 96 | |
ted12842 | 39:1f8558902454 | 97 | // set network |
ted12842 | 39:1f8558902454 | 98 | config(dot,plan); |
harryyyy | 40:71d8df33ef7d | 99 | |
ted12842 | 39:1f8558902454 | 100 | while (true) { |
ted12842 | 39:1f8558902454 | 101 | int tmp,ret; |
ted12842 | 39:1f8558902454 | 102 | std::vector<uint8_t> data; |
ted12842 | 39:1f8558902454 | 103 | stringstream ss; |
ted12842 | 39:1f8558902454 | 104 | std::string data_str ; |
ted12842 | 39:1f8558902454 | 105 | |
ted12842 | 39:1f8558902454 | 106 | tmp = in.read_u16(); |
harryyyy | 40:71d8df33ef7d | 107 | //printf("%d\r\n",tmp); |
ted12842 | 39:1f8558902454 | 108 | |
harryyyy | 40:71d8df33ef7d | 109 | if(tmp < 37000 ){ |
harryyyy | 40:71d8df33ef7d | 110 | // format data for sending to the gateway |
harryyyy | 40:71d8df33ef7d | 111 | ss << "yes"; |
harryyyy | 40:71d8df33ef7d | 112 | ss >> data_str; |
harryyyy | 40:71d8df33ef7d | 113 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
harryyyy | 40:71d8df33ef7d | 114 | data.push_back((uint8_t) *it); |
harryyyy | 40:71d8df33ef7d | 115 | |
harryyyy | 40:71d8df33ef7d | 116 | // send the data to the gateway |
harryyyy | 40:71d8df33ef7d | 117 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
harryyyy | 40:71d8df33ef7d | 118 | logError("failed to send\r\n", ret, mDot::getReturnCodeString(ret).c_str()); |
harryyyy | 40:71d8df33ef7d | 119 | } else { |
harryyyy | 40:71d8df33ef7d | 120 | logInfo("successfully sent data to gateway\r\n"); |
harryyyy | 40:71d8df33ef7d | 121 | } |
harryyyy | 40:71d8df33ef7d | 122 | |
harryyyy | 40:71d8df33ef7d | 123 | // we use US but in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
harryyyy | 40:71d8df33ef7d | 124 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
harryyyy | 40:71d8df33ef7d | 125 | |
harryyyy | 40:71d8df33ef7d | 126 | } |
harryyyy | 40:71d8df33ef7d | 127 | else if (tmp > 40000 ){ |
harryyyy | 40:71d8df33ef7d | 128 | // format data for sending to the gateway |
harryyyy | 40:71d8df33ef7d | 129 | ss << "no"; |
harryyyy | 40:71d8df33ef7d | 130 | ss >> data_str; |
harryyyy | 40:71d8df33ef7d | 131 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
harryyyy | 40:71d8df33ef7d | 132 | data.push_back((uint8_t) *it); |
harryyyy | 40:71d8df33ef7d | 133 | |
harryyyy | 40:71d8df33ef7d | 134 | // send the data to the gateway |
harryyyy | 40:71d8df33ef7d | 135 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
harryyyy | 40:71d8df33ef7d | 136 | logError("failed to send\r\n", ret, mDot::getReturnCodeString(ret).c_str()); |
harryyyy | 40:71d8df33ef7d | 137 | } else { |
harryyyy | 40:71d8df33ef7d | 138 | logInfo("successfully sent data to gateway\r\n"); |
harryyyy | 40:71d8df33ef7d | 139 | } |
harryyyy | 40:71d8df33ef7d | 140 | |
harryyyy | 40:71d8df33ef7d | 141 | // we use US but in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
harryyyy | 40:71d8df33ef7d | 142 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
harryyyy | 40:71d8df33ef7d | 143 | |
ted12842 | 39:1f8558902454 | 144 | } |
ted12842 | 39:1f8558902454 | 145 | } |
mfiore | 0:a151a6350d7f | 146 | |
ted12842 | 39:1f8558902454 | 147 | return 0; |
ted12842 | 39:1f8558902454 | 148 | |
ted12842 | 39:1f8558902454 | 149 | } |
ted12842 | 39:1f8558902454 | 150 | |
ted12842 | 39:1f8558902454 | 151 | |
ted12842 | 39:1f8558902454 | 152 | |
Evan Hosseini |
30:2f5ae37e6c47 | 153 | #endif |
Evan Hosseini |
30:2f5ae37e6c47 | 154 |