B-L072 ST Board BMP280 LoRaWAN end node

Dependencies:   BME280

Temperature/Pressure LoRaWAN End Node

Revision:
61:d220d51ae9d8
Parent:
60:ff7a31e79593
Child:
62:078d66d985f8
diff -r ff7a31e79593 -r d220d51ae9d8 main.cpp
--- a/main.cpp	Thu Jul 23 11:56:52 2020 +0000
+++ b/main.cpp	Sat Jul 25 06:40:08 2020 +0000
@@ -1,28 +1,11 @@
-/**
- * Copyright (c) 2017, Arm Limited and affiliates.
- * SPDX-License-Identifier: Apache-2.0
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
 #include <stdio.h>
+#include "BME280.h"
 
 #include "lorawan/LoRaWANInterface.h"
 #include "lorawan/system/lorawan_data_structures.h"
 #include "events/EventQueue.h"
 
-// Application helpers
-#include "DummySensor.h"
-#include "trace_helper.h"
+//#include "trace_helper.h"
 #include "lora_radio_helper.h"
 
 using namespace events;
@@ -30,7 +13,8 @@
 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
 // This example only communicates with much shorter messages (<30 bytes).
 // If longer messages are used, these buffers must be changed accordingly.
-uint8_t tx_buffer[30];
+//uint8_t tx_buffer[30];
+uint8_t tx_buffer[12];
 uint8_t rx_buffer[30];
 
 /*
@@ -50,15 +34,10 @@
  */
 #define CONFIRMED_MSG_RETRY_COUNTER     3
 
-/**
- * Dummy pin for dummy sensor
- */
-#define PC_9                            0
+#define DEBUG   1
+#define BUFFER_SIZE     12
 
-/**
- * Dummy sensor class object
- */
-DS1820  ds1820(PC_9);
+BME280 sensor(PB_9, PB_8, 0x77);
 
 /**
 * This event queue is the global event queue for both the
@@ -94,18 +73,20 @@
 int main(void)
 {
     // setup tracing
-    setup_trace();
+    //setup_trace();
 
     // stores the status of a call to LoRaWAN protocol
     lorawan_status_t retcode;
 
     // Initialize LoRaWAN stack
     if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
-        printf("\r\n LoRa initialization failed! \r\n");
+        if(DEBUG)
+            printf("\r\n LoRa initialization failed! \r\n");
         return -1;
     }
 
-    printf("\r\n Mbed LoRaWANStack initialized \r\n");
+    if(DEBUG)
+        printf("\r\n Mbed LoRaWANStack initialized \r\n");
 
     // prepare application callbacks
     callbacks.events = mbed::callback(lora_event_handler);
@@ -114,41 +95,39 @@
     // Set number of retries in case of CONFIRMED messages
     if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
             != LORAWAN_STATUS_OK) {
-        printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
+        if(DEBUG)
+            printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
         return -1;
     }
 
-    printf("\r\n CONFIRMED message retries : %d \r\n",
-           CONFIRMED_MSG_RETRY_COUNTER);
+    if(DEBUG)
+        printf("\r\n CONFIRMED message retries : %d \r\n", CONFIRMED_MSG_RETRY_COUNTER);
 
     // Enable adaptive data rate
     if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
-        printf("\r\n enable_adaptive_datarate failed! \r\n");
+        if(DEBUG)
+            printf("\r\n enable_adaptive_datarate failed! \r\n");
         return -1;
     }
 
-    printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
+    if(DEBUG)
+        printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
 
     retcode = lorawan.connect();
 
-    if (retcode == LORAWAN_STATUS_OK ||
-            retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
+    if (retcode == LORAWAN_STATUS_OK || retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
     } else {
-        printf("\r\n Connection error, code = %d \r\n", retcode);
+        if(DEBUG)
+            printf("\r\n Connection error, code = %d \r\n", retcode);
         return -1;
     }
 
-    printf("\r\n Connection - In Progress ...\r\n");
+    if(DEBUG)
+        printf("\r\n Connection - In Progress ...\r\n");
 
     // make your event queue dispatching events forever
     ev_queue.dispatch_forever();
     
- //   for(int i=0;i<5;i++)
-//    {
-//        send_message();
-//        //wait(2);
-//    }
-
     return 0;
 }
 
@@ -157,29 +136,28 @@
  */
 static void send_message()
 {
-    uint16_t packet_len;
+    uint16_t packet_len = BUFFER_SIZE;
     int16_t retcode;
-    int32_t sensor_value;
-    printf("send message\n");
-//
-//    if (ds1820.begin()) {
-//        ds1820.startConversion();
-//        sensor_value = ds1820.read();
-//        printf("\r\n Dummy Sensor Value = %d \r\n", sensor_value);
-//        ds1820.startConversion();
-//    } else {
-//        printf("\r\n No sensor found \r\n");
-//        return;
-//    }
+    float sensor_values[3];
+    
+    sensor.trigger();
+    sensor_values[0] = sensor.getTemperature();
+    sensor_values[1] = sensor.getPressure();
+    sensor_values[2] = sensor.getHumidity();
 
-    packet_len = sprintf((char *) tx_buffer, "Dummy Sensor Value is");
+    memcpy(tx_buffer, sensor_values, packet_len);
+    
+    //packet_len = sprintf((char *) tx_buffer, "Dummy Sensor Value is");
 
-    retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
-                           MSG_UNCONFIRMED_FLAG);
+    //retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG);
+
+    retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG);
 
     if (retcode < 0) {
-        retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
-        : printf("\r\n send() - Error code %d \r\n", retcode);
+        
+        if(DEBUG)
+            retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
+                : printf("\r\n send() - Error code %d \r\n", retcode);
 
         if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
             //retry in 3 seconds
@@ -190,8 +168,9 @@
         return;
     }
 
