This program is designed to work with RedBearLab BLE Controller App - Chat App. Type something from the Terminal to send to the BLEController App or vice verse. Characteristics received from App will print on Terminal. This Program scans some analog and digital 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. Other types of commands can be send via serial interface from a PC, especially data extraction from flash.

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_SimpleChat_VT by Valentin Tanasa

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. 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).

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.

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.

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)
  • following rows up to the next page:
    0: 5;364; 1; 15 => minutes: seconds - time delta (time elapsed since last measurement); A3, A4, 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)
  • [Optional]: MKUSB 20 Board (for Voltage Supply and for Serial Communication with a PC)

Supported Commands

  1. 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
    • 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
  1. 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 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. A good starting point are these pages:

For this Application:

Committer:
tanasaro10
Date:
Fri May 06 18:14:18 2016 +0000
Revision:
11:baafa4f7a15e
Parent:
10:c7d53e4e0602
minor updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tanasaro10 9:303d3628986a 1 #include "myData.h"
tanasaro10 9:303d3628986a 2
tanasaro10 9:303d3628986a 3
tanasaro10 9:303d3628986a 4 uint8_t eNrDaysPerMonth[12]= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
tanasaro10 9:303d3628986a 5 static uint8_t g_currPage = MAX_PAGE_NUM; // current page in Flash to be write
tanasaro10 9:303d3628986a 6
tanasaro10 9:303d3628986a 7 uint8_t flash_currPage(){
tanasaro10 9:303d3628986a 8 return g_currPage;
tanasaro10 9:303d3628986a 9 }
tanasaro10 9:303d3628986a 10
tanasaro10 9:303d3628986a 11 uint8_t flash_go_nextPage(){
tanasaro10 9:303d3628986a 12 g_currPage --;
tanasaro10 9:303d3628986a 13 if (g_currPage < MIN_PAGE_NUM) {
tanasaro10 9:303d3628986a 14 g_currPage = MAX_PAGE_NUM;
tanasaro10 9:303d3628986a 15 }
tanasaro10 9:303d3628986a 16 return g_currPage;
tanasaro10 9:303d3628986a 17 }
tanasaro10 9:303d3628986a 18
tanasaro10 9:303d3628986a 19
tanasaro10 9:303d3628986a 20 uint8_t flash_prev_N_Page(uint8_t nr_of_pages){
tanasaro10 9:303d3628986a 21 uint8_t retVal;
tanasaro10 9:303d3628986a 22 retVal = g_currPage + nr_of_pages;
tanasaro10 9:303d3628986a 23
tanasaro10 9:303d3628986a 24 if (retVal > MAX_PAGE_NUM ) {
tanasaro10 9:303d3628986a 25 retVal = MIN_PAGE_NUM + (retVal % MAX_PAGE_NUM - 1);
tanasaro10 9:303d3628986a 26 }
tanasaro10 9:303d3628986a 27 return retVal;
tanasaro10 9:303d3628986a 28 }
tanasaro10 9:303d3628986a 29
tanasaro10 11:baafa4f7a15e 30 void search_latest_in_flash(mdate_time_t * outDateTime){
tanasaro10 11:baafa4f7a15e 31 uint8_t page_nr, sizeB,temp[6];
tanasaro10 9:303d3628986a 32 uint16_t max_page=(uint16_t)MAX_PAGE_NUM+1u;
tanasaro10 9:303d3628986a 33 int retVal;
tanasaro10 11:baafa4f7a15e 34 mdate_time_t max_datetime={16,1,1,0,0,0}, inv_datetime={255,255,255,255,255,255};
tanasaro10 9:303d3628986a 35
tanasaro10 11:baafa4f7a15e 36 sizeB=sizeof(mdate_time_t);
tanasaro10 9:303d3628986a 37 uint32_t* p_curr_addr;
tanasaro10 9:303d3628986a 38
tanasaro10 9:303d3628986a 39 for (page_nr = MAX_PAGE_NUM; page_nr>= MIN_PAGE_NUM; page_nr --){
tanasaro10 9:303d3628986a 40 p_curr_addr= (uint32_t *)((uint16_t)BLE_FLASH_PAGE_SIZE * page_nr);
tanasaro10 9:303d3628986a 41 p_curr_addr += 2; // skip the magic number and the word count
tanasaro10 11:baafa4f7a15e 42
tanasaro10 11:baafa4f7a15e 43 memcpy(temp, p_curr_addr, sizeB);
tanasaro10 11:baafa4f7a15e 44 retVal = memcmp(&temp, &inv_datetime, sizeB);
tanasaro10 9:303d3628986a 45 if (retVal!=0) {
tanasaro10 11:baafa4f7a15e 46 retVal = memcmp(&temp, &max_datetime, sizeB);
tanasaro10 11:baafa4f7a15e 47 if (retVal >0) {
tanasaro10 11:baafa4f7a15e 48 memcpy(&max_datetime, &temp, sizeB);
tanasaro10 11:baafa4f7a15e 49 max_page= page_nr;
tanasaro10 9:303d3628986a 50 }
tanasaro10 11:baafa4f7a15e 51 }
tanasaro10 9:303d3628986a 52 }
tanasaro10 9:303d3628986a 53
tanasaro10 11:baafa4f7a15e 54 memcpy(outDateTime, &max_datetime, sizeB);
tanasaro10 9:303d3628986a 55 g_currPage= (uint8_t)(max_page-1u);
tanasaro10 9:303d3628986a 56 }
tanasaro10 9:303d3628986a 57
tanasaro10 11:baafa4f7a15e 58 void update_time(mdatetime_manager_t* myDateTimeVar, uint16_t tseconds){
tanasaro10 11:baafa4f7a15e 59 //memcpy(&myDateTimeVar->newDateTime, &myDateTimeVar->currentDateTime, sizeof(mdate_time_t));
tanasaro10 11:baafa4f7a15e 60 if (myDateTimeVar->updateDateTime ==false){
tanasaro10 11:baafa4f7a15e 61 myDateTimeVar->newDateTime.seconds = (myDateTimeVar->currentDateTime.seconds + tseconds)% 60;
tanasaro10 11:baafa4f7a15e 62 myDateTimeVar->newDateTime.minutes = (myDateTimeVar->currentDateTime.minutes + ((tseconds + myDateTimeVar->currentDateTime.seconds) / 60))%60;
tanasaro10 11:baafa4f7a15e 63 if (myDateTimeVar->newDateTime.minutes< myDateTimeVar->currentDateTime.minutes ) {
tanasaro10 11:baafa4f7a15e 64 myDateTimeVar->currentDateTime.hours++;
tanasaro10 11:baafa4f7a15e 65 }
tanasaro10 11:baafa4f7a15e 66 myDateTimeVar->newDateTime.hours = (myDateTimeVar->currentDateTime.hours + (tseconds / 3600+myDateTimeVar->newDateTime.minutes/60))%24;
tanasaro10 11:baafa4f7a15e 67 if (myDateTimeVar->newDateTime.hours < myDateTimeVar->currentDateTime.hours){
tanasaro10 11:baafa4f7a15e 68 myDateTimeVar->newDateTime.day = (myDateTimeVar->currentDateTime.day + 1)%(eNrDaysPerMonth[myDateTimeVar->currentDateTime.month+1]+1);
tanasaro10 11:baafa4f7a15e 69 if (myDateTimeVar->newDateTime.day < myDateTimeVar->currentDateTime.day ){
tanasaro10 11:baafa4f7a15e 70 myDateTimeVar->newDateTime.month = (myDateTimeVar->currentDateTime.month+ 1)%13+1;
tanasaro10 11:baafa4f7a15e 71 if (myDateTimeVar->newDateTime.month< myDateTimeVar->currentDateTime.month){
tanasaro10 11:baafa4f7a15e 72 myDateTimeVar->newDateTime.year = (myDateTimeVar->currentDateTime.year+ 1);
tanasaro10 11:baafa4f7a15e 73 }
tanasaro10 11:baafa4f7a15e 74 }
tanasaro10 11:baafa4f7a15e 75 }
tanasaro10 11:baafa4f7a15e 76 } else {
tanasaro10 11:baafa4f7a15e 77 myDateTimeVar->updateDateTime =false;
tanasaro10 9:303d3628986a 78 }
tanasaro10 9:303d3628986a 79
tanasaro10 11:baafa4f7a15e 80 if (myDateTimeVar->updateDateTime ==true){ // there is a new Date ?
tanasaro10 11:baafa4f7a15e 81 myDateTimeVar->updateDateTime =true;
tanasaro10 9:303d3628986a 82 }
tanasaro10 11:baafa4f7a15e 83 memcpy(&myDateTimeVar->currentDateTime,&myDateTimeVar->newDateTime, sizeof(mdate_time_t));
tanasaro10 9:303d3628986a 84 }
tanasaro10 9:303d3628986a 85
tanasaro10 9:303d3628986a 86
tanasaro10 9:303d3628986a 87 int buzz_int(PwmOut* buzzer, uint8_t period, uint8_t duty_cycle){
tanasaro10 9:303d3628986a 88 int retVal = 0;
tanasaro10 9:303d3628986a 89 if ((duty_cycle<10)&&(period<10)){
tanasaro10 9:303d3628986a 90 if (period!=0) {
tanasaro10 9:303d3628986a 91 buzzer->period_ms(period);
tanasaro10 9:303d3628986a 92 *buzzer = (10.0 - (float)duty_cycle)/9.0;
tanasaro10 9:303d3628986a 93 } else {
tanasaro10 9:303d3628986a 94 *buzzer = 0;
tanasaro10 9:303d3628986a 95 }
tanasaro10 9:303d3628986a 96 } else {
tanasaro10 9:303d3628986a 97 retVal=-1;
tanasaro10 9:303d3628986a 98 }
tanasaro10 9:303d3628986a 99 return retVal;
tanasaro10 9:303d3628986a 100 }
tanasaro10 9:303d3628986a 101
tanasaro10 9:303d3628986a 102 void assert_error_app(bool condition, Serial *pc, uint16_t error, uint16_t line, const char* file){
tanasaro10 9:303d3628986a 103 if (condition) {
tanasaro10 9:303d3628986a 104 pc->printf("App err = %d, line = %d, file = %s\r\n",error, line, file);
tanasaro10 9:303d3628986a 105 }
tanasaro10 9:303d3628986a 106 }