alrm working version
Dependencies: DHT libmDot mbed-rtos mbed
Fork of mDot_Connect_IoTClub_one_wire_4ch_8_24 by
main.cpp@9:cf45820af9b9, 2016-08-13 (annotated)
- Committer:
- wang1tao
- Date:
- Sat Aug 13 09:34:57 2016 +0000
- Revision:
- 9:cf45820af9b9
- Parent:
- 8:dfcee7a96019
- Child:
- 10:79124c0a5952
Changed for multi-channel PH measurement
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 | 9:cf45820af9b9 | 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 | 9:cf45820af9b9 | 22 | AnalogPHSensor CH2_PH(PA_4, 0.0, 0.0); |
wang1tao | 9:cf45820af9b9 | 23 | #endif |
wang1tao | 9:cf45820af9b9 | 24 | #if (NUM_OF_CH >3) |
wang1tao | 9:cf45820af9b9 | 25 | AnalogPHSensor CH3_PH(PA_5, 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"; |
mfiore | 0:09250cd371d2 | 32 | |
mfiore | 0:09250cd371d2 | 33 | int main() { |
wang1tao | 9:cf45820af9b9 | 34 | |
mfiore | 0:09250cd371d2 | 35 | mDot* dot; |
mfiore | 0:09250cd371d2 | 36 | std::vector<uint8_t> data; |
wang1tao | 9:cf45820af9b9 | 37 | float phValue; |
wang1tao | 9:cf45820af9b9 | 38 | char _header[] = "PH Sensors"; |
wang1tao | 8:dfcee7a96019 | 39 | char dataBuf[11]; |
wang1tao | 9:cf45820af9b9 | 40 | int i, k; |
wang1tao | 9:cf45820af9b9 | 41 | |
mfiore | 0:09250cd371d2 | 42 | // get a mDot handle |
mfiore | 0:09250cd371d2 | 43 | dot = mDot::getInstance(); |
JCheng | 5:6c7d2f4d7377 | 44 | |
mfiore | 2:6e2c378339d9 | 45 | // print library version information |
mfiore | 2:6e2c378339d9 | 46 | logInfo("version: %s", dot->getId().c_str()); |
mfiore | 0:09250cd371d2 | 47 | |
mfiore | 2:6e2c378339d9 | 48 | //******************************************* |
mfiore | 2:6e2c378339d9 | 49 | // configuration |
mfiore | 2:6e2c378339d9 | 50 | //******************************************* |
mfiore | 0:09250cd371d2 | 51 | // reset to default config so we know what state we're in |
mfiore | 0:09250cd371d2 | 52 | dot->resetConfig(); |
JCheng | 5:6c7d2f4d7377 | 53 | |
mfiore | 4:36e214ebfa56 | 54 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
wang1tao | 9:cf45820af9b9 | 55 | #ifndef NO_MULTITECH_GATEWAY |
mfiore | 2:6e2c378339d9 | 56 | // set up the mDot with our network information: frequency sub band, network name, and network password |
mfiore | 2:6e2c378339d9 | 57 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
JCheng | 5:6c7d2f4d7377 | 58 | |
mfiore | 4:36e214ebfa56 | 59 | // frequency sub band is only applicable in the 915 (US) frequency band |
mfiore | 4:36e214ebfa56 | 60 | // 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 | 61 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
mfiore | 2:6e2c378339d9 | 62 | logInfo("setting frequency sub band"); |
mfiore | 0:09250cd371d2 | 63 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 64 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 2:6e2c378339d9 | 65 | } |
JCheng | 5:6c7d2f4d7377 | 66 | |
mfiore | 2:6e2c378339d9 | 67 | logInfo("setting network name"); |
mfiore | 2:6e2c378339d9 | 68 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 69 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 70 | } |
JCheng | 5:6c7d2f4d7377 | 71 | |
mfiore | 2:6e2c378339d9 | 72 | logInfo("setting network password"); |
mfiore | 2:6e2c378339d9 | 73 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 74 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 0:09250cd371d2 | 75 | } |
JCheng | 5:6c7d2f4d7377 | 76 | |
mfiore | 4:36e214ebfa56 | 77 | // a higher spreading factor allows for longer range but lower throughput |
mfiore | 4:36e214ebfa56 | 78 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
mfiore | 4:36e214ebfa56 | 79 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
mfiore | 4:36e214ebfa56 | 80 | logInfo("setting TX spreading factor"); |
mfiore | 4:36e214ebfa56 | 81 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
mfiore | 4:36e214ebfa56 | 82 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 83 | } |
JCheng | 5:6c7d2f4d7377 | 84 | |
mfiore | 4:36e214ebfa56 | 85 | // request receive confirmation of packets from the gateway |
mfiore | 4:36e214ebfa56 | 86 | logInfo("enabling ACKs"); |
mfiore | 4:36e214ebfa56 | 87 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
mfiore | 4:36e214ebfa56 | 88 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 89 | } |
JCheng | 5:6c7d2f4d7377 | 90 | |
mfiore | 4:36e214ebfa56 | 91 | // save this configuration to the mDot's NVM |
mfiore | 2:6e2c378339d9 | 92 | logInfo("saving config"); |
mfiore | 2:6e2c378339d9 | 93 | if (! dot->saveConfig()) { |
mfiore | 2:6e2c378339d9 | 94 | logError("failed to save configuration"); |
mfiore | 0:09250cd371d2 | 95 | } |
mfiore | 2:6e2c378339d9 | 96 | //******************************************* |
mfiore | 2:6e2c378339d9 | 97 | // end of configuration |
mfiore | 2:6e2c378339d9 | 98 | //******************************************* |
mfiore | 0:09250cd371d2 | 99 | |
mfiore | 0:09250cd371d2 | 100 | // attempt to join the network |
mfiore | 2:6e2c378339d9 | 101 | logInfo("joining network"); |
mfiore | 0:09250cd371d2 | 102 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
mfiore | 2:6e2c378339d9 | 103 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
mfiore | 4:36e214ebfa56 | 104 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
mfiore | 4:36e214ebfa56 | 105 | osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
mfiore | 0:09250cd371d2 | 106 | } |
mfiore | 0:09250cd371d2 | 107 | |
mfiore | 0:09250cd371d2 | 108 | // format data for sending to the gateway |
wang1tao | 9:cf45820af9b9 | 109 | for( int i=0; i< strlen(_header); i++ ) |
wang1tao | 9:cf45820af9b9 | 110 | data.push_back( _header[i] ); |
wang1tao | 9:cf45820af9b9 | 111 | |
wang1tao | 9:cf45820af9b9 | 112 | // send the data to the gateway |
wang1tao | 9:cf45820af9b9 | 113 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
wang1tao | 9:cf45820af9b9 | 114 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 9:cf45820af9b9 | 115 | } else { |
wang1tao | 9:cf45820af9b9 | 116 | logInfo("successfully sent data to gateway"); |
wang1tao | 9:cf45820af9b9 | 117 | } |
wang1tao | 9:cf45820af9b9 | 118 | #else |
wang1tao | 9:cf45820af9b9 | 119 | logInfo("%s", _header); |
wang1tao | 9:cf45820af9b9 | 120 | #endif |
mfiore | 0:09250cd371d2 | 121 | while (true) { |
wang1tao | 9:cf45820af9b9 | 122 | |
wang1tao | 9:cf45820af9b9 | 123 | // Read the PH values |
wang1tao | 9:cf45820af9b9 | 124 | for(k=0; k<NUM_OF_CH; k++){ |
wang1tao | 9:cf45820af9b9 | 125 | switch(k){ |
wang1tao | 9:cf45820af9b9 | 126 | case 0: phValue = CH0_PH; break; |
wang1tao | 9:cf45820af9b9 | 127 | #if (NUM_OF_CH >1) |
wang1tao | 9:cf45820af9b9 | 128 | case 1: phValue = CH1_PH; break; |
wang1tao | 9:cf45820af9b9 | 129 | #endif |
wang1tao | 9:cf45820af9b9 | 130 | #if (NUM_OF_CH >2) |
wang1tao | 9:cf45820af9b9 | 131 | case 2: phValue = CH2_PH; break; |
wang1tao | 9:cf45820af9b9 | 132 | #endif |
wang1tao | 9:cf45820af9b9 | 133 | #if (NUM_OF_CH >3) |
wang1tao | 9:cf45820af9b9 | 134 | case 3: phValue = CH3_PH; break; |
wang1tao | 9:cf45820af9b9 | 135 | #endif |
wang1tao | 9:cf45820af9b9 | 136 | default: |
wang1tao | 9:cf45820af9b9 | 137 | break; |
wang1tao | 9:cf45820af9b9 | 138 | } |
wang1tao | 9:cf45820af9b9 | 139 | //for(i=0; i<1;i++) printf("3:[CH%d]", k); |
wang1tao | 9:cf45820af9b9 | 140 | sprintf(dataBuf, "CH%d:%5.4f", k, phValue); |
wang1tao | 9:cf45820af9b9 | 141 | //for(i=0; i<1;i++) printf("4:[%s]", dataBuf); |
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 | 9:cf45820af9b9 | 154 | } else { |
mfiore | 2:6e2c378339d9 | 155 | logInfo("successfully sent data to gateway"); |
wang1tao | 9:cf45820af9b9 | 156 | } |
wang1tao | 9:cf45820af9b9 | 157 | #endif |
wang1tao | 9:cf45820af9b9 | 158 | } //for |
mfiore | 4:36e214ebfa56 | 159 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
wang1tao | 9:cf45820af9b9 | 160 | #ifndef NO_MULTITECH_GATEWAY |
mfiore | 4:36e214ebfa56 | 161 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
wang1tao | 9:cf45820af9b9 | 162 | #else |
wang1tao | 9:cf45820af9b9 | 163 | osDelay(1000); |
wang1tao | 9:cf45820af9b9 | 164 | #endif |
mfiore | 0:09250cd371d2 | 165 | } |
mfiore | 0:09250cd371d2 | 166 | |
mfiore | 0:09250cd371d2 | 167 | } |