r1.0

Dependencies:   DHT libmDot mbed-rtos mbed

Fork of mDot_Connect_IoTClub_one_wire by wireless sensor

Committer:
wang1tao
Date:
Sat Aug 20 11:43:09 2016 +0000
Revision:
13:5d050b414f16
Parent:
12:b31e43c9fb15
Child:
14:0616e4361f0c
Alarm supported

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>
wang1tao 9:cf45820af9b9 7 #include "AnalogPH.h"
wang1tao 9:cf45820af9b9 8
wang1tao 13:5d050b414f16 9 const char TURNON[] = "ON";
wang1tao 13:5d050b414f16 10 const char TURNOFF[] = "OFF";
wang1tao 13:5d050b414f16 11
wang1tao 13:5d050b414f16 12 DigitalOut Alarm(PA_2);
wang1tao 13:5d050b414f16 13
wang1tao 9:cf45820af9b9 14 //defined for mDot SVB debug, comment it if applying for whole system
wang1tao 10:79124c0a5952 15 //#define NO_MULTITECH_GATEWAY
wang1tao 9:cf45820af9b9 16
wang1tao 9:cf45820af9b9 17 //Define the number of channels
wang1tao 13:5d050b414f16 18 #define NUM_OF_CH 1
JCheng 5:6c7d2f4d7377 19
wang1tao 9:cf45820af9b9 20 AnalogPHSensor CH0_PH(PB_0, 0.0, 0.0);
wang1tao 9:cf45820af9b9 21
wang1tao 9:cf45820af9b9 22 #if (NUM_OF_CH >1)
wang1tao 9:cf45820af9b9 23 AnalogPHSensor CH1_PH(PB_1, 0.0, 0.0);
wang1tao 9:cf45820af9b9 24 #endif
wang1tao 9:cf45820af9b9 25
wang1tao 9:cf45820af9b9 26 #if (NUM_OF_CH >2)
wang1tao 10:79124c0a5952 27 AnalogPHSensor CH2_PH(PA_5, 0.0, 0.0);
wang1tao 9:cf45820af9b9 28 #endif
wang1tao 9:cf45820af9b9 29 #if (NUM_OF_CH >3)
wang1tao 10:79124c0a5952 30 AnalogPHSensor CH3_PH(PA_4, 0.0, 0.0);
wang1tao 9:cf45820af9b9 31 #endif
mfiore 0:09250cd371d2 32
mfiore 2:6e2c378339d9 33 // these options must match the settings on your Conduit
mfiore 2:6e2c378339d9 34 // uncomment the following lines and edit their values to match your configuration
JCheng 5:6c7d2f4d7377 35 static std::string config_network_name = "chinaiot";
JCheng 5:6c7d2f4d7377 36 static std::string config_network_pass = "password";
wang1tao 10:79124c0a5952 37 static uint8_t config_frequency_sub_band = 2;
mfiore 0:09250cd371d2 38
mfiore 0:09250cd371d2 39 int main() {
wang1tao 10:79124c0a5952 40 int32_t ret;
mfiore 0:09250cd371d2 41 mDot* dot;
wang1tao 13:5d050b414f16 42 std::vector<uint8_t> sendData, recvData;
wang1tao 9:cf45820af9b9 43 float phValue;
wang1tao 9:cf45820af9b9 44 char _header[] = "PH Sensors";
wang1tao 13:5d050b414f16 45 char sendBuf[11], recvBuf[11];
wang1tao 9:cf45820af9b9 46 int i, k;
wang1tao 12:b31e43c9fb15 47 int send_failed;
wang1tao 13:5d050b414f16 48 bool alarm_on;
wang1tao 9:cf45820af9b9 49
mfiore 0:09250cd371d2 50 // get a mDot handle
mfiore 0:09250cd371d2 51 dot = mDot::getInstance();
JCheng 5:6c7d2f4d7377 52
mfiore 2:6e2c378339d9 53 // print library version information
mfiore 2:6e2c378339d9 54 logInfo("version: %s", dot->getId().c_str());
mfiore 0:09250cd371d2 55
mfiore 2:6e2c378339d9 56 //*******************************************
mfiore 2:6e2c378339d9 57 // configuration
mfiore 2:6e2c378339d9 58 //*******************************************
mfiore 0:09250cd371d2 59 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 60 dot->resetConfig();
JCheng 5:6c7d2f4d7377 61
mfiore 4:36e214ebfa56 62 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
wang1tao 9:cf45820af9b9 63 #ifndef NO_MULTITECH_GATEWAY
mfiore 2:6e2c378339d9 64 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 65 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
JCheng 5:6c7d2f4d7377 66
mfiore 4:36e214ebfa56 67 // frequency sub band is only applicable in the 915 (US) frequency band
mfiore 4:36e214ebfa56 68 // 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 69 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
mfiore 2:6e2c378339d9 70 logInfo("setting frequency sub band");
mfiore 0:09250cd371d2 71 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 72 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 2:6e2c378339d9 73 }
JCheng 5:6c7d2f4d7377 74
mfiore 2:6e2c378339d9 75 logInfo("setting network name");
mfiore 2:6e2c378339d9 76 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 77 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 78 }
JCheng 5:6c7d2f4d7377 79
mfiore 2:6e2c378339d9 80 logInfo("setting network password");
mfiore 2:6e2c378339d9 81 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 82 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 83 }
JCheng 5:6c7d2f4d7377 84
mfiore 4:36e214ebfa56 85 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 86 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 87 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
mfiore 4:36e214ebfa56 88 logInfo("setting TX spreading factor");
mfiore 4:36e214ebfa56 89 if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 90 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 91 }
JCheng 5:6c7d2f4d7377 92
mfiore 4:36e214ebfa56 93 // request receive confirmation of packets from the gateway
mfiore 4:36e214ebfa56 94 logInfo("enabling ACKs");
mfiore 4:36e214ebfa56 95 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 96 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 97 }
JCheng 5:6c7d2f4d7377 98
mfiore 4:36e214ebfa56 99 // save this configuration to the mDot's NVM
mfiore 2:6e2c378339d9 100 logInfo("saving config");
mfiore 2:6e2c378339d9 101 if (! dot->saveConfig()) {
mfiore 2:6e2c378339d9 102 logError("failed to save configuration");
mfiore 0:09250cd371d2 103 }
mfiore 2:6e2c378339d9 104 //*******************************************
mfiore 2:6e2c378339d9 105 // end of configuration
mfiore 2:6e2c378339d9 106 //*******************************************
mfiore 0:09250cd371d2 107
mfiore 0:09250cd371d2 108 // attempt to join the network
mfiore 2:6e2c378339d9 109 logInfo("joining network");
mfiore 0:09250cd371d2 110 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 111 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 112 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 113 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 114 }
mfiore 0:09250cd371d2 115
wang1tao 13:5d050b414f16 116 sendData.clear();
mfiore 0:09250cd371d2 117 // format data for sending to the gateway
wang1tao 9:cf45820af9b9 118 for( int i=0; i< strlen(_header); i++ )
wang1tao 13:5d050b414f16 119 sendData.push_back( _header[i] );
wang1tao 9:cf45820af9b9 120
wang1tao 9:cf45820af9b9 121 // send the data to the gateway
wang1tao 13:5d050b414f16 122 if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
wang1tao 9:cf45820af9b9 123 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 9:cf45820af9b9 124 } else {
wang1tao 9:cf45820af9b9 125 logInfo("successfully sent data to gateway");
wang1tao 9:cf45820af9b9 126 }
wang1tao 9:cf45820af9b9 127 #else
wang1tao 9:cf45820af9b9 128 logInfo("%s", _header);
wang1tao 9:cf45820af9b9 129 #endif
mfiore 0:09250cd371d2 130 while (true) {
wang1tao 12:b31e43c9fb15 131 send_failed = 0;
wang1tao 13:5d050b414f16 132 alarm_on = false;
wang1tao 9:cf45820af9b9 133 // Read the PH values
wang1tao 9:cf45820af9b9 134 for(k=0; k<NUM_OF_CH; k++){
wang1tao 9:cf45820af9b9 135 switch(k){
wang1tao 9:cf45820af9b9 136 case 0: phValue = CH0_PH; break;
wang1tao 9:cf45820af9b9 137 #if (NUM_OF_CH >1)
wang1tao 9:cf45820af9b9 138 case 1: phValue = CH1_PH; break;
wang1tao 9:cf45820af9b9 139 #endif
wang1tao 9:cf45820af9b9 140 #if (NUM_OF_CH >2)
wang1tao 9:cf45820af9b9 141 case 2: phValue = CH2_PH; break;
wang1tao 9:cf45820af9b9 142 #endif
wang1tao 9:cf45820af9b9 143 #if (NUM_OF_CH >3)
wang1tao 9:cf45820af9b9 144 case 3: phValue = CH3_PH; break;
wang1tao 9:cf45820af9b9 145 #endif
wang1tao 9:cf45820af9b9 146 default:
wang1tao 9:cf45820af9b9 147 break;
wang1tao 10:79124c0a5952 148 }
wang1tao 13:5d050b414f16 149 sprintf(sendBuf, "CH%d:%5.4f", k, phValue);
wang1tao 13:5d050b414f16 150 logInfo("%s", sendBuf);
wang1tao 9:cf45820af9b9 151
wang1tao 9:cf45820af9b9 152 #ifndef NO_MULTITECH_GATEWAY
wang1tao 9:cf45820af9b9 153 //Send the data to Gateway
wang1tao 13:5d050b414f16 154 sendData.clear();
wang1tao 9:cf45820af9b9 155 // probably not the most efficent way to do this
wang1tao 13:5d050b414f16 156 for( int i=0; i< strlen(sendBuf); i++ )
wang1tao 13:5d050b414f16 157 sendData.push_back( sendBuf[i] );
wang1tao 9:cf45820af9b9 158
wang1tao 9:cf45820af9b9 159 // send the data to the gateway
wang1tao 13:5d050b414f16 160 if ((ret = dot->send(sendData)) != mDot::MDOT_OK) {
wang1tao 9:cf45820af9b9 161 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 12:b31e43c9fb15 162 send_failed++;
wang1tao 13:5d050b414f16 163 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
wang1tao 12:b31e43c9fb15 164 } else {
wang1tao 12:b31e43c9fb15 165 logInfo("successfully sent data to gateway");
wang1tao 13:5d050b414f16 166 for(i=0;i<11;i++)recvBuf[i]=0; //clear recv buffer
wang1tao 13:5d050b414f16 167 recvData.clear(); //clear recv data
wang1tao 13:5d050b414f16 168 if ((ret = dot->recv(recvData)) != mDot::MDOT_OK) {
wang1tao 13:5d050b414f16 169 logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 13:5d050b414f16 170 } else {
wang1tao 13:5d050b414f16 171 //logInfo("datasize = %d", recvData.size());
wang1tao 13:5d050b414f16 172 for(i=0; i< recvData.size(); i++ )
wang1tao 13:5d050b414f16 173 recvBuf[i] = recvData[i];
wang1tao 13:5d050b414f16 174 logInfo("RECV:%s", recvBuf);
wang1tao 13:5d050b414f16 175 if(0==strcmp(recvBuf, TURNON)) alarm_on = true;
wang1tao 13:5d050b414f16 176 }
wang1tao 9:cf45820af9b9 177 }
wang1tao 13:5d050b414f16 178 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
wang1tao 9:cf45820af9b9 179 #endif
wang1tao 12:b31e43c9fb15 180 }
wang1tao 13:5d050b414f16 181
wang1tao 13:5d050b414f16 182 //handle the alarm
wang1tao 13:5d050b414f16 183 if(alarm_on){
wang1tao 13:5d050b414f16 184 logInfo("Turn on the alarm");
wang1tao 13:5d050b414f16 185 Alarm = 1;
wang1tao 13:5d050b414f16 186 }else{
wang1tao 13:5d050b414f16 187 logInfo("Turn off the alarm");
wang1tao 13:5d050b414f16 188 Alarm = 0;
wang1tao 13:5d050b414f16 189 }
wang1tao 13:5d050b414f16 190
wang1tao 12:b31e43c9fb15 191 if(send_failed>=3){
wang1tao 12:b31e43c9fb15 192 // attempt to rejoin the network
wang1tao 12:b31e43c9fb15 193 logInfo("Attemp to rejoin network....");
wang1tao 12:b31e43c9fb15 194 if ((ret = dot->joinNetworkOnce()) != mDot::MDOT_OK) {
wang1tao 12:b31e43c9fb15 195 logError("Failed to rejoin network!"); // %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
wang1tao 12:b31e43c9fb15 196 }else{
wang1tao 12:b31e43c9fb15 197 logInfo("Rejoin network successfully!");
wang1tao 12:b31e43c9fb15 198 }
wang1tao 13:5d050b414f16 199 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
wang1tao 12:b31e43c9fb15 200 }
wang1tao 13:5d050b414f16 201
wang1tao 10:79124c0a5952 202 //printf("++++++++++++++++++++++\t");
mfiore 4:36e214ebfa56 203 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
wang1tao 9:cf45820af9b9 204 #ifndef NO_MULTITECH_GATEWAY
mfiore 4:36e214ebfa56 205 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
wang1tao 9:cf45820af9b9 206 #else
wang1tao 9:cf45820af9b9 207 osDelay(1000);
wang1tao 9:cf45820af9b9 208 #endif
mfiore 0:09250cd371d2 209 }
mfiore 0:09250cd371d2 210
mfiore 0:09250cd371d2 211 }