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