Code for the Reed Switch on an mDot

Dependencies:   DHT22 libmDot-mbed5

Committer:
lootspk
Date:
Tue Jun 12 02:38:01 2018 +0000
Revision:
14:c05bcdee1483
Parent:
13:dced485e5e95
Gate Monitor Code

Who changed what in which revision?

UserRevisionLine numberNew 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 "mbed.h"
kellybs1 10:02615da7a9fe 17 #include "DHT22.h"
mfiore 0:09250cd371d2 18 #include <string>
mfiore 0:09250cd371d2 19 #include <vector>
mfiore 4:36e214ebfa56 20 #include <algorithm>
kellybs1 10:02615da7a9fe 21 #include <sstream>
lootspk 14:c05bcdee1483 22
kellybs1 10:02615da7a9fe 23 //analogue ldr pin (A0 on UDK 2)
kellybs1 10:02615da7a9fe 24 AnalogIn a0(PB_1);
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
lootspk 14:c05bcdee1483 35 static uint8_t network_address[] = { 0x06, 0x10, 0x9f, 0xb0 };
kellybs1 11:45465d7cff1f 36 //network session key
lootspk 14:c05bcdee1483 37 static uint8_t network_session_key[] = { 0x04, 0xb9, 0xb4, 0x7f, 0x20, 0xdb, 0x3a, 0xa3, 0x8f, 0xf5, 0xef, 0xd1, 0x39, 0x02, 0xde, 0x5e };
kellybs1 11:45465d7cff1f 38 //application sesssion or data session key
lootspk 14:c05bcdee1483 39 static uint8_t data_session_key[] = { 0x92, 0x57, 0xa7, 0xe4, 0x2e, 0x47, 0x04, 0x54, 0x5c, 0xed, 0x0a, 0xbe, 0x5a, 0x99, 0xcd, 0xf9 };
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() {
kellybs1 11:45465d7cff1f 62 //setting serial rate
kellybs1 9:7f7194b5b4e3 63 pc.baud(9600);
mfiore 2:6e2c378339d9 64
kellybs1 9:7f7194b5b4e3 65 // use AU915 plan
kellybs1 9:7f7194b5b4e3 66 lora::ChannelPlan* plan = new lora::ChannelPlan_AU915();
kellybs1 7:e29228e39982 67 assert(plan);
kellybs1 11:45465d7cff1f 68 // get a mDot handle with the plan we chose
kellybs1 10:02615da7a9fe 69 dot = mDot::getInstance(plan);
kellybs1 11:45465d7cff1f 70 assert(dot);
mfiore 4:36e214ebfa56 71
kellybs1 10:02615da7a9fe 72 if (!dot->getStandbyFlag()) {
kellybs1 10:02615da7a9fe 73 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
kellybs1 10:02615da7a9fe 74
kellybs1 10:02615da7a9fe 75 // start from a well-known state
kellybs1 10:02615da7a9fe 76 logInfo("defaulting Dot configuration");
kellybs1 10:02615da7a9fe 77 dot->resetConfig();
kellybs1 10:02615da7a9fe 78 dot->resetNetworkSession();
kellybs1 10:02615da7a9fe 79
kellybs1 10:02615da7a9fe 80 // make sure library logging is turned on
kellybs1 10:02615da7a9fe 81 dot->setLogLevel(mts::MTSLog::DEBUG_LEVEL);
kellybs1 10:02615da7a9fe 82
kellybs1 10:02615da7a9fe 83 // update configuration if necessary
kellybs1 10:02615da7a9fe 84 if (dot->getJoinMode() != mDot::MANUAL) {
kellybs1 10:02615da7a9fe 85 logInfo("changing network join mode to MANUAL");
kellybs1 10:02615da7a9fe 86 if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 87 logError("failed to set network join mode to MANUAL");
kellybs1 10:02615da7a9fe 88 }
kellybs1 10:02615da7a9fe 89 }
kellybs1 10:02615da7a9fe 90 // in MANUAL join mode there is no join request/response transaction
kellybs1 10:02615da7a9fe 91 // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate
kellybs1 10:02615da7a9fe 92 // network address - 4 bytes (00000001 - FFFFFFFE)
kellybs1 10:02615da7a9fe 93 // network session key - 16 bytes
kellybs1 10:02615da7a9fe 94 // data session key - 16 bytes
kellybs1 10:02615da7a9fe 95 // to provision your Dot with a Conduit gateway, follow the following steps
kellybs1 10:02615da7a9fe 96 // * ssh into the Conduit
kellybs1 10:02615da7a9fe 97 // * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/
kellybs1 10:02615da7a9fe 98 // lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304
kellybs1 10:02615da7a9fe 99 // * if you change the network address, network session key, or data session key, make sure you update them on the gateway
kellybs1 10:02615da7a9fe 100 // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation
kellybs1 10:02615da7a9fe 101 update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, public_network, ack);
kellybs1 10:02615da7a9fe 102
kellybs1 10:02615da7a9fe 103
kellybs1 10:02615da7a9fe 104 // enable or disable Adaptive Data Rate
kellybs1 10:02615da7a9fe 105 dot->setAdr(adr);
kellybs1 10:02615da7a9fe 106
kellybs1 10:02615da7a9fe 107 //* AU915 Datarates
kellybs1 10:02615da7a9fe 108 //* ---------------
kellybs1 10:02615da7a9fe 109 //* DR0 - SF10BW125 -- 11 bytes
kellybs1 10:02615da7a9fe 110 //* DR1 - SF9BW125 -- 53 bytes
kellybs1 10:02615da7a9fe 111 //* DR2 - SF8BW125 -- 129 byte
kellybs1 10:02615da7a9fe 112 //* DR3 - SF7BW125 -- 242 bytes
kellybs1 10:02615da7a9fe 113 //* DR4 - SF8BW500 -- 242 bytes
kellybs1 10:02615da7a9fe 114 dot->setTxDataRate(mDot::DR2);
kellybs1 10:02615da7a9fe 115
kellybs1 10:02615da7a9fe 116 // save changes to configuration
kellybs1 10:02615da7a9fe 117 logInfo("saving configuration");
kellybs1 10:02615da7a9fe 118 if (!dot->saveConfig()) {
kellybs1 10:02615da7a9fe 119 logError("failed to save configuration");
kellybs1 10:02615da7a9fe 120 }
kellybs1 10:02615da7a9fe 121
kellybs1 10:02615da7a9fe 122 // display configuration
kellybs1 10:02615da7a9fe 123 display_config();
kellybs1 10:02615da7a9fe 124 } else {
kellybs1 10:02615da7a9fe 125 // restore the saved session if the dot woke from deepsleep mode
kellybs1 10:02615da7a9fe 126 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
kellybs1 10:02615da7a9fe 127 logInfo("restoring network session from NVM");
kellybs1 10:02615da7a9fe 128 dot->restoreNetworkSession();
jreiss 5:6b988a804fcb 129 }
jreiss 5:6b988a804fcb 130
kellybs1 11:45465d7cff1f 131 //this is where the magic happens
mfiore 0:09250cd371d2 132 while (true) {
kellybs1 10:02615da7a9fe 133
kellybs1 11:45465d7cff1f 134 //wake up
kellybs1 11:45465d7cff1f 135 wait(5);
kellybs1 10:02615da7a9fe 136 //init data variable
kellybs1 10:02615da7a9fe 137 std::vector<uint8_t> data;
lootspk 14:c05bcdee1483 138 logInfo("Starting reed sensor readings");
lootspk 14:c05bcdee1483 139 float reed1 = a0.read() * 10;
kellybs1 10:02615da7a9fe 140 wait_ms(100);
lootspk 14:c05bcdee1483 141 float reed2 = a0.read() * 10;
kellybs1 10:02615da7a9fe 142 wait_ms(100);
lootspk 14:c05bcdee1483 143 float reed3 = a0.read() * 10;
kellybs1 10:02615da7a9fe 144 wait_ms(100);
lootspk 12:8e9badbc7314 145
lootspk 14:c05bcdee1483 146 float reedFinal = (reed1 + reed2 + reed3)/3;
lootspk 14:c05bcdee1483 147 logInfo("Reed sensor readings taken");
lootspk 12:8e9badbc7314 148
lootspk 14:c05bcdee1483 149 if (reedFinal <= 0.1)
lootspk 14:c05bcdee1483 150 {
lootspk 14:c05bcdee1483 151 logInfo("/n");
lootspk 14:c05bcdee1483 152 logInfo("Gate is Closed!");
lootspk 14:c05bcdee1483 153 logInfo("/n");
lootspk 14:c05bcdee1483 154 }
lootspk 14:c05bcdee1483 155 else
lootspk 14:c05bcdee1483 156 {
lootspk 14:c05bcdee1483 157 logInfo("/n");
lootspk 14:c05bcdee1483 158 logInfo("Gate is Open!");
lootspk 14:c05bcdee1483 159 logInfo("/n");
lootspk 14:c05bcdee1483 160 }
kellybs1 10:02615da7a9fe 161
kellybs1 11:45465d7cff1f 162 //build our output string now
lootspk 14:c05bcdee1483 163 string reed_str = ToString(reedFinal);
lootspk 14:c05bcdee1483 164 string output = "Reed Switch Reading:" + reed_str;
lootspk 12:8e9badbc7314 165
kellybs1 10:02615da7a9fe 166
kellybs1 11:45465d7cff1f 167 //serial output for debugging
kellybs1 10:02615da7a9fe 168 logInfo("Sending %s", output.c_str());
kellybs1 10:02615da7a9fe 169
kellybs1 10:02615da7a9fe 170 // format data for sending to the gateway
kellybs1 10:02615da7a9fe 171 for (std::string::iterator it = output.begin(); it != output.end(); it++)
kellybs1 10:02615da7a9fe 172 data.push_back((uint8_t) *it);
kellybs1 10:02615da7a9fe 173
kellybs1 11:45465d7cff1f 174 //now send
kellybs1 10:02615da7a9fe 175 send_data(data);
lootspk 12:8e9badbc7314 176 logInfo("Data sent!");
mfiore 0:09250cd371d2 177
kellybs1 11:45465d7cff1f 178 // go to sleep and wake up automatically sleep_time seconds later
lootspk 14:c05bcdee1483 179 uint32_t sleep_time = 7;
kellybs1 11:45465d7cff1f 180 //false is "don't deep sleep" - mDot doesn't do that
kellybs1 11:45465d7cff1f 181 dot->sleep(sleep_time, mDot::RTC_ALARM, false);
mfiore 0:09250cd371d2 182 }
kellybs1 10:02615da7a9fe 183
kellybs1 11:45465d7cff1f 184 return 0; //shouldn't happen
kellybs1 10:02615da7a9fe 185 }
mfiore 0:09250cd371d2 186