App for BLE Nano to monitor the power consumption for a specific location, by intercepting the led flashes of a standard power meter. It counts and log the flashes for each second. It works with RedBear App for smart phone (Simple Chat App).
Dependencies: BLE_API lib_mma8451q mbed nRF51822
Fork of nRF51822_DataLogger_with_Chat by
Revision 12:27e9c3db28b9, committed 2016-08-14
- Comitter:
- tanasaro10
- Date:
- Sun Aug 14 14:57:09 2016 +0000
- Parent:
- 11:baafa4f7a15e
- Commit message:
- First release for counting and logging power impulses of a power meter. Useful to track the power consumption for a specific location.
Changed in this revision
diff -r baafa4f7a15e -r 27e9c3db28b9 lib_mma8451q.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib_mma8451q.lib Sun Aug 14 14:57:09 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/dudmuck/code/lib_mma8451q/#4bc96749141e
diff -r baafa4f7a15e -r 27e9c3db28b9 main.cpp --- a/main.cpp Fri May 06 18:14:18 2016 +0000 +++ b/main.cpp Sun Aug 14 14:57:09 2016 +0000 @@ -32,6 +32,11 @@ #include <Gap.h> //#include "ble_flash.h" #include "ble_flash.c" +#include "BatteryService.h" +//#include "DFUService.h" + +BLE ble; +//DFUService dfu(ble); #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ @@ -41,10 +46,15 @@ #define MyASSERT(cond , serialpc, errVal) assert_error_app((bool)cond, serialpc, (uint16_t)errVal, __LINE__, __FILE__) -BLE ble; Serial pc(USBTX, USBRX); +//uint8_t batteryLevel=100; +uint16_t nr_of_hits = 0; +uint32_t g_nrOfHits=0; +float gmaxV=0,gminV=3,gmedianV=0.18; // Voltage of the PhotoResistorstatic float measure: max, min, median; + +bool bTimeReady = false; // The Nordic UART Service static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; @@ -52,8 +62,10 @@ static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; static const int8_t txPower = 0xCD; +//BatteryService *batteryService = NULL; -uint8_t txPayload[TXRX_BUF_LEN] = {0, p28}; +//uint8_t txPayload[TXRX_BUF_LEN] = {0, p28}; +uint8_t txPayload[TXRX_BUF_LEN] = {0,}; uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; static uint8_t rx_buf[TXRX_BUF_LEN]; @@ -61,29 +73,29 @@ static uint32_t gTimeInstant = 1; // TimerTick Resolution, in seconds + bool g_bIsConnected = false; bool g_bIsAdvertising = false; bool g_bConnDisabled = false; -bool g_LogActive = false; +bool g_LogActive = false;// g_bAccEnabled=false, g_bCalib = false; static myDataLog_t g_MyData; uint8_t g_MyDataIdx=0; + // pins connected for measuring -DigitalOut led(LED1); -PwmOut buzzer(p15); -InterruptIn event(p29); // button -AnalogIn VP3(A3); -AnalogIn VP4(A4); -AnalogIn VP5(A5); -AnalogIn* VP[3]= {&VP3,&VP4,&VP5}; -#define NUM_OF_READINGS (4u) -myPayload_t g_currMeasures; // last measurements +DigitalOut led(LED1), //redLed(D4);//, accBuzzAlarm(D3), accLEDAlarm(D5); + +//uint8_t tempAlarm, accAlarm; + +//PwmOut buzzer(D1); // changed from P15 +AnalogIn photoVoltage(A5); // photo voltaga measurement -Timeout timeout_err; // timeout for buzz on error -Ticker periodicActions; +myPayload_t g_currMeasures2; // last measurements +//Timeout timeout_err; // timeout for buzz on error +Ticker periodicActions, doMeasures; mdatetime_manager_t g_myDateTimeVar; - +//uint8_t gBatteryValue=0; GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); @@ -92,6 +104,15 @@ GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); +void decode(uint8_t * buffer, uint16_t length); + +void alarm(){ + + //accBuzzAlarm = 1; + //accLEDAlarm = 0; + //timeout_err.attach(&at_timeout_err, 2); +} + void sendRadioMsg(const uint8_t* buf, uint16_t length) { uint8_t retVal; @@ -119,11 +140,6 @@ g_bIsAdvertising = false; } -void at_timeout_err() -{ - // stop buzz - buzz_int(&buzzer, 0,0); -} void connectionUpdate(connection_update_t option) { if (g_bConnDisabled == false) { @@ -158,9 +174,8 @@ } } } -void write_data_to_flash(uint32_t *tick) +void write_data_to_flash(uint32_t *tick, myPayload_t * currMeasures) { - uint32_t retVal=0; uint8_t page_num=0; if (g_MyDataIdx==0) { @@ -168,26 +183,29 @@ connectionUpdate(eStartAdvertising); // time and date used to initialize the g_MyData variable memcpy(&g_MyData.startData.datetime,&g_myDateTimeVar.currentDateTime, sizeof(mdate_time_t)); - memcpy(&g_MyData.startData.data,&g_currMeasures, sizeof(myPayload_t)); + memcpy(&g_MyData.startData.data,currMeasures, sizeof(myPayload_t)); } else { // it should be logged here the time difference from last record... g_MyData.myData[g_MyDataIdx-1].minutes = (uint16_t)(*tick*gTimeInstant / 60); g_MyData.myData[g_MyDataIdx-1].seconds = (*tick*gTimeInstant% 60); - memcpy(&g_MyData.myData[g_MyDataIdx-1].data,&g_currMeasures, sizeof(myPayload_t)); + memcpy(&g_MyData.myData[g_MyDataIdx-1].data,currMeasures, sizeof(myPayload_t)); } *tick = 0; - if (g_MyDataIdx==(MAXBUFFER-5)) { + if (g_MyDataIdx==(MAXBUFFER-3)) { //initiate disconnection connectionUpdate(eDisconnect); } if (g_MyDataIdx == MAXBUFFER) { // write2Flash the current page num + //connectionUpdate(eDisconnect); + page_num=flash_currPage(); // write2Flash the current page data - retVal=ble_flash_page_write(page_num, (uint32_t*)&(g_MyData), 251u); - pc.printf("retValWr: %d, Pg:%d, Min: %d \r\n",retVal, page_num,g_myDateTimeVar.currentDateTime.minutes); + ble_flash_page_write(page_num, (uint32_t*)&(g_MyData), 253); + memset(&g_MyData,0,sizeof(myDataLog_t)); + //pc.printf("retValWr: %d, Pg:%d, Min: %d \r\n",retVal, page_num,g_myDateTimeVar.currentDateTime.minutes); flash_go_nextPage(); } g_MyDataIdx = (g_MyDataIdx+1)%(MAXBUFFER+1); @@ -197,10 +215,9 @@ void on_error_radioMsg() { char myBuf[TXRX_BUF_LEN]; - sprintf(myBuf,"%s","WrongSyntax"); - buzz_int(&buzzer,5,3); - timeout_err.attach(&at_timeout_err, 2); + //buzz_int(&buzzer,5,3); + //timeout_err.attach(&at_timeout_err, 2); sendRadioMsg((uint8_t*)&myBuf[0], 12); } @@ -212,92 +229,184 @@ uint8_t i; p_curr_addr += 2; // skip the magic number and the word count - memcpy((uint32_t*)&initialData, p_curr_addr, 6*sizeof(uint32_t)); + memcpy((uint32_t*)&initialData, p_curr_addr, 3*sizeof(uint32_t)); pdate = &initialData.startData.datetime; pc.printf("20%2d_%2d_%2d H:%2d P:%4x\r",pdate->year, pdate->month, pdate->day, pdate->hours, p_curr_addr); - pc.printf("%2d:%2d;%3d;%3d;%3d \r",pdate->minutes, pdate->seconds, initialData.startData.data.light, initialData.startData.data.gndV, initialData.startData.data.temp); - pc.printf("%2d:%2d;%3d;%3d;%3d;%2d\r",initialData.myData.minutes, initialData.myData.seconds, initialData.myData.data.light, initialData.myData.data.gndV, initialData.myData.data.temp); - p_curr_addr += 6; + pc.printf("%2d:%2d;%3d;%2d:%2d;%3d;\r",pdate->minutes, pdate->seconds, initialData.startData.data.hits,initialData.myData.minutes, initialData.myData.seconds, initialData.myData.data.hits); + p_curr_addr += 3; - for (i=0; i<49; i++) { - memcpy((uint32_t*)&dataOut, p_curr_addr, 5*sizeof(uint32_t)); - pc.printf("%2d:%2d;%3d;%3d;%3d;%2d\r",dataOut[0].minutes, dataOut[0].seconds, dataOut[0].data.light, dataOut[0].data.gndV, dataOut[0].data.temp, i); - pc.printf("%2d:%2d;%3d;%3d;%3d\r",dataOut[1].minutes, dataOut[1].seconds, dataOut[1].data.light, dataOut[1].data.gndV, dataOut[1].data.temp); - p_curr_addr += 5; + for (i=0; i<125; i++) { + memcpy((uint32_t*)&dataOut, p_curr_addr, 2*sizeof(uint32_t)); + pc.printf("%2d:%2d;%3d;%2d:%2d;%3d;\r",dataOut[0].minutes, dataOut[0].seconds, dataOut[0].data.hits,dataOut[1].minutes, dataOut[1].seconds, dataOut[1].data.hits); + p_curr_addr += 2; } } -int update_measurements() -{ - int retVal; - static myPayload_t prevMeasures=(myPayload_t) { - 0, 0, 0, 0, 0 - }; +int sign(int nr){ + int retVal=0; + if (nr>0) retVal=1; + else if (nr<0) retVal=-1; + + return retVal; +} - g_currMeasures = (myPayload_t) { - VP[0]->read_u16(), VP[2]->read_u16(), VP[1]->read_u16(), led, 0 - }; - retVal = memcmp(&g_currMeasures,&prevMeasures,sizeof(myPayload_t)); - memcpy(&prevMeasures,&g_currMeasures,sizeof(myPayload_t)); - return retVal; +float read_real_value(void){ + uint32_t wrk,reg0,reg1,reg2; + reg0 = NRF_ADC->ENABLE; // save register value + reg1 = NRF_ADC->CONFIG; // save register value + reg2 = NRF_ADC->RESULT; // save register value + NRF_ADC->ENABLE = ADC_ENABLE_ENABLE_Enabled; + NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) | + (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | + (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | + (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | + (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); + NRF_ADC->EVENTS_END = 0; + NRF_ADC->TASKS_START = 1; + while (!NRF_ADC->EVENTS_END) {;} + wrk = NRF_ADC->RESULT; // 10 bit result + NRF_ADC->ENABLE = reg0; // recover register value + NRF_ADC->CONFIG = reg1; // recover register value + NRF_ADC->RESULT = reg2; // recover register value + NRF_ADC->EVENTS_END = 0; + return ((float)wrk / 1024 * 1.2 * 3.0); } +uint8_t read100(void) +{ + float wrk_vdd, v0p= 2.7 ,v100p= 3.3; + wrk_vdd = read_real_value(); + if (wrk_vdd <= v0p){ + return 0; + } else if (wrk_vdd >= v100p){ + led = 0; + return 100; + + } + led = 1; + wrk_vdd = (wrk_vdd - v0p) / (v100p - v0p); + return (uint8_t)(wrk_vdd * 100); +} + + void at_eachInstant() { static uint32_t tick=0; - int retVal; + static uint8_t ltick=0; + uint8_t rcBuff[2]={'r','0'}; + //static myPayload_t prevMeasures2=(myPayload_t) { 0, 0, 0, 0}; // update time update_time(&g_myDateTimeVar, gTimeInstant); - + + //batteryService->updateBatteryLevel(gBatteryValue ); //update measurements - retVal = update_measurements(); - + if (g_LogActive==true){ // if there are changes in data save - if ((retVal!=0)&&(g_LogActive==true)) { - write_data_to_flash(&tick); + if (0!=g_currMeasures2.hits){ + write_data_to_flash(&tick, &g_currMeasures2); + } + if (g_bIsConnected == true){ + // print info + rcBuff[1]='0'+ltick; + decode(rcBuff,2); + ltick =(ltick+1)%4; } tick++; + + } +} + +void update_measurements() +{ + nr_of_hits ++; + g_nrOfHits ++; + /*if (bNewSample==false){ + //g_currMeasures2.hits =nr_of_hits; + nr_of_hits = 1; + bNewSample = true; + } */ + + //gBatteryValue = read100(); + +} + +void at_eachADC(){ + static bool isLow = false; + static bool bPrevState=isLow; + static uint8_t counter = 0; + static float measure; + + + measure = photoVoltage.read(); + if (g_LogActive==false){ + if (measure > gmaxV) { + maxV=measure; + } + if (measure < minV) { + gminV=measure; + } + //gmedian = gminV + (gminV + gmaxV)*10/4; % 25% + } + + isLow = (measure < gmedianV); + if ((bPrevState != isLow)&&(isLow == true)){ + nr_of_hits ++; + g_nrOfHits ++; + } + bPrevState = isLow; + + counter = (counter + 1)%100; + if (counter==0){ + // 1 second is passed + bTimeReady=true; + g_currMeasures2.hits =nr_of_hits; + nr_of_hits = 0; + } } // Radio commands decode void decode(uint8_t * buffer, uint16_t length) { + char myBuf[TXRX_BUF_LEN]; uint16_t len; - char myBuf[TXRX_BUF_LEN]; - switch (buffer[0]) { - case 'i': {// Analog Input Read Request + case 'r': {// Read Operations switch (buffer[1]) { - case '0': { - // display all inputs - sprintf(myBuf,"All:%3d,%3d,%3d,%2d", g_currMeasures.light,g_currMeasures.gndV,g_currMeasures.temp,g_currMeasures.led_on); + case '0':{ + sprintf(myBuf,"NH:%2d,TNH:%8d\r\n", g_currMeasures2.hits,g_nrOfHits); + len = 19; + break; + } + case '1':{ + sprintf(myBuf,"P:%1.2f_%1.2f_%1.2f\r\n", gmaxV,photoVoltage.read(),gminV); len = 18; break; } - case '1': { - sprintf(myBuf,"Input 1 = %3d", g_currMeasures.light); - len = 13; - break; - } - case '2': { - sprintf(myBuf,"Input 2 = %3d", g_currMeasures.gndV); - len = 13; - break; + case '2':{ + sprintf(myBuf,"g_idx=%2d Page=%3d",g_MyDataIdx, flash_currPage()); + len = 18; + break; } case '3': { - sprintf(myBuf,"Input 3 = %3d", g_currMeasures.temp); - len = 13; + sprintf(myBuf,"V:%2.3f\r\n", read_real_value()); + len = 11; break; } case '4': { - sprintf(myBuf,"Input 4 = %2d", g_currMeasures.led_on); - len = 12; + sprintf(myBuf,"PhVol:%2.2f,Av:%2.2f\r\n",photoVoltage.read(),gmedian); + len = 20; break; } - default: { - sprintf(myBuf,"All:%3d,%3d,%3d,%2d", g_currMeasures.light,g_currMeasures.gndV,g_currMeasures.temp,g_currMeasures.led_on); - len = 18; + case '5': { + //sprintf(myBuf,"Vmx_mn:%1.2f_%1.2f\r\n",maxV,minV); + //len = 18; + break; + } + + default:{ + sprintf(myBuf,"Nothing \r\n"); + len = 10; break; } } @@ -307,26 +416,27 @@ case 'l': {// toogle led led = ! led; if (led==0) { - sprintf(myBuf,"%s","ON"); + sprintf(myBuf,"%s","OFF"); len = 2; } else { - sprintf(myBuf,"%s","OFF"); + sprintf(myBuf,"%s","ON"); len = 3; } sendRadioMsg((uint8_t *)myBuf, len); break; - } - case 's': {// buzzer - if (((buffer[1]>'9')||(buffer[1]<'0'))||((buffer[2]>'9')||(buffer[2]<'0'))) { - MyASSERT(true,&pc, buffer[1]); // notify on serial interface - on_error_radioMsg(); // notify on radio - break; - } else { - buzz_int(&buzzer, (buffer[1]-'0'),(buffer[2]-'0')); - sprintf(myBuf,"%s:%f","S",buzzer.read()); - len= 7; + } + case 'i':{ // Insert data values + switch (buffer[1]){ + case 'm':{ // median change + memcpy(myBuf,&buffer[2],2); + gmedian=(float)atoi(myBuf)/10; // TODO check if it is a number + break; + } + + default: { + // on_error_radioMsg(); // notify on radio + } } - sendRadioMsg((uint8_t *)myBuf, len); break; } case 'd': @@ -385,13 +495,25 @@ len = 12; sendRadioMsg((uint8_t *)myBuf, len); g_LogActive = true; + break; } case '3': { // stop measuring + doMeasures.detach(); sprintf(myBuf,"Stop Meas"); len = 11; sendRadioMsg((uint8_t *)myBuf, len); g_LogActive = false; + ble_flash_page_write(flash_currPage(), (uint32_t*)&(g_MyData), 253); + memset(&g_MyData,0,sizeof(myDataLog_t)); + flash_go_nextPage(); + break; + } + case '4':{ + break; + } + case '5':{ // read one measure + break; } default: { @@ -513,29 +635,6 @@ } } -void button() -{ - uint8_t buf[TXRX_BUF_LEN+1]; - buf[0]='B'; - buf[1]='U'; - buf[2]='T'; - buf[3]='N'; - - ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 4); - g_bConnDisabled = !g_bConnDisabled; - led = !led; - if (g_bConnDisabled == true){ - if (g_bIsConnected == true){ - ble.disconnect((Gap::DisconnectionReason_t)0x12); - g_bIsConnected = false; - } else if (g_bIsAdvertising == true) { - ble.stopAdvertising(); - g_bIsAdvertising = false; - } - } else { - connectionUpdate(eStartAdvertising); - } -} void g_varInit() { @@ -548,20 +647,24 @@ { ble.init(); g_varInit(); + led= 1; + ble.onDisconnection(disconnectionCallback); ble.onConnection(connectionCallback); ble.onDataWritten(WrittenHandler); - event.rise(&button); - + //event.rise(&button); + + + //event.rise(&accInt1); pc.baud(19200); pc.printf("SimpleChat Init \r\n"); - + pc.attach( uartCB , pc.RxIrq); // setup advertising ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, - (const uint8_t *)"MyBleVT", sizeof("MyBleVT") - 1); + (const uint8_t *)"CntBle3", sizeof("CntBle3") - 1); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); //ble.accumulateAdvertisingPayload(GapAdvertisingData::TX_POWER_LEVEL,(const uint8_t *)txPower, sizeof(txPower)); @@ -576,10 +679,17 @@ //ble.onRadioNotification(ptrFunc); */ ble.addService(uartService); + //batteryService = new BatteryService(ble, batteryLevel); ble.startAdvertising(); - pc.printf("Advertising Start \r\n"); - periodicActions.attach(&at_eachInstant,gTimeInstant); + pc.printf("Advertising Start \r\n"); + //periodicActions.attach(&at_eachInstant,gTimeInstant); + doMeasures.attach_us(&at_eachADC,10000); + //gBatteryValue = read100(); while(1) { ble.waitForEvent(); + if (bTimeReady==true){ + bTimeReady=false; + at_eachInstant(); + } } }
diff -r baafa4f7a15e -r 27e9c3db28b9 myData.h --- a/myData.h Fri May 06 18:14:18 2016 +0000 +++ b/myData.h Sun Aug 14 14:57:09 2016 +0000 @@ -1,11 +1,12 @@ #include "ble_flash.h" #include "mbed.h" -#define MAXBUFFER 99u //(((BLE_FLASH_PAGE_SIZE - 4 - sizeof(myData_t))/sizeof(myDataL_t)) -#define MAX_WORD_PAGE_DATA 254u // to be computed as round_next_int(MAXBUFFER*sizeof(myDataL_t)+sizeof(myData_t)) +//#define MAXBUFFER 99u //(((BLE_FLASH_PAGE_SIZE - 4 - sizeof(myData_t))/sizeof(myDataL_t)) +#define MAXBUFFER 251u //(((BLE_FLASH_PAGE_SIZE - 4 - sizeof(myData_t))/sizeof(myDataL_t)) +//#define MAX_WORD_PAGE_DATA 254u // to be computed as round_next_int(MAXBUFFER*sizeof(myDataL_t)+sizeof(myData_t)) #define MAX_PAGE_NUM (NRF_FICR->CODESIZE - 1) // 255 -#define MIN_PAGE_NUM 155u +#define MIN_PAGE_NUM 151u typedef struct{ @@ -32,11 +33,7 @@ } connection_update_t; typedef struct { - uint16_t light; - uint16_t gndV; - uint16_t temp; - bool led_on; - bool led2_on; + uint16_t hits; } myPayload_t; @@ -48,7 +45,7 @@ typedef struct { mdate_time_t datetime; // 6 bytes length - myPayload_t data; // 8 bytes lenth + myPayload_t data; // 2 bytes lenth } myData_t; typedef struct { @@ -57,8 +54,8 @@ } myDataLog_t; typedef struct { - myData_t startData; - myDataL_t myData; + myData_t startData; // 8 bytes + myDataL_t myData; // 4 bytes } myDataLogShort_t; /* Return current page to be written in Flash */
diff -r baafa4f7a15e -r 27e9c3db28b9 myFunctions.cpp --- a/myFunctions.cpp Fri May 06 18:14:18 2016 +0000 +++ b/myFunctions.cpp Sun Aug 14 14:57:09 2016 +0000 @@ -65,9 +65,15 @@ } myDateTimeVar->newDateTime.hours = (myDateTimeVar->currentDateTime.hours + (tseconds / 3600+myDateTimeVar->newDateTime.minutes/60))%24; if (myDateTimeVar->newDateTime.hours < myDateTimeVar->currentDateTime.hours){ - myDateTimeVar->newDateTime.day = (myDateTimeVar->currentDateTime.day + 1)%(eNrDaysPerMonth[myDateTimeVar->currentDateTime.month+1]+1); + myDateTimeVar->newDateTime.day = (myDateTimeVar->currentDateTime.day + 1)%(eNrDaysPerMonth[myDateTimeVar->currentDateTime.month-1]+1); + if (myDateTimeVar->newDateTime.day == 0){ + myDateTimeVar->newDateTime.day ++; + } if (myDateTimeVar->newDateTime.day < myDateTimeVar->currentDateTime.day ){ - myDateTimeVar->newDateTime.month = (myDateTimeVar->currentDateTime.month+ 1)%13+1; + myDateTimeVar->newDateTime.month = (myDateTimeVar->currentDateTime.month+ 1)%13; + if (myDateTimeVar->newDateTime.month ==0){ + myDateTimeVar->newDateTime.month++; + } if (myDateTimeVar->newDateTime.month< myDateTimeVar->currentDateTime.month){ myDateTimeVar->newDateTime.year = (myDateTimeVar->currentDateTime.year+ 1); }
diff -r baafa4f7a15e -r 27e9c3db28b9 read_me.md --- a/read_me.md Fri May 06 18:14:18 2016 +0000 +++ b/read_me.md Sun Aug 14 14:57:09 2016 +0000 @@ -1,67 +1,77 @@ -# nRF51822_SimpleChat Extended With Data Measurements & Logger -This repo is derived from the initial SimpleChat Application and is build for BLE Nano Device, used in peripheral mode. -This Application scan and measures some inputs and save them in flash with timestamp. A RTC has been implemented. -Using chat application (available for smart phones) some commands can be send to BLE Nano in order to perform some tasks. +# nRF51822_SimpleChat Extended With Data Measurements & Logger for Power Consumption Monitoring +This repo is derived from the initial SimpleChat Application and it is build for BLE Nano Device, used in peripheral mode. +The aim of this app is to count the flashing red LED of a power consumption meter. +This Application scan every 10 msec and measures AI port A5 to which a photoresistor is connected. Each 1 second the system save the collected data +(if there is something to save) into flash with timestamp added. A RTC has been implemented. +Using chat application (available for smart phones) some commands can be send to BLE Nano in order to perform some tasks. Also some real time measurement +are displayed on the phone app. Other types of commands can be send via serial interface from a PC, especially data extraction from flash. #Usage/Description: After flashing or power on, the ble device will start to advertise and also its local time counter starts. While initializing, application will search through all flash pages, last data previosly written. From this pages will retrieve the last data and time available. A Timer Tick (period set to 1 second) is used for updating RTC and for data measurements. When the application starts the data will not be saved -in flash until a command is given to start (xf2). When started, it is recommended after connecting to ble device to set the time and date; -After the date and time updated, activate measurement logging (xf2). +in flash until a command is given to start (xf2 + enter). When started, it is recommended after connecting to ble device to set the time and date; +After the date and time updated, activate measurement logging (command: xf2 +enter). -Please be aware that for 5-6 seconds, each 100 seconds the connection is stopped by the application. This is due to fact that -writing to flash is safe when radio connection is off. When g_MyDataIdx is between 95..100, connection should remain closed. +Please be aware that from time to time the connection is stopped by the application in order to perform a flash writing procedure. +This is due to fact that writing to flash is safe when radio connection is off. When g_MyDataIdx is between 248..251, connection should remain closed. The Application can use somewhere around 100 flash pages (100K). This means, if data is sampled each second, the pages will start -to overwrite after 100*100 seconds = 2.7 hours. Anyway the data is inserted into logger when it is different compared to previous -measurement. Therefore the time the flash memory can log is bigger than 2.7 hours. +to overwrite after 251*104 seconds = 7.25 hours. Anyway the data is inserted into logger if there is a measurement different than 0. +Therefore the time the flash memory can log is bigger than 7.25 hours. When retrieving the data log via serial, the format is as follows: -> page starts with: 2016_ 4_22 H: 7 => year, month, day, and hour -26:51;363; 1; 15 => minutes: seconds; A3,A4,A5 (no info about digital inputs) +26:51;10;=> minutes: seconds; A5 (no info about digital inputs) -> following rows up to the next page: -0: 5;364; 1; 15 => minutes: seconds - time delta (time elapsed since last measurement); A3, A4, A5; +0: 5;12; => minutes: seconds - time delta (time elapsed since last measurement); A5; #Hardware Connections The BLE Nano Device is connected to some peripheral components as follows: -* Light Sensor ( A3 input) -* Temperature Sensor (A4 input) -* Gnd Voltage (or anything else) (A5 input) -* Buzzer (D6 output) -* Push Button (D5 input) +* Light Photo Sensor ( A5 input). This sensor must be placed on the top of the LED that it monitors, and must be completely covered + (to avoid other light source disturbances) [*] Optional: MKUSB 20 Board (for Voltage Supply and for Serial Communication with a PC) # Supported Commands From RedBear Chat Application following messages are valid: * Any message that not start with 'x' is echoed with 'R:' as prefix, and also is send via Serial Interface to PC * Any message that start with 'x' is interpreted as command: - * xi0 / xi* except {1,2,3,4} => prints the analog inputs and one digital input (the led status) as uint16 values - * xi1 /xi2 / xi3 / xi4 =>prints the uint16 value for light, temp, gndV, led status - * xl => toggle led status - * xs[0-9][0-9] => buzzer; depending on digits, a digital pwm signal is send to buzzer; to deactivate: xs00 + * xr* * = {0-7} => prints some data parameters or data measurements as follows: + * xr0 ->NH (last number of hits per second) and TNH (total number of hits, from the moment of Power On) + * xr1 ->maxV,photoVoltage.read(),minV : Highest Voltage measured on AI5, Current Measurement of AI5 and the lowest reading. + (maximum and minimum are computed at the start of BLE, until the xf2 command is given (start of collecting data). + These values are used to set the median Voltage used to identify a transition from low to high and vice-versa. + * xr2 -> prints the value of g_MyDataIdx (a value now between 0..251) representing the steps until a new page will be written + in flash with new data; It also print the current flash page that will be written (between 151 and 255) + * xr3 -> prints the voltage of the RedBear power supply (if it is on battery it will be a lower value than 3.3V). USefull to see how + discharged the battery is; + * xr4 -> prints the voltage of the AI5 (current photoresistor drop voltage) and the median voltage used to counter the transition from + low to high and vice-versa; + * xl => toggle led status (LED1, on the bottom side of the BLE Nano) * xtg => returns the current time * xti[0-9]{6} => insert time - exp: xti181004 means: 18H10M and 4 seconds * xdg => returns the current date * xdi[0-9]{6} => insert date - exp: xti160424 means: 2016 Y, 04 Month, 24 Day - * xf1 => prints the value of g_MyDataIdx (a value now between 0..99) representing the steps until a new page will be written - in flash with new data; It also print the current flash page that will be written (between 155 and 255) - * xf2 => activate measurements logging - * xf3 => deactivate measurements logging - * x* => for invalid syntax a sound for 3 seconds is activated, with a message error + * xf1 => prints the value of g_MyDataIdx (a value now between 0..251) representing the steps until a new page will be written + in flash with new data; It also print the current flash page that will be written (between 151 and 255) + * xf2 => activate measurements logging; + * xf3 => deactivate measurements logging and save the last data in the flash region; + + * xim => eg ximXY =>introduce a value for median Voltage. This value will be computed as median = XY/10; + From PC serial interface (with MKUSB 20 Board), following commands apply: * xf[0-9] => request printing on serial of a specific flash page ( 0 - current page; 1 - previous page, etc) * xd => request dump of all recorded logs (starting with current page) - * xg => prints the value of g_MyDataIdx (a value now between 0..99) representing the steps until a new page will be written + * xg => prints the value of g_MyDataIdx (a value now between 0..251) representing the steps until a new page will be written in flash with new data; * xca => open Radio Advertising (should be used when the Advertising is Off) * xcc => Close Radio connection (should be used when Connection is in Connected state) * xcs => Stop Radio Advertising (should be used when Advertising is active) Other Commands: - * button push => closes the connection/advertising and turn off the led. If push again reset the previous conditions; + - # References The mbed BLE API is meant to be used in projects on developer.mbed.org. Please see examples and sample project files there.