Full functions

Dependencies:   DHT22 libmDot-mbed5

Fork of mDot_LoRa_Connect_ABPA by Brendan Kelly

Committer:
kellybs1
Date:
Mon Aug 28 00:42:34 2017 +0000
Revision:
11:45465d7cff1f
Parent:
10:02615da7a9fe
Updating with forked libMdot

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>
kellybs1 10:02615da7a9fe 22
kellybs1 11:45465d7cff1f 23 //dht 22 pin (D6 on UDK 2)
kellybs1 10:02615da7a9fe 24 DHT22 dht22(PA_1);
kellybs1 10:02615da7a9fe 25 //analogue ldr pin (A0 on UDK 2)
kellybs1 10:02615da7a9fe 26 AnalogIn a0(PB_1);
mfiore 0:09250cd371d2 27
mfiore 2:6e2c378339d9 28 // these options must match the settings on your Conduit
kellybs1 10:02615da7a9fe 29 /*
kellybs1 10:02615da7a9fe 30 Current test settings
kellybs1 10:02615da7a9fe 31 dev address: 072389f7
kellybs1 10:02615da7a9fe 32 net sess key: b35aca73d283996dc3cbc0803af04547
kellybs1 10:02615da7a9fe 33 app sess key: d6f28430da4035273b9e3c07eb30c0dd
kellybs1 10:02615da7a9fe 34 */
kellybs1 11:45465d7cff1f 35 //device address
kellybs1 10:02615da7a9fe 36 static uint8_t network_address[] = { 0x07, 0x23, 0x89, 0xf7 };
kellybs1 11:45465d7cff1f 37 //network session key
kellybs1 10:02615da7a9fe 38 static uint8_t network_session_key[] = { 0xb3, 0x5a, 0xca, 0x73, 0xd2, 0x83, 0x99, 0x6d, 0xc3, 0xcb, 0xc0, 0x80, 0x3a, 0xf0, 0x45, 0x47 };
kellybs1 11:45465d7cff1f 39 //application sesssion or data session key
kellybs1 10:02615da7a9fe 40 static uint8_t data_session_key[] = { 0xd6, 0xf2, 0x84, 0x30, 0xda, 0x40, 0x35, 0x27, 0x3b, 0x9e, 0x3c, 0x07, 0xeb, 0x30, 0xc0, 0xdd };
kellybs1 11:45465d7cff1f 41 static uint8_t frequency_sub_band = 2; //VFI
kellybs1 10:02615da7a9fe 42 static bool public_network = true;
kellybs1 11:45465d7cff1f 43 //enable receipt of ackknowledge packets 0 = No, 1 = Yes
kellybs1 10:02615da7a9fe 44 static uint8_t ack = 0;
kellybs1 11:45465d7cff1f 45 //adaptive data rate enabler
kellybs1 10:02615da7a9fe 46 static bool adr = false;
mfiore 0:09250cd371d2 47
kellybs1 11:45465d7cff1f 48 //USB serial
kellybs1 9:7f7194b5b4e3 49 Serial pc(USBTX, USBRX);
kellybs1 9:7f7194b5b4e3 50
kellybs1 11:45465d7cff1f 51 //get ourselves an mDot pointer - we will assign to it in main()
kellybs1 10:02615da7a9fe 52 mDot* dot = NULL;
kellybs1 9:7f7194b5b4e3 53
kellybs1 10:02615da7a9fe 54 //converts value to string
kellybs1 10:02615da7a9fe 55 template <typename T>
kellybs1 10:02615da7a9fe 56 string ToString(T val) {
kellybs1 10:02615da7a9fe 57 stringstream stream;
kellybs1 10:02615da7a9fe 58 stream << val;
kellybs1 10:02615da7a9fe 59 return stream.str();
kellybs1 10:02615da7a9fe 60 }
kellybs1 10:02615da7a9fe 61
mfiore 0:09250cd371d2 62 int main() {
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;
kellybs1 10:02615da7a9fe 139
kellybs1 10:02615da7a9fe 140 //read LDR
kellybs1 11:45465d7cff1f 141 //read LDR as float (0.0-1.0, multiply by 1000)
kellybs1 11:45465d7cff1f 142 //read multiple times - this just smoothes the value out a little bit
kellybs1 10:02615da7a9fe 143 float ldr1 = a0.read() * 1000;
kellybs1 10:02615da7a9fe 144 wait_ms(100);
kellybs1 10:02615da7a9fe 145 float ldr2 = a0.read() * 1000;
kellybs1 10:02615da7a9fe 146 wait_ms(100);
kellybs1 10:02615da7a9fe 147 float ldr3 = a0.read() * 1000;
kellybs1 10:02615da7a9fe 148 wait_ms(100);
kellybs1 11:45465d7cff1f 149 //average the multiple readings
kellybs1 10:02615da7a9fe 150 float ldr = (ldr1 + ldr2 + ldr3) / 3;
kellybs1 10:02615da7a9fe 151
kellybs1 10:02615da7a9fe 152 //read DHT22
kellybs1 10:02615da7a9fe 153 //get dht22 sample
kellybs1 10:02615da7a9fe 154 int error = dht22.sample();
kellybs1 11:45465d7cff1f 155 //sampling is a little slow - give it time
kellybs1 10:02615da7a9fe 156 wait_ms(100);
kellybs1 10:02615da7a9fe 157 //it's required to divide these values by ten for some reason
kellybs1 10:02615da7a9fe 158 float h = (float)dht22.getHumidity() / 10;
kellybs1 10:02615da7a9fe 159 float t = (float)dht22.getTemperature() / 10;
kellybs1 10:02615da7a9fe 160
kellybs1 11:45465d7cff1f 161 //build our output string now
kellybs1 10:02615da7a9fe 162 string l_str = ToString(ldr);
kellybs1 10:02615da7a9fe 163 string h_str = ToString(h);
kellybs1 10:02615da7a9fe 164 string t_str = ToString(t);
kellybs1 10:02615da7a9fe 165 string output = "L:" + l_str + " H:" + h_str + " T:" + t_str;
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);
mfiore 0:09250cd371d2 176
kellybs1 11:45465d7cff1f 177 // go to sleep and wake up automatically sleep_time seconds later
kellybs1 11:45465d7cff1f 178 uint32_t sleep_time = 60;
kellybs1 11:45465d7cff1f 179 //false is "don't deep sleep" - mDot doesn't do that
kellybs1 11:45465d7cff1f 180 dot->sleep(sleep_time, mDot::RTC_ALARM, false);
mfiore 0:09250cd371d2 181 }
kellybs1 10:02615da7a9fe 182
kellybs1 11:45465d7cff1f 183 return 0; //shouldn't happen
kellybs1 10:02615da7a9fe 184 }
mfiore 0:09250cd371d2 185