mDot - 915 MultiTech mDot UDK board DHT22 Sensor 10 KOhm resistor 330Ohm resistor, standard light dependent resistor and a sparkfun soil moisture sensor

Dependencies:   DHT22 libmDot-mbed5

Fork of mDot_LoRa_Connect_ABPA_DHT22_sleep by Brendan Kelly

Committer:
lootspk
Date:
Thu May 17 02:09:26 2018 +0000
Revision:
13:dced485e5e95
Parent:
12:8e9badbc7314
Changed the unique keys

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);
lootspk 12:8e9badbc7314 27 //Analogue soil moisture pin
lootspk 12:8e9badbc7314 28 AnalogIn a2(PC_1);
mfiore 0:09250cd371d2 29
mfiore 2:6e2c378339d9 30 // these options must match the settings on your Conduit
kellybs1 10:02615da7a9fe 31 /*
kellybs1 10:02615da7a9fe 32 Current test settings
lootspk 13:dced485e5e95 33 dev address: 064ac45b
lootspk 13:dced485e5e95 34 net sess key: 5526fe0a28a974eb070f7b450433c35c
lootspk 13:dced485e5e95 35 app sess key: f42ea6890802d8f2c3e9d0d9f37c80a6
kellybs1 10:02615da7a9fe 36 */
kellybs1 11:45465d7cff1f 37 //device address
lootspk 13:dced485e5e95 38 static uint8_t network_address[] = { 0x06, 0x4a, 0xc4, 0x5b };
kellybs1 11:45465d7cff1f 39 //network session key
lootspk 13:dced485e5e95 40 static uint8_t network_session_key[] = { 0x55, 0x26, 0xfe, 0x0a, 0x28, 0xa9, 0x74, 0xeb, 0x07, 0x0f, 0x7b, 0x45, 0x04, 0x33, 0xc3, 0x5c };
kellybs1 11:45465d7cff1f 41 //application sesssion or data session key
lootspk 13:dced485e5e95 42 static uint8_t data_session_key[] = { 0xf4, 0x2e, 0xa6, 0x89, 0x08, 0x02, 0xd8, 0xf2, 0xc3, 0xe9, 0xd0, 0xd9, 0xf3, 0x7c, 0x80, 0xa6 };
kellybs1 11:45465d7cff1f 43 static uint8_t frequency_sub_band = 2; //VFI
kellybs1 10:02615da7a9fe 44 static bool public_network = true;
kellybs1 11:45465d7cff1f 45 //enable receipt of ackknowledge packets 0 = No, 1 = Yes
kellybs1 10:02615da7a9fe 46 static uint8_t ack = 0;
kellybs1 11:45465d7cff1f 47 //adaptive data rate enabler
kellybs1 10:02615da7a9fe 48 static bool adr = false;
mfiore 0:09250cd371d2 49
kellybs1 11:45465d7cff1f 50 //USB serial
kellybs1 9:7f7194b5b4e3 51 Serial pc(USBTX, USBRX);
kellybs1 9:7f7194b5b4e3 52
kellybs1 11:45465d7cff1f 53 //get ourselves an mDot pointer - we will assign to it in main()
kellybs1 10:02615da7a9fe 54 mDot* dot = NULL;
kellybs1 9:7f7194b5b4e3 55
kellybs1 10:02615da7a9fe 56 //converts value to string
kellybs1 10:02615da7a9fe 57 template <typename T>
kellybs1 10:02615da7a9fe 58 string ToString(T val) {
kellybs1 10:02615da7a9fe 59 stringstream stream;
kellybs1 10:02615da7a9fe 60 stream << val;
kellybs1 10:02615da7a9fe 61 return stream.str();
kellybs1 10:02615da7a9fe 62 }
kellybs1 10:02615da7a9fe 63
mfiore 0:09250cd371d2 64 int main() {
kellybs1 11:45465d7cff1f 65 //setting serial rate
kellybs1 9:7f7194b5b4e3 66 pc.baud(9600);
mfiore 2:6e2c378339d9 67
kellybs1 9:7f7194b5b4e3 68 // use AU915 plan
kellybs1 9:7f7194b5b4e3 69 lora::ChannelPlan* plan = new lora::ChannelPlan_AU915();
kellybs1 7:e29228e39982 70 assert(plan);
kellybs1 11:45465d7cff1f 71 // get a mDot handle with the plan we chose
kellybs1 10:02615da7a9fe 72 dot = mDot::getInstance(plan);
kellybs1 11:45465d7cff1f 73 assert(dot);
mfiore 4:36e214ebfa56 74
kellybs1 10:02615da7a9fe 75 if (!dot->getStandbyFlag()) {
kellybs1 10:02615da7a9fe 76 logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
kellybs1 10:02615da7a9fe 77
kellybs1 10:02615da7a9fe 78 // start from a well-known state
kellybs1 10:02615da7a9fe 79 logInfo("defaulting Dot configuration");
kellybs1 10:02615da7a9fe 80 dot->resetConfig();
kellybs1 10:02615da7a9fe 81 dot->resetNetworkSession();
kellybs1 10:02615da7a9fe 82
kellybs1 10:02615da7a9fe 83 // make sure library logging is turned on
kellybs1 10:02615da7a9fe 84 dot->setLogLevel(mts::MTSLog::DEBUG_LEVEL);
kellybs1 10:02615da7a9fe 85
kellybs1 10:02615da7a9fe 86 // update configuration if necessary
kellybs1 10:02615da7a9fe 87 if (dot->getJoinMode() != mDot::MANUAL) {
kellybs1 10:02615da7a9fe 88 logInfo("changing network join mode to MANUAL");
kellybs1 10:02615da7a9fe 89 if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) {
kellybs1 10:02615da7a9fe 90 logError("failed to set network join mode to MANUAL");
kellybs1 10:02615da7a9fe 91 }
kellybs1 10:02615da7a9fe 92 }
kellybs1 10:02615da7a9fe 93 // in MANUAL join mode there is no join request/response transaction
kellybs1 10:02615da7a9fe 94 // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate
kellybs1 10:02615da7a9fe 95 // network address - 4 bytes (00000001 - FFFFFFFE)
kellybs1 10:02615da7a9fe 96 // network session key - 16 bytes
kellybs1 10:02615da7a9fe 97 // data session key - 16 bytes
kellybs1 10:02615da7a9fe 98 // to provision your Dot with a Conduit gateway, follow the following steps
kellybs1 10:02615da7a9fe 99 // * ssh into the Conduit
kellybs1 10:02615da7a9fe 100 // * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/
kellybs1 10:02615da7a9fe 101 // lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304
kellybs1 10:02615da7a9fe 102 // * if you change the network address, network session key, or data session key, make sure you update them on the gateway
kellybs1 10:02615da7a9fe 103 // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation
kellybs1 10:02615da7a9fe 104 update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, public_network, ack);
kellybs1 10:02615da7a9fe 105
kellybs1 10:02615da7a9fe 106
kellybs1 10:02615da7a9fe 107 // enable or disable Adaptive Data Rate
kellybs1 10:02615da7a9fe 108 dot->setAdr(adr);
kellybs1 10:02615da7a9fe 109
kellybs1 10:02615da7a9fe 110 //* AU915 Datarates
kellybs1 10:02615da7a9fe 111 //* ---------------
kellybs1 10:02615da7a9fe 112 //* DR0 - SF10BW125 -- 11 bytes
kellybs1 10:02615da7a9fe 113 //* DR1 - SF9BW125 -- 53 bytes
kellybs1 10:02615da7a9fe 114 //* DR2 - SF8BW125 -- 129 byte
kellybs1 10:02615da7a9fe 115 //* DR3 - SF7BW125 -- 242 bytes
kellybs1 10:02615da7a9fe 116 //* DR4 - SF8BW500 -- 242 bytes
kellybs1 10:02615da7a9fe 117 dot->setTxDataRate(mDot::DR2);
kellybs1 10:02615da7a9fe 118
kellybs1 10:02615da7a9fe 119 // save changes to configuration
kellybs1 10:02615da7a9fe 120 logInfo("saving configuration");
kellybs1 10:02615da7a9fe 121 if (!dot->saveConfig()) {
kellybs1 10:02615da7a9fe 122 logError("failed to save configuration");
kellybs1 10:02615da7a9fe 123 }
kellybs1 10:02615da7a9fe 124
kellybs1 10:02615da7a9fe 125 // display configuration
kellybs1 10:02615da7a9fe 126 display_config();
kellybs1 10:02615da7a9fe 127 } else {
kellybs1 10:02615da7a9fe 128 // restore the saved session if the dot woke from deepsleep mode
kellybs1 10:02615da7a9fe 129 // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
kellybs1 10:02615da7a9fe 130 logInfo("restoring network session from NVM");
kellybs1 10:02615da7a9fe 131 dot->restoreNetworkSession();
jreiss 5:6b988a804fcb 132 }
jreiss 5:6b988a804fcb 133
kellybs1 11:45465d7cff1f 134 //this is where the magic happens
mfiore 0:09250cd371d2 135 while (true) {
kellybs1 10:02615da7a9fe 136
kellybs1 11:45465d7cff1f 137 //wake up
kellybs1 11:45465d7cff1f 138 wait(5);
kellybs1 10:02615da7a9fe 139 //init data variable
kellybs1 10:02615da7a9fe 140 std::vector<uint8_t> data;
lootspk 12:8e9badbc7314 141 logInfo("Staring Light Readings:");
kellybs1 10:02615da7a9fe 142 //read LDR
kellybs1 11:45465d7cff1f 143 //read LDR as float (0.0-1.0, multiply by 1000)
kellybs1 11:45465d7cff1f 144 //read multiple times - this just smoothes the value out a little bit
kellybs1 10:02615da7a9fe 145 float ldr1 = a0.read() * 1000;
lootspk 12:8e9badbc7314 146 //printf("%d\n", ldr1);
kellybs1 10:02615da7a9fe 147 wait_ms(100);
kellybs1 10:02615da7a9fe 148 float ldr2 = a0.read() * 1000;
lootspk 12:8e9badbc7314 149 //printf("%d\n", ldr2);
kellybs1 10:02615da7a9fe 150 wait_ms(100);
kellybs1 10:02615da7a9fe 151 float ldr3 = a0.read() * 1000;
lootspk 12:8e9badbc7314 152 //printf("%d\n", ldr3);
kellybs1 10:02615da7a9fe 153 wait_ms(100);
kellybs1 11:45465d7cff1f 154 //average the multiple readings
lootspk 12:8e9badbc7314 155
kellybs1 10:02615da7a9fe 156 float ldr = (ldr1 + ldr2 + ldr3) / 3;
lootspk 12:8e9badbc7314 157
lootspk 12:8e9badbc7314 158 logInfo("All light readings taken");
lootspk 12:8e9badbc7314 159 logInfo("Starting Moisture readings:");
lootspk 12:8e9badbc7314 160
lootspk 12:8e9badbc7314 161 //read sensor
lootspk 12:8e9badbc7314 162 //read multiple times - this just smoothes the value out a little bit
lootspk 12:8e9badbc7314 163 float moist1 = a2.read() * 1023;
lootspk 12:8e9badbc7314 164 wait_ms(100);
lootspk 12:8e9badbc7314 165 float moist2 = a2.read() * 1023;
lootspk 12:8e9badbc7314 166 wait_ms(100);
lootspk 12:8e9badbc7314 167 float moist3 = a2.read() * 1023;
lootspk 12:8e9badbc7314 168 wait_ms(100);
lootspk 12:8e9badbc7314 169 //average the multiple readings
lootspk 12:8e9badbc7314 170 float moistF = (moist1 + moist2 + moist3) / 3;
lootspk 12:8e9badbc7314 171
lootspk 12:8e9badbc7314 172 logInfo("All Moisture readings taken");
lootspk 12:8e9badbc7314 173 logInfo("Starting Temp and Humidity readings");
kellybs1 10:02615da7a9fe 174
kellybs1 10:02615da7a9fe 175 //read DHT22
kellybs1 10:02615da7a9fe 176 //get dht22 sample
kellybs1 10:02615da7a9fe 177 int error = dht22.sample();
kellybs1 11:45465d7cff1f 178 //sampling is a little slow - give it time
kellybs1 10:02615da7a9fe 179 wait_ms(100);
kellybs1 10:02615da7a9fe 180 //it's required to divide these values by ten for some reason
kellybs1 10:02615da7a9fe 181 float h = (float)dht22.getHumidity() / 10;
kellybs1 10:02615da7a9fe 182 float t = (float)dht22.getTemperature() / 10;
kellybs1 10:02615da7a9fe 183
kellybs1 11:45465d7cff1f 184 //build our output string now
kellybs1 10:02615da7a9fe 185 string l_str = ToString(ldr);
kellybs1 10:02615da7a9fe 186 string h_str = ToString(h);
kellybs1 10:02615da7a9fe 187 string t_str = ToString(t);
lootspk 12:8e9badbc7314 188 string m_str = ToString(moistF);
lootspk 12:8e9badbc7314 189 string output = "L:" + l_str + " H:" + h_str + " T:" + t_str + "M:" + m_str;
lootspk 12:8e9badbc7314 190
kellybs1 10:02615da7a9fe 191
kellybs1 11:45465d7cff1f 192 //serial output for debugging
kellybs1 10:02615da7a9fe 193 logInfo("Sending %s", output.c_str());
kellybs1 10:02615da7a9fe 194
kellybs1 10:02615da7a9fe 195 // format data for sending to the gateway
kellybs1 10:02615da7a9fe 196 for (std::string::iterator it = output.begin(); it != output.end(); it++)
kellybs1 10:02615da7a9fe 197 data.push_back((uint8_t) *it);
kellybs1 10:02615da7a9fe 198
kellybs1 11:45465d7cff1f 199 //now send
kellybs1 10:02615da7a9fe 200 send_data(data);
lootspk 12:8e9badbc7314 201 logInfo("Data sent!");
mfiore 0:09250cd371d2 202
kellybs1 11:45465d7cff1f 203 // go to sleep and wake up automatically sleep_time seconds later
lootspk 12:8e9badbc7314 204 uint32_t sleep_time = 20;
kellybs1 11:45465d7cff1f 205 //false is "don't deep sleep" - mDot doesn't do that
kellybs1 11:45465d7cff1f 206 dot->sleep(sleep_time, mDot::RTC_ALARM, false);
mfiore 0:09250cd371d2 207 }
kellybs1 10:02615da7a9fe 208
kellybs1 11:45465d7cff1f 209 return 0; //shouldn't happen
kellybs1 10:02615da7a9fe 210 }
mfiore 0:09250cd371d2 211