Push BLE Uart Data to Colud from Microbit

Dependencies:   BLE_API mbed nRF51822

Fork of uBit_BLE_UART_Voltmeter by Shahariar Hossain

Files at this revision

API Documentation at this revision

Comitter:
suntopbd
Date:
Fri Sep 07 20:17:07 2018 +0000
Parent:
10:053397a8dc40
Commit message:
Microbit BLE UART IoT

Changed in this revision

ConfigParamsPersistence.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
nrfConfigParamsPersistence.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 053397a8dc40 -r 6916c05fde52 ConfigParamsPersistence.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ConfigParamsPersistence.h	Fri Sep 07 20:17:07 2018 +0000
@@ -0,0 +1,53 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2015 ARM Limited
+ *
+ * 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.
+ */
+
+#ifndef __BLE_CONFIG_PARAMS_PERSISTENCE_H__
+#define __BLE_CONFIG_PARAMS_PERSISTENCE_H__
+
+#include "ble/services/URIBeaconConfigService.h"
+
+/**
+ * Generic API to load the URIBeacon configuration parameters from persistent
+ * storage. If persistent storage isn't available, the persistenceSignature
+ * member of params may be left un-initialized to the MAGIC, and this will cause
+ * a reset to default values.
+ *
+ * @param[out] paramsP
+ *                 The parameters to be filled in from persistence storage. This
+                   argument can be NULL if the caller is only interested in
+                   discovering the persistence status of params.
+
+ * @return true if params were loaded from persistent storage and have usefully
+ *         initialized fields.
+ */
+bool loadURIBeaconConfigParams(URIBeaconConfigService::Params_t *paramsP);
+
+/**
+ * Generic API to store the URIBeacon configuration parameters to persistent
+ * storage. It typically initializes the persistenceSignature member of the
+ * params to the MAGIC value to indicate persistence.
+ *
+ * @note: the save operation may be asynchronous. It may be a short while before
+ * the request takes affect. Reading back saved configParams may not yield
+ * correct behaviour if attempted soon after a store.
+ *
+ * @param[in/out] paramsP
+ *                    The params to be saved; persistenceSignature member gets
+ *                    updated if persistence is successful.
+ */
+void saveURIBeaconConfigParams(const URIBeaconConfigService::Params_t *paramsP);
+
+#endif /* #ifndef __BLE_CONFIG_PARAMS_PERSISTENCE_H__*/
diff -r 053397a8dc40 -r 6916c05fde52 main.cpp
--- a/main.cpp	Thu Sep 06 22:56:26 2018 +0000
+++ b/main.cpp	Fri Sep 07 20:17:07 2018 +0000
@@ -27,12 +27,18 @@
 #include "stdio.h"
 #include "UARTService.h"
 
+#include "ble/BLE.h"
+#include "ble/services/URIBeaconConfigService.h"
+#include "ble/services/DFUService.h"
+#include "ble/services/DeviceInformationService.h"
+#include "ConfigParamsPersistence.h"
+
 
 #define NEED_CONSOLE_OUTPUT 1 // if BLE printf messages needed on the remote console (PC/phone/host);                            
 #if NEED_CONSOLE_OUTPUT
-#define BLEprintf(STR) { if (uart) uart->write(STR, strlen(STR)); }
+#define blePrintf(STR) { if (uart) uart->write(STR, strlen(STR)); }
 #else
-#define BLEprintf(...) 
+#define blePrintf(...) 
 #endif
 
 ///////////////// pin table /////////////////////////
@@ -62,17 +68,13 @@
 
 
 BLEDevice  ble;
+DigitalOut led1(LED1);
 UARTService *uart;
 AnalogIn   ain(P0_3);
-DigitalOut   dout(P0_16);
-DigitalIn   din(P0_17);
+int val,dVal, dec, i;
+char result[100];
+float batt = 0.03;
  
-int dVal, dec, i,j;  
-float val,exBatt,f;
-char buffer[10];
-/////////////////// functions ///////////////////////
-
-
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
@@ -81,67 +83,56 @@
 
 void periodicCallback(void)
 {
-    BLEprintf("\n");
-    BLEprintf("Battery Volt: ");
-    BLEprintf(buffer);
-   // BLEprintf("\r\n");
     
+    blePrintf("BATT VOlT: ");
+    blePrintf(result);
+    blePrintf("\r\n");
 }
-///////////////////////////////////////////////
-///////////////// MAIN FUNC ///////////////////
-///////////////////////////////////////////////
- 
+
 int main(void)
 {
     Ticker ticker;
-    ticker.attach(periodicCallback, 2);
+    ticker.attach(periodicCallback, 5);
+
+    blePrintf("Initialising the nRF51822\n\r");
     ble.init();
-    ble.onDisconnection(disconnectionCallback);    
+    ble.onDisconnection(disconnectionCallback);
+    
     uart = new UARTService(ble);
 
+    /* setup advertising */
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
                                      (const uint8_t *)"uBit BLE", sizeof("uBit BLE") - 1);
+                                     // device name on BLE //
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
 
-    ble.setAdvertisingInterval(160); // 100ms; in multiples of 0.625ms. //
+    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
     ble.startAdvertising();
 
