lora experiments

Dependencies:   BLE_API LoRaWAN-lib SX1276Lib mbed nRF51822 HCSR04Lib

Fork of LoRa by Olav Nymoen

Revision:
3:70d40f678f37
Parent:
2:ce7cea075e95
Child:
4:63d6744a61b6
--- a/main.cpp	Wed Jun 01 14:07:25 2016 +0000
+++ b/main.cpp	Wed Jun 08 12:40:38 2016 +0000
@@ -8,6 +8,15 @@
 #include "ble/BLE.h"
 #include "ble/services/DFUService.h"
 
+#include "hcsr04.h"
+
+/* I/O stuff */
+InterruptIn button(BUTTON3);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+HCSR04 usonic(P0_2, P0_3);
+
+
 /*!
  * Join requests trials duty cycle.
  */
@@ -152,7 +161,8 @@
 
 
 /* BLE stuff */
-const uint16_t customServiceUUID    = 0xA000;
+const uint16_t loraConfServiceUUID  = 0xA000;
+const uint16_t echoServiceUUID      = 0xB000;
 const static char DEVICE_NAME[]     = "TD Test Device";
 
 #if( OVER_THE_AIR_ACTIVATION != 0 )
@@ -164,7 +174,7 @@
 ReadWriteArrayGattCharacteristic<uint8_t, sizeof(AppEui)> AppEuiChar(AppEuiUUID, AppEui);
 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(AppKey)> AppKeyChar(AppKeyUUID, AppKey);
 
-GattCharacteristic *characteristics[] = {&DevEuiChar, &AppEuiChar, &AppKeyChar};
+GattCharacteristic *loraChars[] = {&DevEuiChar, &AppEuiChar, &AppKeyChar};
 #else
 const uint16_t DevAddrUUID          = 0xA004;
 const uint16_t NwkSKeyUUID          = 0xA005;
@@ -174,15 +184,16 @@
 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(NwkSKey)> NwkSKeyChar(NwkSKeyUUID, NwkSKey);
 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(AppSKey)> AppSKeyChar(AppSKeyUUID, AppSKey);
 
-GattCharacteristic *characteristics[] = {&DevAddrChar, &NwkSKeyChar, &AppSKeyChar};
+GattCharacteristic *loraChars[] = {&DevAddrChar, &NwkSKeyChar, &AppSKeyChar};
 #endif
-GattService        customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
-
+GattService        loraConfService(loraConfServiceUUID, loraChars, sizeof(loraChars) / sizeof(GattCharacteristic *));
 
-/* I/O stuff */
-InterruptIn button(BUTTON3);
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
+uint32_t echoDist = 0;
+const uint16_t EchoDistUUID          = 0xB001;
+ReadOnlyGattCharacteristic<uint32_t> echoDistChar(EchoDistUUID, &echoDist);
+GattCharacteristic *echoChars[] = {&echoDistChar};
+GattService        echoService(echoServiceUUID, echoChars, sizeof(echoChars) / sizeof(GattCharacteristic *));
+
 
 /*!
  * Indicates if the MAC layer network join status has changed.
@@ -479,7 +490,7 @@
  */
 void bleInitComplete(BLE::InitializationCompleteCallbackContext * params)
 {
-    const uint16_t uuid16_list[]        = { customServiceUUID, DFUServiceShortUUID };
+    const uint16_t uuid16_list[]        = { loraConfServiceUUID, echoServiceUUID, DFUServiceShortUUID };
     BLE &ble          = params->ble;
     ble_error_t error = params->error;
     
@@ -497,13 +508,15 @@
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
     ble.gap().setAdvertisingInterval(100);
  
-    ble.addService(customService);
+    ble.addService(loraConfService);
+    ble.addService(echoService);
     static DFUService dfu(ble);
  
     /* Start advertising */
     ble.gap().startAdvertising();
 }
 
+
 void buttonPressedCallback(void)
 {
     printf("Button pressed \r\n");
@@ -512,6 +525,15 @@
     printf("res: %d \r\n",NextTx);
 }
 
+void echoReceivedCallback(uint32_t dist)
+{
+    printf("Object is %u mm away!\r\n", dist);
+    echoDist = dist;
+    BLE::Instance(BLE::DEFAULT_INSTANCE).updateCharacteristicValue(echoDistChar.getValueHandle(),
+        (const uint8_t *)&echoDist, sizeof (echoDist));
+}
+
+
 int main( void )
 {
     printf("Hello world! \r\n");
@@ -519,13 +541,14 @@
     LoRaMacPrimitives_t LoRaMacPrimitives;
     LoRaMacCallback_t LoRaMacCallbacks;
     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
-    
+
     BoardInit( );
 
     ble.init(bleInitComplete);
 
     button.fall(buttonPressedCallback);
-    
+    usonic.start(echoReceivedCallback, 10.0);
+
     /* SpinWait for initialization to complete. This is necessary because the
      * BLE object is used in the main loop below. */
     while (ble.hasInitialized()  == false) { /* spin loop */ }
@@ -653,4 +676,4 @@
             }
         }
     }
-}
\ No newline at end of file
+}