asd
Dependencies: DHT libmDot ISL29011
main.cpp@0:0577c3a1738e, 2017-03-27 (annotated)
- Committer:
- hooony
- Date:
- Mon Mar 27 09:43:43 2017 +0000
- Revision:
- 0:0577c3a1738e
asd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hooony | 0:0577c3a1738e | 1 | #include "mbed.h" |
hooony | 0:0577c3a1738e | 2 | #include "mDot.h" |
hooony | 0:0577c3a1738e | 3 | #include "MTSLog.h" |
hooony | 0:0577c3a1738e | 4 | #include "DHT.h" |
hooony | 0:0577c3a1738e | 5 | #include <string> |
hooony | 0:0577c3a1738e | 6 | #include <vector> |
hooony | 0:0577c3a1738e | 7 | #include <algorithm> |
hooony | 0:0577c3a1738e | 8 | |
hooony | 0:0577c3a1738e | 9 | // these options must match the settings on your Conduit |
hooony | 0:0577c3a1738e | 10 | // uncomment the following lines and edit their values to match your configuration |
hooony | 0:0577c3a1738e | 11 | static std::string config_network_name = "<house123>"; |
hooony | 0:0577c3a1738e | 12 | static std::string config_network_pass = "<12345678>"; |
hooony | 0:0577c3a1738e | 13 | static uint8_t config_frequency_sub_band = 7; |
hooony | 0:0577c3a1738e | 14 | DHT sensor(PB_1, DHT11); |
hooony | 0:0577c3a1738e | 15 | |
hooony | 0:0577c3a1738e | 16 | Serial pc(USBTX,USBRX); |
hooony | 0:0577c3a1738e | 17 | |
hooony | 0:0577c3a1738e | 18 | int main() { |
hooony | 0:0577c3a1738e | 19 | int32_t ret; |
hooony | 0:0577c3a1738e | 20 | mDot* dot; |
hooony | 0:0577c3a1738e | 21 | std::vector<uint8_t> data; |
hooony | 0:0577c3a1738e | 22 | std::string data_str = "hello!"; |
hooony | 0:0577c3a1738e | 23 | |
hooony | 0:0577c3a1738e | 24 | float temperature = 0.0; |
hooony | 0:0577c3a1738e | 25 | // pc.baud(115200); |
hooony | 0:0577c3a1738e | 26 | pc.printf("mDot LoRa Temperature & Humidity Sensor data\n\r"); |
hooony | 0:0577c3a1738e | 27 | |
hooony | 0:0577c3a1738e | 28 | // printf("hihihi\n"); |
hooony | 0:0577c3a1738e | 29 | wait(0.2); |
hooony | 0:0577c3a1738e | 30 | while (true) |
hooony | 0:0577c3a1738e | 31 | { |
hooony | 0:0577c3a1738e | 32 | int readResult = sensor.readData(); |
hooony | 0:0577c3a1738e | 33 | if (readResult != 0) |
hooony | 0:0577c3a1738e | 34 | { |
hooony | 0:0577c3a1738e | 35 | printf("\r\n error %d", readResult); |
hooony | 0:0577c3a1738e | 36 | } |
hooony | 0:0577c3a1738e | 37 | else { |
hooony | 0:0577c3a1738e | 38 | printf ("\r\n Temp = %f", sensor.ReadTemperature(CELCIUS)); |
hooony | 0:0577c3a1738e | 39 | } |
hooony | 0:0577c3a1738e | 40 | wait(20); |
hooony | 0:0577c3a1738e | 41 | } |
hooony | 0:0577c3a1738e | 42 | |
hooony | 0:0577c3a1738e | 43 | // get a mDot handle |
hooony | 0:0577c3a1738e | 44 | dot = mDot::getInstance(); |
hooony | 0:0577c3a1738e | 45 | |
hooony | 0:0577c3a1738e | 46 | // print library version information |
hooony | 0:0577c3a1738e | 47 | logInfo("version: %s", dot->getId().c_str()); |
hooony | 0:0577c3a1738e | 48 | |
hooony | 0:0577c3a1738e | 49 | //******************************************* |
hooony | 0:0577c3a1738e | 50 | // configuration |
hooony | 0:0577c3a1738e | 51 | //******************************************* |
hooony | 0:0577c3a1738e | 52 | // reset to default config so we know what state we're in |
hooony | 0:0577c3a1738e | 53 | dot->resetConfig(); |
hooony | 0:0577c3a1738e | 54 | |
hooony | 0:0577c3a1738e | 55 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
hooony | 0:0577c3a1738e | 56 | |
hooony | 0:0577c3a1738e | 57 | // set up the mDot with our network information: frequency sub band, network name, and network password |
hooony | 0:0577c3a1738e | 58 | // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() |
hooony | 0:0577c3a1738e | 59 | |
hooony | 0:0577c3a1738e | 60 | // frequency sub band is only applicable in the 915 (US) frequency band |
hooony | 0:0577c3a1738e | 61 | // 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 |
hooony | 0:0577c3a1738e | 62 | // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels |
hooony | 0:0577c3a1738e | 63 | logInfo("setting frequency sub band"); |
hooony | 0:0577c3a1738e | 64 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
hooony | 0:0577c3a1738e | 65 | logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
hooony | 0:0577c3a1738e | 66 | } |
hooony | 0:0577c3a1738e | 67 | |
hooony | 0:0577c3a1738e | 68 | logInfo("setting network name"); |
hooony | 0:0577c3a1738e | 69 | if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
hooony | 0:0577c3a1738e | 70 | logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
hooony | 0:0577c3a1738e | 71 | } |
hooony | 0:0577c3a1738e | 72 | |
hooony | 0:0577c3a1738e | 73 | logInfo("setting network password"); |
hooony | 0:0577c3a1738e | 74 | if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
hooony | 0:0577c3a1738e | 75 | logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
hooony | 0:0577c3a1738e | 76 | } |
hooony | 0:0577c3a1738e | 77 | |
hooony | 0:0577c3a1738e | 78 | // a higher spreading factor allows for longer range but lower throughput |
hooony | 0:0577c3a1738e | 79 | // in the 915 (US) frequency band, spreading factors 7 - 10 are available |
hooony | 0:0577c3a1738e | 80 | // in the 868 (EU) frequency band, spreading factors 7 - 12 are available |
hooony | 0:0577c3a1738e | 81 | logInfo("setting TX spreading factor"); |
hooony | 0:0577c3a1738e | 82 | if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { |
hooony | 0:0577c3a1738e | 83 | logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
hooony | 0:0577c3a1738e | 84 | } |
hooony | 0:0577c3a1738e | 85 | |
hooony | 0:0577c3a1738e | 86 | // request receive confirmation of packets from the gateway |
hooony | 0:0577c3a1738e | 87 | logInfo("enabling ACKs"); |
hooony | 0:0577c3a1738e | 88 | if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { |
hooony | 0:0577c3a1738e | 89 | logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
hooony | 0:0577c3a1738e | 90 | } |
hooony | 0:0577c3a1738e | 91 | |
hooony | 0:0577c3a1738e | 92 | // set join mode to AUTO_OTA so the mDot doesn't have to rejoin after sleeping |
hooony | 0:0577c3a1738e | 93 | logInfo("setting join mode to AUTO_OTA"); |
hooony | 0:0577c3a1738e | 94 | if ((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) { |
hooony | 0:0577c3a1738e | 95 | logError("failed to set join mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
hooony | 0:0577c3a1738e | 96 | } |
hooony | 0:0577c3a1738e | 97 | |
hooony | 0:0577c3a1738e | 98 | // save this configuration to the mDot's NVM |
hooony | 0:0577c3a1738e | 99 | logInfo("saving config"); |
hooony | 0:0577c3a1738e | 100 | if (! dot->saveConfig()) { |
hooony | 0:0577c3a1738e | 101 | logError("failed to save configuration"); |
hooony | 0:0577c3a1738e | 102 | } |
hooony | 0:0577c3a1738e | 103 | //******************************************* |
hooony | 0:0577c3a1738e | 104 | // end of configuration |
hooony | 0:0577c3a1738e | 105 | //******************************************* |
hooony | 0:0577c3a1738e | 106 | |
hooony | 0:0577c3a1738e | 107 | // format data for sending to the gateway |
hooony | 0:0577c3a1738e | 108 | for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) |
hooony | 0:0577c3a1738e | 109 | data.push_back((uint8_t) *it); |
hooony | 0:0577c3a1738e | 110 | |
hooony | 0:0577c3a1738e | 111 | // join the network if not joined |
hooony | 0:0577c3a1738e | 112 | if (!dot->getNetworkJoinStatus()) { |
hooony | 0:0577c3a1738e | 113 | logInfo("network not joined, joining network"); |
hooony | 0:0577c3a1738e | 114 | if ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
hooony | 0:0577c3a1738e | 115 | logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
hooony | 0:0577c3a1738e | 116 | } |
hooony | 0:0577c3a1738e | 117 | } |
hooony | 0:0577c3a1738e | 118 | if (dot->getNetworkJoinStatus()) { |
hooony | 0:0577c3a1738e | 119 | // send the data |
hooony | 0:0577c3a1738e | 120 | // ACKs are enabled by default, so we're expecting to get one back |
hooony | 0:0577c3a1738e | 121 | if ((ret = dot->send(data)) != mDot::MDOT_OK) { |
hooony | 0:0577c3a1738e | 122 | logError("failed to send %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
hooony | 0:0577c3a1738e | 123 | } else { |
hooony | 0:0577c3a1738e | 124 | logInfo("successfully sent data to gateway"); |
hooony | 0:0577c3a1738e | 125 | } |
hooony | 0:0577c3a1738e | 126 | } |
hooony | 0:0577c3a1738e | 127 | |
hooony | 0:0577c3a1738e | 128 | // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again |
hooony | 0:0577c3a1738e | 129 | uint32_t sleep_time = std::max((uint32_t)10000, (uint32_t)dot->getNextTxMs()) / 1000; |
hooony | 0:0577c3a1738e | 130 | logInfo("going to sleep..."); |
hooony | 0:0577c3a1738e | 131 | |
hooony | 0:0577c3a1738e | 132 | // go to deepsleep and wake up automatically sleep_time seconds later |
hooony | 0:0577c3a1738e | 133 | dot->sleep(sleep_time, mDot::RTC_ALARM); |
hooony | 0:0577c3a1738e | 134 | |
hooony | 0:0577c3a1738e | 135 | // go to deepsleep and wake up on rising edge of WKUP pin (PA0/XBEE_CTS/XBEE_DIO7) |
hooony | 0:0577c3a1738e | 136 | // dot->sleep(0, mDot::INTERRUPT); |
hooony | 0:0577c3a1738e | 137 | |
hooony | 0:0577c3a1738e | 138 | return 0; |
hooony | 0:0577c3a1738e | 139 | } |