fist release of rssi test
Dependencies: libmDot mbed-rtos mbed
Fork of CardRead_20170216 by
main.cpp@18:ee1ec5f500bf, 2017-05-08 (annotated)
- Committer:
- wang1tao
- Date:
- Mon May 08 13:47:22 2017 +0000
- Revision:
- 18:ee1ec5f500bf
- Parent:
- 17:b60549bc8626
first release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wang1tao | 16:847c028f4130 | 1 | #include "mbed.h" |
mfiore | 0:09250cd371d2 | 2 | #include "mbed.h" |
mfiore | 0:09250cd371d2 | 3 | #include "mDot.h" |
mfiore | 4:36e214ebfa56 | 4 | #include "MTSLog.h" |
mfiore | 0:09250cd371d2 | 5 | #include <string> |
mfiore | 0:09250cd371d2 | 6 | #include <vector> |
mfiore | 4:36e214ebfa56 | 7 | #include <algorithm> |
wang1tao | 16:847c028f4130 | 8 | #include <math.h> |
Dengjj | 15:04b37f329c84 | 9 | |
wang1tao | 16:847c028f4130 | 10 | #define RETURN_OK 0 |
wang1tao | 16:847c028f4130 | 11 | #define RETURN_ERR -1 |
wang1tao | 16:847c028f4130 | 12 | //define baudrate |
wang1tao | 16:847c028f4130 | 13 | #define PC_BAUDRATE 115200 |
wang1tao | 16:847c028f4130 | 14 | |
wang1tao | 16:847c028f4130 | 15 | /* functions prototype */ |
Dengjj | 15:04b37f329c84 | 16 | |
wang1tao | 16:847c028f4130 | 17 | int configNetwork(void); |
wang1tao | 16:847c028f4130 | 18 | int joinNetwork(void); |
wang1tao | 16:847c028f4130 | 19 | int send_data(char *str); |
wang1tao | 13:5d050b414f16 | 20 | |
wang1tao | 16:847c028f4130 | 21 | /* Global variables */ |
wang1tao | 16:847c028f4130 | 22 | Serial pc (USBTX, USBRX); // tx, rx |
wang1tao | 9:cf45820af9b9 | 23 | |
wang1tao | 16:847c028f4130 | 24 | mDot* dot; |
wang1tao | 16:847c028f4130 | 25 | |
JCheng | 5:6c7d2f4d7377 | 26 | static std::string config_network_name = "chinaiot"; |
JCheng | 5:6c7d2f4d7377 | 27 | static std::string config_network_pass = "password"; |
wang1tao | 10:79124c0a5952 | 28 | static uint8_t config_frequency_sub_band = 2; |
mfiore | 0:09250cd371d2 | 29 | |
wang1tao | 16:847c028f4130 | 30 | int main() |
wang1tao | 16:847c028f4130 | 31 | { |
wang1tao | 18:ee1ec5f500bf | 32 | int32_t ret, counter; |
wang1tao | 18:ee1ec5f500bf | 33 | char dataBuf[15]; |
wang1tao | 16:847c028f4130 | 34 | |
wang1tao | 16:847c028f4130 | 35 | pc.baud(PC_BAUDRATE); |
wang1tao | 16:847c028f4130 | 36 | pc.printf("\n\r\n\r\n\r"); |
wang1tao | 16:847c028f4130 | 37 | pc.printf("============================================\n\r"); |
wang1tao | 16:847c028f4130 | 38 | pc.printf("SiFOX remote card read system!\n\r"); |
wang1tao | 16:847c028f4130 | 39 | pc.printf("============================================\n\r"); |
wang1tao | 16:847c028f4130 | 40 | pc.printf("PC COM RS232 baudrate: %d \n\r", PC_BAUDRATE); |
wang1tao | 16:847c028f4130 | 41 | |
wang1tao | 16:847c028f4130 | 42 | //******************************************* |
wang1tao | 16:847c028f4130 | 43 | // Configurate Network |
wang1tao | 16:847c028f4130 | 44 | //******************************************* |
wang1tao | 16:847c028f4130 | 45 | ret = configNetwork(); |
wang1tao | 16:847c028f4130 | 46 | if(ret != RETURN_OK) |
wang1tao | 16:847c028f4130 | 47 | dot->sleep(5, mDot::RTC_ALARM); //sleep a while and restart |
wang1tao | 16:847c028f4130 | 48 | |
wang1tao | 16:847c028f4130 | 49 | //******************************************* |
wang1tao | 16:847c028f4130 | 50 | // Join Network |
wang1tao | 16:847c028f4130 | 51 | //******************************************* |
wang1tao | 16:847c028f4130 | 52 | ret = joinNetwork(); |
wang1tao | 16:847c028f4130 | 53 | if(ret != RETURN_OK) |
wang1tao | 16:847c028f4130 | 54 | dot->sleep(5, mDot::RTC_ALARM); //sleep a while and restart |
wang1tao | 16:847c028f4130 | 55 | |
wang1tao | 18:ee1ec5f500bf | 56 | counter = 0; |
wang1tao | 16:847c028f4130 | 57 | while(1) { |
wang1tao | 18:ee1ec5f500bf | 58 | sprintf(dataBuf, "Send:%d", counter); |
wang1tao | 18:ee1ec5f500bf | 59 | /* Send data to gateway */ |
wang1tao | 18:ee1ec5f500bf | 60 | send_data(dataBuf); |
wang1tao | 16:847c028f4130 | 61 | /* wait a while */ |
wang1tao | 18:ee1ec5f500bf | 62 | wait(10); |
wang1tao | 18:ee1ec5f500bf | 63 | |
wang1tao | 18:ee1ec5f500bf | 64 | counter++; |
wang1tao | 16:847c028f4130 | 65 | } |
wang1tao | 16:847c028f4130 | 66 | } |
wang1tao | 16:847c028f4130 | 67 | |
wang1tao | 16:847c028f4130 | 68 | |
wang1tao | 16:847c028f4130 | 69 | int configNetwork(void) |
wang1tao | 16:847c028f4130 | 70 | { |
wang1tao | 10:79124c0a5952 | 71 | int32_t ret; |
wang1tao | 16:847c028f4130 | 72 | |
wang1tao | 16:847c028f4130 | 73 | dot = mDot::getInstance(); |
Dengjj | 14:0616e4361f0c | 74 | |
mfiore | 2:6e2c378339d9 | 75 | // print library version information |
wang1tao | 16:847c028f4130 | 76 | pc.printf("version: %s\n\r", dot->getId().c_str()); |
wang1tao | 16:847c028f4130 | 77 | |
mfiore | 2:6e2c378339d9 | 78 | //******************************************* |
mfiore | 2:6e2c378339d9 | 79 | // configuration |
mfiore | 2:6e2c378339d9 | 80 | //******************************************* |
mfiore | 0:09250cd371d2 | 81 | // reset to default config so we know what state we're in |
mfiore | 0:09250cd371d2 | 82 | dot->resetConfig(); |
JCheng | 5:6c7d2f4d7377 | 83 | |
mfiore | 4:36e214ebfa56 | 84 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
Dengjj | 15:04b37f329c84 | 85 | |
mfiore | 2:6e2c378339d9 | 86 | // set up the mDot with our network information: frequency sub band, network name, and network password |
mfiore | 2:6e2c378339d9 | 87 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
JCheng | 5:6c7d2f4d7377 | 88 | |
mfiore | 4:36e214ebfa56 | 89 | // frequency sub band is only applicable in the 915 (US) frequency band |
mfiore | 4:36e214ebfa56 | 90 | // 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 | 91 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
wang1tao | 18:ee1ec5f500bf | 92 | //pc.printf("Setting TX frequency band as 868MHz\n\r"); |
wang1tao | 18:ee1ec5f500bf | 93 | //if ((ret = dot->setTxFrequency(mDot::FB_868)) != mDot::MDOT_OK){ |
wang1tao | 18:ee1ec5f500bf | 94 | // pc.printf("Error:failed to set TX frequency band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 18:ee1ec5f500bf | 95 | // return RETURN_ERR; |
wang1tao | 18:ee1ec5f500bf | 96 | //} |
wang1tao | 18:ee1ec5f500bf | 97 | //pc.printf("Setting RX frequency band as 868MHz\n\r"); |
wang1tao | 18:ee1ec5f500bf | 98 | //if ((ret = dot->setRxFrequency(mDot::FB_868)) != mDot::MDOT_OK){ |
wang1tao | 18:ee1ec5f500bf | 99 | // pc.printf("Error:failed to set RX frequency band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 18:ee1ec5f500bf | 100 | // return RETURN_ERR; |
wang1tao | 18:ee1ec5f500bf | 101 | //} |
wang1tao | 16:847c028f4130 | 102 | pc.printf("Setting frequency sub band\n\r"); |
mfiore | 0:09250cd371d2 | 103 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
wang1tao | 16:847c028f4130 | 104 | pc.printf("Error:failed to set frequency sub band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 16:847c028f4130 | 105 | return RETURN_ERR; |
mfiore | 2:6e2c378339d9 | 106 | } |
JCheng | 5:6c7d2f4d7377 | 107 | |
wang1tao | 16:847c028f4130 | 108 | pc.printf("Setting network name\n\r"); |
mfiore | 2:6e2c378339d9 | 109 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
wang1tao | 16:847c028f4130 | 110 | pc.printf("Error:failed to set network name %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 16:847c028f4130 | 111 | return RETURN_ERR; |
mfiore | 0:09250cd371d2 | 112 | } |
JCheng | 5:6c7d2f4d7377 | 113 | |
wang1tao | 16:847c028f4130 | 114 | pc.printf("Setting network password\n\r"); |
mfiore | 2:6e2c378339d9 | 115 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
wang1tao | 16:847c028f4130 | 116 | pc.printf("Error:failed to set network password %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 16:847c028f4130 | 117 | return RETURN_ERR; |
mfiore | 0:09250cd371d2 | 118 | } |
wang1tao | 16:847c028f4130 | 119 | |
mfiore | 4:36e214ebfa56 | 120 | // a higher spreading factor allows for longer range but lower throughput |
mfiore | 4:36e214ebfa56 | 121 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
mfiore | 4:36e214ebfa56 | 122 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
wang1tao | 16:847c028f4130 | 123 | pc.printf("Setting TX spreading factor\n\r"); |
mfiore | 4:36e214ebfa56 | 124 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
wang1tao | 16:847c028f4130 | 125 | pc.printf("Error:failed to set TX datarate %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 16:847c028f4130 | 126 | return RETURN_ERR; |
mfiore | 4:36e214ebfa56 | 127 | } |
JCheng | 5:6c7d2f4d7377 | 128 | |
mfiore | 4:36e214ebfa56 | 129 | // request receive confirmation of packets from the gateway |
wang1tao | 16:847c028f4130 | 130 | pc.printf("Enabling ACKs\n\r"); |
mfiore | 4:36e214ebfa56 | 131 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
wang1tao | 16:847c028f4130 | 132 | pc.printf("Error:failed to enable ACKs %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 16:847c028f4130 | 133 | return RETURN_OK; |
mfiore | 4:36e214ebfa56 | 134 | } |
JCheng | 5:6c7d2f4d7377 | 135 | |
mfiore | 4:36e214ebfa56 | 136 | // save this configuration to the mDot's NVM |
wang1tao | 16:847c028f4130 | 137 | pc.printf("Saving config\n\r"); |
mfiore | 2:6e2c378339d9 | 138 | if (! dot->saveConfig()) { |
wang1tao | 16:847c028f4130 | 139 | pc.printf("Error:failed to save configuration\n\r"); |
mfiore | 0:09250cd371d2 | 140 | } |
Dengjj | 15:04b37f329c84 | 141 | |
wang1tao | 16:847c028f4130 | 142 | return RETURN_OK; |
wang1tao | 16:847c028f4130 | 143 | } // end of configuration |
mfiore | 0:09250cd371d2 | 144 | |
Dengjj | 15:04b37f329c84 | 145 | |
wang1tao | 16:847c028f4130 | 146 | int joinNetwork(void) |
wang1tao | 16:847c028f4130 | 147 | { |
wang1tao | 16:847c028f4130 | 148 | int32_t ret,i; |
wang1tao | 16:847c028f4130 | 149 | std::vector<uint8_t> sendData; |
wang1tao | 18:ee1ec5f500bf | 150 | char _header[] = "Reset!"; |
wang1tao | 16:847c028f4130 | 151 | |
wang1tao | 16:847c028f4130 | 152 | // attempt to join the network |
wang1tao | 16:847c028f4130 | 153 | pc.printf("Joining network...\n\r"); |
wang1tao | 16:847c028f4130 | 154 | |
wang1tao | 16:847c028f4130 | 155 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
wang1tao | 16:847c028f4130 | 156 | pc.printf("Error: failed to join network %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 16:847c028f4130 | 157 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
wang1tao | 16:847c028f4130 | 158 | return RETURN_ERR; |
mfiore | 0:09250cd371d2 | 159 | } |
mfiore | 0:09250cd371d2 | 160 | |
wang1tao | 16:847c028f4130 | 161 | sendData.clear(); |
wang1tao | 16:847c028f4130 | 162 | // format data for sending to the gateway |
wang1tao | 16:847c028f4130 | 163 | for( i=0; i< strlen(_header); i++ ) |
wang1tao | 16:847c028f4130 | 164 | sendData.push_back( _header[i] ); |
wang1tao | 16:847c028f4130 | 165 | |
wang1tao | 16:847c028f4130 | 166 | // send the data to the gateway |
wang1tao | 16:847c028f4130 | 167 | pc.printf("Send header to the gateway\n\r"); |
wang1tao | 16:847c028f4130 | 168 | |
wang1tao | 16:847c028f4130 | 169 | if ((ret = dot->send(sendData)) != mDot::MDOT_OK) { |
wang1tao | 16:847c028f4130 | 170 | pc.printf("Error: failed to send %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 16:847c028f4130 | 171 | return RETURN_ERR; |
wang1tao | 16:847c028f4130 | 172 | } else { |
wang1tao | 16:847c028f4130 | 173 | pc.printf("Successfully sent data to gateway\n\r"); |
wang1tao | 16:847c028f4130 | 174 | } |
wang1tao | 16:847c028f4130 | 175 | |
wang1tao | 16:847c028f4130 | 176 | return RETURN_OK; |
mfiore | 0:09250cd371d2 | 177 | } |
wang1tao | 16:847c028f4130 | 178 | |
wang1tao | 16:847c028f4130 | 179 | int send_data(char *str) |
wang1tao | 16:847c028f4130 | 180 | { |
wang1tao | 16:847c028f4130 | 181 | int32_t i, ret; |
wang1tao | 16:847c028f4130 | 182 | std::vector<uint8_t> sendData; |
wang1tao | 16:847c028f4130 | 183 | |
wang1tao | 16:847c028f4130 | 184 | //Send the data to Gateway |
wang1tao | 16:847c028f4130 | 185 | sendData.clear(); |
wang1tao | 16:847c028f4130 | 186 | // probably not the most efficent way to do this |
wang1tao | 16:847c028f4130 | 187 | for(i=0; i< strlen(str); i++ ) |
wang1tao | 16:847c028f4130 | 188 | sendData.push_back(str[i] ); |
wang1tao | 16:847c028f4130 | 189 | |
wang1tao | 16:847c028f4130 | 190 | // send the data to the gateway |
wang1tao | 16:847c028f4130 | 191 | pc.printf("Send %s to Gateway \n\r", str); |
wang1tao | 16:847c028f4130 | 192 | |
wang1tao | 16:847c028f4130 | 193 | if ((ret = dot->send(sendData)) != mDot::MDOT_OK) { |
wang1tao | 16:847c028f4130 | 194 | pc.printf("Error:failed to send\n\r", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 16:847c028f4130 | 195 | return RETURN_ERR; |
wang1tao | 16:847c028f4130 | 196 | } else { |
wang1tao | 16:847c028f4130 | 197 | pc.printf("Sent data to gateway successfully!\n\r"); |
wang1tao | 16:847c028f4130 | 198 | } |
wang1tao | 16:847c028f4130 | 199 | return RETURN_OK; |
wang1tao | 16:847c028f4130 | 200 | } |
wang1tao | 16:847c028f4130 | 201 | |
wang1tao | 16:847c028f4130 | 202 |