r1.0

Dependencies:   DHT libmDot mbed-rtos mbed

Fork of mDot_Connect_IoTClub_one_wire by wireless sensor

Committer:
Dengjj
Date:
Thu Aug 25 12:58:12 2016 +0000
Revision:
15:04b37f329c84
Parent:
14:0616e4361f0c
one alarm

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:09250cd371d2 1 #include "mbed.h"
mfiore 0:09250cd371d2 2 #include "mDot.h"
mfiore 4:36e214ebfa56 3 #include "MTSLog.h"
mfiore 0:09250cd371d2 4 #include <string>
mfiore 0:09250cd371d2 5 #include <vector>
mfiore 4:36e214ebfa56 6 #include <algorithm>
Dengjj 15:04b37f329c84 7
wang1tao 9:cf45820af9b9 8
wang1tao 13:5d050b414f16 9 const char TURNON[] = "ON";
wang1tao 13:5d050b414f16 10 const char TURNOFF[] = "OFF";
wang1tao 13:5d050b414f16 11
Dengjj 15:04b37f329c84 12 bool cmpStr(char* str1, const char* str2, int len)
Dengjj 15:04b37f329c84 13 {
Dengjj 15:04b37f329c84 14 int k;
Dengjj 15:04b37f329c84 15 for (k=0; k<len; k++){
Dengjj 15:04b37f329c84 16 if (str1[k] != str2[k]) return false;
Dengjj 15:04b37f329c84 17 }
Dengjj 15:04b37f329c84 18 return true;
Dengjj 15:04b37f329c84 19 }
Dengjj 15:04b37f329c84 20
Dengjj 15:04b37f329c84 21 DigitalOut Alarm(PB_0);
wang1tao 13:5d050b414f16 22
wang1tao 9:cf45820af9b9 23 //defined for mDot SVB debug, comment it if applying for whole system
wang1tao 10:79124c0a5952 24 //#define NO_MULTITECH_GATEWAY
wang1tao 9:cf45820af9b9 25
mfiore 2:6e2c378339d9 26 // these options must match the settings on your Conduit
mfiore 2:6e2c378339d9 27 // uncomment the following lines and edit their values to match your configuration
JCheng 5:6c7d2f4d7377 28 static std::string config_network_name = "chinaiot";
JCheng 5:6c7d2f4d7377 29 static std::string config_network_pass = "password";
wang1tao 10:79124c0a5952 30 static uint8_t config_frequency_sub_band = 2;
mfiore 0:09250cd371d2 31
mfiore 0:09250cd371d2 32 int main() {
wang1tao 10:79124c0a5952 33 int32_t ret;
mfiore 0:09250cd371d2 34 mDot* dot;
wang1tao 13:5d050b414f16 35 std::vector<uint8_t> sendData, recvData;
Dengjj 15:04b37f329c84 36 std::vector<uint8_t> sendData1,sendData2, sendData3, sendData4;
Dengjj 15:04b37f329c84 37 char recvBuf[11];
wang1tao 12:b31e43c9fb15 38 int send_failed;
Dengjj 15:04b37f329c84 39 int i;
Dengjj 15:04b37f329c84 40 //bool alarm_on;
Dengjj 15:04b37f329c84 41 std::string data_str1 = "REQ1";
Dengjj 15:04b37f329c84 42 std::string data_str2 = "REQ2";
Dengjj 15:04b37f329c84 43 std::string data_str3 = "REQ3";
Dengjj 15:04b37f329c84 44 std::string data_str4 = "REQ4";
Dengjj 15:04b37f329c84 45 bool a1, a2, a3, a4;
Dengjj 14:0616e4361f0c 46
Dengjj 14:0616e4361f0c 47
mfiore 0:09250cd371d2 48 // get a mDot handle
mfiore 0:09250cd371d2 49 dot = mDot::getInstance();
JCheng 5:6c7d2f4d7377 50
mfiore 2:6e2c378339d9 51 // print library version information
mfiore 2:6e2c378339d9 52 logInfo("version: %s", dot->getId().c_str());
mfiore 0:09250cd371d2 53
mfiore 2:6e2c378339d9 54 //*******************************************
mfiore 2:6e2c378339d9 55 // configuration
mfiore 2:6e2c378339d9 56 //*******************************************
mfiore 0:09250cd371d2 57 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 58 dot->resetConfig();
JCheng 5:6c7d2f4d7377 59
mfiore 4:36e214ebfa56 60 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
Dengjj 15:04b37f329c84 61
mfiore 2:6e2c378339d9 62 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 63 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
JCheng 5:6c7d2f4d7377 64
mfiore 4:36e214ebfa56 65 // frequency sub band is only applicable in the 915 (US) frequency band
mfiore 4:36e214ebfa56 66 // 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 67 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
mfiore 2:6e2c378339d9 68 logInfo("setting frequency sub band");
mfiore 0:09250cd371d2 69 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 70 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 2:6e2c378339d9 71 }
JCheng 5:6c7d2f4d7377 72
mfiore 2:6e2c378339d9 73 logInfo("setting network name");
mfiore 2:6e2c378339d9 74 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 75 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 76 }
JCheng 5:6c7d2f4d7377 77
mfiore 2:6e2c378339d9 78 logInfo("setting network password");
mfiore 2:6e2c378339d9 79 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 80 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 81 }
JCheng 5:6c7d2f4d7377 82
mfiore 4:36e214ebfa56 83 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 84 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 85 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
mfiore 4:36e214ebfa56 86 logInfo("setting TX spreading factor");
mfiore 4:36e214ebfa56 87 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 88 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 89 }
JCheng 5:6c7d2f4d7377 90
mfiore 4:36e214ebfa56 91 // request receive confirmation of packets from the gateway
mfiore 4:36e214ebfa56 92 logInfo("enabling ACKs");
mfiore 4:36e214ebfa56 93 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 94 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 95 }
JCheng 5:6c7d2f4d7377 96
mfiore 4:36e214ebfa56 97 // save this configuration to the mDot's NVM
mfiore 2:6e2c378339d9 98 logInfo("saving config");
mfiore 2:6e2c378339d9 99 if (! dot->saveConfig()) {
mfiore 2:6e2c378339d9 100 logError("failed to save configuration");
mfiore 0:09250cd371d2 101 }
mfiore 2:6e2c378339d9 102 //*******************************************
mfiore 2:6e2c378339d9 103 // end of configuration
mfiore 2:6e2c378339d9 104 //*******************************************
mfiore 0:09250cd371d2 105
mfiore 0:09250cd371d2 106 // attempt to join the network
mfiore 2:6e2c378339d9 107 logInfo("joining network");
mfiore 0:09250cd371d2 108 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 109 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 110 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 111 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 112 }
Dengjj 15:04b37f329c84 113
Dengjj 15:04b37f329c84 114 // format data for sending to the gateway
Dengjj 15:04b37f329c84 115 for (std::string::iterator it = data_str1.begin(); it != data_str1.end(); it++)
Dengjj 15:04b37f329c84 116 sendData1.push_back((uint8_t) *it);
Dengjj 15:04b37f329c84 117
Dengjj 15:04b37f329c84 118 // format data for sending to the gateway
Dengjj 15:04b37f329c84 119 for (std::string::iterator it = data_str2.begin(); it != data_str2.end(); it++)
Dengjj 15:04b37f329c84 120 sendData2.push_back((uint8_t) *it);
Dengjj 15:04b37f329c84 121
Dengjj 15:04b37f329c84 122 // format data for sending to the gateway
Dengjj 15:04b37f329c84 123 for (std::string::iterator it = data_str3.begin(); it != data_str3.end(); it++)
Dengjj 15:04b37f329c84 124 sendData3.push_back((uint8_t) *it);
mfiore 0:09250cd371d2 125
mfiore 0:09250cd371d2 126 // format data for sending to the gateway
Dengjj 15:04b37f329c84 127 for (std::string::iterator it = data_str4.begin(); it != data_str4.end(); it++)
Dengjj 15:04b37f329c84 128 sendData4.push_back((uint8_t) *it);
Dengjj 15:04b37f329c84 129
Dengjj 15:04b37f329c84 130 send_failed = 0;
Dengjj 15:04b37f329c84 131 while (true) {
Dengjj 15:04b37f329c84 132 // send the data to the gateway
Dengjj 15:04b37f329c84 133 if ((ret = dot->send(sendData1)) != mDot::MDOT_OK) {
Dengjj 15:04b37f329c84 134 send_failed++;
Dengjj 15:04b37f329c84 135 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 15:04b37f329c84 136 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
Dengjj 15:04b37f329c84 137 } else {
Dengjj 15:04b37f329c84 138 send_failed=0;
Dengjj 15:04b37f329c84 139 logInfo("successfully sent data to gateway");
Dengjj 15:04b37f329c84 140 for(i=0;i<11;i++)recvBuf[i]=0; //clear recv buffer
Dengjj 15:04b37f329c84 141 recvData.clear(); //clear recv data
Dengjj 15:04b37f329c84 142 if ((ret = dot->recv(recvData)) != mDot::MDOT_OK) {
Dengjj 15:04b37f329c84 143 logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 15:04b37f329c84 144 } else {
Dengjj 15:04b37f329c84 145 //logInfo("datasize = %d", recvData.size());
Dengjj 15:04b37f329c84 146 for(i=0; i< recvData.size(); i++ )
Dengjj 15:04b37f329c84 147 recvBuf[i] = recvData[i];
Dengjj 15:04b37f329c84 148 //logInfo("RECV:%s", recvBuf);
Dengjj 15:04b37f329c84 149 if(cmpStr(recvBuf, TURNON, 2)){
Dengjj 15:04b37f329c84 150 a1 = true;
Dengjj 15:04b37f329c84 151 logInfo("A1 is on!");
Dengjj 15:04b37f329c84 152 }
Dengjj 15:04b37f329c84 153 if(cmpStr(recvBuf, TURNOFF, 3)){
Dengjj 15:04b37f329c84 154 a1 = false;
Dengjj 15:04b37f329c84 155 logInfo("A1 is off!");
Dengjj 15:04b37f329c84 156 }
Dengjj 15:04b37f329c84 157 }
Dengjj 15:04b37f329c84 158 }
wang1tao 9:cf45820af9b9 159
Dengjj 15:04b37f329c84 160 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
Dengjj 15:04b37f329c84 161
wang1tao 9:cf45820af9b9 162 // send the data to the gateway
Dengjj 15:04b37f329c84 163 if ((ret = dot->send(sendData2)) != mDot::MDOT_OK) {
Dengjj 15:04b37f329c84 164 send_failed++;
wang1tao 9:cf45820af9b9 165 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 13:5d050b414f16 166 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
Dengjj 15:04b37f329c84 167 } else {
Dengjj 15:04b37f329c84 168 send_failed=0;
Dengjj 15:04b37f329c84 169 logInfo("successfully sent data to gateway");
wang1tao 13:5d050b414f16 170 for(i=0;i<11;i++)recvBuf[i]=0; //clear recv buffer
wang1tao 13:5d050b414f16 171 recvData.clear(); //clear recv data
wang1tao 13:5d050b414f16 172 if ((ret = dot->recv(recvData)) != mDot::MDOT_OK) {
wang1tao 13:5d050b414f16 173 logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 13:5d050b414f16 174 } else {
wang1tao 13:5d050b414f16 175 //logInfo("datasize = %d", recvData.size());
wang1tao 13:5d050b414f16 176 for(i=0; i< recvData.size(); i++ )
wang1tao 13:5d050b414f16 177 recvBuf[i] = recvData[i];
Dengjj 15:04b37f329c84 178 //logInfo("RECV:%s", recvBuf);
Dengjj 15:04b37f329c84 179 if(cmpStr(recvBuf, TURNON, 2)){
Dengjj 15:04b37f329c84 180 a2 = true;
Dengjj 15:04b37f329c84 181 logInfo("A2 is on!");
Dengjj 15:04b37f329c84 182 }
Dengjj 15:04b37f329c84 183 if(cmpStr(recvBuf, TURNOFF, 3)){
Dengjj 15:04b37f329c84 184 a2 = false;
Dengjj 15:04b37f329c84 185 logInfo("A2 is off!");
Dengjj 15:04b37f329c84 186 }
Dengjj 15:04b37f329c84 187 }
Dengjj 15:04b37f329c84 188 }
Dengjj 15:04b37f329c84 189
Dengjj 15:04b37f329c84 190 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
Dengjj 15:04b37f329c84 191
Dengjj 15:04b37f329c84 192 // send the data to the gateway
Dengjj 15:04b37f329c84 193 if ((ret = dot->send(sendData3)) != mDot::MDOT_OK) {
Dengjj 15:04b37f329c84 194 send_failed++;
Dengjj 15:04b37f329c84 195 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 15:04b37f329c84 196 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
Dengjj 15:04b37f329c84 197 } else {
Dengjj 15:04b37f329c84 198 send_failed=0;
Dengjj 15:04b37f329c84 199 logInfo("successfully sent data to gateway");
Dengjj 15:04b37f329c84 200 for(i=0;i<11;i++)recvBuf[i]=0; //clear recv buffer
Dengjj 15:04b37f329c84 201 recvData.clear(); //clear recv data
Dengjj 15:04b37f329c84 202 if ((ret = dot->recv(recvData)) != mDot::MDOT_OK) {
Dengjj 15:04b37f329c84 203 logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 15:04b37f329c84 204 } else {
Dengjj 15:04b37f329c84 205 //logInfo("datasize = %d", recvData.size());
Dengjj 15:04b37f329c84 206 for(i=0; i< recvData.size(); i++ )
Dengjj 15:04b37f329c84 207 recvBuf[i] = recvData[i];
Dengjj 15:04b37f329c84 208 //logInfo("RECV:%s", recvBuf);
Dengjj 15:04b37f329c84 209 if(cmpStr(recvBuf, TURNON, 2)){
Dengjj 15:04b37f329c84 210 a3 = true;
Dengjj 15:04b37f329c84 211 logInfo("A3 is on!");
Dengjj 15:04b37f329c84 212 }
Dengjj 15:04b37f329c84 213 if(cmpStr(recvBuf, TURNOFF, 3)){
Dengjj 15:04b37f329c84 214 a3 = false;
Dengjj 15:04b37f329c84 215 logInfo("A3 is off!");
Dengjj 15:04b37f329c84 216 }
Dengjj 15:04b37f329c84 217 }
Dengjj 15:04b37f329c84 218 }
Dengjj 15:04b37f329c84 219
Dengjj 15:04b37f329c84 220 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
Dengjj 15:04b37f329c84 221
Dengjj 15:04b37f329c84 222 // send the data to the gateway
Dengjj 15:04b37f329c84 223 if ((ret = dot->send(sendData4)) != mDot::MDOT_OK) {
Dengjj 15:04b37f329c84 224 send_failed++;
Dengjj 15:04b37f329c84 225 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 15:04b37f329c84 226 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
Dengjj 15:04b37f329c84 227 } else {
Dengjj 15:04b37f329c84 228 send_failed=0;
Dengjj 15:04b37f329c84 229 logInfo("successfully sent data to gateway");
Dengjj 15:04b37f329c84 230 for(i=0;i<11;i++)recvBuf[i]=0; //clear recv buffer
Dengjj 15:04b37f329c84 231 recvData.clear(); //clear recv data
Dengjj 15:04b37f329c84 232 if ((ret = dot->recv(recvData)) != mDot::MDOT_OK) {
Dengjj 15:04b37f329c84 233 logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
Dengjj 15:04b37f329c84 234 } else {
Dengjj 15:04b37f329c84 235 //logInfo("datasize = %d", recvData.size());
Dengjj 15:04b37f329c84 236 for(i=0; i< recvData.size(); i++ )
Dengjj 15:04b37f329c84 237 recvBuf[i] = recvData[i];
Dengjj 15:04b37f329c84 238 //logInfo("RECV:%s", recvBuf);
Dengjj 15:04b37f329c84 239 if(cmpStr(recvBuf, TURNON, 2)){
Dengjj 15:04b37f329c84 240 a4 = true;
Dengjj 15:04b37f329c84 241 logInfo("A4 is on!");
Dengjj 15:04b37f329c84 242 }
Dengjj 15:04b37f329c84 243 if(cmpStr(recvBuf, TURNOFF, 3)){
Dengjj 15:04b37f329c84 244 a4 = false;
Dengjj 15:04b37f329c84 245 logInfo("A4 is off!");
Dengjj 15:04b37f329c84 246 }
wang1tao 13:5d050b414f16 247 }
wang1tao 9:cf45820af9b9 248 }
wang1tao 13:5d050b414f16 249 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
Dengjj 15:04b37f329c84 250
Dengjj 15:04b37f329c84 251 if(a1 || a2 || a3 || a4){
Dengjj 15:04b37f329c84 252 logInfo("============================");
Dengjj 15:04b37f329c84 253 logInfo("Alarm is ON");
Dengjj 15:04b37f329c84 254 Alarm = 1;
Dengjj 15:04b37f329c84 255 }else{
Dengjj 15:04b37f329c84 256 logInfo("============================");
Dengjj 15:04b37f329c84 257 logInfo("Alarm is OFF");
Dengjj 15:04b37f329c84 258 Alarm = 0;
Dengjj 15:04b37f329c84 259 }
Dengjj 15:04b37f329c84 260
Dengjj 15:04b37f329c84 261 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
Dengjj 15:04b37f329c84 262
wang1tao 13:5d050b414f16 263
wang1tao 12:b31e43c9fb15 264 if(send_failed>=3){
Dengjj 15:04b37f329c84 265 send_failed=0;
wang1tao 12:b31e43c9fb15 266 // attempt to rejoin the network
wang1tao 12:b31e43c9fb15 267 logInfo("Attemp to rejoin network....");
wang1tao 12:b31e43c9fb15 268 if ((ret = dot->joinNetworkOnce()) != mDot::MDOT_OK) {
wang1tao 12:b31e43c9fb15 269 logError("Failed to rejoin network!"); // %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 12:b31e43c9fb15 270 }else{
Dengjj 15:04b37f329c84 271 logInfo("Rejoin network successfully!");
wang1tao 12:b31e43c9fb15 272 }
wang1tao 13:5d050b414f16 273 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
wang1tao 12:b31e43c9fb15 274 }
wang1tao 13:5d050b414f16 275
mfiore 0:09250cd371d2 276 }
mfiore 0:09250cd371d2 277
mfiore 0:09250cd371d2 278 }