tfsd

Dependencies:   Servo GroveGPS Cayenne-LPP driver_mbed_TH02

Revision:
64:4cd8fed3f437
Parent:
63:cd8ab5860303
Child:
65:724a61ad654c
--- a/main.cpp	Tue Oct 20 13:21:32 2020 +0000
+++ b/main.cpp	Tue Mar 15 08:54:01 2022 +0000
@@ -15,8 +15,13 @@
  * limitations under the License.
  */
 #include <stdio.h>
-#include "th02.h"
+#include <stdlib.h>  // <cstdlib> en C++
+#include "driver_mbed_TH02.h"
+#include "GroveGPS.h"
 #include "CayenneLPP.h"
+
+#include "MFRC522.h"
+
 #include "lorawan/LoRaWANInterface.h"
 #include "lorawan/system/lorawan_data_structures.h"
 #include "events/EventQueue.h"
@@ -52,7 +57,15 @@
 
 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
+GroveGPS MyGPS(PA_9, PA_10);
+
+#define MF_RESET    D9  //Reset D7
+#define SPI_MOSI    D11   //MOSI
+#define SPI_MISO    D12   //MISO
+#define SPI_SCK     D13  //Clock
+#define SPI_CS      D7  //Chip Select D9
+MFRC522 RfChip(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
+//driver_mbed_TH02 MyTH02 (PB_9,PB_8);// connect hsensor on RX2 TX2
 /*
  * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
  */
@@ -109,10 +122,20 @@
 
 void servo(uint8_t uAngle)
 {
-    
-    }
+
+}
 
 
+double tempo = 0.3; // LED blinking delay
+
+// Change LEDs blinking frequency
+void change_blinking_frequency()
+{
+    if (tempo == 0.3) // If leds have low frequency
+        tempo = 0.1;  // Set the fast frequency
+    else              // If leds have fast frequency
+        tempo = 0.3;  // Set the low frequency
+}
 
 /**
  * Entry point for application
@@ -121,14 +144,14 @@
 {
     // setup tracing
     setup_trace();
-   //  th02 temerature sensor section 
+    //  th02 temerature sensor section
     int iTemp,iTime,iTempbrute,iRH,iRHbrute;
-  //  Myservo.calibrate(0.0005, 45); 
+    //  Myservo.calibrate(0.0005, 45);
     printf ("\n\r start reading TH02 for first time");
-     MyTH02.startTempConv(true,true);
-  
-
-
+    //myTH02.startTempConv(true,true);
+    printf("\r\n*********************Initialization**********************************");
+    RfChip.PCD_Init();           /* Init. RC522 Chip*/
+    printf("\r\n**********************Starting Reading Tags**************************");   
 
     // stores the status of a call to LoRaWAN protocol
     lorawan_status_t retcode;
@@ -183,44 +206,72 @@
 /*************************************************************************************************************
  * Sends a message to the Network Server
  *************************************************************************************************************/
+
+ 
 static void send_message()
