Samples temperature once per minute - Stores the time series on a file - Displays the time series on STDout, from the beginning of the file up to current time, when pressing the button
Dependencies: BSP_B-L475E-IOT01
main.cpp@0:e53ff6cb04fd, 2018-12-06 (annotated)
- Committer:
- vicara
- Date:
- Thu Dec 06 17:49:10 2018 +0000
- Revision:
- 0:e53ff6cb04fd
temperature once per minute
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vicara | 0:e53ff6cb04fd | 1 | #include "mbed.h" |
vicara | 0:e53ff6cb04fd | 2 | |
vicara | 0:e53ff6cb04fd | 3 | #include <stdio.h> |
vicara | 0:e53ff6cb04fd | 4 | #include <errno.h> |
vicara | 0:e53ff6cb04fd | 5 | |
vicara | 0:e53ff6cb04fd | 6 | // Block devices |
vicara | 0:e53ff6cb04fd | 7 | #if COMPONENT_SPIF |
vicara | 0:e53ff6cb04fd | 8 | #include "SPIFBlockDevice.h" |
vicara | 0:e53ff6cb04fd | 9 | #endif |
vicara | 0:e53ff6cb04fd | 10 | |
vicara | 0:e53ff6cb04fd | 11 | #if COMPONENT_DATAFLASH |
vicara | 0:e53ff6cb04fd | 12 | #include "DataFlashBlockDevice.h" |
vicara | 0:e53ff6cb04fd | 13 | #endif |
vicara | 0:e53ff6cb04fd | 14 | |
vicara | 0:e53ff6cb04fd | 15 | #if COMPONENT_SD |
vicara | 0:e53ff6cb04fd | 16 | #include "SDBlockDevice.h" |
vicara | 0:e53ff6cb04fd | 17 | #endif |
vicara | 0:e53ff6cb04fd | 18 | |
vicara | 0:e53ff6cb04fd | 19 | #include "HeapBlockDevice.h" |
vicara | 0:e53ff6cb04fd | 20 | |
vicara | 0:e53ff6cb04fd | 21 | // File systems |
vicara | 0:e53ff6cb04fd | 22 | #include "LittleFileSystem.h" |
vicara | 0:e53ff6cb04fd | 23 | #include "FATFileSystem.h" |
vicara | 0:e53ff6cb04fd | 24 | |
vicara | 0:e53ff6cb04fd | 25 | // Physical block device, can be any device that supports the BlockDevice API |
vicara | 0:e53ff6cb04fd | 26 | /*SPIFBlockDevice bd( |
vicara | 0:e53ff6cb04fd | 27 | MBED_CONF_SPIF_DRIVER_SPI_MOSI, |
vicara | 0:e53ff6cb04fd | 28 | MBED_CONF_SPIF_DRIVER_SPI_MISO, |
vicara | 0:e53ff6cb04fd | 29 | MBED_CONF_SPIF_DRIVER_SPI_CLK, |
vicara | 0:e53ff6cb04fd | 30 | MBED_CONF_SPIF_DRIVER_SPI_CS);*/ |
vicara | 0:e53ff6cb04fd | 31 | |
vicara | 0:e53ff6cb04fd | 32 | #define BLOCK_SIZE 512 |
vicara | 0:e53ff6cb04fd | 33 | HeapBlockDevice bd(16384, BLOCK_SIZE); |
vicara | 0:e53ff6cb04fd | 34 | #include "stm32l475e_iot01_tsensor.h" |
vicara | 0:e53ff6cb04fd | 35 | |
vicara | 0:e53ff6cb04fd | 36 | |
vicara | 0:e53ff6cb04fd | 37 | // File system declaration |
vicara | 0:e53ff6cb04fd | 38 | LittleFileSystem fs("fs"); |
vicara | 0:e53ff6cb04fd | 39 | |
vicara | 0:e53ff6cb04fd | 40 | Ticker timeout_ticker; |
vicara | 0:e53ff6cb04fd | 41 | Thread t; |
vicara | 0:e53ff6cb04fd | 42 | |
vicara | 0:e53ff6cb04fd | 43 | static FILE *f; |
vicara | 0:e53ff6cb04fd | 44 | volatile int seconds_passed = 0; |
vicara | 0:e53ff6cb04fd | 45 | EventQueue queue(32 * EVENTS_EVENT_SIZE); |
vicara | 0:e53ff6cb04fd | 46 | |
vicara | 0:e53ff6cb04fd | 47 | void write_temperature_on_file() { |
vicara | 0:e53ff6cb04fd | 48 | float temperature = 0; |
vicara | 0:e53ff6cb04fd | 49 | temperature = BSP_TSENSOR_ReadTemp(); |
vicara | 0:e53ff6cb04fd | 50 | fprintf(f, "%f\n", temperature); |
vicara | 0:e53ff6cb04fd | 51 | fflush(f); |
vicara | 0:e53ff6cb04fd | 52 | fflush(stdout); |
vicara | 0:e53ff6cb04fd | 53 | printf("New temperature stored in the file\n"); |
vicara | 0:e53ff6cb04fd | 54 | } |
vicara | 0:e53ff6cb04fd | 55 | |
vicara | 0:e53ff6cb04fd | 56 | void print_temperature_from_file() { |
vicara | 0:e53ff6cb04fd | 57 | printf("\n====== PRINTING TEMPERATURE RECORDED SO FAR ======\n"); |
vicara | 0:e53ff6cb04fd | 58 | int order = 1; |
vicara | 0:e53ff6cb04fd | 59 | fflush(stdout); |
vicara | 0:e53ff6cb04fd | 60 | fflush(f); |
vicara | 0:e53ff6cb04fd | 61 | fseek(f, 0, SEEK_SET); |
vicara | 0:e53ff6cb04fd | 62 | float value; |
vicara | 0:e53ff6cb04fd | 63 | while (!feof(f)) { |
vicara | 0:e53ff6cb04fd | 64 | fscanf(f, "%f", &value); |
vicara | 0:e53ff6cb04fd | 65 | printf(" %d: %f\n", order, value); |
vicara | 0:e53ff6cb04fd | 66 | order++; |
vicara | 0:e53ff6cb04fd | 67 | } |
vicara | 0:e53ff6cb04fd | 68 | fflush(stdout); |
vicara | 0:e53ff6cb04fd | 69 | printf("\n"); |
vicara | 0:e53ff6cb04fd | 70 | } |
vicara | 0:e53ff6cb04fd | 71 | |
vicara | 0:e53ff6cb04fd | 72 | InterruptIn button(USER_BUTTON); |
vicara | 0:e53ff6cb04fd | 73 | |
vicara | 0:e53ff6cb04fd | 74 | void toggle() { |
vicara | 0:e53ff6cb04fd | 75 | queue.call(print_temperature_from_file); |
vicara | 0:e53ff6cb04fd | 76 | } |
vicara | 0:e53ff6cb04fd | 77 | |
vicara | 0:e53ff6cb04fd | 78 | void ticker_attach() { |
vicara | 0:e53ff6cb04fd | 79 | queue.call(write_temperature_on_file); |
vicara | 0:e53ff6cb04fd | 80 | } |
vicara | 0:e53ff6cb04fd | 81 | |
vicara | 0:e53ff6cb04fd | 82 | int main(){ |
vicara | 0:e53ff6cb04fd | 83 | t.start(callback(&queue, &EventQueue::dispatch_forever)); |
vicara | 0:e53ff6cb04fd | 84 | BSP_TSENSOR_Init(); |
vicara | 0:e53ff6cb04fd | 85 | button.rise(&toggle); |
vicara | 0:e53ff6cb04fd | 86 | |
vicara | 0:e53ff6cb04fd | 87 | // Try to mount the filesystem |
vicara | 0:e53ff6cb04fd | 88 | printf("Mounting the filesystem... "); |
vicara | 0:e53ff6cb04fd | 89 | fflush(stdout); |
vicara | 0:e53ff6cb04fd | 90 | int err = fs.mount(&bd); |
vicara | 0:e53ff6cb04fd | 91 | printf("%s\n", (err ? "Fail :(" : "OK")); |
vicara | 0:e53ff6cb04fd | 92 | if (err) { |
vicara | 0:e53ff6cb04fd | 93 | // Reformat if we can't mount the filesystem |
vicara | 0:e53ff6cb04fd | 94 | // this should only happen on the first boot |
vicara | 0:e53ff6cb04fd | 95 | printf("No filesystem found, formatting... "); |
vicara | 0:e53ff6cb04fd | 96 | fflush(stdout); |
vicara | 0:e53ff6cb04fd | 97 | err = fs.reformat(&bd); |
vicara | 0:e53ff6cb04fd | 98 | printf("%s\n", (err ? "Fail :(" : "OK")); |
vicara | 0:e53ff6cb04fd | 99 | if (err) { |
vicara | 0:e53ff6cb04fd | 100 | error("error: %s (%d)\n", strerror(-err), err); |
vicara | 0:e53ff6cb04fd | 101 | } |
vicara | 0:e53ff6cb04fd | 102 | } |
vicara | 0:e53ff6cb04fd | 103 | |
vicara | 0:e53ff6cb04fd | 104 | // Open the numbers file |
vicara | 0:e53ff6cb04fd | 105 | printf("Opening \"/fs/numbers.txt\"... "); |
vicara | 0:e53ff6cb04fd | 106 | fflush(stdout); |
vicara | 0:e53ff6cb04fd | 107 | f = fopen("/fs/numbers.txt", "r+"); |
vicara | 0:e53ff6cb04fd | 108 | printf("%s\n", (!f ? "Fail :(" : "OK")); |
vicara | 0:e53ff6cb04fd | 109 | if (!f) { |
vicara | 0:e53ff6cb04fd | 110 | // Create the numbers file if it doesn't exist |
vicara | 0:e53ff6cb04fd | 111 | printf("No file found, creating a new file... "); |
vicara | 0:e53ff6cb04fd | 112 | fflush(stdout); |
vicara | 0:e53ff6cb04fd | 113 | f = fopen("/fs/numbers.txt", "w+"); |
vicara | 0:e53ff6cb04fd | 114 | printf("%s\n", (!f ? "Fail :(" : "OK")); |
vicara | 0:e53ff6cb04fd | 115 | if (!f) { |
vicara | 0:e53ff6cb04fd | 116 | error("error: %s (%d)\n", strerror(errno), -errno); |
vicara | 0:e53ff6cb04fd | 117 | } |
vicara | 0:e53ff6cb04fd | 118 | } |
vicara | 0:e53ff6cb04fd | 119 | |
vicara | 0:e53ff6cb04fd | 120 | timeout_ticker.attach(&ticker_attach, 60); |
vicara | 0:e53ff6cb04fd | 121 | } |