Da ka xi tong.

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_Connect_IoTClub_one_wire_4ch_8_24 by wireless sensor

Committer:
Dengjj
Date:
Wed Aug 24 11:52:28 2016 +0000
Revision:
14:0616e4361f0c
Parent:
13:5d050b414f16
Child:
15:04b37f329c84
4ch working

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