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