Dependencies:   Servo Cayenne-LPP

Files at this revision

API Documentation at this revision

Comitter:
suhaimiamiruddin
Date:
Fri Jun 11 14:01:47 2021 +0000
Parent:
60:f4e2002a9138
Commit message:
eggs tracker lorawan

Changed in this revision

Cayenne-LPP.lib Show annotated file Show diff for this revision Revisions of this file
DummySensor.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
--- a/Cayenne-LPP.lib	Mon Jan 27 14:05:26 2020 +0000
+++ b/Cayenne-LPP.lib	Fri Jun 11 14:01:47 2021 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/teams/myDevicesIoT/code/Cayenne-LPP/#5a9d65b33e85
+https://os.mbed.com/users/suhaimiamiruddin/code/Cayenne-LPP/#43d16dd86e84
--- a/DummySensor.h	Mon Jan 27 14:05:26 2020 +0000
+++ b/DummySensor.h	Fri Jun 11 14:01:47 2021 +0000
@@ -35,6 +35,8 @@
     int32_t read()
     {
         value += 2;
+        if(value>255){
+            value=1;}
         return value;
     }
 
--- a/main.cpp	Mon Jan 27 14:05:26 2020 +0000
+++ b/main.cpp	Fri Jun 11 14:01:47 2021 +0000
@@ -30,7 +30,7 @@
 using namespace events;
 
 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
-// This example only communicates with much shorter messages (<30 bytes).
+// This example only communicates with much shorter messages (<30 uint8_ts).
 // If longer messages are used, these buffers must be changed accordingly.
 uint8_t tx_buffer[30];
 uint8_t rx_buffer[30];
@@ -50,9 +50,10 @@
 int size = 0;
 
 
-DigitalOut Alarme (PC_13);// alarme LED output
+/*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
+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
  */
@@ -80,6 +81,18 @@
  */
 DS1820  ds1820(PC_9);
 
+//adc internal temperature
+AnalogIn adc_temp(ADC_TEMP);
+#define TCAL_30                                           ((uint16_t *)(0x1FF8007A ))//des valeurs pour la calibration
+#define TCAL_130                                          ((uint16_t *)(0x1FF8007E ))
+
+#define CALIBRATION_REFERENCE_VOLTAGE                     ((uint16_t *)(0x1FF80078 ))
+
+#define REFERENCE_VOLTAGE                                 3.0F // supplied with Vref+ or VDDA
+
+uint8_t calculcrc(uint8_t[],int);
+uint8_t simplecrc8(uint8_t);
+
 /**
 * This event queue is the global event queue for both the
 * application and stack. To conserve memory, the stack is designed to run
@@ -121,10 +134,10 @@
 {
     // setup tracing
     setup_trace();
-   //  th02 temerature sensor section 
+   /*  th02 temerature sensor section 
     int iTemp,iTime,iTempbrute,iRH,iRHbrute;
     printf ("\n\r start reading TH02 for first time");
-     MyTH02.startTempConv(true,true);
+     MyTH02.startTempConv(true,true);*/
   
 
 
@@ -179,24 +192,42 @@
     return 0;
 }
 
-/**
+/*****************************************************
  * 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;
+    int32_t sensor_value, temp_value, rh_value;
+    float adcCalTemp30C,adcCalTemp130C,ftemp;
+    double temp;
+    uint8_t badge, crc1, crc2;uint8_t ibadge[] = {0x1A, 0x14, 0xA3, 0xB5, 0xC4, 0x03, 0x2E, 0x61, 0x52, 0x32, 0x46};
 
  /*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 );
-  */
- 
+    printf ("\n\r temp value=%d  %d",sensor_value,iTempbrute );*/
 
-
+     adcCalTemp30C = *TCAL_30 * (REFERENCE_VOLTAGE/ *CALIBRATION_REFERENCE_VOLTAGE);
+     adcCalTemp130C = *TCAL_130 * (REFERENCE_VOLTAGE/ *CALIBRATION_REFERENCE_VOLTAGE);
+     
+     /*printf("reference voltage %x\r\n", *CALIBRATION_REFERENCE_VOLTAGE);
+     printf("30 degrees %x\r\n", *TCAL_30);
+     printf("130 degrees %x\r\n", *TCAL_130);*/
+     
+     badge = 0x1A;
+     crc1 = simplecrc8(badge);
+     printf("crcsend = %x\r\n\n\n", crc1);
+     crc2 = calculcrc(ibadge, sizeof(ibadge));
+     printf("crcreceive = %x\r\n", crc2);
+     
+       /* temp=adc_temp.read_u16();
+        printf("ADC Temp Value = %f\r\n", temp);
+        ftemp = ( ( (130 - 30)*(temp - *TCAL_30) ) / (*TCAL_130 - *TCAL_30) ) + 30;
+        printf("temperature = %f \r\n",ftemp); */
+    
     if (ds1820.begin()) {
         ds1820.startConversion();
         sensor_value = ds1820.read();
@@ -207,9 +238,10 @@
         return;
     }
     
-          
     Payload.reset();
-    size = Payload.addTemperature(1, (float) sensor_value/100);    
+    Payload.addTemperature(1, (float) sensor_value/100);
+    Payload.addRelativeHumidity(2, (float) sensor_value/10);
+    Payload.addDigitalInput(3, sensor_value);
    
 
 
@@ -229,13 +261,13 @@
         return;
     }
 