- {int iTime,iTempbrute,iRHbrute; 
- float fTemp,fRH;  
- uint16_t packet_len;
+{
+    int iTime,iTempbrute,iRHbrute;
+    float fTemp,fRH;
+    uint16_t packet_len;
     int16_t retcode;
     int32_t sensor_value, rh_value;
+    float fHumid;
+    fTemp=myTH02.ReadTemperature();
+    printf("\r\n   temp=%f",fTemp);
+    fHumid=myTH02.ReadHumidity();
+    printf("\r\n   humid=%f",fHumid);
 
- MyTH02.startTempConv(true,true);
-    iTime= MyTH02.waitEndConversion();// wait until onversion  is done
-     fTemp= (float)MyTH02.getConversionValue()/10;
-   
-    printf ("\n\r temp value=%2.1f",fTemp);
-  
- MyTH02.startRHConv(true,true);
-    iTime= MyTH02.waitEndConversion();// wait until onversion  is done
-     fRH= (float) MyTH02.getConversionValue()/10;
-   
-    printf ("\n\r humidity value=  %2.1f",fRH );
+    //GPS
+    char gpsLat[16];
+    char gpsLon[16];
+    char gpsalt[16];
+    MyGPS.getLatitude(gpsLat);
+    MyGPS.getLongitude(gpsLon);
+    MyGPS.getAltitude(gpsalt);
+
+    printf("\r\n   gpsLatitude=%s", gpsLat);
+    printf("\r\n   gpsLongitude=%s", gpsLon);
+    printf("\r\n   gpsAltitude=%s", gpsalt);
+    
+    //RFID
+    if ( ! RfChip.PICC_IsNewCardPresent())
+    {
+      wait_ms(500);
+    }
 
-/*
-    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;
+    // Select one of the cards
+    if (!RfChip.PICC_ReadCardSerial())
+    {
+      wait_ms(500);
+      printf("\r\nReading a card");
+    }
+
+    // Print Card UID
+    char nfcData[100];
+    printf("\r\nCard UID: ");
+    for (uint8_t i = 0; i < RfChip.uid.size; i++)
+    {
+      printf(" %X02", RfChip.uid.uidByte[i]);
+      nfcData[i] = RfChip.uid.uidByte[i];
     }
-  */  
-          
+    printf("\r\n");
+
+    // Print Card type
+    uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
+    printf("PICC Type: %s \r\n", RfChip.PICC_GetTypeName(piccType));
+    strcat(nfcData, RfChip.PICC_GetTypeName(piccType));
+    printf("DATA send: %s \r\n", nfcData);
+    wait_ms(1000);
+    
     Payload.reset();
-    size = Payload.addTemperature(1, (float) fTemp);    
-    size =size+ Payload.addRelativeHumidity(2, fRH);  
+    size = Payload.addTemperature(1, (float) fTemp);
+    size = size + Payload.addRelativeHumidity(2, fHumid);
+    size = size + Payload.addGPS(3, atof(gpsLat), atof(gpsLon), atof(gpsalt));
+    size = size + Payload.addNFC(4, (char *)nfcData);
 
 // send complete message with cayenne format
-      retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, Payload.getBuffer(), Payload.getSize(),
-                           MSG_UNCONFIRMED_FLAG);                
+    retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, Payload.getBuffer(), Payload.getSize(),
+                           MSG_UNCONFIRMED_FLAG);
 
     if (retcode < 0) {
         retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n")
@@ -243,7 +294,8 @@
  * Receive a message from the Network Server
  */
 static void receive_message()
-{int num_port, iPosition=0,iIndex,iEtatAlarme;
+{
+    int num_port, iPosition=0,iIndex,iEtatAlarme;
     uint8_t port;
     int flags;
     int16_t retcode = lorawan.receive(rx_buffer, sizeof(rx_buffer), port, flags);
@@ -257,36 +309,37 @@
     for (uint8_t i = 0; i < retcode; i++) {
         printf("%02x", rx_buffer[i]);
     }
-    // printf("\n test value=%d", port); 
- // *****************************code todo here   ********************************************
-     switch (port){
+    //printf("\n test value=%d", port);
+// *****************************code todo here   ********************************************
+    switch (port) {
         case 3: // control led
-         printf("\n led=%d", (int)rx_buffer[0]); 
-          if ((rx_buffer[0]-0x30)==0)
-          
-          iEtatAlarme=0;
-          else iEtatAlarme=1;
+            printf("\n led=%d", (int)rx_buffer[0]);
+            if ((rx_buffer[0]-0x30)==0)
+
+                iEtatAlarme=0;
+            else iEtatAlarme=1;
             Alarme.write(iEtatAlarme);
-            
-            printf("\n alarme=%d",iEtatAlarme); 
-        break;
+
+            printf("\n alarme=%d",iEtatAlarme);
+            break;
         case 4:// control servomotor
-        for (iIndex=0;iIndex<retcode;iIndex++)
-        {iPosition=iPosition*10+(rx_buffer[iIndex]-0x30);// convert receive string to angular position
-        } 
-              
-      
-        printf("\n servo position =%d",iPosition); 
-      Myservo.position ( iPosition-45 ); // set servo motor position from 0 to 180
-       break;
-       default: printf("\n port inconnu =%d",(int)port); 
-       break;
-        }
- 
- 
- //  ***************************** end code todo here   *****************************************
-   
-        
+            for (iIndex=0; iIndex<retcode; iIndex++) {
+                iPosition=iPosition*10+(rx_buffer[iIndex]-0x30);// convert receive string to angular position
+            }
+
+
+            printf("\n servo position =%d",iPosition);
+            Myservo.position ( iPosition-45 ); // set servo motor position from 0 to 180
+            break;
+        default:
+            printf("\n port inconnu =%d",(int)port);
+            break;
+    }
+
+
+//  ***************************** end code todo here   *****************************************
+
+
     memset(rx_buffer, 0, sizeof(rx_buffer));
 }
 
@@ -330,7 +383,7 @@
             receive_message();
             break;
         case RX_TIMEOUT:
-         printf("\r\n timeout in reception - Code = %d \r\n", event);
+            printf("\r\n timeout in reception - Code = %d \r\n", event);
             break;
         case RX_ERROR:
             printf("\r\n Error in reception - Code = %d \r\n", event);