This program connects to the The Things Network backend in OTAA Mode. It logs sensor values from a BME 280 to the backend. Tried adding support for Grove GPS using SerialGPS library but it is not working - conflicting with mbed-rtos, so it commented. Deep Sleep for mDot implemented BUT avoiding reprogramming of the mDot config is NOT working.

Dependencies:   BME280 SerialGPS libmDot mbed-rtos mbed

Revision:
8:c17b68b03791
Parent:
7:9e2454b0318a
Child:
10:8071e1ae92ac
diff -r 9e2454b0318a -r c17b68b03791 main.cpp
--- a/main.cpp	Fri Jul 08 03:09:14 2016 +0000
+++ b/main.cpp	Fri Jul 08 03:26:39 2016 +0000
@@ -63,6 +63,7 @@
 //BME280 sensor(I2C_SDA, I2C_SCL)
 // MDot UDK - I2C_SDA and I2C_SCL connected to PC_9/PA_*
 BME280 b280(PC_9, PA_8);
+AnalogIn light(PB_0); // This corresponds to A1 Connector on the Grove Shield
 
 // Serial via USB for debugging only
 //Serial pc(USBTX,USBRX);
@@ -72,16 +73,19 @@
 void setUpLEDBlink();
 void blink();
 void readandprintBME280();
+float readLightSensor();
 void mDotConfig();
 void mDotGotoDeepSleep(int seconds);
 void mDotConfigPrint();
 void initSerialGPS();
 void setupNetwork();
 void joinNetwork();
+void sendData();
 
 // Globals
 Ticker tick;
 mDot* dot;
+float llevel;
 
 /*****************************************************
 *                MAIN
@@ -94,11 +98,146 @@
     setupNetwork();
     //wait(15);
     joinNetwork();
-    endLessTestLoop();
+    sendData();
+    // endLessTestLoop();
     
     return 0;
 }
 
+void sendData() {
+    std::vector<uint8_t> data;
+    std::string data_str = "hello!";
+    char string_buffer[64];
+    std::string separator_str = ",";
+    std::string temp_cls = "TC";
+    float temperature;
+    float pressure;
+    float humidity;
+    int32_t ret;
+ 
+    logInfo("Joined Network"); 
+
+    
+    while (true) {
+        data.clear();
+    
+        // Temperature
+        temperature = b280.getTemperature();
+        sprintf(string_buffer, "%s%3.2f", "TC:", temperature);
+        logInfo("The temperature is %s", string_buffer);
+        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()));
+        data.clear();    
+
+        // Pressure
+        pressure = b280.getPressure();
+        sprintf(string_buffer, "%s%04.2f", "hPa:", pressure);
+        logInfo("The pressure is %s", string_buffer);
+        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()));        
+        
+        data.clear();        
+        
+        // Humidity
+        humidity = b280.getHumidity();
+        sprintf(string_buffer, "%s%03.2f", "H%:", humidity);
+        logInfo("The humidty is %s", string_buffer);
+    
+        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()));
+
+        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()));
+        
+    }
+    
+}
+
+
 /*****************************************************
  *         mDot Functions
  ****************************************************/
@@ -267,7 +406,7 @@
         osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
     }
     logInfo("Joined Network"); 
-    wait(10);
+    wait(5);
     
 }
 
@@ -365,7 +504,16 @@
     //printf("%2.2f degC, %04.2f hPa, %2.2f %%\n", temperature, pressure, humidity);
 }
 
+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;
 
+}
 
 /*****************************************************
  *         FUNCTIONS for Simple Testing