for PH sensor Hub
Dependencies: Analog_PH_Sensor libmDot mbed-rtos mbed
Fork of LoRA_PH_SenorHub by
main.cpp@0:edb3ff445825, 2016-08-11 (annotated)
- Committer:
- wang1tao
- Date:
- Thu Aug 11 11:11:36 2016 +0000
- Revision:
- 0:edb3ff445825
- Child:
- 1:7e277a45b3f7
First PH sensor hub
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wang1tao | 0:edb3ff445825 | 1 | #include "mbed.h" |
wang1tao | 0:edb3ff445825 | 2 | #include "mDot.h" |
wang1tao | 0:edb3ff445825 | 3 | #include "MTSLog.h" |
wang1tao | 0:edb3ff445825 | 4 | #include <string> |
wang1tao | 0:edb3ff445825 | 5 | #include <vector> |
wang1tao | 0:edb3ff445825 | 6 | #include <algorithm> |
wang1tao | 0:edb3ff445825 | 7 | |
wang1tao | 0:edb3ff445825 | 8 | #include "AnalogPHSensor.h" |
wang1tao | 0:edb3ff445825 | 9 | |
wang1tao | 0:edb3ff445825 | 10 | AnalogPHSensor PHSensor0(PB_0); |
wang1tao | 0:edb3ff445825 | 11 | //AnalogPHSensor PHSensor1(PB_1); |
wang1tao | 0:edb3ff445825 | 12 | //AnalogPHSensor PHSensor2(PB_2); |
wang1tao | 0:edb3ff445825 | 13 | //AnalogPHSensor PHSensor3(PB_3); |
wang1tao | 0:edb3ff445825 | 14 | |
wang1tao | 0:edb3ff445825 | 15 | // these options must match the settings on your Conduit |
wang1tao | 0:edb3ff445825 | 16 | // uncomment the following lines and edit their values to match your configuration |
wang1tao | 0:edb3ff445825 | 17 | static std::string config_network_name = "phsensorhub"; |
wang1tao | 0:edb3ff445825 | 18 | static std::string config_network_pass = "iotlab"; |
wang1tao | 0:edb3ff445825 | 19 | static uint8_t config_frequency_sub_band = 2; |
wang1tao | 0:edb3ff445825 | 20 | |
wang1tao | 0:edb3ff445825 | 21 | int main() { |
wang1tao | 0:edb3ff445825 | 22 | int32_t ret; |
wang1tao | 0:edb3ff445825 | 23 | mDot* dot; |
wang1tao | 0:edb3ff445825 | 24 | std::vector<uint8_t> data; |
wang1tao | 0:edb3ff445825 | 25 | std::vector<uint8_t> send_data; |
wang1tao | 0:edb3ff445825 | 26 | float PhValue; |
wang1tao | 0:edb3ff445825 | 27 | std::string data_str = "PH Sensors Hub"; |
wang1tao | 0:edb3ff445825 | 28 | int error = 0; |
wang1tao | 0:edb3ff445825 | 29 | float ph_ch0; |
wang1tao | 0:edb3ff445825 | 30 | char dataBuf[20]; |
wang1tao | 0:edb3ff445825 | 31 | |
wang1tao | 0:edb3ff445825 | 32 | #ifdef NO_MULTITECH_GATEWAY |
wang1tao | 0:edb3ff445825 | 33 | // get a mDot handle |
wang1tao | 0:edb3ff445825 | 34 | dot = mDot::getInstance(); |
wang1tao | 0:edb3ff445825 | 35 | #endif |
wang1tao | 0:edb3ff445825 | 36 | |
wang1tao | 0:edb3ff445825 | 37 | // print library version information |
wang1tao | 0:edb3ff445825 | 38 | logInfo("version: %s", dot->getId().c_str()); |
wang1tao | 0:edb3ff445825 | 39 | |
wang1tao | 0:edb3ff445825 | 40 | #ifdef NO_MULTITECH_GATEWAY |
wang1tao | 0:edb3ff445825 | 41 | //******************************************* |
wang1tao | 0:edb3ff445825 | 42 | // configuration |
wang1tao | 0:edb3ff445825 | 43 | //******************************************* |
wang1tao | 0:edb3ff445825 | 44 | // reset to default config so we know what state we're in |
wang1tao | 0:edb3ff445825 | 45 | dot->resetConfig(); |
wang1tao | 0:edb3ff445825 | 46 | |
wang1tao | 0:edb3ff445825 | 47 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
wang1tao | 0:edb3ff445825 | 48 | |
wang1tao | 0:edb3ff445825 | 49 | // set up the mDot with our network information: frequency sub band, network name, and network password |
wang1tao | 0:edb3ff445825 | 50 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
wang1tao | 0:edb3ff445825 | 51 | |
wang1tao | 0:edb3ff445825 | 52 | // frequency sub band is only applicable in the 915 (US) frequency band |
wang1tao | 0:edb3ff445825 | 53 | // 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 |
wang1tao | 0:edb3ff445825 | 54 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
wang1tao | 0:edb3ff445825 | 55 | logInfo("setting frequency sub band"); |
wang1tao | 0:edb3ff445825 | 56 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
wang1tao | 0:edb3ff445825 | 57 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 0:edb3ff445825 | 58 | } |
wang1tao | 0:edb3ff445825 | 59 | |
wang1tao | 0:edb3ff445825 | 60 | logInfo("setting network name"); |
wang1tao | 0:edb3ff445825 | 61 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
wang1tao | 0:edb3ff445825 | 62 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 0:edb3ff445825 | 63 | } |
wang1tao | 0:edb3ff445825 | 64 | |
wang1tao | 0:edb3ff445825 | 65 | logInfo("setting network password"); |
wang1tao | 0:edb3ff445825 | 66 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
wang1tao | 0:edb3ff445825 | 67 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 0:edb3ff445825 | 68 | } |
wang1tao | 0:edb3ff445825 | 69 | |
wang1tao | 0:edb3ff445825 | 70 | // a higher spreading factor allows for longer range but lower throughput |
wang1tao | 0:edb3ff445825 | 71 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
wang1tao | 0:edb3ff445825 | 72 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
wang1tao | 0:edb3ff445825 | 73 | logInfo("setting TX spreading factor"); |
wang1tao | 0:edb3ff445825 | 74 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
wang1tao | 0:edb3ff445825 | 75 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 0:edb3ff445825 | 76 | } |
wang1tao | 0:edb3ff445825 | 77 | |
wang1tao | 0:edb3ff445825 | 78 | // request receive confirmation of packets from the gateway |
wang1tao | 0:edb3ff445825 | 79 | logInfo("enabling ACKs"); |
wang1tao | 0:edb3ff445825 | 80 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
wang1tao | 0:edb3ff445825 | 81 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 0:edb3ff445825 | 82 | } |
wang1tao | 0:edb3ff445825 | 83 | |
wang1tao | 0:edb3ff445825 | 84 | // save this configuration to the mDot's NVM |
wang1tao | 0:edb3ff445825 | 85 | logInfo("saving config"); |
wang1tao | 0:edb3ff445825 | 86 | if (! dot->saveConfig()) { |
wang1tao | 0:edb3ff445825 | 87 | logError("failed to save configuration"); |
wang1tao | 0:edb3ff445825 | 88 | } |
wang1tao | 0:edb3ff445825 | 89 | //******************************************* |
wang1tao | 0:edb3ff445825 | 90 | // end of configuration |
wang1tao | 0:edb3ff445825 | 91 | //******************************************* |
wang1tao | 0:edb3ff445825 | 92 | |
wang1tao | 0:edb3ff445825 | 93 | // attempt to join the network |
wang1tao | 0:edb3ff445825 | 94 | logInfo("joining network"); |
wang1tao | 0:edb3ff445825 | 95 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
wang1tao | 0:edb3ff445825 | 96 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 0:edb3ff445825 | 97 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
wang1tao | 0:edb3ff445825 | 98 | osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); |
wang1tao | 0:edb3ff445825 | 99 | } |
wang1tao | 0:edb3ff445825 | 100 | |
wang1tao | 0:edb3ff445825 | 101 | // format data for sending to the gateway |
wang1tao | 0:edb3ff445825 | 102 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
wang1tao | 0:edb3ff445825 | 103 | data.push_back((uint8_t) *it); |
wang1tao | 0:edb3ff445825 | 104 | #endif |
wang1tao | 0:edb3ff445825 | 105 | |
wang1tao | 0:edb3ff445825 | 106 | while (true) { |
wang1tao | 0:edb3ff445825 | 107 | #ifdef NO_MULTITECH_GATEWAY |
wang1tao | 0:edb3ff445825 | 108 | // send the data to the gateway |
wang1tao | 0:edb3ff445825 | 109 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
wang1tao | 0:edb3ff445825 | 110 | logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); |
wang1tao | 0:edb3ff445825 | 111 | } else { |
wang1tao | 0:edb3ff445825 | 112 | logInfo("successfully sent data to gateway"); |
wang1tao | 0:edb3ff445825 | 113 | } |
wang1tao | 0:edb3ff445825 | 114 | #endif |
wang1tao | 0:edb3ff445825 | 115 | |
wang1tao | 0:edb3ff445825 | 116 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
wang1tao | 0:edb3ff445825 | 117 | osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); |
wang1tao | 0:edb3ff445825 | 118 | |
wang1tao | 0:edb3ff445825 | 119 | //read data from CH0 |
wang1tao | 0:edb3ff445825 | 120 | ph_ch0 = PHSensor0; |
wang1tao | 0:edb3ff445825 | 121 | logInfo("CH0[PH]: %5.4f", ph_ch0); |
wang1tao | 0:edb3ff445825 | 122 | |
wang1tao | 0:edb3ff445825 | 123 | sprintf(dataBuf, "CH0[PH]: %5.4f", ph_ch0); |
wang1tao | 0:edb3ff445825 | 124 | |
wang1tao | 0:edb3ff445825 | 125 | // probably not the most efficent way to do this |
wang1tao | 0:edb3ff445825 | 126 | for( int i=0; i< strlen(dataBuf); i++ ){ |
wang1tao | 0:edb3ff445825 | 127 | data.push_back( dataBuf[i]); |
wang1tao | 0:edb3ff445825 | 128 | } |
wang1tao | 0:edb3ff445825 | 129 | |
wang1tao | 0:edb3ff445825 | 130 | } |
wang1tao | 0:edb3ff445825 | 131 | |
wang1tao | 0:edb3ff445825 | 132 | // return 0; |
wang1tao | 0:edb3ff445825 | 133 | } |