for PH sensor Hub

Dependencies:   Analog_PH_Sensor libmDot mbed-rtos mbed

Fork of LoRA_PH_SenorHub by tao wang1

Committer:
wang1tao
Date:
Thu Aug 11 11:23:38 2016 +0000
Revision:
2:0aa55ad78695
Parent:
1:7e277a45b3f7
add debug info

Who changed what in which revision?

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