anurag sharma / Mbed 2 deprecated SmartWaterTank

Dependencies:   mbed microbit BLE_API nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  //////////////////////////////////////////////////////////
00017 ///////////////////////NOTE ///////////////////////////////
00018 // This program uses BBC microbit as a NRF51822 board    //
00019 // with limited onboard functionality, mainly focused    //
00020 // BLE-uart/ADC/DIO/PWM capability, see pin maping below //
00021 ///////////////////////////////////////////////////////////
00022 ///////////////////////////////////////////////////////////
00023         
00024 #include <string.h>
00025 #include "mbed.h"
00026 #include "BLE.h"
00027 #include "stdio.h"
00028 #include "UARTService.h"
00029 
00030 #include "ble/BLE.h"
00031 #include "ble/services/URIBeaconConfigService.h"
00032 #include "ble/services/DFUService.h"
00033 #include "ble/services/DeviceInformationService.h"
00034 #include "ConfigParamsPersistence.h"
00035 
00036 
00037 #define NEED_CONSOLE_OUTPUT 1 // if BLE printf messages needed on the remote console (PC/phone/host);                            
00038 #if NEED_CONSOLE_OUTPUT
00039 #define blePrintf(STR) { if (uart) uart->write(STR, strlen(STR)); }
00040 #else
00041 #define blePrintf(...) 
00042 #endif
00043 
00044 ///////////////// pin table /////////////////////////
00045 /////////////////////////////////////////////////////
00046 // ubit.pin    nrf51822pin    functions      note  //
00047 /////////////////////////////////////////////////////
00048 //  P2           P0_1        ADC/PWM/DIO      2    //
00049 //  P1           P0_2        ADC/PWM/DIO      1    // 
00050 //  P0           P0_3        ADC/PWM/DIO      0    //
00051 //  P16          P_16            DIO               //
00052 //  P14          P0_25       SPI MIS/DIO           //
00053 //  P5           P_17        Button A/DI    pullup //
00054 //  P11          P_26        Button B/DI    pullup //
00055 
00056 /////////////////////////////////////////////////////
00057 ///////////////// not mapped yet ////////////////////
00058 //  P20                      I2C SDA/DIO    pullup //
00059 //  P19                      I2C SCL/DIO    pullup //
00060 //  P15                      SPI MOS/DIO           //
00061 //  P13                      SPI SCK/DIO           //
00062 //  P16                                            //
00063 //  P8                                             //
00064 
00065 /////////////////////////////////////////////////////
00066 //      LED Matrix pins are not mapped             //
00067 /////////////////////////////////////////////////////
00068 
00069 
00070 BLEDevice  ble;
00071 DigitalOut led1(LED1);
00072 UARTService *uart;
00073 AnalogIn   ain(P0_3);
00074 int val,dVal, dec, i;
00075 char result[100];
00076 float batt = 0.03;
00077  
00078 
00079 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00080 {
00081     ble.startAdvertising();
00082 }
00083 
00084 void periodicCallback(void)
00085 {
00086     
00087     blePrintf("BATT VOlT: ");
00088     blePrintf(result);
00089     blePrintf("\r\n");
00090 }
00091 
00092 int main(void)
00093 {
00094     Ticker ticker;
00095     ticker.attach(periodicCallback, 5);
00096 
00097     blePrintf("Initialising the nRF51822\n\r");
00098     ble.init();
00099     ble.onDisconnection(disconnectionCallback);
00100     
00101     uart = new UARTService(ble);
00102 
00103     /* setup advertising */
00104     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00105     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00106     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00107                                      (const uint8_t *)"uBit BLE", sizeof("uBit BLE") - 1);
00108                                      // device name on BLE //
00109     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00110                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
00111 
00112     ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
00113     ble.startAdvertising();
00114 
00115     while (true) {
00116         ble.waitForEvent();
00117         val = ain.read_u16();
00118     // 22k+100K VOLTAGE DIVIDER AND 0.15 IS OFFSET   
00119     batt = (122/22)*3.6*val/1023.0 - 0.15;
00120     dVal = batt;
00121     dec = (int)(batt * 100) % 100;
00122 
00123     memset(result, 0, 100);
00124     result[0] = (dVal / 10) + '0';
00125     result[1] = (dVal % 10) + '0';
00126     result[2] = '.';
00127     result[3] = (dec / 10) + '0';
00128     result[4] = (dec % 10) + '0';
00129 
00130 
00131     for (i=strlen(result)-1; i>=0; i--)
00132         putc(result[i], stdout);
00133       
00134             }
00135 }
00136 
00137 
00138