code base MDOT lorawan

Dependencies:   Servo Cayenne-LPP

Revision:
58:81c66fac6476
Parent:
57:5ec0379d69d6
Child:
59:7b58c5775a56
--- a/main.cpp	Mon Sep 30 18:06:49 2019 +0000
+++ b/main.cpp	Tue Oct 22 08:45:41 2019 +0000
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 #include <stdio.h>
+#include "th02.h"
 #include "CayenneLPP.h"
 #include "lorawan/LoRaWANInterface.h"
 #include "lorawan/system/lorawan_data_structures.h"
@@ -24,6 +25,7 @@
 #include "DummySensor.h"
 #include "trace_helper.h"
 #include "lora_radio_helper.h"
+#include "Servo.h"
 
 using namespace events;
 
@@ -48,8 +50,10 @@
 int size = 0;
 
 
+DigitalOut Alarme (PC_13);// alarme LED output
+Servo Myservo(PA_7); //servomotor output
 
-
+TH02 MyTH02 (I2C_SDA,I2C_SCL,TH02_I2C_ADDR<<1);// connect hsensor on RX2 TX2
 /*
  * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
  */
@@ -104,6 +108,13 @@
  */
 static lorawan_app_callbacks_t callbacks;
 
+void servo(uint8_t uAngle)
+{
+    
+    }
+
+
+
 /**
  * Entry point for application
  */
@@ -111,6 +122,13 @@
 {
     // setup tracing
     setup_trace();
+   //  th02 temerature sensor section 
+    int iTemp,iTime,iTempbrute,iRH,iRHbrute;
+    printf ("\n\r start reading TH02 for first time");
+     MyTH02.startTempConv(true,true);
+  
+
+
 
     // stores the status of a call to LoRaWAN protocol
     lorawan_status_t retcode;
@@ -166,13 +184,25 @@
  * Sends a message to the Network Server
  */
 static void send_message()
-
-{
+ {int iTime,iTempbrute,iRHbrute;   
+ uint16_t packet_len;
+    int16_t retcode;
+    int32_t sensor_value, rh_value;
 
-    uint16_t packet_len;
-    int16_t retcode;
-    int32_t sensor_value;
+  MyTH02.startTempConv(true,true);
+    iTime= MyTH02.waitEndConversion();// wait until onversion  is done
+     iTempbrute= MyTH02.getConversionValue();
+    sensor_value=MyTH02.getLastRawTemp();
+    printf ("\n\r temp value=%d  %d",sensor_value,iTempbrute );
+  
+  MyTH02.startRHConv(true,true);
+       iTime= MyTH02.waitEndConversion();// wait until onversion  is done
+         printf ("\n\r time=%d",iTime);
+      iRHbrute= MyTH02.getConversionValue();
+        rh_value=MyTH02.getLastRawRH();
+          printf ("\n\r RH value=%d  %d",rh_value,iRHbrute );
 
+/*
     if (ds1820.begin()) {
         ds1820.startConversion();
         sensor_value = ds1820.read();
@@ -181,21 +211,26 @@
     } else {
         printf("\r\n No sensor found \r\n");
         return;
-    }
-
+    }*/
+    
   /*  packet_len = sprintf((char *) tx_buffer, "Dummy Sensor Value is %d",
-                         sensor_value);*/
-                         
+                         sensor_value);*/                       
     Payload.reset();
-    size = Payload.addTemperature(1, (float) sensor_value*10.0);    
+    size = Payload.addTemperature(1, (float) sensor_value/100);    
+   // LORA_SEND(Payload.getBuffer(), Payload.getSize());       
+    // Payload.copy(tx_buffer);          
+    
+     // retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, Payload.getBuffer(), Payload.getSize(),
+                         //  MSG_UNCONFIRMED_FLAG);                
+  /*  retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
+                           MSG_UNCONFIRMED_FLAG);*/
+
+
+    size = size+Payload.addRelativeHumidity(2, (float) rh_value/100);    
    // LORA_SEND(Payload.getBuffer(), Payload.getSize());       
     // Payload.copy(tx_buffer);          
       retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, Payload.getBuffer(), Payload.getSize(),
                            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")
@@ -218,7 +253,7 @@
  * Receive a message from the Network Server
  */
 static void receive_message()
-{
+{int num_port;
     uint8_t port;
     int flags;
     int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags);
@@ -232,8 +267,32 @@
     for (uint8_t i = 0; i < retcode; i++) {
         printf("%02x ", rx_buffer[i]);
     }
-    printf("\r\n");
-    
+     printf("\n test value=%d", port); 
+   // printf("\r\n");
+  // num_port=port;
+ /*  if (true)
+   {
+         printf("\n led=%d", (int)rx_buffer[0]); 
+            //Alarme.write((int) rx_buffer[0]);
+            }
+   if (port==2)
+   {
+         printf("\n servo=%d",(int) rx_buffer[0]); 
+      // Myservo.position ( rx_buffer[0]);
+     }   */      
+    switch (port){
+        case 1: // control led
+         printf("\n led=%d", (int)rx_buffer[0]); 
+            Alarme.write((int) rx_buffer[0]);
+        break;
+        case 2:// control servomotor
+        printf("\n servo=%d",(int) rx_buffer[0]); 
+      Myservo.position ( rx_buffer[0]);
+       break;
+       default: printf("\n port inconnu =%d",(int)port); 
+       break;
+        }
+        
     memset(rx_buffer, 0, sizeof(rx_buffer));
 }