Full functions

Dependencies:   DHT22 libmDot-mbed5

Fork of mDot_LoRa_Connect_ABPA by Brendan Kelly

Revision:
11:45465d7cff1f
Parent:
10:02615da7a9fe
--- a/main.cpp	Mon Aug 21 05:01:01 2017 +0000
+++ b/main.cpp	Mon Aug 28 00:42:34 2017 +0000
@@ -1,3 +1,13 @@
+/*
+This program:
+ - connects to a LoRaWAN by ABP/MANUAL
+ - reads light data from an analogue Light Dependent Resistor
+ - reads temperaure and humidity from a DHT22 sensor
+ - sends the recorded data onto the LoRaWAN
+ - sets the mDot to sleep
+ - repeats these operations in a loop
+*/
+
 #include "mbed.h"
 #include "mDot.h"
 #include "ChannelPlans.h"
@@ -10,32 +20,37 @@
 #include <algorithm>
 #include <sstream>
  
- //dht 22 ping (D6 on UDK 2)
+ //dht 22 pin (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
-
 /*
 Current test settings
 dev address: 072389f7
 net sess key: b35aca73d283996dc3cbc0803af04547
 app sess key: d6f28430da4035273b9e3c07eb30c0dd
 */
+//device address
 static uint8_t network_address[] = { 0x07, 0x23, 0x89, 0xf7 };
+//network session key
 static uint8_t network_session_key[] = { 0xb3, 0x5a, 0xca, 0x73, 0xd2, 0x83, 0x99, 0x6d, 0xc3, 0xcb, 0xc0, 0x80, 0x3a, 0xf0, 0x45, 0x47 };
+//application sesssion or data session key
 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 uint8_t frequency_sub_band = 2; //VFI
 static bool public_network = true;
+//enable receipt of ackknowledge packets 0 = No, 1 = Yes
 static uint8_t ack = 0;
+//adaptive data rate enabler
 static bool adr = false;
 
+//USB serial 
 Serial pc(USBTX, USBRX);
 
+//get ourselves an mDot pointer - we will assign to it in main()
 mDot* dot = NULL;
 
-
 //converts value to string
 template <typename T>
 string ToString(T val) {
@@ -45,16 +60,15 @@
 }
 
 int main() {
+    //setting serial rate
     pc.baud(9600);
-    //std::string data_str = "hello world";
     
     // use AU915 plan
     lora::ChannelPlan* plan = new lora::ChannelPlan_AU915();
     assert(plan);
-    // get a mDot handle
+    // get a mDot handle with the plan we chose
     dot = mDot::getInstance(plan);
-    assert(dot);
-    
+    assert(dot); 
     
     if (!dot->getStandbyFlag()) {
         logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
@@ -115,48 +129,57 @@
         dot->restoreNetworkSession();
     }
     
-
+    //this is where the magic happens
     while (true) {
     
+        //wake up
+        wait(5);
         //init data variable
         std::vector<uint8_t> data;
         
         //read LDR
-         //read LDR as float (0.0-1.0, multiply by 1000)
+        //read LDR as float (0.0-1.0, multiply by 1000)
+        //read multiple times - this just smoothes the value out a little bit
         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
+        //average the multiple readings
         float ldr = (ldr1 + ldr2 + ldr3) / 3;
         
         //read DHT22
         //get dht22 sample
         int error = dht22.sample();
+        //sampling is a little slow - give it time
         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;
     
+        //build our output string now
         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; 
     
+        //serial output for debugging
         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);
     
+        //now send
         send_data(data);
 
-        //wait before resending
-        wait(15);
+        // go to sleep and wake up automatically sleep_time seconds later 
+        uint32_t sleep_time = 60;
+        //false is "don't deep sleep" - mDot doesn't do that 
+        dot->sleep(sleep_time, mDot::RTC_ALARM, false);
     }
  
-    return 0;
+    return 0; //shouldn't happen
 }