lora

Revision:
60:e8f234134c86
Parent:
56:39847849d219
--- a/main.cpp	Thu Nov 28 09:01:59 2019 +0000
+++ b/main.cpp	Thu Feb 20 07:51:11 2020 +0000
@@ -1,19 +1,4 @@
-/**
- * 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 "mbed.h"
 #include <stdio.h>
 
 #include "lorawan/LoRaWANInterface.h"
@@ -24,19 +9,57 @@
 #include "DummySensor.h"
 #include "trace_helper.h"
 #include "lora_radio_helper.h"
+#include "MFRC522.h"
+
+// BL072Z-LRWAN1 SPI2 pins
+#define MF_RESET    PA_4
+#define SPI_MOSI    PB_15
+#define SPI_MISO    PB_14
+#define SPI_SCK     PB_13
+#define SPI_CS      PB_12
 
 using namespace events;
 
 // 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 rx_buffer[30];
+uint8_t tx_buffer[50];
+uint8_t rx_buffer[50];
 
+char verstr[40];
+/*************************************************************************************************************/
+static int nonce=0;
+static int sen=-1;
+InterruptIn button(USER_BUTTON);
+int i=0;
+//DigitalOut led(LED1);
+DigitalOut led4(LED4);
+DigitalOut LedGreen(LED3);
+MFRC522    RfChip   (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET);
+/*
+void pressed()
+{
+    if(nonce%2==0)
+    {
+        
+        led4 = 1;
+        i=1;
+        sen=1;
+      }  
+      else
+      {
+   
+        led4 = 0;
+        i=0;  
+        sen=-1; 
+          }
+}
+*/
+/************************************************************************************************************/
 /*
  * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
  */
-#define TX_TIMER                        10000
+#define TX_TIMER                        30000
 
 /**
  * Maximum number of events for the event queue.
@@ -92,9 +115,13 @@
  */
 int main(void)
 {
+ //   button.fall(&pressed);
+    RfChip.PCD_Init();
+    
     // setup tracing
     setup_trace();
-
+    
+    
     // stores the status of a call to LoRaWAN protocol
     lorawan_status_t retcode;
 
@@ -104,7 +131,7 @@
         return -1;
     }
 
-    printf("\r\n Mbed LoRaWANStack initialized \r\n");
+    printf("\r\n LoRaWANStack initialized \r\n");
 
     // prepare application callbacks
     callbacks.events = mbed::callback(lora_event_handler);
@@ -117,16 +144,16 @@
         return -1;
     }
 
-    printf("\r\n CONFIRMED message retries : %d \r\n",
+ /*   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");
         return -1;
     }
 
-    printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
+ //   printf("\r\n Adaptive data  rate (ADR) - Enabled \r\n");
 
     retcode = lorawan.connect();
 
@@ -137,7 +164,7 @@
         return -1;
     }
 
-    printf("\r\n Connection - In Progress ...\r\n");
+//    printf("\r\n Connection - In Progress ...\r\n");
 
     // make your event queue dispatching events forever
     ev_queue.dispatch_forever();
@@ -153,19 +180,74 @@
     uint16_t packet_len;
     int16_t retcode;
     int32_t sensor_value;
+    PCD_DumpVersionToSerial(verstr);
+    printf(verstr);
+        /************************************************************************************************************/
+   // Look for new cards
+    if (RfChip.PICC_IsNewCardPresent())
+    {
+      wait_ms(500);
+      //continue;
+      printf("card present ");
+      sen=-1;
+      
+    }
+    else
+    {
+        sen=1;
+        
+        }
+    
+    // Select one of the cards
+    if (!RfChip.PICC_ReadCardSerial())
+    {
+      wait_ms(500);
+    //  printf("card read\r\n");
+    //  continue;
+    }
 
+    LedGreen = 0;
+    
+ if(sen==-1)
+ {
+    // Print Card UID
+    printf("Card UID: ");
+    
+    for (uint8_t i = 0; i < RfChip.uid.size; i++)
+    {
+        printf(" %X02", RfChip.uid.uidByte[i]);
+    }
+    printf("\n\r");
+
+    // Print Card type
+    uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak);
+    printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType));
+    wait_ms(1000);
+    }
+    /***********************************************************************************************************/
+    
+    nonce=nonce+1;
     if (ds1820.begin()) {
         ds1820.startConversion();
         sensor_value = ds1820.read();
-        printf("\r\n Dummy Sensor Value = %d \r\n", sensor_value);
+//        printf("\r\n Dummy Sensor Value = %d \r\n", sensor_value);
         ds1820.startConversion();
     } else {
         printf("\r\n No sensor found \r\n");
         return;
     }
-
-    packet_len = sprintf((char *) tx_buffer, "Dummy Sensor Value is %d",
-                         sensor_value);
+      /*      if(nonce%2==0)
+            {
+                sen=1;
+                }
+                else
+                {
+                sen=-1;
+                    }
+                    */
+      packet_len = sprintf((char *) tx_buffer, "{\"id\":5678,\"sen\":%d,\"nonce\":%d}",
+                         sen, nonce); 
+    printf("%s\n", tx_buffer); 
 
     retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
                            MSG_UNCONFIRMED_FLAG);
@@ -183,8 +265,16 @@
         return;
     }
 
-    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
+//    printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
     memset(tx_buffer, 0, sizeof(tx_buffer));
+    RfChip.PICC_IsNewCardPresent();
+    //RfChip.PCD_Reset();
+ /*   if (sen==1)
+    {
+        sen=-1;
+        }
+        */
+        
 }
 
 /**
@@ -217,7 +307,7 @@
 {
     switch (event) {
         case CONNECTED:
-            printf("\r\n Connection - Successful \r\n");
+//            printf("\r\n Connection - Successful \r\n");
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             } else {
@@ -230,7 +320,7 @@
             printf("\r\n Disconnected Successfully \r\n");
             break;
         case TX_DONE:
-            printf("\r\n Message Sent to Network Server \r\n");
+            printf("\r Data Sent to Server \r\n");
             if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
                 send_message();
             }