https://www.hackster.io/PSoC_Rocks/water-quality-monitoring-autonomous-robot-0bbf88
Dependencies: BLE_API MAG3110 MMA8652 PID mbed nRF51822
Fork of uBit_BLE_UART_Voltmeter_IoT by
Diff: main.cpp
- Revision:
- 11:6916c05fde52
- Parent:
- 10:053397a8dc40
- Child:
- 12:0777ec4114c8
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 ///////////////// + + +