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

Committer:
highroads
Date:
Sun Mar 12 14:19:59 2017 +0000
Revision:
7:447042d1d562
Parent:
6:7b16a72aebe8
For use with Nordic or Bluefruit LE apps.; Output accelerometer Z value every two seconds when connected to central.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
highroads 0:3e21be32c98b 1 /*
highroads 0:3e21be32c98b 2
highroads 0:3e21be32c98b 3 Copyright (c) 2012-2014 RedBearLab
highroads 0:3e21be32c98b 4
highroads 0:3e21be32c98b 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
highroads 0:3e21be32c98b 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
highroads 0:3e21be32c98b 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
highroads 0:3e21be32c98b 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
highroads 0:3e21be32c98b 9 subject to the following conditions:
highroads 0:3e21be32c98b 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
highroads 0:3e21be32c98b 11
highroads 0:3e21be32c98b 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
highroads 0:3e21be32c98b 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
highroads 0:3e21be32c98b 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
highroads 0:3e21be32c98b 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
highroads 0:3e21be32c98b 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
highroads 0:3e21be32c98b 17
highroads 0:3e21be32c98b 18 */
highroads 0:3e21be32c98b 19
highroads 0:3e21be32c98b 20 /*
highroads 0:3e21be32c98b 21 * The application works with the BLEController iOS/Android App.
highroads 0:3e21be32c98b 22 * Type something from the Terminal to send
highroads 0:3e21be32c98b 23 * to the BLEController App or vice verse.
highroads 0:3e21be32c98b 24 * Characteristics received from App will print on Terminal.
highroads 0:3e21be32c98b 25 */
highroads 0:3e21be32c98b 26
highroads 0:3e21be32c98b 27 // Import libraries
highroads 0:3e21be32c98b 28 #include "Arduino.h"
highroads 0:3e21be32c98b 29 #include "BLEPeripheral.h"
highroads 7:447042d1d562 30 #include "MMA8451Q.h"
highroads 7:447042d1d562 31 #include "stdio.h"
highroads 7:447042d1d562 32
highroads 7:447042d1d562 33 #define MMA8451_I2C_ADDRESS (0x1d<<1)
highroads 7:447042d1d562 34
highroads 0:3e21be32c98b 35
highroads 0:3e21be32c98b 36 Serial serial(USBTX, USBRX);
highroads 0:3e21be32c98b 37
highroads 0:3e21be32c98b 38 // The SPI construct, REQN and RDYN IO construct should be modified manually
highroads 0:3e21be32c98b 39 // It depend on the board you are using and the REQN&RDYN configuration on BLE Shield
highroads 0:3e21be32c98b 40 // config for KL25Z
highroads 3:b5dc2454dfc3 41 // VIN to 5V on KL25
highroads 3:b5dc2454dfc3 42 // GND to GND on KL25
highroads 0:3e21be32c98b 43 SPI spi(PTD2, PTD3, PTD1); // MOSI , MISO , SCLK
highroads 0:3e21be32c98b 44 DigitalInOut BLE_RDY(PTD5); //
highroads 0:3e21be32c98b 45 DigitalInOut BLE_REQ(PTD0); //
highroads 1:b394935d1db1 46 DigitalInOut BLE_RESET(PTA13);
highroads 0:3e21be32c98b 47
highroads 0:3e21be32c98b 48 unsigned char txbuf[16] = {0};
highroads 0:3e21be32c98b 49 unsigned char txlen = 0;
highroads 7:447042d1d562 50 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
highroads 7:447042d1d562 51 Ticker sensor_out;
highroads 0:3e21be32c98b 52
highroads 4:fb6008860882 53 //*----- BLE Utility -------------------------------------------------------------------------*/
highroads 0:3e21be32c98b 54 // create peripheral instance, see pinouts above
highroads 5:83be623f6ad4 55 BLEPeripheral blePeripheral = BLEPeripheral(&BLE_REQ, &BLE_RDY, &BLE_RESET);
highroads 4:fb6008860882 56
highroads 0:3e21be32c98b 57 // create service
highroads 7:447042d1d562 58 BLEService uartService = BLEService("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
highroads 4:fb6008860882 59
highroads 0:3e21be32c98b 60 // create characteristic
highroads 7:447042d1d562 61 BLECharacteristic txCharacteristic = BLECharacteristic("6E400003-B5A3-F393-E0A9-E50E24DCCA9E", BLENotify, 20);
highroads 7:447042d1d562 62 BLECharacteristic rxCharacteristic = BLECharacteristic("6E400002-B5A3-F393-E0A9-E50E24DCCA9E", BLEWriteWithoutResponse, 20);
highroads 0:3e21be32c98b 63 /*--------------------------------------------------------------------------------------------*/
highroads 5:83be623f6ad4 64 //
highroads 6:7b16a72aebe8 65 // Nordic characteristics work with Nordic and Bluefruit LE apps
highroads 5:83be623f6ad4 66 // uartService 6E400001-B5A3-F393-­E0A9-­E50E24DCCA9E
highroads 6:7b16a72aebe8 67 // txCharacteristic 6E400003-B5A3-F393-­E0A9-­E50E24DCCA9E
highroads 6:7b16a72aebe8 68 // rxCharacteristic 6E400002-B5A3-F393-­E0A9-­E50E24DCCA9E
highroads 5:83be623f6ad4 69 // RedBearLabs characteristics work with RedBear BLE Controller app
highroads 5:83be623f6ad4 70 // uartService 713d0000503e4c75ba943148f18d941e
highroads 5:83be623f6ad4 71 // txCharacteristic 713d0002503e4c75ba943148f18d941e
highroads 5:83be623f6ad4 72 // rxCharacteristic 713d0003503e4c75ba943148f18d941e
highroads 5:83be623f6ad4 73
highroads 5:83be623f6ad4 74
highroads 0:3e21be32c98b 75
highroads 0:3e21be32c98b 76 unsigned int interval = 0;
highroads 0:3e21be32c98b 77 unsigned char count_on = 0;
highroads 7:447042d1d562 78 void sens()
highroads 7:447042d1d562 79 {
highroads 7:447042d1d562 80 char sensbuf[16];
highroads 7:447042d1d562 81 char senslen;
highroads 7:447042d1d562 82 float accZ = abs(acc.getAccZ());
highroads 7:447042d1d562 83
highroads 7:447042d1d562 84 serial.printf("measured %f \r\n",accZ);
highroads 7:447042d1d562 85 sprintf(sensbuf,"%f",accZ);
highroads 7:447042d1d562 86 senslen=strlen(sensbuf);
highroads 7:447042d1d562 87 txCharacteristic.setValue((const unsigned char *)sensbuf, senslen);
highroads 7:447042d1d562 88 }
highroads 0:3e21be32c98b 89 int main()
highroads 0:3e21be32c98b 90 {
highroads 0:3e21be32c98b 91 serial.baud(115200);
highroads 0:3e21be32c98b 92 serial.printf("Serial begin!\r\n");
highroads 0:3e21be32c98b 93
highroads 0:3e21be32c98b 94 /*----- BLE Utility ---------------------------------------------*/
highroads 0:3e21be32c98b 95 // set advertised local name and service UUID
highroads 0:3e21be32c98b 96 blePeripheral.setLocalName("BLE Shield");
highroads 0:3e21be32c98b 97
highroads 0:3e21be32c98b 98 blePeripheral.setAdvertisedServiceUuid(uartService.uuid());
highroads 0:3e21be32c98b 99
highroads 0:3e21be32c98b 100 // add service and characteristic
highroads 0:3e21be32c98b 101 blePeripheral.addAttribute(uartService);
highroads 0:3e21be32c98b 102 blePeripheral.addAttribute(rxCharacteristic);
highroads 0:3e21be32c98b 103 blePeripheral.addAttribute(txCharacteristic);
highroads 0:3e21be32c98b 104
highroads 0:3e21be32c98b 105 // begin initialization
highroads 0:3e21be32c98b 106 blePeripheral.begin();
highroads 0:3e21be32c98b 107 /*---------------------------------------------------------------*/
highroads 0:3e21be32c98b 108
highroads 0:3e21be32c98b 109 serial.printf("BLE UART Peripheral begin!\r\n");
highroads 0:3e21be32c98b 110
highroads 7:447042d1d562 111
highroads 0:3e21be32c98b 112 while(1)
highroads 0:3e21be32c98b 113 {
highroads 0:3e21be32c98b 114 BLECentral central = blePeripheral.central();
highroads 0:3e21be32c98b 115
highroads 0:3e21be32c98b 116 if (central)
highroads 0:3e21be32c98b 117 {
highroads 0:3e21be32c98b 118 // central connected to peripheral
highroads 0:3e21be32c98b 119 serial.printf("Connected to central\r\n");
highroads 7:447042d1d562 120 sensor_out.attach(&sens, 2.0); // the address of the function to be attached (sens) and the interval (2 seconds)
highroads 0:3e21be32c98b 121 while (central.connected())
highroads 0:3e21be32c98b 122 {
highroads 7:447042d1d562 123
highroads 0:3e21be32c98b 124 // central still connected to peripheral
highroads 0:3e21be32c98b 125 if (rxCharacteristic.written())
highroads 0:3e21be32c98b 126 {
highroads 0:3e21be32c98b 127 unsigned char rxlen = rxCharacteristic.valueLength();
highroads 0:3e21be32c98b 128 const unsigned char *val = rxCharacteristic.value();
highroads 0:3e21be32c98b 129 serial.printf("didCharacteristicWritten, Length: %d\r\n", rxlen);
highroads 0:3e21be32c98b 130 unsigned char i = 0;
highroads 0:3e21be32c98b 131 while(i<rxlen)
highroads 0:3e21be32c98b 132 {
highroads 0:3e21be32c98b 133 serial.printf("%d, ",val[i++]);
highroads 0:3e21be32c98b 134 }
highroads 0:3e21be32c98b 135 serial.printf("\r\n");
highroads 0:3e21be32c98b 136 }
highroads 0:3e21be32c98b 137
highroads 0:3e21be32c98b 138 if(serial.readable()) // Do not spend much time on doing other things when serial available! Otherwisee, data will lose.
highroads 0:3e21be32c98b 139 {
highroads 0:3e21be32c98b 140 if(!count_on)
highroads 0:3e21be32c98b 141 {
highroads 0:3e21be32c98b 142 count_on = 1;
highroads 0:3e21be32c98b 143 }
highroads 0:3e21be32c98b 144 interval = 0;
highroads 0:3e21be32c98b 145 txbuf[txlen] = serial.getc();
highroads 0:3e21be32c98b 146 txlen++;
highroads 0:3e21be32c98b 147 }
highroads 0:3e21be32c98b 148
highroads 0:3e21be32c98b 149 if(count_on) // Count the interval after receiving a new char from terminate
highroads 0:3e21be32c98b 150 {
highroads 0:3e21be32c98b 151 interval++;
highroads 0:3e21be32c98b 152 }
highroads 0:3e21be32c98b 153
highroads 0:3e21be32c98b 154 if(interval == 10) // If there is no char available last the interval, send the received chars to central.
highroads 0:3e21be32c98b 155 {
highroads 0:3e21be32c98b 156 interval = 0;
highroads 0:3e21be32c98b 157 count_on = 0;
highroads 0:3e21be32c98b 158
highroads 0:3e21be32c98b 159 serial.printf("Received from terminal: %d bytes\r\n", txlen);
highroads 0:3e21be32c98b 160 txCharacteristic.setValue((const unsigned char *)txbuf, txlen);
highroads 0:3e21be32c98b 161 txlen = 0;
highroads 0:3e21be32c98b 162 }
highroads 0:3e21be32c98b 163 }
highroads 0:3e21be32c98b 164
highroads 0:3e21be32c98b 165 // central disconnected
highroads 7:447042d1d562 166 sensor_out.detach();
highroads 0:3e21be32c98b 167 serial.printf("Disconnected from central\r\n");
highroads 0:3e21be32c98b 168 }
highroads 0:3e21be32c98b 169 }
highroads 0:3e21be32c98b 170 }