Ashu Joshi / Mbed 2 deprecated mDot_LoRa_Connect_Example_GroveSensorTempAndLight

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_LoRa_Connect_Example by MultiTech

Files at this revision

API Documentation at this revision

Comitter:
AshuJoshi
Date:
Thu Jun 16 00:37:16 2016 +0000
Parent:
6:f0b6fea36e28
Commit message:
Modified the LoRa Connect Example published by MultiTech for reading Grove Temperature and Light Sensors and sending the sensor reading to the MultiTech mLinux LoRa Gateway

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Apr 12 23:44:25 2016 +0000
+++ b/main.cpp	Thu Jun 16 00:37:16 2016 +0000
@@ -5,17 +5,58 @@
 #include <vector>
 #include <algorithm>
 
+// Connect the Grove Shield to the UDK
+// Connect the Grove Temperature Sensor to A0 on Shield
+// http://www.seeedstudio.com/wiki/Grove_-_Temperature_Sensor
+// Connect the Grove Light Sensor to the A1
+// http://www.seeedstudio.com/wiki/Grove_-_Light_Sensor
+
+AnalogIn in(PB_1); // This corresponds to A0 Connector on the Grove Shield
+AnalogIn light(PB_0); // This corresponds to A1 Connector on the Grove Shield
+
+float temperature;
+float llevel;
+
 // these options must match the settings on your Conduit
 // uncomment the following lines and edit their values to match your configuration
-static std::string config_network_name = "<lora network id>";
-static std::string config_network_pass = "<lora network key>";
-static uint8_t config_frequency_sub_band = 1;
+static std::string config_network_name = "ATLANTA_TTN";
+static std::string config_network_pass = "ATLANTA_TTN";
+static uint8_t config_frequency_sub_band = 7;
+
+float readTemperature(){
+    float temp;
+    int B=39751;
+    float resistance;
+    int a;
+    // Read & Print Temperature
+    a = in.read_u16();
+    //printf("analog value: %i \r\n", a);
+    resistance = (float)(65534-a)*10000/a; //get the resistance of the sensor;
+    temp=1/(log(resistance/10000)/B+1/298.15)-273.15;//convert to temperature via datasheet
+    //logInfo("Temperature: %f, temp);
+    return temp;
+}
+
+float readLightSensor() {
+    float sensorValue;
+    float rsensor; 
+    sensorValue = light.read();
+    rsensor = (float)(1023-sensorValue)*10/sensorValue;
+    printf("Sensor reading: %2.2f - %2.2f\r\n", sensorValue, rsensor);
+  
+    return rsensor;
+
+}
+
 
 int main() {
     int32_t ret;
     mDot* dot;
     std::vector<uint8_t> data;
     std::string data_str = "hello!";
+    char string_buffer[64];
+    std::string separator_str = ",";
+    std::string temp_cls = "TC";
     
     // get a mDot handle
     dot = mDot::getInstance();
@@ -27,10 +68,11 @@
     // configuration
     //*******************************************
     // reset to default config so we know what state we're in
-    dot->resetConfig();
+    //dot->resetConfig();
     
-    dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
-
+    //dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
+    dot->setLogLevel(mts::MTSLog::TRACE_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()
     
@@ -38,10 +80,10 @@
     // 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 to public network");
-//    if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
-//        logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
-//    }
+    logInfo("setting to public network");
+    if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
+        logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    }
 
     logInfo("setting frequency sub band");    
     if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
@@ -90,10 +132,29 @@
     }
 
     // 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);
+    //for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
+    //    data.push_back((uint8_t) *it);
 
     while (true) {
+        data.clear();
+        temperature = readTemperature();
+        
+        // Temperature
+        sprintf(string_buffer, "%s%3.2f", "TC:", temperature);
+        //sprintf(string_buffer, "%3.1f", temperature);
+        for (int i = 0; i<strlen(string_buffer); i++)
+        {
+            data.push_back(((char*)string_buffer)[i]);
+        }
+            
+        logDebug("Sending LoRa message, length: %d", data.size());
+        logDebug("sending data: ");
+        for(int i = 0; i < data.size(); i++)
+        {
+            printf("%c", data[i]);
+        }
+        printf("\n");
+        
         // send the data to the gateway
         if ((ret = dot->send(data)) != mDot::MDOT_OK) {
             logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
@@ -103,6 +164,33 @@
 
         // 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()));
+        
+        data.clear();
+        // Light Level
+        llevel = readLightSensor();
+        sprintf(string_buffer, "%s%5.1f", "LL:", llevel);
+        for (int i = 0; i<strlen(string_buffer); i++)
+        {
+            data.push_back(((char*)string_buffer)[i]);
+        }
+        logDebug("Sending LoRa message, length: %d", data.size());
+        logDebug("sending data: ");
+        for(int i = 0; i < data.size(); i++)
+        {
+            printf("%c", data[i]);
+        }
+        printf("\n");
+        
+        // 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");
+        }
+
+        // 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()));
+        
     }
 
     return 0;