Full functions
Dependencies: DHT22 libmDot-mbed5
Fork of mDot_LoRa_Connect_ABPA by
main.cpp@10:02615da7a9fe, 2017-08-21 (annotated)
- Committer:
- kellybs1
- Date:
- Mon Aug 21 05:01:01 2017 +0000
- Revision:
- 10:02615da7a9fe
- Parent:
- 9:7f7194b5b4e3
- Child:
- 11:45465d7cff1f
Working version
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" |
kellybs1 | 8:206e0563e1a1 | 3 | #include "ChannelPlans.h" |
mfiore | 4:36e214ebfa56 | 4 | #include "MTSLog.h" |
kellybs1 | 10:02615da7a9fe | 5 | #include "dot_util.h" |
kellybs1 | 10:02615da7a9fe | 6 | #include "mbed.h" |
kellybs1 | 10:02615da7a9fe | 7 | #include "DHT22.h" |
mfiore | 0:09250cd371d2 | 8 | #include <string> |
mfiore | 0:09250cd371d2 | 9 | #include <vector> |
mfiore | 4:36e214ebfa56 | 10 | #include <algorithm> |
kellybs1 | 10:02615da7a9fe | 11 | #include <sstream> |
kellybs1 | 10:02615da7a9fe | 12 | |
kellybs1 | 10:02615da7a9fe | 13 | //dht 22 ping (D6 on UDK 2) |
kellybs1 | 10:02615da7a9fe | 14 | DHT22 dht22(PA_1); |
kellybs1 | 10:02615da7a9fe | 15 | //analogue ldr pin (A0 on UDK 2) |
kellybs1 | 10:02615da7a9fe | 16 | AnalogIn a0(PB_1); |
mfiore | 0:09250cd371d2 | 17 | |
mfiore | 2:6e2c378339d9 | 18 | // these options must match the settings on your Conduit |
kellybs1 | 10:02615da7a9fe | 19 | |
kellybs1 | 10:02615da7a9fe | 20 | /* |
kellybs1 | 10:02615da7a9fe | 21 | Current test settings |
kellybs1 | 10:02615da7a9fe | 22 | dev address: 072389f7 |
kellybs1 | 10:02615da7a9fe | 23 | net sess key: b35aca73d283996dc3cbc0803af04547 |
kellybs1 | 10:02615da7a9fe | 24 | app sess key: d6f28430da4035273b9e3c07eb30c0dd |
kellybs1 | 10:02615da7a9fe | 25 | */ |
kellybs1 | 10:02615da7a9fe | 26 | static uint8_t network_address[] = { 0x07, 0x23, 0x89, 0xf7 }; |
kellybs1 | 10:02615da7a9fe | 27 | static uint8_t network_session_key[] = { 0xb3, 0x5a, 0xca, 0x73, 0xd2, 0x83, 0x99, 0x6d, 0xc3, 0xcb, 0xc0, 0x80, 0x3a, 0xf0, 0x45, 0x47 }; |
kellybs1 | 10:02615da7a9fe | 28 | static uint8_t data_session_key[] = { 0xd6, 0xf2, 0x84, 0x30, 0xda, 0x40, 0x35, 0x27, 0x3b, 0x9e, 0x3c, 0x07, 0xeb, 0x30, 0xc0, 0xdd }; |
kellybs1 | 10:02615da7a9fe | 29 | static uint8_t frequency_sub_band = 2; |
kellybs1 | 10:02615da7a9fe | 30 | static bool public_network = true; |
kellybs1 | 10:02615da7a9fe | 31 | static uint8_t ack = 0; |
kellybs1 | 10:02615da7a9fe | 32 | static bool adr = false; |
mfiore | 0:09250cd371d2 | 33 | |
kellybs1 | 9:7f7194b5b4e3 | 34 | Serial pc(USBTX, USBRX); |
kellybs1 | 9:7f7194b5b4e3 | 35 | |
kellybs1 | 10:02615da7a9fe | 36 | mDot* dot = NULL; |
kellybs1 | 9:7f7194b5b4e3 | 37 | |
kellybs1 | 9:7f7194b5b4e3 | 38 | |
kellybs1 | 10:02615da7a9fe | 39 | //converts value to string |
kellybs1 | 10:02615da7a9fe | 40 | template <typename T> |
kellybs1 | 10:02615da7a9fe | 41 | string ToString(T val) { |
kellybs1 | 10:02615da7a9fe | 42 | stringstream stream; |
kellybs1 | 10:02615da7a9fe | 43 | stream << val; |
kellybs1 | 10:02615da7a9fe | 44 | return stream.str(); |
kellybs1 | 10:02615da7a9fe | 45 | } |
kellybs1 | 10:02615da7a9fe | 46 | |
mfiore | 0:09250cd371d2 | 47 | int main() { |
kellybs1 | 9:7f7194b5b4e3 | 48 | pc.baud(9600); |
kellybs1 | 10:02615da7a9fe | 49 | //std::string data_str = "hello world"; |
mfiore | 2:6e2c378339d9 | 50 | |
kellybs1 | 9:7f7194b5b4e3 | 51 | // use AU915 plan |
kellybs1 | 9:7f7194b5b4e3 | 52 | lora::ChannelPlan* plan = new lora::ChannelPlan_AU915(); |
kellybs1 | 7:e29228e39982 | 53 | assert(plan); |
mfiore | 0:09250cd371d2 | 54 | // get a mDot handle |
kellybs1 | 10:02615da7a9fe | 55 | dot = mDot::getInstance(plan); |
kellybs1 | 7:e29228e39982 | 56 | assert(dot); |
mfiore | 2:6e2c378339d9 | 57 | |
mfiore | 4:36e214ebfa56 | 58 | |
kellybs1 | 10:02615da7a9fe | 59 | if (!dot->getStandbyFlag()) { |
kellybs1 | 10:02615da7a9fe | 60 | logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
kellybs1 | 10:02615da7a9fe | 61 | |
kellybs1 | 10:02615da7a9fe | 62 | // start from a well-known state |
kellybs1 | 10:02615da7a9fe | 63 | logInfo("defaulting Dot configuration"); |
kellybs1 | 10:02615da7a9fe | 64 | dot->resetConfig(); |
kellybs1 | 10:02615da7a9fe | 65 | dot->resetNetworkSession(); |
kellybs1 | 10:02615da7a9fe | 66 | |
kellybs1 | 10:02615da7a9fe | 67 | // make sure library logging is turned on |
kellybs1 | 10:02615da7a9fe | 68 | dot->setLogLevel(mts::MTSLog::DEBUG_LEVEL); |
kellybs1 | 10:02615da7a9fe | 69 | |
kellybs1 | 10:02615da7a9fe | 70 | // update configuration if necessary |
kellybs1 | 10:02615da7a9fe | 71 | if (dot->getJoinMode() != mDot::MANUAL) { |
kellybs1 | 10:02615da7a9fe | 72 | logInfo("changing network join mode to MANUAL"); |
kellybs1 | 10:02615da7a9fe | 73 | if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) { |
kellybs1 | 10:02615da7a9fe | 74 | logError("failed to set network join mode to MANUAL"); |
kellybs1 | 10:02615da7a9fe | 75 | } |
kellybs1 | 10:02615da7a9fe | 76 | } |
kellybs1 | 10:02615da7a9fe | 77 | // in MANUAL join mode there is no join request/response transaction |
kellybs1 | 10:02615da7a9fe | 78 | // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate |
kellybs1 | 10:02615da7a9fe | 79 | // network address - 4 bytes (00000001 - FFFFFFFE) |
kellybs1 | 10:02615da7a9fe | 80 | // network session key - 16 bytes |
kellybs1 | 10:02615da7a9fe | 81 | // data session key - 16 bytes |
kellybs1 | 10:02615da7a9fe | 82 | // to provision your Dot with a Conduit gateway, follow the following steps |
kellybs1 | 10:02615da7a9fe | 83 | // * ssh into the Conduit |
kellybs1 | 10:02615da7a9fe | 84 | // * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/ |
kellybs1 | 10:02615da7a9fe | 85 | // lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304 |
kellybs1 | 10:02615da7a9fe | 86 | // * if you change the network address, network session key, or data session key, make sure you update them on the gateway |
kellybs1 | 10:02615da7a9fe | 87 | // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation |
kellybs1 | 10:02615da7a9fe | 88 | update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, public_network, ack); |
kellybs1 | 10:02615da7a9fe | 89 | |
kellybs1 | 10:02615da7a9fe | 90 | |
kellybs1 | 10:02615da7a9fe | 91 | // enable or disable Adaptive Data Rate |
kellybs1 | 10:02615da7a9fe | 92 | dot->setAdr(adr); |
kellybs1 | 10:02615da7a9fe | 93 | |
kellybs1 | 10:02615da7a9fe | 94 | //* AU915 Datarates |
kellybs1 | 10:02615da7a9fe | 95 | //* --------------- |
kellybs1 | 10:02615da7a9fe | 96 | //* DR0 - SF10BW125 -- 11 bytes |
kellybs1 | 10:02615da7a9fe | 97 | //* DR1 - SF9BW125 -- 53 bytes |
kellybs1 | 10:02615da7a9fe | 98 | //* DR2 - SF8BW125 -- 129 byte |
kellybs1 | 10:02615da7a9fe | 99 | //* DR3 - SF7BW125 -- 242 bytes |
kellybs1 | 10:02615da7a9fe | 100 | //* DR4 - SF8BW500 -- 242 bytes |
kellybs1 | 10:02615da7a9fe | 101 | dot->setTxDataRate(mDot::DR2); |
kellybs1 | 10:02615da7a9fe | 102 | |
kellybs1 | 10:02615da7a9fe | 103 | // save changes to configuration |
kellybs1 | 10:02615da7a9fe | 104 | logInfo("saving configuration"); |
kellybs1 | 10:02615da7a9fe | 105 | if (!dot->saveConfig()) { |
kellybs1 | 10:02615da7a9fe | 106 | logError("failed to save configuration"); |
kellybs1 | 10:02615da7a9fe | 107 | } |
kellybs1 | 10:02615da7a9fe | 108 | |
kellybs1 | 10:02615da7a9fe | 109 | // display configuration |
kellybs1 | 10:02615da7a9fe | 110 | display_config(); |
kellybs1 | 10:02615da7a9fe | 111 | } else { |
kellybs1 | 10:02615da7a9fe | 112 | // restore the saved session if the dot woke from deepsleep mode |
kellybs1 | 10:02615da7a9fe | 113 | // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep |
kellybs1 | 10:02615da7a9fe | 114 | logInfo("restoring network session from NVM"); |
kellybs1 | 10:02615da7a9fe | 115 | dot->restoreNetworkSession(); |
jreiss | 5:6b988a804fcb | 116 | } |
jreiss | 5:6b988a804fcb | 117 | |
mfiore | 0:09250cd371d2 | 118 | |
mfiore | 0:09250cd371d2 | 119 | while (true) { |
kellybs1 | 10:02615da7a9fe | 120 | |
kellybs1 | 10:02615da7a9fe | 121 | //init data variable |
kellybs1 | 10:02615da7a9fe | 122 | std::vector<uint8_t> data; |
kellybs1 | 10:02615da7a9fe | 123 | |
kellybs1 | 10:02615da7a9fe | 124 | //read LDR |
kellybs1 | 10:02615da7a9fe | 125 | //read LDR as float (0.0-1.0, multiply by 1000) |
kellybs1 | 10:02615da7a9fe | 126 | float ldr1 = a0.read() * 1000; |
kellybs1 | 10:02615da7a9fe | 127 | wait_ms(100); |
kellybs1 | 10:02615da7a9fe | 128 | float ldr2 = a0.read() * 1000; |
kellybs1 | 10:02615da7a9fe | 129 | wait_ms(100); |
kellybs1 | 10:02615da7a9fe | 130 | float ldr3 = a0.read() * 1000; |
kellybs1 | 10:02615da7a9fe | 131 | wait_ms(100); |
kellybs1 | 10:02615da7a9fe | 132 | //average the multiple readings - smoother output |
kellybs1 | 10:02615da7a9fe | 133 | float ldr = (ldr1 + ldr2 + ldr3) / 3; |
kellybs1 | 10:02615da7a9fe | 134 | |
kellybs1 | 10:02615da7a9fe | 135 | //read DHT22 |
kellybs1 | 10:02615da7a9fe | 136 | //get dht22 sample |
kellybs1 | 10:02615da7a9fe | 137 | int error = dht22.sample(); |
kellybs1 | 10:02615da7a9fe | 138 | wait_ms(100); |
kellybs1 | 10:02615da7a9fe | 139 | //it's required to divide these values by ten for some reason |
kellybs1 | 10:02615da7a9fe | 140 | float h = (float)dht22.getHumidity() / 10; |
kellybs1 | 10:02615da7a9fe | 141 | float t = (float)dht22.getTemperature() / 10; |
kellybs1 | 10:02615da7a9fe | 142 | |
kellybs1 | 10:02615da7a9fe | 143 | string l_str = ToString(ldr); |
kellybs1 | 10:02615da7a9fe | 144 | string h_str = ToString(h); |
kellybs1 | 10:02615da7a9fe | 145 | string t_str = ToString(t); |
kellybs1 | 10:02615da7a9fe | 146 | string output = "L:" + l_str + " H:" + h_str + " T:" + t_str; |
kellybs1 | 10:02615da7a9fe | 147 | |
kellybs1 | 10:02615da7a9fe | 148 | logInfo("Sending %s", output.c_str()); |
kellybs1 | 10:02615da7a9fe | 149 | |
kellybs1 | 10:02615da7a9fe | 150 | // format data for sending to the gateway |
kellybs1 | 10:02615da7a9fe | 151 | for (std::string::iterator it = output.begin(); it != output.end(); it++) |
kellybs1 | 10:02615da7a9fe | 152 | data.push_back((uint8_t) *it); |
kellybs1 | 10:02615da7a9fe | 153 | |
kellybs1 | 10:02615da7a9fe | 154 | send_data(data); |
mfiore | 0:09250cd371d2 | 155 | |
kellybs1 | 10:02615da7a9fe | 156 | //wait before resending |
kellybs1 | 10:02615da7a9fe | 157 | wait(15); |
mfiore | 0:09250cd371d2 | 158 | } |
kellybs1 | 10:02615da7a9fe | 159 | |
kellybs1 | 10:02615da7a9fe | 160 | return 0; |
kellybs1 | 10:02615da7a9fe | 161 | } |
mfiore | 0:09250cd371d2 | 162 |