Full functions

Dependencies:   DHT22 libmDot-mbed5

Fork of mDot_LoRa_Connect_ABPA by Brendan Kelly

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 This program:
00003  - connects to a LoRaWAN by ABP/MANUAL
00004  - reads light data from an analogue Light Dependent Resistor
00005  - reads temperaure and humidity from a DHT22 sensor
00006  - sends the recorded data onto the LoRaWAN
00007  - sets the mDot to sleep
00008  - repeats these operations in a loop
00009 */
00010 
00011 #include "mbed.h"
00012 #include "mDot.h"
00013 #include "ChannelPlans.h"
00014 #include "MTSLog.h"
00015 #include "dot_util.h"
00016 #include "mbed.h"
00017 #include "DHT22.h"
00018 #include <string>
00019 #include <vector>
00020 #include <algorithm>
00021 #include <sstream>
00022  
00023  //dht 22 pin (D6 on UDK 2)
00024  DHT22 dht22(PA_1);
00025  //analogue ldr pin (A0 on UDK 2)
00026  AnalogIn a0(PB_1);
00027 
00028 // these options must match the settings on your Conduit
00029 /*
00030 Current test settings
00031 dev address: 072389f7
00032 net sess key: b35aca73d283996dc3cbc0803af04547
00033 app sess key: d6f28430da4035273b9e3c07eb30c0dd
00034 */
00035 //device address
00036 static uint8_t network_address[] = { 0x07, 0x23, 0x89, 0xf7 };
00037 //network session key
00038 static uint8_t network_session_key[] = { 0xb3, 0x5a, 0xca, 0x73, 0xd2, 0x83, 0x99, 0x6d, 0xc3, 0xcb, 0xc0, 0x80, 0x3a, 0xf0, 0x45, 0x47 };
00039 //application sesssion or data session key
00040 static uint8_t data_session_key[] = { 0xd6, 0xf2, 0x84, 0x30, 0xda, 0x40, 0x35, 0x27, 0x3b, 0x9e, 0x3c, 0x07, 0xeb, 0x30, 0xc0, 0xdd };
00041 static uint8_t frequency_sub_band = 2; //VFI
00042 static bool public_network = true;
00043 //enable receipt of ackknowledge packets 0 = No, 1 = Yes
00044 static uint8_t ack = 0;
00045 //adaptive data rate enabler
00046 static bool adr = false;
00047 
00048 //USB serial 
00049 Serial pc(USBTX, USBRX);
00050 
00051 //get ourselves an mDot pointer - we will assign to it in main()
00052 mDot* dot = NULL;
00053 
00054 //converts value to string
00055 template <typename T>
00056 string ToString(T val) {
00057     stringstream stream;
00058     stream << val;
00059     return stream.str();
00060 }
00061 
00062 int main() {
00063     //setting serial rate
00064     pc.baud(9600);
00065     
00066     // use AU915 plan
00067     lora::ChannelPlan* plan = new lora::ChannelPlan_AU915();
00068     assert(plan);
00069     // get a mDot handle with the plan we chose
00070     dot = mDot::getInstance(plan);
00071     assert(dot); 
00072     
00073     if (!dot->getStandbyFlag()) {
00074         logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
00075 
00076         // start from a well-known state
00077         logInfo("defaulting Dot configuration");
00078         dot->resetConfig();
00079         dot->resetNetworkSession();
00080 
00081         // make sure library logging is turned on
00082         dot->setLogLevel(mts::MTSLog::DEBUG_LEVEL);
00083 
00084         // update configuration if necessary
00085         if (dot->getJoinMode() != mDot::MANUAL) {
00086             logInfo("changing network join mode to MANUAL");
00087             if (dot->setJoinMode(mDot::MANUAL) != mDot::MDOT_OK) {
00088                 logError("failed to set network join mode to MANUAL");
00089             }
00090         }
00091         // in MANUAL join mode there is no join request/response transaction
00092         // as long as the Dot is configured correctly and provisioned correctly on the gateway, it should be able to communicate
00093         // network address - 4 bytes (00000001 - FFFFFFFE)
00094         // network session key - 16 bytes
00095         // data session key - 16 bytes
00096         // to provision your Dot with a Conduit gateway, follow the following steps
00097         //   * ssh into the Conduit
00098         //   * provision the Dot using the lora-query application: http://www.multitech.net/developer/software/lora/lora-network-server/
00099         //      lora-query -a 01020304 A 0102030401020304 <your Dot's device ID> 01020304010203040102030401020304 01020304010203040102030401020304
00100         //   * if you change the network address, network session key, or data session key, make sure you update them on the gateway
00101         // to provision your Dot with a 3rd party gateway, see the gateway or network provider documentation
00102         update_manual_config(network_address, network_session_key, data_session_key, frequency_sub_band, public_network, ack);
00103 
00104 
00105         // enable or disable Adaptive Data Rate
00106         dot->setAdr(adr);
00107         
00108         //* AU915 Datarates
00109         //* ---------------
00110         //* DR0 - SF10BW125 -- 11 bytes
00111         //* DR1 - SF9BW125 -- 53 bytes
00112         //* DR2 - SF8BW125 -- 129 byte
00113         //* DR3 - SF7BW125 -- 242 bytes
00114         //* DR4 - SF8BW500 -- 242 bytes
00115         dot->setTxDataRate(mDot::DR2);
00116         
00117         // save changes to configuration
00118         logInfo("saving configuration");
00119         if (!dot->saveConfig()) {
00120             logError("failed to save configuration");
00121         }
00122 
00123         // display configuration
00124         display_config();
00125     } else {
00126         // restore the saved session if the dot woke from deepsleep mode
00127         // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
00128         logInfo("restoring network session from NVM");
00129         dot->restoreNetworkSession();
00130     }
00131     
00132     //this is where the magic happens
00133     while (true) {
00134     
00135         //wake up
00136         wait(5);
00137         //init data variable
00138         std::vector<uint8_t> data;
00139         
00140         //read LDR
00141         //read LDR as float (0.0-1.0, multiply by 1000)
00142         //read multiple times - this just smoothes the value out a little bit
00143         float ldr1 = a0.read() * 1000;
00144         wait_ms(100);
00145         float ldr2 = a0.read() * 1000;
00146         wait_ms(100);
00147         float ldr3 = a0.read() * 1000;
00148         wait_ms(100);
00149         //average the multiple readings
00150         float ldr = (ldr1 + ldr2 + ldr3) / 3;
00151         
00152         //read DHT22
00153         //get dht22 sample
00154         int error = dht22.sample();
00155         //sampling is a little slow - give it time
00156         wait_ms(100);
00157         //it's required to divide these values by ten for some reason
00158         float h = (float)dht22.getHumidity() / 10;
00159         float t = (float)dht22.getTemperature() / 10;
00160     
00161         //build our output string now
00162         string l_str = ToString(ldr);
00163         string h_str = ToString(h);
00164         string t_str = ToString(t);
00165         string output = "L:" + l_str + " H:" + h_str + " T:" + t_str; 
00166     
00167         //serial output for debugging
00168         logInfo("Sending %s", output.c_str());
00169         
00170         // format data for sending to the gateway
00171         for (std::string::iterator it = output.begin(); it != output.end(); it++)
00172             data.push_back((uint8_t) *it);
00173     
00174         //now send
00175         send_data(data);
00176 
00177         // go to sleep and wake up automatically sleep_time seconds later 
00178         uint32_t sleep_time = 60;
00179         //false is "don't deep sleep" - mDot doesn't do that 
00180         dot->sleep(sleep_time, mDot::RTC_ALARM, false);
00181     }
00182  
00183     return 0; //shouldn't happen
00184 }
00185