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
Diff: main.cpp
- Revision:
- 10:02615da7a9fe
- Parent:
- 9:7f7194b5b4e3
- Child:
- 11:45465d7cff1f
--- a/main.cpp Mon Aug 07 07:33:20 2017 +0000 +++ b/main.cpp Mon Aug 21 05:01:01 2017 +0000 @@ -2,134 +2,161 @@ #include "mDot.h" #include "ChannelPlans.h" #include "MTSLog.h" +#include "dot_util.h" +#include "mbed.h" +#include "DHT22.h" #include <string> #include <vector> #include <algorithm> +#include <sstream> + + //dht 22 ping (D6 on UDK 2) + DHT22 dht22(PA_1); + //analogue ldr pin (A0 on UDK 2) + AnalogIn a0(PB_1); // these options must match the settings on your Conduit -// uncomment the following lines and edit their values to match your configuration -static uint8_t config_network_addr[] = { 0x01, 0x02, 0x03, 0x04 }; -static uint8_t config_network_nskey[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; -static uint8_t config_network_dskey[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; -static uint8_t config_frequency_sub_band = 1; + +/* +Current test settings +dev address: 072389f7 +net sess key: b35aca73d283996dc3cbc0803af04547 +app sess key: d6f28430da4035273b9e3c07eb30c0dd +*/ +static uint8_t network_address[] = { 0x07, 0x23, 0x89, 0xf7 }; +static uint8_t network_session_key[] = { 0xb3, 0x5a, 0xca, 0x73, 0xd2, 0x83, 0x99, 0x6d, 0xc3, 0xcb, 0xc0, 0x80, 0x3a, 0xf0, 0x45, 0x47 }; +static uint8_t data_session_key[] = { 0xd6, 0xf2, 0x84, 0x30, 0xda, 0x40, 0x35, 0x27, 0x3b, 0x9e, 0x3c, 0x07, 0xeb, 0x30, 0xc0, 0xdd }; +static uint8_t frequency_sub_band = 2; +static bool public_network = true; +static uint8_t ack = 0; +static bool adr = false; Serial pc(USBTX, USBRX); +mDot* dot = NULL; +//converts value to string +template <typename T> +string ToString(T val) { + stringstream stream; + stream << val; + return stream.str(); +} + int main() { pc.baud(9600); - int32_t ret; - std::vector<uint8_t> data; - std::string data_str = "hello!"; + //std::string data_str = "hello world"; // use AU915 plan lora::ChannelPlan* plan = new lora::ChannelPlan_AU915(); assert(plan); // get a mDot handle - mDot* dot = mDot::getInstance(plan); + dot = mDot::getInstance(plan); assert(dot); - - // print library version information - logInfo("version: %s", dot->getId().c_str()); - - //******************************************* - // configuration - //******************************************* - // reset to default config so we know what state we're in - dot->resetConfig(); - - dot->setLogLevel(mts::MTSLog::INFO_LEVEL); - - // set up the mDot with our network information: frequency sub band, network name, and network password - // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig() - // frequency sub band is only applicable in the 915 (US) frequency band - // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band - // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels - logInfo("setting frequency sub band"); - if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { - logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); - } - - std::vector<uint8_t> temp; - - for (int i = 0; i < 4; i++) { - temp.push_back(config_network_addr[i]); - } - - logInfo("setting network addr"); - if ((ret = dot->setNetworkAddress(temp)) != mDot::MDOT_OK) { - logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); - } - - temp.clear(); - for (int i = 0; i < 16; i++) { - temp.push_back(config_network_nskey[i]); - } - - logInfo("setting network password"); - if ((ret = dot->setNetworkSessionKey(temp)) != mDot::MDOT_OK) { - logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); - } - - temp.clear(); - for (int i = 0; i < 16; i++) { - temp.push_back(config_network_dskey[i]); + if (!dot->getStandbyFlag()) { + logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); + + // start from a well-known state + logInfo("defaulting Dot configuration"); + dot->resetConfig(); + dot->resetNetworkSession(); + + // make sure library logging is turned on + dot->setLogLevel(mts::MTSLog::DEBUG_LEVEL); + + // update configuration if necessary + if (dot->getJoinMode() != mDot::MANUAL) { + logInfo("changing network join mode to MANUAL"); + if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) { + logError("failed to set network join mode to MANUAL"); + } + } + // in MANUAL join mode there is no join request/response transaction + // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate + // network address - 4 bytes (00000001 - FFFFFFFE) + // network session key - 16 bytes + // data session key - 16 bytes + // to provision your Dot with a Conduit gateway, follow the following steps + // * ssh into the Conduit + // * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/ + // lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304 + // * if you change the network address, network session key, or data session key, make sure you update them on the gateway + // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation + update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, public_network, ack); + + + // enable or disable Adaptive Data Rate + dot->setAdr(adr); + + //* AU915 Datarates + //* --------------- + //* DR0 - SF10BW125 -- 11 bytes + //* DR1 - SF9BW125 -- 53 bytes + //* DR2 - SF8BW125 -- 129 byte + //* DR3 - SF7BW125 -- 242 bytes + //* DR4 - SF8BW500 -- 242 bytes + dot->setTxDataRate(mDot::DR2); + + // save changes to configuration + logInfo("saving configuration"); + if (!dot->saveConfig()) { + logError("failed to save configuration"); + } + + // display configuration + display_config(); + } else { + // restore the saved session if the dot woke from deepsleep mode + // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep + logInfo("restoring network session from NVM"); + dot->restoreNetworkSession(); } - logInfo("setting network password"); - if ((ret = dot->setDataSessionKey(temp)) != mDot::MDOT_OK) { - logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); - } - - // a higher spreading factor allows for longer range but lower throughput - // in the 915 (US) frequency band, spreading factors 7 - 10 are available - // in the 868 (EU) frequency band, spreading factors 7 - 12 are available - logInfo("setting TX spreading factor"); - if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) { - logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); - } - - // request receive confirmation of packets from the gateway - logInfo("enabling ACKs"); - if ((ret = dot->setAck(1)) != mDot::MDOT_OK) { - logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); - } - - // save this configuration to the mDot's NVM - logInfo("saving config"); - if (! dot->saveConfig()) { - logError("failed to save configuration"); - } - //******************************************* - // end of configuration - //******************************************* - - // attempt to join the network - logInfo("joining network"); - while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { - logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); - // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again - osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs())); - } - - // format data for sending to the gateway - for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++) - data.push_back((uint8_t) *it); while (true) { - // send the data to the gateway - if ((ret = dot->send(data)) != mDot::MDOT_OK) { - logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str()); - } else { - logInfo("successfully sent data to gateway"); - } + + //init data variable + std::vector<uint8_t> data; + + //read LDR + //read LDR as float (0.0-1.0, multiply by 1000) + float ldr1 = a0.read() * 1000; + wait_ms(100); + float ldr2 = a0.read() * 1000; + wait_ms(100); + float ldr3 = a0.read() * 1000; + wait_ms(100); + //average the multiple readings - smoother output + float ldr = (ldr1 + ldr2 + ldr3) / 3; + + //read DHT22 + //get dht22 sample + int error = dht22.sample(); + wait_ms(100); + //it's required to divide these values by ten for some reason + float h = (float)dht22.getHumidity() / 10; + float t = (float)dht22.getTemperature() / 10; + + string l_str = ToString(ldr); + string h_str = ToString(h); + string t_str = ToString(t); + string output = "L:" + l_str + " H:" + h_str + " T:" + t_str; + + logInfo("Sending %s", output.c_str()); + + // format data for sending to the gateway + for (std::string::iterator it = output.begin(); it != output.end(); it++) + data.push_back((uint8_t) *it); + + send_data(data); - // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again - osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs())); + //wait before resending + wait(15); } + + return 0; +} - return 0; //unreachable -}