Adafruit Bluefruit with KL25Z sending MMA8451Q acceleration data over BLE UART to Nordic oder Adafruit app

Dependencies:   BLE_nRF8001 MMA8451Q mbed

Fork of BLE_KL25_nRF8001 by Ian Kilburn

Revision:
7:447042d1d562
Parent:
6:7b16a72aebe8
--- a/main.cpp	Fri Mar 10 23:08:18 2017 +0000
+++ b/main.cpp	Sun Mar 12 14:19:59 2017 +0000
@@ -27,6 +27,11 @@
 // Import libraries 
 #include "Arduino.h"
 #include "BLEPeripheral.h"
+#include "MMA8451Q.h"
+#include "stdio.h"
+ 
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+ 
 
 Serial serial(USBTX, USBRX);
 
@@ -42,17 +47,19 @@
 
 unsigned char txbuf[16] = {0};
 unsigned char txlen = 0;
+MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
+Ticker sensor_out;
 
 //*----- BLE Utility -------------------------------------------------------------------------*/
 // create peripheral instance, see pinouts above
 BLEPeripheral            blePeripheral        = BLEPeripheral(&BLE_REQ, &BLE_RDY, &BLE_RESET);
  
 // create service
-BLEService               uartService          = BLEService("713d0000503e4c75ba943148f18d941e");
+BLEService               uartService          = BLEService("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
  
 // create characteristic
-BLECharacteristic    txCharacteristic = BLECharacteristic("713d0002503e4c75ba943148f18d941e", BLENotify, 20);
-BLECharacteristic    rxCharacteristic = BLECharacteristic("713d0003503e4c75ba943148f18d941e", BLEWriteWithoutResponse, 20);
+BLECharacteristic    txCharacteristic = BLECharacteristic("6E400003-B5A3-F393-E0A9-E50E24DCCA9E", BLENotify, 20);
+BLECharacteristic    rxCharacteristic = BLECharacteristic("6E400002-B5A3-F393-E0A9-E50E24DCCA9E", BLEWriteWithoutResponse, 20);
 /*--------------------------------------------------------------------------------------------*/
 //
 // Nordic characteristics work with Nordic and Bluefruit LE apps
@@ -68,7 +75,17 @@
 
 unsigned int interval = 0;
 unsigned char count_on = 0;
- 
+void sens()
+{
+  char sensbuf[16];
+  char senslen;
+  float accZ = abs(acc.getAccZ());
+
+  serial.printf("measured %f \r\n",accZ);
+  sprintf(sensbuf,"%f",accZ);
+  senslen=strlen(sensbuf);
+  txCharacteristic.setValue((const unsigned char *)sensbuf, senslen);
+}
 int main()
 { 
     serial.baud(115200);
@@ -91,6 +108,7 @@
     
     serial.printf("BLE UART Peripheral begin!\r\n");
     
+    
     while(1)
     {
         BLECentral central = blePeripheral.central();
@@ -99,9 +117,10 @@
         {
             // central connected to peripheral
             serial.printf("Connected to central\r\n");
-            
+            sensor_out.attach(&sens, 2.0); // the address of the function to be attached (sens) and the interval (2 seconds)           
             while (central.connected()) 
             {
+ 
                 // central still connected to peripheral
                 if (rxCharacteristic.written()) 
                 {
@@ -144,6 +163,7 @@
             }
         
             // central disconnected
+            sensor_out.detach();
             serial.printf("Disconnected from central\r\n");
         }
     }