Da ka xi tong.

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_Connect_IoTClub_one_wire_4ch_8_24 by wireless sensor

Committer:
wang1tao
Date:
Thu Feb 16 14:43:17 2017 +0000
Revision:
16:847c028f4130
Parent:
15:04b37f329c84
Child:
17:b60549bc8626
Dakatitong

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wang1tao 16:847c028f4130 1 #include "mbed.h"
wang1tao 16:847c028f4130 2 //#include "DataLoggerRS232.h"
mfiore 0:09250cd371d2 3 #include "mbed.h"
mfiore 0:09250cd371d2 4 #include "mDot.h"
mfiore 4:36e214ebfa56 5 #include "MTSLog.h"
mfiore 0:09250cd371d2 6 #include <string>
mfiore 0:09250cd371d2 7 #include <vector>
mfiore 4:36e214ebfa56 8 #include <algorithm>
wang1tao 16:847c028f4130 9 #include <math.h>
Dengjj 15:04b37f329c84 10
wang1tao 16:847c028f4130 11 #define RETURN_OK 0
wang1tao 16:847c028f4130 12 #define RETURN_ERR -1
wang1tao 16:847c028f4130 13 //define baudrate
wang1tao 16:847c028f4130 14 #define PC_BAUDRATE 115200
wang1tao 16:847c028f4130 15 #define CARD_MACHINE_BAUDRATE 19200
wang1tao 9:cf45820af9b9 16
wang1tao 16:847c028f4130 17 #define COMMAND_LENGTH 7
wang1tao 16:847c028f4130 18 #define RESPONSE_LENGTH 16
wang1tao 16:847c028f4130 19 #define RINGBUFFER_SIZE 100
wang1tao 16:847c028f4130 20
wang1tao 16:847c028f4130 21 #define READ_PERIOD 1
wang1tao 16:847c028f4130 22 #define SEND_PERIOD 5
wang1tao 16:847c028f4130 23
wang1tao 16:847c028f4130 24 const char readCmd[COMMAND_LENGTH] = {0x09,0x41,0x31,0x46,0x33,0x46,0x0d};
wang1tao 16:847c028f4130 25
wang1tao 16:847c028f4130 26 char readBuf[RESPONSE_LENGTH] = {0};
wang1tao 16:847c028f4130 27 char ringBuffer[RINGBUFFER_SIZE][RESPONSE_LENGTH] = {0};
wang1tao 13:5d050b414f16 28
wang1tao 16:847c028f4130 29 unsigned int inIndex = 0;
wang1tao 16:847c028f4130 30 unsigned int outIndex = 0;
wang1tao 16:847c028f4130 31
wang1tao 16:847c028f4130 32 /* functions prototype */
wang1tao 16:847c028f4130 33 void readCardID(void);
wang1tao 16:847c028f4130 34 void sendCardID(void);
wang1tao 16:847c028f4130 35
wang1tao 16:847c028f4130 36 void my_strcpy(char *dst, char *src);
wang1tao 16:847c028f4130 37 void my_strclear(char *str, unsigned int len);
Dengjj 15:04b37f329c84 38
wang1tao 16:847c028f4130 39 int configNetwork(void);
wang1tao 16:847c028f4130 40 int joinNetwork(void);
wang1tao 16:847c028f4130 41 int send_data(char *str);
wang1tao 13:5d050b414f16 42
wang1tao 16:847c028f4130 43 /* Global variables */
wang1tao 16:847c028f4130 44 Serial pc (USBTX, USBRX); // tx, rx
wang1tao 16:847c028f4130 45 Serial cm_rs485 (PA_2,PA_3); // tx, rx
wang1tao 9:cf45820af9b9 46
wang1tao 16:847c028f4130 47 mDot* dot;
wang1tao 16:847c028f4130 48
JCheng 5:6c7d2f4d7377 49 static std::string config_network_name = "chinaiot";
JCheng 5:6c7d2f4d7377 50 static std::string config_network_pass = "password";
wang1tao 10:79124c0a5952 51 static uint8_t config_frequency_sub_band = 2;
mfiore 0:09250cd371d2 52
wang1tao 16:847c028f4130 53 int main()
wang1tao 16:847c028f4130 54 {
wang1tao 16:847c028f4130 55 int32_t ret;
wang1tao 16:847c028f4130 56
wang1tao 16:847c028f4130 57 pc.baud(PC_BAUDRATE);
wang1tao 16:847c028f4130 58 pc.printf("\n\r\n\r\n\r");
wang1tao 16:847c028f4130 59 pc.printf("============================================\n\r");
wang1tao 16:847c028f4130 60 pc.printf("SiFOX remote card read system!\n\r");
wang1tao 16:847c028f4130 61 pc.printf("============================================\n\r");
wang1tao 16:847c028f4130 62 pc.printf("PC COM RS232 baudrate: %d \n\r", PC_BAUDRATE);
wang1tao 16:847c028f4130 63 cm_rs485.baud(CARD_MACHINE_BAUDRATE);
wang1tao 16:847c028f4130 64 cm_rs485.format(9,SerialBase::Even,1);
wang1tao 16:847c028f4130 65 pc.printf("Card Machine RS485 baudrate: %d!\n\r", CARD_MACHINE_BAUDRATE);
wang1tao 16:847c028f4130 66
wang1tao 16:847c028f4130 67 //*******************************************
wang1tao 16:847c028f4130 68 // Configurate Network
wang1tao 16:847c028f4130 69 //*******************************************
wang1tao 16:847c028f4130 70 ret = configNetwork();
wang1tao 16:847c028f4130 71 if(ret != RETURN_OK)
wang1tao 16:847c028f4130 72 dot->sleep(5, mDot::RTC_ALARM); //sleep a while and restart
wang1tao 16:847c028f4130 73
wang1tao 16:847c028f4130 74 //*******************************************
wang1tao 16:847c028f4130 75 // Join Network
wang1tao 16:847c028f4130 76 //*******************************************
wang1tao 16:847c028f4130 77 ret = joinNetwork();
wang1tao 16:847c028f4130 78 if(ret != RETURN_OK)
wang1tao 16:847c028f4130 79 dot->sleep(5, mDot::RTC_ALARM); //sleep a while and restart
wang1tao 16:847c028f4130 80
wang1tao 16:847c028f4130 81 pc.printf("Wait 15S or card machine booting...\n\r");
wang1tao 16:847c028f4130 82 wait(15);
wang1tao 16:847c028f4130 83
wang1tao 16:847c028f4130 84 while(1) {
wang1tao 16:847c028f4130 85 /* Read Card ID */
wang1tao 16:847c028f4130 86 readCardID();
wang1tao 16:847c028f4130 87
wang1tao 16:847c028f4130 88 /* Send Card ID to gateway */
wang1tao 16:847c028f4130 89 sendCardID();
wang1tao 16:847c028f4130 90
wang1tao 16:847c028f4130 91 /* wait a while */
wang1tao 16:847c028f4130 92 wait(2);
wang1tao 16:847c028f4130 93 }
wang1tao 16:847c028f4130 94 }
wang1tao 16:847c028f4130 95
wang1tao 16:847c028f4130 96
wang1tao 16:847c028f4130 97 int configNetwork(void)
wang1tao 16:847c028f4130 98 {
wang1tao 10:79124c0a5952 99 int32_t ret;
wang1tao 16:847c028f4130 100
wang1tao 16:847c028f4130 101 dot = mDot::getInstance();
Dengjj 14:0616e4361f0c 102
mfiore 2:6e2c378339d9 103 // print library version information
wang1tao 16:847c028f4130 104 pc.printf("version: %s\n\r", dot->getId().c_str());
wang1tao 16:847c028f4130 105
mfiore 2:6e2c378339d9 106 //*******************************************
mfiore 2:6e2c378339d9 107 // configuration
mfiore 2:6e2c378339d9 108 //*******************************************
mfiore 0:09250cd371d2 109 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 110 dot->resetConfig();
JCheng 5:6c7d2f4d7377 111
mfiore 4:36e214ebfa56 112 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
Dengjj 15:04b37f329c84 113
mfiore 2:6e2c378339d9 114 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 115 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
JCheng 5:6c7d2f4d7377 116
mfiore 4:36e214ebfa56 117 // frequency sub band is only applicable in the 915 (US) frequency band
mfiore 4:36e214ebfa56 118 // 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 119 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
wang1tao 16:847c028f4130 120 pc.printf("Setting frequency sub band\n\r");
mfiore 0:09250cd371d2 121 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
wang1tao 16:847c028f4130 122 pc.printf("Error:failed to set frequency sub band %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 16:847c028f4130 123 return RETURN_ERR;
mfiore 2:6e2c378339d9 124 }
JCheng 5:6c7d2f4d7377 125
wang1tao 16:847c028f4130 126 pc.printf("Setting network name\n\r");
mfiore 2:6e2c378339d9 127 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
wang1tao 16:847c028f4130 128 pc.printf("Error:failed to set network name %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 16:847c028f4130 129 return RETURN_ERR;
mfiore 0:09250cd371d2 130 }
JCheng 5:6c7d2f4d7377 131
wang1tao 16:847c028f4130 132 pc.printf("Setting network password\n\r");
mfiore 2:6e2c378339d9 133 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
wang1tao 16:847c028f4130 134 pc.printf("Error:failed to set network password %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 16:847c028f4130 135 return RETURN_ERR;
mfiore 0:09250cd371d2 136 }
wang1tao 16:847c028f4130 137
mfiore 4:36e214ebfa56 138 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 139 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 140 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
wang1tao 16:847c028f4130 141 pc.printf("Setting TX spreading factor\n\r");
mfiore 4:36e214ebfa56 142 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
wang1tao 16:847c028f4130 143 pc.printf("Error:failed to set TX datarate %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 16:847c028f4130 144 return RETURN_ERR;
mfiore 4:36e214ebfa56 145 }
JCheng 5:6c7d2f4d7377 146
mfiore 4:36e214ebfa56 147 // request receive confirmation of packets from the gateway
wang1tao 16:847c028f4130 148 pc.printf("Enabling ACKs\n\r");
mfiore 4:36e214ebfa56 149 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
wang1tao 16:847c028f4130 150 pc.printf("Error:failed to enable ACKs %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 16:847c028f4130 151 return RETURN_OK;
mfiore 4:36e214ebfa56 152 }
JCheng 5:6c7d2f4d7377 153
mfiore 4:36e214ebfa56 154 // save this configuration to the mDot's NVM
wang1tao 16:847c028f4130 155 pc.printf("Saving config\n\r");
mfiore 2:6e2c378339d9 156 if (! dot->saveConfig()) {
wang1tao 16:847c028f4130 157 pc.printf("Error:failed to save configuration\n\r");
mfiore 0:09250cd371d2 158 }
Dengjj 15:04b37f329c84 159
wang1tao 16:847c028f4130 160 return RETURN_OK;
wang1tao 16:847c028f4130 161 } // end of configuration
mfiore 0:09250cd371d2 162
Dengjj 15:04b37f329c84 163
wang1tao 16:847c028f4130 164 int joinNetwork(void)
wang1tao 16:847c028f4130 165 {
wang1tao 16:847c028f4130 166 int32_t ret,i;
wang1tao 16:847c028f4130 167 std::vector<uint8_t> sendData;
wang1tao 16:847c028f4130 168 char _header[] = "ChongQi!";
wang1tao 16:847c028f4130 169
wang1tao 16:847c028f4130 170 // attempt to join the network
wang1tao 16:847c028f4130 171 pc.printf("Joining network...\n\r");
wang1tao 16:847c028f4130 172
wang1tao 16:847c028f4130 173 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
wang1tao 16:847c028f4130 174 pc.printf("Error: failed to join network %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 16:847c028f4130 175 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
wang1tao 16:847c028f4130 176 return RETURN_ERR;
mfiore 0:09250cd371d2 177 }
mfiore 0:09250cd371d2 178
wang1tao 16:847c028f4130 179 sendData.clear();
wang1tao 16:847c028f4130 180 // format data for sending to the gateway
wang1tao 16:847c028f4130 181 for( i=0; i< strlen(_header); i++ )
wang1tao 16:847c028f4130 182 sendData.push_back( _header[i] );
wang1tao 16:847c028f4130 183
wang1tao 16:847c028f4130 184 // send the data to the gateway
wang1tao 16:847c028f4130 185 pc.printf("Send header to the gateway\n\r");
wang1tao 16:847c028f4130 186
wang1tao 16:847c028f4130 187 if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
wang1tao 16:847c028f4130 188 pc.printf("Error: failed to send %d:%s\n\r", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 16:847c028f4130 189 return RETURN_ERR;
wang1tao 16:847c028f4130 190 } else {
wang1tao 16:847c028f4130 191 pc.printf("Successfully sent data to gateway\n\r");
wang1tao 16:847c028f4130 192 }
wang1tao 16:847c028f4130 193
wang1tao 16:847c028f4130 194 return RETURN_OK;
mfiore 0:09250cd371d2 195 }
wang1tao 16:847c028f4130 196
wang1tao 16:847c028f4130 197 int send_data(char *str)
wang1tao 16:847c028f4130 198 {
wang1tao 16:847c028f4130 199 int32_t i, ret;
wang1tao 16:847c028f4130 200 std::vector<uint8_t> sendData;
wang1tao 16:847c028f4130 201
wang1tao 16:847c028f4130 202 //Send the data to Gateway
wang1tao 16:847c028f4130 203 sendData.clear();
wang1tao 16:847c028f4130 204 // probably not the most efficent way to do this
wang1tao 16:847c028f4130 205 for(i=0; i< strlen(str); i++ )
wang1tao 16:847c028f4130 206 sendData.push_back(str[i] );
wang1tao 16:847c028f4130 207
wang1tao 16:847c028f4130 208 // send the data to the gateway
wang1tao 16:847c028f4130 209 pc.printf("Send %s to Gateway \n\r", str);
wang1tao 16:847c028f4130 210
wang1tao 16:847c028f4130 211 if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
wang1tao 16:847c028f4130 212 pc.printf("Error:failed to send\n\r", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 16:847c028f4130 213 return RETURN_ERR;
wang1tao 16:847c028f4130 214 } else {
wang1tao 16:847c028f4130 215 pc.printf("Sent data to gateway successfully!\n\r");
wang1tao 16:847c028f4130 216 }
wang1tao 16:847c028f4130 217 return RETURN_OK;
wang1tao 16:847c028f4130 218 }
wang1tao 16:847c028f4130 219
wang1tao 16:847c028f4130 220
wang1tao 16:847c028f4130 221 void my_strcpy(char *dst, char *src)
wang1tao 16:847c028f4130 222 {
wang1tao 16:847c028f4130 223 int j=0;
wang1tao 16:847c028f4130 224
wang1tao 16:847c028f4130 225 while(src[j] != 0)
wang1tao 16:847c028f4130 226 {
wang1tao 16:847c028f4130 227 dst[j] = src[j];
wang1tao 16:847c028f4130 228 j++;
wang1tao 16:847c028f4130 229 }
wang1tao 16:847c028f4130 230 }
wang1tao 16:847c028f4130 231
wang1tao 16:847c028f4130 232
wang1tao 16:847c028f4130 233 void my_strclear(char *str, unsigned int len)
wang1tao 16:847c028f4130 234 {
wang1tao 16:847c028f4130 235 unsigned int j;
wang1tao 16:847c028f4130 236 for(j=0; j<len; j++)str[j]=0;
wang1tao 16:847c028f4130 237 }
wang1tao 16:847c028f4130 238
wang1tao 16:847c028f4130 239
wang1tao 16:847c028f4130 240 void readCardID(void)
wang1tao 16:847c028f4130 241 {
wang1tao 16:847c028f4130 242 unsigned int i;
wang1tao 16:847c028f4130 243 char chr;
wang1tao 16:847c028f4130 244
wang1tao 16:847c028f4130 245 //pc.printf("Read Tick!\n\r");
wang1tao 16:847c028f4130 246
wang1tao 16:847c028f4130 247 /* clear the read buffer */
wang1tao 16:847c028f4130 248 my_strclear(readBuf, RESPONSE_LENGTH);
wang1tao 16:847c028f4130 249
wang1tao 16:847c028f4130 250 pc.printf("Send the READ command!\n\r");//, &readCmd[1]);
wang1tao 16:847c028f4130 251 if(cm_rs485.writeable()){
wang1tao 16:847c028f4130 252 for(i=0; i<COMMAND_LENGTH; i++){
wang1tao 16:847c028f4130 253 cm_rs485.putc(readCmd[i]);
wang1tao 16:847c028f4130 254 }
wang1tao 16:847c028f4130 255 }
wang1tao 16:847c028f4130 256
wang1tao 16:847c028f4130 257 /* read the response */
wang1tao 16:847c028f4130 258 chr = cm_rs485.getc();
wang1tao 16:847c028f4130 259 i = 0;
wang1tao 16:847c028f4130 260 while(chr != 0x0d)
wang1tao 16:847c028f4130 261 {
wang1tao 16:847c028f4130 262 readBuf[i] = chr;
wang1tao 16:847c028f4130 263 i++;
wang1tao 16:847c028f4130 264 chr = cm_rs485.getc();
wang1tao 16:847c028f4130 265 }
wang1tao 16:847c028f4130 266
wang1tao 16:847c028f4130 267 /* print the response for debug */
wang1tao 16:847c028f4130 268 pc.printf("Response[ASCII]:");
wang1tao 16:847c028f4130 269
wang1tao 16:847c028f4130 270 for(i=0; i< RESPONSE_LENGTH; i++){
wang1tao 16:847c028f4130 271 pc.printf("%0X ", readBuf[i]);
wang1tao 16:847c028f4130 272 if (readBuf[i] == 0) break;
wang1tao 16:847c028f4130 273 }
wang1tao 16:847c028f4130 274 pc.printf("Response[Text]:%s\n\r", &readBuf[3]);
wang1tao 16:847c028f4130 275
wang1tao 16:847c028f4130 276 /* add to ringBuffer if valid */
wang1tao 16:847c028f4130 277 if((readBuf[3] != 0) && (readBuf[4] != 0)){
wang1tao 16:847c028f4130 278 if((readBuf[3]!= '3') && (readBuf[4] !='C')) //valid response
wang1tao 16:847c028f4130 279 {
wang1tao 16:847c028f4130 280 my_strcpy(ringBuffer[inIndex], &readBuf[3]); //strim the 3 bytes prehead
wang1tao 16:847c028f4130 281 //pc.printf("addr = 0x%0X, inIndex = %d\n\r", &ringBuffer[inIndex],inIndex);
wang1tao 16:847c028f4130 282 inIndex++;
wang1tao 16:847c028f4130 283 if(inIndex >= RINGBUFFER_SIZE) inIndex = 0;
wang1tao 16:847c028f4130 284 if(ringBuffer[inIndex][0] != 0) //is not empty
wang1tao 16:847c028f4130 285 {
wang1tao 16:847c028f4130 286 pc.printf("Warning! Ringbuffer overflow at [%d]!\n\r", inIndex);
wang1tao 16:847c028f4130 287 pc.printf("Please have a check of the network!\n\r");
wang1tao 16:847c028f4130 288 pc.printf("Data in ring buffer:\n\r");
wang1tao 16:847c028f4130 289 for(i=0; i<RINGBUFFER_SIZE; i++)
wang1tao 16:847c028f4130 290 {
wang1tao 16:847c028f4130 291 pc.printf("Data[%d]: %s \n\r", i, ringBuffer[i]);
wang1tao 16:847c028f4130 292 }
wang1tao 16:847c028f4130 293
wang1tao 16:847c028f4130 294 wait(10); //in seconds
wang1tao 16:847c028f4130 295 }
wang1tao 16:847c028f4130 296 }
wang1tao 16:847c028f4130 297 }
wang1tao 16:847c028f4130 298 }
wang1tao 16:847c028f4130 299
wang1tao 16:847c028f4130 300 void sendCardID(void)
wang1tao 16:847c028f4130 301 {
wang1tao 16:847c028f4130 302 int32_t ret;
wang1tao 16:847c028f4130 303
wang1tao 16:847c028f4130 304 //pc.printf("Send Tick!\n\r");
wang1tao 16:847c028f4130 305 /*send the data */
wang1tao 16:847c028f4130 306 if(ringBuffer[outIndex][0] != 0) //not empty
wang1tao 16:847c028f4130 307 {
wang1tao 16:847c028f4130 308 pc.printf("Send dada[%s] to Gateway!\n\r", ringBuffer[outIndex]);
wang1tao 16:847c028f4130 309 #ifndef NO_MULTITECH_GATEWAY
wang1tao 16:847c028f4130 310 /* t.b.d : send data though LoRA */
wang1tao 16:847c028f4130 311 if(send_data(ringBuffer[outIndex]) != RETURN_OK)
wang1tao 16:847c028f4130 312 {
wang1tao 16:847c028f4130 313 pc.printf("Failed to send data to netwotk!\n\r");
wang1tao 16:847c028f4130 314 pc.printf("Rejoin network and try one more time!\n\r");
wang1tao 16:847c028f4130 315 // attempt to rejoin the network
wang1tao 16:847c028f4130 316 pc.printf("Attemp to rejoin network....\n\r");
wang1tao 16:847c028f4130 317 if ((ret = dot->joinNetworkOnce()) != mDot::MDOT_OK) {
wang1tao 16:847c028f4130 318 pc.printf("Failed to rejoin network! %d:%s \n\r", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 16:847c028f4130 319 return;
wang1tao 16:847c028f4130 320 }else{
wang1tao 16:847c028f4130 321 pc.printf("Rejoin network successfully!");
wang1tao 16:847c028f4130 322 if(send_data(ringBuffer[outIndex]) != RETURN_OK) return; //abort to send
wang1tao 16:847c028f4130 323 }
wang1tao 16:847c028f4130 324 }
wang1tao 16:847c028f4130 325 #endif
wang1tao 16:847c028f4130 326 //clear the buffer
wang1tao 16:847c028f4130 327 my_strclear(ringBuffer[outIndex], RESPONSE_LENGTH);
wang1tao 16:847c028f4130 328 outIndex++;
wang1tao 16:847c028f4130 329 if(outIndex >= RINGBUFFER_SIZE)outIndex = 0;
wang1tao 16:847c028f4130 330 }else{ //empty
wang1tao 16:847c028f4130 331 pc.printf("Nothing to send!\n\r");
wang1tao 16:847c028f4130 332 //wait(10);
wang1tao 16:847c028f4130 333 }
wang1tao 16:847c028f4130 334
wang1tao 16:847c028f4130 335 }