-    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
-    memset(tx_buffer, 0, sizeof(tx_buffer));
+    if(DEBUG)
+        printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
+    //memset(tx_buffer, 0, sizeof(tx_buffer));
 }
 
 /**
@@ -224,7 +203,8 @@
 {
     switch (event) {
         case CONNECTED:
-            printf("\r\n Connection - Successful \r\n");
+            if(DEBUG)
+                printf("\r\n Connection - Successful \r\n");
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             } else {
@@ -234,10 +214,12 @@
             break;
         case DISCONNECTED:
             ev_queue.break_dispatch();
-            printf("\r\n Disconnected Successfully \r\n");
+            if(DEBUG)
+                printf("\r\n Disconnected Successfully \r\n");
             break;
         case TX_DONE:
-            printf("\r\n Message Sent to Network Server \r\n");
+            if(DEBUG)
+                printf("\r\n Message Sent to Network Server \r\n");
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             }
@@ -246,25 +228,30 @@
         case TX_ERROR:
         case TX_CRYPTO_ERROR:
         case TX_SCHEDULING_ERROR:
-            printf("\r\n Transmission Error - EventCode = %d \r\n", event);
+            if(DEBUG)
+                printf("\r\n Transmission Error - EventCode = %d \r\n", event);
             // try again
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             }
             break;
         case RX_DONE:
-            printf("\r\n Received message from Network Server \r\n");
+            if(DEBUG)
+                printf("\r\n Received message from Network Server \r\n");
             receive_message();
             break;
         case RX_TIMEOUT:
         case RX_ERROR:
-            printf("\r\n Error in reception - Code = %d \r\n", event);
+            if(DEBUG)
+                printf("\r\n Error in reception - Code = %d \r\n", event);
             break;
         case JOIN_FAILURE:
-            printf("\r\n OTAA Failed - Check Keys \r\n");
+            if(DEBUG)
+                printf("\r\n OTAA Failed - Check Keys \r\n");
             break;
         case UPLINK_REQUIRED:
-            printf("\r\n Uplink required by NS \r\n");
+            if(DEBUG)
+                printf("\r\n Uplink required by NS \r\n");
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             }