Gate Sensor code
Dependencies: libmDot-mbed5 DHT22
main.cpp@15:e7d3271a84f3, 2019-03-07 (annotated)
- Committer:
- jakef1
- Date:
- Thu Mar 07 02:03:37 2019 +0000
- Revision:
- 15:e7d3271a84f3
- Parent:
- 14:c05bcdee1483
Gate Sensor code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kellybs1 | 11:45465d7cff1f | 1 | /* |
kellybs1 | 11:45465d7cff1f | 2 | This program: |
kellybs1 | 11:45465d7cff1f | 3 | - connects to a LoRaWAN by ABP/MANUAL |
kellybs1 | 11:45465d7cff1f | 4 | - reads light data from an analogue Light Dependent Resistor |
kellybs1 | 11:45465d7cff1f | 5 | - reads temperaure and humidity from a DHT22 sensor |
kellybs1 | 11:45465d7cff1f | 6 | - sends the recorded data onto the LoRaWAN |
kellybs1 | 11:45465d7cff1f | 7 | - sets the mDot to sleep |
kellybs1 | 11:45465d7cff1f | 8 | - repeats these operations in a loop |
kellybs1 | 11:45465d7cff1f | 9 | */ |
kellybs1 | 11:45465d7cff1f | 10 | |
mfiore | 0:09250cd371d2 | 11 | #include "mbed.h" |
mfiore | 0:09250cd371d2 | 12 | #include "mDot.h" |
kellybs1 | 8:206e0563e1a1 | 13 | #include "ChannelPlans.h" |
mfiore | 4:36e214ebfa56 | 14 | #include "MTSLog.h" |
kellybs1 | 10:02615da7a9fe | 15 | #include "dot_util.h" |
kellybs1 | 10:02615da7a9fe | 16 | #include "DHT22.h" |
mfiore | 0:09250cd371d2 | 17 | #include <string> |
mfiore | 0:09250cd371d2 | 18 | #include <vector> |
mfiore | 4:36e214ebfa56 | 19 | #include <algorithm> |
kellybs1 | 10:02615da7a9fe | 20 | #include <sstream> |
lootspk | 14:c05bcdee1483 | 21 | |
kellybs1 | 10:02615da7a9fe | 22 | //analogue ldr pin (A0 on UDK 2) |
jakef1 | 15:e7d3271a84f3 | 23 | //AnalogIn gateopen(PB_1); |
jakef1 | 15:e7d3271a84f3 | 24 | DigitalIn gateopen(D0); |
lootspk | 14:c05bcdee1483 | 25 | |
mfiore | 0:09250cd371d2 | 26 | |
mfiore | 2:6e2c378339d9 | 27 | // these options must match the settings on your Conduit |
kellybs1 | 10:02615da7a9fe | 28 | /* |
kellybs1 | 10:02615da7a9fe | 29 | Current test settings |
lootspk | 13:dced485e5e95 | 30 | dev address: 064ac45b |
lootspk | 13:dced485e5e95 | 31 | net sess key: 5526fe0a28a974eb070f7b450433c35c |
lootspk | 13:dced485e5e95 | 32 | app sess key: f42ea6890802d8f2c3e9d0d9f37c80a6 |
kellybs1 | 10:02615da7a9fe | 33 | */ |
kellybs1 | 11:45465d7cff1f | 34 | //device address |
jakef1 | 15:e7d3271a84f3 | 35 | static uint8_t network_address[] = { 0x07, 0xd4, 0xe9, 0xec }; |
kellybs1 | 11:45465d7cff1f | 36 | //network session key |
jakef1 | 15:e7d3271a84f3 | 37 | static uint8_t network_session_key[] = { 0xd6, 0xcf, 0x2b, 0x6c, 0xcd, 0xb0, 0xcf, 0x9f, 0xe4, 0xb1, 0xca, 0x15, 0x98, 0x56, 0xd3, 0x58 }; |
kellybs1 | 11:45465d7cff1f | 38 | //application sesssion or data session key |
jakef1 | 15:e7d3271a84f3 | 39 | static uint8_t data_session_key[] = { 0x4f, 0x84, 0x76, 0x93, 0xab, 0x1b, 0x50, 0x69, 0x08, 0x84, 0x25, 0xc3, 0xb4, 0x9b, 0x96, 0xd6 }; |
kellybs1 | 11:45465d7cff1f | 40 | static uint8_t frequency_sub_band = 2; //VFI |
kellybs1 | 10:02615da7a9fe | 41 | static bool public_network = true; |
kellybs1 | 11:45465d7cff1f | 42 | //enable receipt of ackknowledge packets 0 = No, 1 = Yes |
kellybs1 | 10:02615da7a9fe | 43 | static uint8_t ack = 0; |
kellybs1 | 11:45465d7cff1f | 44 | //adaptive data rate enabler |
kellybs1 | 10:02615da7a9fe | 45 | static bool adr = false; |
mfiore | 0:09250cd371d2 | 46 | |
kellybs1 | 11:45465d7cff1f | 47 | //USB serial |
kellybs1 | 9:7f7194b5b4e3 | 48 | Serial pc(USBTX, USBRX); |
kellybs1 | 9:7f7194b5b4e3 | 49 | |
kellybs1 | 11:45465d7cff1f | 50 | //get ourselves an mDot pointer - we will assign to it in main() |
kellybs1 | 10:02615da7a9fe | 51 | mDot* dot = NULL; |
kellybs1 | 9:7f7194b5b4e3 | 52 | |
kellybs1 | 10:02615da7a9fe | 53 | //converts value to string |
kellybs1 | 10:02615da7a9fe | 54 | template <typename T> |
kellybs1 | 10:02615da7a9fe | 55 | string ToString(T val) { |
kellybs1 | 10:02615da7a9fe | 56 | stringstream stream; |
kellybs1 | 10:02615da7a9fe | 57 | stream << val; |
kellybs1 | 10:02615da7a9fe | 58 | return stream.str(); |
kellybs1 | 10:02615da7a9fe | 59 | } |
kellybs1 | 10:02615da7a9fe | 60 | |
mfiore | 0:09250cd371d2 | 61 | int main() { |
jakef1 | 15:e7d3271a84f3 | 62 | gateopen.mode(PullUp); |
kellybs1 | 11:45465d7cff1f | 63 | //setting serial rate |
kellybs1 | 9:7f7194b5b4e3 | 64 | pc.baud(9600); |
mfiore | 2:6e2c378339d9 | 65 | |
kellybs1 | 9:7f7194b5b4e3 | 66 | // use AU915 plan |
kellybs1 | 9:7f7194b5b4e3 | 67 | lora::ChannelPlan* plan = new lora::ChannelPlan_AU915(); |
kellybs1 | 7:e29228e39982 | 68 | assert(plan); |
kellybs1 | 11:45465d7cff1f | 69 | // get a mDot handle with the plan we chose |
kellybs1 | 10:02615da7a9fe | 70 | dot = mDot::getInstance(plan); |
kellybs1 | 11:45465d7cff1f | 71 | assert(dot); |
mfiore | 4:36e214ebfa56 | 72 | |
kellybs1 | 10:02615da7a9fe | 73 | if (!dot->getStandbyFlag()) { |
kellybs1 | 10:02615da7a9fe | 74 | logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
kellybs1 | 10:02615da7a9fe | 75 | |
kellybs1 | 10:02615da7a9fe | 76 | // start from a well-known state |
kellybs1 | 10:02615da7a9fe | 77 | logInfo("defaulting Dot configuration"); |
kellybs1 | 10:02615da7a9fe | 78 | dot->resetConfig(); |
kellybs1 | 10:02615da7a9fe | 79 | dot->resetNetworkSession(); |
kellybs1 | 10:02615da7a9fe | 80 | |
kellybs1 | 10:02615da7a9fe | 81 | // make sure library logging is turned on |
kellybs1 | 10:02615da7a9fe | 82 | dot->setLogLevel(mts::MTSLog::DEBUG_LEVEL); |
kellybs1 | 10:02615da7a9fe | 83 | |
kellybs1 | 10:02615da7a9fe | 84 | // update configuration if necessary |
kellybs1 | 10:02615da7a9fe | 85 | if (dot->getJoinMode() != mDot::MANUAL) { |
kellybs1 | 10:02615da7a9fe | 86 | logInfo("changing network join mode to MANUAL"); |
kellybs1 | 10:02615da7a9fe | 87 | if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) { |
kellybs1 | 10:02615da7a9fe | 88 | logError("failed to set network join mode to MANUAL"); |
kellybs1 | 10:02615da7a9fe | 89 | } |
kellybs1 | 10:02615da7a9fe | 90 | } |
kellybs1 | 10:02615da7a9fe | 91 | // in MANUAL join mode there is no join request/response transaction |
kellybs1 | 10:02615da7a9fe | 92 | // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate |
kellybs1 | 10:02615da7a9fe | 93 | // network address - 4 bytes (00000001 - FFFFFFFE) |
kellybs1 | 10:02615da7a9fe | 94 | // network session key - 16 bytes |
kellybs1 | 10:02615da7a9fe | 95 | // data session key - 16 bytes |
kellybs1 | 10:02615da7a9fe | 96 | // to provision your Dot with a Conduit gateway, follow the following steps |
kellybs1 | 10:02615da7a9fe | 97 | // * ssh into the Conduit |
kellybs1 | 10:02615da7a9fe | 98 | // * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/ |
kellybs1 | 10:02615da7a9fe | 99 | // lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304 |
kellybs1 | 10:02615da7a9fe | 100 | // * if you change the network address, network session key, or data session key, make sure you update them on the gateway |
kellybs1 | 10:02615da7a9fe | 101 | // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation |
kellybs1 | 10:02615da7a9fe | 102 | update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, public_network, ack); |
kellybs1 | 10:02615da7a9fe | 103 | |
kellybs1 | 10:02615da7a9fe | 104 | |
kellybs1 | 10:02615da7a9fe | 105 | // enable or disable Adaptive Data Rate |
kellybs1 | 10:02615da7a9fe | 106 | dot->setAdr(adr); |
kellybs1 | 10:02615da7a9fe | 107 | |
kellybs1 | 10:02615da7a9fe | 108 | //* AU915 Datarates |
kellybs1 | 10:02615da7a9fe | 109 | //* --------------- |
kellybs1 | 10:02615da7a9fe | 110 | //* DR0 - SF10BW125 -- 11 bytes |
kellybs1 | 10:02615da7a9fe | 111 | //* DR1 - SF9BW125 -- 53 bytes |
kellybs1 | 10:02615da7a9fe | 112 | //* DR2 - SF8BW125 -- 129 byte |
kellybs1 | 10:02615da7a9fe | 113 | //* DR3 - SF7BW125 -- 242 bytes |
kellybs1 | 10:02615da7a9fe | 114 | //* DR4 - SF8BW500 -- 242 bytes |
kellybs1 | 10:02615da7a9fe | 115 | dot->setTxDataRate(mDot::DR2); |
kellybs1 | 10:02615da7a9fe | 116 | |
kellybs1 | 10:02615da7a9fe | 117 | // save changes to configuration |
kellybs1 | 10:02615da7a9fe | 118 | logInfo("saving configuration"); |
kellybs1 | 10:02615da7a9fe | 119 | if (!dot->saveConfig()) { |
kellybs1 | 10:02615da7a9fe | 120 | logError("failed to save configuration"); |
kellybs1 | 10:02615da7a9fe | 121 | } |
kellybs1 | 10:02615da7a9fe | 122 | |
kellybs1 | 10:02615da7a9fe | 123 | // display configuration |
kellybs1 | 10:02615da7a9fe | 124 | display_config(); |
kellybs1 | 10:02615da7a9fe | 125 | } else { |
kellybs1 | 10:02615da7a9fe | 126 | // restore the saved session if the dot woke from deepsleep mode |
kellybs1 | 10:02615da7a9fe | 127 | // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep |
kellybs1 | 10:02615da7a9fe | 128 | logInfo("restoring network session from NVM"); |
kellybs1 | 10:02615da7a9fe | 129 | dot->restoreNetworkSession(); |
jreiss | 5:6b988a804fcb | 130 | } |
jreiss | 5:6b988a804fcb | 131 | |
kellybs1 | 11:45465d7cff1f | 132 | //this is where the magic happens |
mfiore | 0:09250cd371d2 | 133 | while (true) { |
kellybs1 | 10:02615da7a9fe | 134 | |
kellybs1 | 11:45465d7cff1f | 135 | //wake up |
kellybs1 | 11:45465d7cff1f | 136 | wait(5); |
kellybs1 | 10:02615da7a9fe | 137 | //init data variable |
kellybs1 | 10:02615da7a9fe | 138 | std::vector<uint8_t> data; |
lootspk | 14:c05bcdee1483 | 139 | logInfo("Starting reed sensor readings"); |
jakef1 | 15:e7d3271a84f3 | 140 | string reed_str = ""; |
jakef1 | 15:e7d3271a84f3 | 141 | if (gateopen) |
lootspk | 14:c05bcdee1483 | 142 | { |
lootspk | 14:c05bcdee1483 | 143 | logInfo("/n"); |
jakef1 | 15:e7d3271a84f3 | 144 | logInfo("Gate is Open!"); |
lootspk | 14:c05bcdee1483 | 145 | logInfo("/n"); |
jakef1 | 15:e7d3271a84f3 | 146 | reed_str = ToString(0); |
lootspk | 14:c05bcdee1483 | 147 | } |
lootspk | 14:c05bcdee1483 | 148 | else |
lootspk | 14:c05bcdee1483 | 149 | { |
lootspk | 14:c05bcdee1483 | 150 | logInfo("/n"); |
jakef1 | 15:e7d3271a84f3 | 151 | logInfo("Gate is Close!"); |
lootspk | 14:c05bcdee1483 | 152 | logInfo("/n"); |
jakef1 | 15:e7d3271a84f3 | 153 | reed_str = ToString(1); |
lootspk | 14:c05bcdee1483 | 154 | } |
kellybs1 | 10:02615da7a9fe | 155 | |
kellybs1 | 11:45465d7cff1f | 156 | //build our output string now |
jakef1 | 15:e7d3271a84f3 | 157 | string output = reed_str; |
lootspk | 12:8e9badbc7314 | 158 | |
kellybs1 | 10:02615da7a9fe | 159 | |
kellybs1 | 11:45465d7cff1f | 160 | //serial output for debugging |
kellybs1 | 10:02615da7a9fe | 161 | logInfo("Sending %s", output.c_str()); |
kellybs1 | 10:02615da7a9fe | 162 | |
kellybs1 | 10:02615da7a9fe | 163 | // format data for sending to the gateway |
kellybs1 | 10:02615da7a9fe | 164 | for (std::string::iterator it = output.begin(); it != output.end(); it++) |
kellybs1 | 10:02615da7a9fe | 165 | data.push_back((uint8_t) *it); |
kellybs1 | 10:02615da7a9fe | 166 | |
kellybs1 | 11:45465d7cff1f | 167 | //now send |
kellybs1 | 10:02615da7a9fe | 168 | send_data(data); |
lootspk | 12:8e9badbc7314 | 169 | logInfo("Data sent!"); |
mfiore | 0:09250cd371d2 | 170 | |
kellybs1 | 11:45465d7cff1f | 171 | // go to sleep and wake up automatically sleep_time seconds later |
lootspk | 14:c05bcdee1483 | 172 | uint32_t sleep_time = 7; |
kellybs1 | 11:45465d7cff1f | 173 | //false is "don't deep sleep" - mDot doesn't do that |
kellybs1 | 11:45465d7cff1f | 174 | dot->sleep(sleep_time, mDot::RTC_ALARM, false); |
mfiore | 0:09250cd371d2 | 175 | } |
kellybs1 | 10:02615da7a9fe | 176 | |
kellybs1 | 11:45465d7cff1f | 177 | return 0; //shouldn't happen |
jakef1 | 15:e7d3271a84f3 | 178 | } |