application: add Bluetooth commands to list, remove, and dump log files

This commit is contained in:
King Kévin 2017-08-19 13:13:56 +02:00
parent 18934af006
commit 52c6a5ab3c
2 changed files with 191 additions and 90 deletions

204
README.md
View File

@ -1,4 +1,4 @@
The forumslogger (or forumslader-logger) is a small man-in-the-middle board for logging [forumslader automatiklader](http://www.forumslader.de/automatiklader/) data.
The forumslogger (or Forumslader-logger) is a small man-in-the-middle board for logging [Forumslader automatiklader](http://www.forumslader.de/automatiklader/) data.
project
=======
@ -6,34 +6,53 @@ project
summary
-------
The [forumslader](http://www.forumslader.de/) [automatiklader](http://www.forumslader.de/automatiklader/) is a power manager for bicycles.
The [Forumslader](http://www.forumslader.de/) [Automatiklader](http://www.forumslader.de/automatiklader/) is a power manager for bicycles.
It used the power generated by a dynamo to charge Li-Ion battery and can also provide 12V (for lights) and 5V (to charge phones over USB).
The automatiklader can also be enhanced with a [bluetooth module](http://www.forumslader.de/automatiklader/erweiterungen/bluetooth/), allowing a phone to log precise speed (using the dynamo activity), battery charge, and other values using the [forumslader Andoird app](https://play.google.com/store/apps/details?id=com.duringsoft.forumslader).
The Automatiklader can also be enhanced with a [bluetooth module](http://www.forumslader.de/automatiklader/erweiterungen/bluetooth/), allowing a phone to log precise speed (using the dynamo activity), battery charge, and other values using the [forumslader Andoird app](https://play.google.com/store/apps/details?id=com.duringsoft.forumslader).
Sometimes you don't have your phone around to log the data though, but you still want the data to be recorded.
This project covers this case, always logging the forumslader data no matter if a phone is present or not.
Sometimes you don't have your phone around to log the data though, but you still want the data to be recorded for later evaluation.
This project covers this case, always logging the Forumslader data no matter if a phone is present or not.
technology
----------
The phone connects to the forumslader using Bluetooth.
The phone connects to the Forumslader using Bluetooth.
The app uses the Bluetooth Serial Port Profile (SPP) to transfer data (see protocol section for an example of data sent by the forumslader).
The forumslader board uses an off-the-shelf bluetooth module.
The Forumslader board uses an off-the-shelf Bluetooth module.
Thus instead of using Bluetooth to get the forumslader data (like the phone would do), we will remove the forumlader Bluetooth module, connect our logger board directly to the underlying serial port, and our board will have a Bluetooth module so to forward the data again to the phone.
Since our board sits between the forumslader and phone it can log the data all the time.
Thus instead of using Bluetooth to get the Forumslader data (like the phone would do), we remove the Forumslader Bluetooth module, connect our logger board directly to the underlying serial port, and our board will have a Bluetooth module so to forward the data again to the phone.
Since our board sits between the Forumslader and phone it can log the data all the time.
The logged data is saved onto a micro-SD card for virtually unlimited storage space.
Each time the forumslader wakes up and starts sending data a new file containing the logged data is created.
Each time the Forumslader wakes up and starts sending data a new file with the current date: `forumslogger_YYYY-MM-DD_hh:mm:ss.txt`.
The internal Real Time Clock (RTC) is used to provide date and time, which is used in the file name of the logged data.
In addition to the forumslader a GPS module is added.
In addition to the Forumslader a GPS module is added.
This allows to automatically set the correct time in the RTC (once the GPS has a fix).
The GPS data is also logged (into the same file).
This allows to add a timestamp to the forumslader data (every second the GPS sends the UTC time) and log the actual route cycled.
While GPS also provides speed, the forumslader data based on the dynamo is much more precise (similar applies to the distance).
The GPS data is also logged (in the same file).
This also adds timestamps to the Forumslader data (every second the GPS sends the UTC time) and log the actual route cycled.
While GPS also provides speed, the Forumslader data based on the dynamo is much more precise (similar applies to the distance).
On top of the forumslader commands, the logger provides additional commands to list, retrieve, and delete the logged data files.
On top of the Forumslader commands, the logger provides additional commands to list, retrieve, and delete the logged data files (starting with `$FLL,`, for Forums-Lader-Logger).
* `$FLL,LS;`: will list the log files. Example
<pre>
$FLL,LS,BOL;
$FLL,LS,forumslogger_2017-08-19_04-30-41.txt;
$FLL,LS,forumslogger_2017-08-17_10-11-55.txt;
$FLL,LS,EOL;
</pre>
* to remove a file, use `$FLL,RM,forumslogger_YYYY-MM-DD_hh:mm:ss.txt;`. This will return `$FLL,RM,OK;` or `$FLL,RM,ERROR;`
* to retrieve/dump a log file, use `$FLL,CAT,forumslogger_YYYY-MM-DD_hh:mm:ss.txt;`. Example:
<pre>
$FLL,CAT,forumslogger_2017-08-19_02-55-16.txt;
$FLL,CAT,BOF;
$GPTXT,01,01,02,u-blox ag - www.u-blox.com*50
$GPTXT,01,01,02,HW UBX-G60xx 00040007 FF7FFFFFp*53
$GPTXT,01,01,02,ROM CORE 7.03 (45969) Mar 17 2011 16:18:34*59
$GPTXT,01,01,02,ANTSTATUS=DONTKNOW*33
...
$FLL,CAT,EOF;
</pre>
bluetooth
---------
@ -58,72 +77,119 @@ Now connect to Bluetooth module to the forumlogger.
When powered the module will rapidly blink (in normal mode).
Once paired and connected the module will double blink every 3 seconds.
state
-----
working:
- SD card read and write
- file read and write (using fatfs)
- GPS data parsing
- RTC date and time synchronisation to GPS time
- forward Bluetooth <-> Forumslader data
- log GPS, Bluetooth, and Forumslader data on SD card (in file `forumslogger_YYYY-%MM-DD_hh-mm-ss.txt`)
TODO:
- provide Bluetooth commands to list, retrieve, and delete files
The forumslader data does not include timestamps.
The data is just sent periodically.
This periodicity is used to calculated the current speed, also using the provided number of dynamo steps recorded.
Thus providing the logged data all at once to the forumslader app will lead to erroneous speed data.
The other information will remain consistent (e.g. distance, battery status, ...) since only the last values are used (this information is not dependent on the periodicity).
protocol
--------
The forumslader data is NMEA-0183 formatted (without checksum).
The app sends
$FLT,5;
TODO check what device responds
- 4: when switching to about tab (probably asking for $FLV)
- 5: when switching to statistics tab (probably asking for $FL5)
- 6: reset tour counter
- 7: reset day counter
- 9: next parameters sets wheel circumference in mm and pole pairs
- 10: next parameter sets the altitude offset in .1 m
The device sends periodically data (every 50-100 ms?):
$FL5,00c000,0,0,4130,4133,4135,-26,0,305,1,219,200,6750,14077;
$FLB,850,101652,219,0;
$FL5,00c000,0,0,4130,4133,4133,-15,0,305,1,218,200,6750,14077;
$FLC,4,258,269,8,9,0;
$FL5,00c000,0,0,4131,4133,4135,-18,0,305,1,217,200,6750,14077;
$FLB,850,101651,219,0;
$FL5,00c000,0,0,4130,4133,4135,-26,0,305,1,216,200,6750,14077;
$FLC,5,66086,100,718,28,31897091;
$FL5,00c000,1,3,4130,4133,4135,-25,0,305,1,240,211,6750,14077;
$FLB,850,101649,221,0;
$FL5,00c000,1,17,4130,4133,4133,-28,0,306,1,240,228,6750,14077;
$FLC,0,16198,49,200,2146,76;
$FL5,00c000,1,17,4130,4131,4133,-29,0,307,1,240,245,6750,14077;
The Forumslader sends
$FLV,500290515,5.01;
provides the versions
50029051 is the firmware of the FL/Forumlader (shown is About in Forumslader App)
5.01 is the firmware of the BT/Bluetooth (shown is About in Forumslader App)
$FLB,850,101652,219,0;
850 is ???
101652 is the pressure in centi-hectopascal
219 is the altitude in dm
0 is the gradient in decidegree
provides measurements
850 is the temperature in .1 C (799 is the maximum valid/displayed value)
101652 is the pressure in .01 hP
219 is the altitude in .1 m
0 is the gradient in .1 %
$FLC,4,258,269,8,9,0;
???
$FLC,0,11360,102,200,2965,79;
provides tour data
0 indicates the following data is for the tour
11360 is the climbed altitude in .1 m
102 is the maximum gradient in .1 %
200 is the maximum temperature in .1 C
2965 is the maximum altitude in .1 m
79 is the maximum speed
$FL5,00c000,0,0,4130,4133,4135,-24,0,305,1,232,260,6750,14077;
00c000 is the status (hex, seperate tab)
the 1st 0 is the switching step in steps
the 2nd 0 is the speed in 0.6 km/h steps
4130 is the cell 1 voltage in mV
4133 is the cell 2 voltage in mV
4135 is the cell 3 voltage in mV
-24 is the accu current in mA
the 3rd 0 is the consumer power in 0.0124 Watt steps (dependant on another parameter)
1 is the consumer current in mA
232 is the temperature in F
260 is the micropulsecounter
6750 is the pulsecounter
$FLC,1,8670,69,200,2965,79;
provides day data
1 indicates the following data is for the day
8670 is the climbed altitude in .1 m
69 is the maximum gradient in .1 %
200 is the maximum temperature in .1 C
2965 is the maximum altitude in .1 m
79 is the maximum speed
$FLC,2,211888,-138,200,-138,200;
provides total, tour, and day data
2 indicates the following data is for the total, tour, and day
211888 is the total climbed altitude in .1 m
-138 is the minimum tour gradient in .1 %
200 is the minimum tour temperature in .1 C
-138 is the minimum day gradient in .1 %
200 is the minimum day temperature in .1 C
$FLC,3,4846,258,174,896,0;
provides energy data
3 indicates the following data is for the energy
4846 is the total FL energy in .1 Wh
258 is the tour FL energy in .1 Wh
174 is the day FL energy in .1 Wh
896 is the raw data FL_SAVEDATABT
0 is ??? (it doesn't show up in the statistics)
$FLC,4,232,226,9,10,0;
provides tour, and day data
4 indicates the following data is for the tour, and day
232 is the average day speed
226 is the average tour speed
9 is the average day gradient in .1 %
10 is the average tour gradient in .1 %
0 is ??? (it doesn't show up in the statistics)
$FLC,5,66262,70,718,31,21673785;
provides measurements
5 indicates the following data is for measurements
66262 is the FL_STARTCOUNTER
70 is the FL5 Acuu state of charge in %
718 is the Full charge capacity in mAh
31 is the Charge cycle count
21673785 is the Coulomb-counter
$FL5,00e000,1,13,3833,3837,3837,-22,0,301,1,240,1438,7862,16629;
provides global information
00e000 is the FL_STATUS in hex (switch to status tab to get meaning of bits)
1 is the FL switching step
13 is the Impluse in Hz
3833 is the Cell1 voltage in mV
3837 is the Cell2 voltage in mV
3837 is the Cell3 voltage in mV
-22 is the Accu current in mA
0 is the Consumer current in mA
301 is the FL temperature in F
1 is the FL_PROGCONSUMER (0: Auto, 1: On, 2: Off, 3: Power)
240 is ???
1438 is the FL_MICROPULSECOUNTER (used to calculate the distance)
7862 is the FL_PULSECOUNTER (used to calculate the distance)
16629 is the FL_TIMECOUNTER in minutes since FL start (used in Total. Tour and day are calculated using the offsets)
$FLP,2250,14,490,7343,15421,7236,15138,377,30;
provided the stored values
2250 is the Wheel circumference in mm (FL_WHEELSIZEBT)
14 is the pole pairs (FL_PLOESBT)
490 is the altitude offset in .1 m (FL_ALTITTUDEOFFSET/10)
7343 is the FL_DAYPULSEOFFSET
15421 is the FL_DAYTIMEOFFSET
7236 is the FL_TOURPULSEOFFSET
15138 is the FL_TOURTIMEOFFSET
377 is the Coulomb-coefficient
30 is ???
the speed in km/h is calculated using the following formula: `(value/FL_PLOESBT)*FL_WHEELSIZEBT/(2500/9)`
board
=====

View File

@ -471,23 +471,6 @@ void main(void)
for (uint32_t i=0; i<1000000; i++) { // wait a bit to remove noise and double trigger
__asm__("nop");
}
if (strlen(log_name)) { // check is card file system is available
printf("log files:\n");
FILINFO info;
result = f_findfirst(&directory, &info, "", "forumslogger_?\??\?-?\?-?\?_?\?-?\?-?\?.txt");
while (FR_OK==result && strlen(info.fname)) {
printf("- %s\n", info.fname);
result = f_findnext(&directory, &info);
}
if (log((char*)"0123456789112345678921234567893123456789412345678951234567896123456789")) { // log message
printf("logging message worked\n");
} else {
printf("logging message failed\n");
}
} else {
printf("card file system not available\n");
}
button_flag = false; // reset flag
}
while (rtc_internal_tick_flag) { // the internal RTC ticked
@ -515,11 +498,63 @@ void main(void)
while (radio_bluetooth_received) { // a message has been received over Bluetooth
radio_bluetooth_received = false; // clear flag
action = true; // action has been performed
sensor_forumslader_transmit((const char*)radio_bluetooth_message); // forward message to forumslader
//printf("Bluetooth: %s", radio_bluetooth_message); // print message
if (!log((char*)radio_bluetooth_message)) { // log Bluetooth message
printf("logging Bluetooth message failed\n");
if (0==strncmp((char*)radio_bluetooth_message, "$FLL,", 5)) { // check if this is a message for us
if (0==strncmp((char*)radio_bluetooth_message, "$FLL,LS;", 8)) { // list files
radio_bluetooth_transmit("$FLL,LS,BOL;\r\n"); // notify we will begin the list of files
if (strlen(log_name)) { // check is card file system is available
FILINFO info; // to get file information
result = f_findfirst(&directory, &info, "", "forumslogger_?\??\?-?\?-?\?_?\?-?\?-?\?.txt"); // find files
while (FR_OK==result && strlen(info.fname)) { // file found
radio_bluetooth_transmit("$FLL,LS,"); // file entry header
radio_bluetooth_transmit(info.fname); // file name
radio_bluetooth_transmit(";\r\n"); // end for file entry
result = f_findnext(&directory, &info); // get next file
}
}
radio_bluetooth_transmit("$FLL,LS,EOL;\r\n"); // notify this is the end of the list
} else if (0==strncmp((char*)radio_bluetooth_message, "$FLL,RM,forumslogger_", 21)) { // remove log file
char filename[36+1] = {0}; // file to delete
for (uint8_t i=0; i<LENGTH(filename)-1 && 8+i<strlen((char*)radio_bluetooth_message) && radio_bluetooth_message[8+i]!=';'&& radio_bluetooth_message[8+i]!='\r'; i++) { // copy file name
filename[i] = radio_bluetooth_message[8+i]; // copy character
filename[i+1] = 0; // end string
}
if (strlen(log_name) && FR_OK==f_unlink(filename)) { // delete file
radio_bluetooth_transmit("$FLL,RM,OK;\r\n"); // notify user file removal succeeded
} else {
radio_bluetooth_transmit("$FLL,RM,ERROR;\r\n"); // notify user file removal failed
}
} else if (0==strncmp((char*)radio_bluetooth_message, "$FLL,CAT,forumslogger_", 22)) { // dump log file
char filename[36+1] = {0}; // file to delete
for (uint8_t i=0; i<LENGTH(filename)-1 && 9+i<strlen((char*)radio_bluetooth_message) && radio_bluetooth_message[9+i]!=';'&& radio_bluetooth_message[9+i]!='\r'; i++) { // copy file name
filename[i] = radio_bluetooth_message[9+i]; // copy character
filename[i+1] = 0; // end string
}
FIL log_file; // log file to read data
if (strlen(log_name) && FR_OK==f_open(&log_file, filename, FA_READ)) { // open file
radio_bluetooth_transmit("$FLL,CAT,BOF;\r\n"); // notify user the file will be dumped
char buffer[80+1]; // log file data (use NMEA size for max transmit size)
UINT read; // number of bytes read
while (FR_OK==f_read(&log_file, buffer, LENGTH(buffer)-1, &read)) { // read log file
if (0==read || read>LENGTH(buffer)-1) { // end of file
break;
}
buffer[read] = 0; // end string
radio_bluetooth_transmit(buffer); // send log data
}
f_close(&log_file); // close file
radio_bluetooth_transmit("$FLL,CAT,EOF;\r\n"); // notify user the file ended
} else {
radio_bluetooth_transmit("$FLL,CAT,ERROR;\r\n"); // notify user file removal failed
}
}
} else {
sensor_forumslader_transmit((const char*)radio_bluetooth_message); // forward message to forumslader
if (!log((char*)radio_bluetooth_message)) { // log Bluetooth message
printf("logging Bluetooth message failed\n");
}
}
//printf("Bluetooth: %s", radio_bluetooth_message); // print message
}
while (radio_gps_received) { // a GPS message has been received
radio_gps_received = false; // clear flag