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