-    while (true) 
-    {
+    while (true) {
         ble.waitForEvent();
-        dout = din.read();
         val = ain.read_u16();
-
- 
-// 100k+22k voltage divider
-// 3.3 Vcc board voltage
- 
-    exBatt = (122/22)*3.6*val/1023.0; // ext batt volt 
-    //exBatt =12.37;
+    // 22k+100K VOLTAGE DIVIDER AND 0.15 IS OFFSET   
+    batt = (122/22)*3.6*val/1023.0 - 0.15
+    dVal = batt;
+    dec = (int)(batt * 100) % 100;
 
-    dVal = exBatt;  
-    
-    
-    dec = (int)(exBatt * 100) % 100;
-    if(exBatt>1 && exBatt<10){i=0;}
-    if(exBatt>=10 && exBatt<100){i=1;}
-    j=i;
-    memset(buffer, 0, 10);
-    
-     while (dVal > 0)
-        {
-        buffer[i] = (dVal % 10) + '0';
-        dVal /= 10;
-        i--;
-         }
-    buffer[j+1] = '.';
-    buffer[j+2] = (dec / 10) + '0';
-    buffer[j+3] = (dec % 10) + '0';
+    memset(result, 0, 100);
+    result[0] = (dVal / 10) + '0';
+    result[1] = (dVal % 10) + '0';
+    result[2] = '.';
+    result[3] = (dec / 10) + '0';
+    result[4] = (dec % 10) + '0';
+
 
-     }
-///////////// end of while(1) //////////////            
+    for (i=strlen(result)-1; i>=0; i--)
+        putc(result[i], stdout);
+      
+            }
 }
-///////////// end of main  /////////////////
+
+
+
diff -r 053397a8dc40 -r 6916c05fde52 nrfConfigParamsPersistence.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nrfConfigParamsPersistence.cpp	Fri Sep 07 20:17:07 2018 +0000
@@ -0,0 +1,105 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2015 ARM Limited
+ *
+ * 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.
+ */
+
+extern "C" {
+#include "pstorage.h"
+}
+#include "nrf_error.h"
+#include "ConfigParamsPersistence.h"
+
+/**
+ * Nordic specific structure used to store params persistently.
+ * It extends URIBeaconConfigService::Params_t with a persistence signature.
+ */
+struct PersistentParams_t {
+    URIBeaconConfigService::Params_t params;
+    uint32_t                         persistenceSignature; /* This isn't really a parameter, but having the expected
+                                                            * magic value in this field indicates persistence. */
+
+    static const uint32_t MAGIC = 0x1BEAC000;              /* Magic that identifies persistence */
+};
+
+/**
+ * The following is a module-local variable to hold configuration parameters for
+ * short periods during flash access. This is necessary because the pstorage
+ * APIs don't copy in the memory provided as data source. The memory cannot be
+ * freed or reused by the application until this flash access is complete. The
+ * load and store operations in this module initialize persistentParams and then
+ * pass it on to the 'pstorage' APIs.
+ */
+static PersistentParams_t persistentParams;
+
+static pstorage_handle_t pstorageHandle;
+
+/**
+ * Dummy callback handler needed by Nordic's pstorage module. This is called
+ * after every flash access.
+ */
+static void pstorageNotificationCallback(pstorage_handle_t *p_handle,
+                                         uint8_t            op_code,
+                                         uint32_t           result,
+                                         uint8_t           *p_data,
+                                         uint32_t           data_len)
+{
+    /* APP_ERROR_CHECK(result); */
+}
+
+/* Platform-specific implementation for persistence on the nRF5x. Based on the
+ * pstorage module provided by the Nordic SDK. */
+bool loadURIBeaconConfigParams(URIBeaconConfigService::Params_t *paramsP)
+{
+    static bool pstorageInitied = false;
+    if (!pstorageInitied) {
+        pstorage_init();
+
+        static pstorage_module_param_t pstorageParams = {
+            .cb          = pstorageNotificationCallback,
+            .block_size  = sizeof(PersistentParams_t),
+            .block_count = 1
+        };
+        pstorage_register(&pstorageParams, &pstorageHandle);
+        pstorageInitied = true;
+    }
+
+    if ((pstorage_load(reinterpret_cast<uint8_t *>(&persistentParams), &pstorageHandle, sizeof(PersistentParams_t), 0) != NRF_SUCCESS) ||
+        (persistentParams.persistenceSignature != PersistentParams_t::MAGIC)) {
+        // On failure zero out and let the service reset to defaults
+        memset(paramsP, 0, sizeof(URIBeaconConfigService::Params_t));
+        return false;
+    }
+
+    memcpy(paramsP, &persistentParams.params, sizeof(URIBeaconConfigService::Params_t));
+    return true;
+}
+
+/* Platform-specific implementation for persistence on the nRF5x. Based on the
+ * pstorage module provided by the Nordic SDK. */
+void saveURIBeaconConfigParams(const URIBeaconConfigService::Params_t *paramsP)
+{
+    memcpy(&persistentParams.params, paramsP, sizeof(URIBeaconConfigService::Params_t));
+    if (persistentParams.persistenceSignature != PersistentParams_t::MAGIC) {
+        persistentParams.persistenceSignature = PersistentParams_t::MAGIC;
+        pstorage_store(&pstorageHandle,
+                       reinterpret_cast<uint8_t *>(&persistentParams),
+                       sizeof(PersistentParams_t),
+                       0 /* offset */);
+    } else {
+        pstorage_update(&pstorageHandle,
+                        reinterpret_cast<uint8_t *>(&persistentParams),
+                        sizeof(PersistentParams_t),
+                        0 /* offset */);
+    }
+}