-    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
-    memset(tx_buffer, 0, sizeof(tx_buffer));
+    printf("\r\n %d uint8_ts scheduled for transmission \r\n", retcode);
+    memset(Payload.getBuffer(), 0, Payload.getSize());
 }
 
-/**
+/*************************************************
  * Receive a message from the Network Server
- */
+ *************************************************/
 static void receive_message()
 {int num_port;
     uint8_t port;
@@ -247,7 +279,7 @@
         return;
     }
 
-    printf(" RX Data on port %u (%d bytes): ", port, retcode);
+    printf(" RX Data on port %u (%d uint8_ts): ", port, retcode);
     for (uint8_t i = 0; i < retcode; i++) {
         printf("%02x", rx_buffer[i]);
     }
@@ -258,9 +290,9 @@
     memset(rx_buffer, 0, sizeof(rx_buffer));
 }
 
-/**
+/******************
  * Event handler
- */
+ *****************/
 static void lora_event_handler(lorawan_event_t event)
 {
     switch (event) {
@@ -318,3 +350,62 @@
 }
 
 // EOF
+
+/******************
+ * Calcul CRC
+ *****************/
+uint8_t calculcrc(uint8_t *byteVal, int taille)
+{
+    uint8_t generator = 0x07;
+    uint8_t crc = 0; /* start with 0 so first byte can be 'xored' in */
+    
+    printf("byteval = %x%x%x%x%x%x%x%x%x%x\r\n", *byteVal, *(byteVal+1), *(byteVal+2), *(byteVal+3), *(byteVal+4), *(byteVal+5), *(byteVal+6), *(byteVal+7), *(byteVal+8), *(byteVal+9), *(byteVal+10)); 
+
+    for(int i=0; i<taille; i++)
+    {
+        printf("current byte = %x\r\n",byteVal[i]);
+        crc ^= byteVal[i]; /* XOR-in the next input byte */
+        printf("current crc = %x\r\n", crc);
+
+        for (int j = 0; j < 8; j++)
+        {
+            printf("msbcrc = %x\r\n", crc & 0x80);
+            if ((crc & 0x80) != 0)
+            {
+                crc = (uint8_t)((crc << 1) ^ generator);
+                printf("crc = %x\r\n", crc);
+            }
+            else
+            {
+                crc <<= 1;
+                printf("msbcrc 0 so we shift = %x\r\n", crc);
+            }
+        }
+    }
+
+    return crc;
+} 
+
+uint8_t simplecrc8(uint8_t byteVal)
+{
+    uint8_t generator = 0x07;
+    uint8_t crc = byteVal; /* init crc directly with input byte instead of 0, avoid useless 8 bitshifts until input byte is in crc register */
+    printf("byteval = %x\r\n", crc);
+
+    for (int i = 0; i < 8; i++)
+    {
+        printf("msbcrc = %x\r\n", crc & 0x80);
+        if ((crc & 0x80) != 0)
+        { /* most significant bit set, shift crc register and perform XOR operation, taking not-saved 9th set bit into account */
+            crc = (uint8_t)((crc << 1) ^ generator);
+            printf("crc after xor with generator = %x\r\n", crc);
+        }
+        else
+        { /* most significant bit not set, go to next bit */
+            crc <<= 1;
+            printf("msbcrc is zero, we shift = %x\r\n", crc);
+        }
+    }
+
+    return crc;
+} 
--- a/mbed_app.json	Mon Jan 27 14:05:26 2020 +0000
+++ b/mbed_app.json	Fri Jun 11 14:01:47 2021 +0000
@@ -35,12 +35,12 @@
             "lora.duty-cycle-on": true,
             "lora.phy": "EU868",
           
-            "lora.device-eui": " { 0x00, 0x01, 0x91, 0xE1, 0x38, 0xC2, 0x23, 0x02 }",
-            "lora.application-eui": "{ 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x02, 0x16, 0x00 }",
-            "lora.application-key": "{ 0x77, 0x8A, 0x26, 0xDF, 0x9D, 0x70, 0xDB, 0xEF, 0x43, 0x03, 0x0F, 0xD2, 0x5F, 0x47, 0x1D, 0x00 }",
-            "lora.appskey": "{ 0x25, 0x69, 0xD2, 0x16, 0xC6, 0x8F, 0x94, 0xC3, 0x04, 0xC5, 0x96, 0x7B, 0x69, 0xA0, 0x3A, 0x00 }",
-            "lora.nwkskey": "{ 0xE9, 0xCA, 0xC2, 0xA8, 0xDC, 0x06, 0x4F, 0x62, 0x4D, 0xAC, 0x06, 0x1D, 0x7C, 0x51, 0x2C, 0x00 }",
-            "lora.device-address": " 0x26011C00"
+            "lora.device-eui": " { 0x00, 0x04, 0x8C, 0x2F, 0x4C, 0xBA, 0xCB, 0x90 }",
+            "lora.application-eui": "{ 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x04, 0x17, 0x1F }",
+            "lora.application-key": "{ 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x04, 0x17, 0x1F }",
+            "lora.appskey": "{ 0xC7, 0x22, 0x05, 0xE8, 0x2A, 0x5A, 0xD3, 0xFC, 0x6E, 0x5D, 0xFB, 0x8F, 0x9A, 0x57, 0x99, 0x57 }",
+            "lora.nwkskey": "{ 0xB9, 0xC9, 0x41, 0x3D, 0x3B, 0x40, 0x9A, 0x45, 0xD9, 0x21, 0x32, 0xD7, 0x10, 0xC1, 0x42, 0x18 }",
+            "lora.device-address": " 0x2601369E"
             
         },