Firmware
Dependencies: BMI160 ADT7410 Thermistor DS1621 max32630fthr Adafruit_FeatherOLED
Revision 0:556294574340, committed 2020-07-09
- Comitter:
- condato_mbed
- Date:
- Thu Jul 09 15:56:48 2020 +0000
- Child:
- 1:c4e09936f3ed
- Commit message:
- coldchainlogger
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ADT7410.lib Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/tkreyche/code/ADT7410/#e1aee50340ec
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Adafruit_FeatherOLED.lib Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/switches/code/Adafruit_FeatherOLED/#c7ddcb2cc3fb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BMI160.lib Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/MaximIntegrated/code/BMI160/#a521606048bb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS1621.lib Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/Bas/code/DS1621/#20bfb7df0470
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Thermistor.lib Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/unix_guru/code/Thermistor/#374faf195af3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ble_functions.cpp Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,1817 @@ +#include "functions.h" +#include "ble_functions.h" +#include "RawSerial.h" +#include <InterruptIn.h> +#include <InterruptManager.h> +#include "bmi160.h" +#include "max32630fthr.h" +#include "max3263x.h" +#include "MAX14690.h" +#include "ADT7410.h" +#include "Adafruit_SSD1306.h" +#include <BLE.h> +#include "ble/BLE.h" +#include "ble/Gap.h" +#include "ble/services/BatteryService.h" +#include "ble/blecommon.h" +#include "UUID.h" +#include "ble/gap/AdvertisingDataTypes.h" +#include "ble/gap/Types.h" +#include "SDBlockDevice.h" +#include "FATFileSystem.h" +#include <errno.h> +#include <stdio.h> +#include <time.h> +#include <stdint.h> + +extern char device_id[]; +extern unsigned short int measure_id; +extern unsigned short int log_id; + +extern Serial pan1326b; +extern DigitalOut bt_rst; + +extern volatile int conn_state; +extern bool in_BT; + +extern AnalogIn battery; + +extern DigitalOut r; +extern DigitalOut g; +extern DigitalOut b; +extern DigitalOut RGBs[]; + +extern MAX32630FTHR pegasus; + +extern ADT7410 myADT7410; +extern I2C i2cm2; +extern MAX14690 max14690; + +extern InterruptIn button; + +extern FATFileSystem fileSystem; +extern SDBlockDevice blockDevice; + +extern I2C i2c; +extern int sensor_temp; +extern Adafruit_SSD1306_I2c featherOLED; + +extern volatile int quit; +extern volatile int ble_return; +extern volatile int conn_state; +extern int counter_ble; + +extern bool error_status; +extern bool BT_error; +extern bool shipping; +extern bool pause; +extern bool init_status; +extern bool after_BT; +extern bool charge; + +extern Timeout after_BLE; +extern Timeout turnoff; +extern Timeout flipper; +extern Timeout advertise_cancel; +extern Timeout connected_cancel; +extern Ticker mess_timer; +extern Ticker ticker; +extern time_t now; +extern time_t raw; + +extern float buffer_temp; +extern struct tm current; +extern struct tm actual; + +extern struct user_config_struct user_config_para; +extern struct tm user_config_time; + +extern Timeout done_rcv; + +extern int next_state; + +extern int unsigned long t_diff; +extern int unsigned long unix_time; + +extern int default_advertise_time; +extern int default_connected_time; + +extern int alert_count; + +int ble_buff_line = 0; +char ble_buff[1024][64]; + +char response = '0'; +char messungen[7]; +int messwerte[5000]; +char logs_array[7]; +char seconds[12]; +float min_temp = 0; +float max_temp = 0; +float avr_temp = 0; +unsigned short counter_m = 0; +unsigned short counter_l = 0; +int sendLedCounter = 0; + +//const static char* SERVICE_UUID = "32372fb9-5f73-4c32-a17b-25e17f52a99a"; +static uint8_t service_data[16]; + +//const static char* CHARACTERISTIC_UUID = "1d6ba3db-d405-4034-96fc-78942ef7075f"; + +uint16_t customServiceUUID = 0xA000; +uint16_t readCharUUID = 0xA001; +uint16_t writeCharUUID = 0xA002; +uint16_t battService = 0x180F; + +const static char DEVICE_NAME[] = "Cold Chain Logger"; + +static const uint16_t uuid16_list[] = {0xFFFF}; +//static const uint8_t advData[26]; + +//UUID service_uuid = UUID(SERVICE_UUID); +//UUID characteristic_uuid = UUID(CHARACTERISTIC_UUID); + +//Gap::ConnectionParams_t fast; + +// Set Up custom Charatiristics +static uint8_t readValue[360]; +//ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue); +ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY ,NULL,0); + +static uint8_t writeValue[20]; +//WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); +WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE ,NULL,0); + +// Set up custom service +GattCharacteristic *characteristics[] = {&readChar, &writeChar}; +GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); + +//Battery service +uint8_t batteryLevel = 50; +static BatteryService* batteryServicePtr; + +void periodicCallback(void) +{ + b = !b; +} + +void writeLedCallback(void) +{ + b = !b; + ticker.detach(); +} + +void sendLedCallback(void) +{ + b = !b; + if(sendLedCounter == 2){ + ticker.detach(); + sendLedCounter = 0; + }else{ + sendLedCounter++; + } +} + +void clear_buffer(){ + if(response != 's'){ + counter_m = 0; + }else if(response != 'l'){ + counter_l = 0; + } + ble_buff_line = 0; + memset(&ble_buff[0][0], 0, sizeof(ble_buff)); + memset(&readValue[0], 0, sizeof(readValue)); + memset(&writeValue[0], 0, sizeof(writeValue)); +} + +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) +{ + printf("\nDisconnection callback\n"); + clear_buffer(); + connected_cancel.detach(); + ticker.attach(periodicCallback, 0.5); + advertise_cancel.attach(&BT_false, user_config_para.advertise); + char string[] = "Bluetooth disconnected"; + write_to_sd_log_single(string); + conn_state = 0; + counter_m = 0; + counter_l = 0; + BLE::Instance(BLE::DEFAULT_INSTANCE).startAdvertising(); + printf("\nAdvertising\n"); +} + +void updateSensorValue() { + float batteryProcent = (100 / 0.271) * (battery.read() - 0.704); + if(batteryProcent > 97){ + batteryProcent = 100; + } + batteryLevel = batteryProcent; + batteryServicePtr->updateBatteryLevel(batteryProcent); +} + +void printMacAddress() +{ + Gap::AddressType_t addr_type; + Gap::Address_t address; + BLE::Instance().gap().getAddress(&addr_type, address); + printf("Device MAC address: "); + for (int i = 5; i >= 1; i--){ + printf("%02x:", address[i]); + } + printf("%02x\r\n", address[0]); +} + +void writeCharCallback(const GattWriteCallbackParams *params) +{ + printf("\nData received\n"); + //b = 1; + //ticker.attach(writeLedCallback, 0.5); + memset(&readValue[0], 0, sizeof(readValue)); + memset(&writeValue[0], 0, sizeof(writeValue)); + BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len); + for(int i = 0; i < 15; i++){ + ble_buff[0][i + ble_buff_line] = readValue[i]; + } + printf("\n%s received (ble_buff)", ble_buff[0]); + ble_buff_line = ble_buff_line + 15; + if(readValue[0] > 0x60 && readValue[0] < 0x7a){ + response = readValue[0]; + }else if(response != 's' && response != 'l' && response != 'e' && response != 't'){ + clear_buffer(); + } + printf("\nresponse: %c\n", response); +} + +void disconnect(){ + Gap::DisconnectionReason_t res=Gap::LOCAL_HOST_TERMINATED_CONNECTION; + BLE::Instance(BLE::DEFAULT_INSTANCE).gap().disconnect(res); +} + +void whenConnected(const Gap::ConnectionCallbackParams_t *) +{ + printf("\nConnected\n"); + advertise_cancel.detach(); + ticker.detach(); + connected_cancel.attach(&disconnect, user_config_para.connected); + conn_state = 1; + if(error_status == true){ + r = 1; + } + b = 0; + response = '0'; + char string[] = "Bluetooth connected"; + write_to_sd_log_single(string); +} + +void onDataSent(unsigned count) +{ + printf("%c", ble_buff[ble_buff_line][count]); + readValue[count] = ble_buff[ble_buff_line][count]; +} + +void after_BL(){ + after_BT = false; + after_BLE.detach(); +} + +void BT_false() +{ + BLE &ble = BLE::Instance(); + in_BT = false; + b = 1; + conn_state = 0; + quit = 0; + clear_buffer(); + ticker.detach(); + advertise_cancel.detach(); + connected_cancel.detach(); + disconnect(); + wait(0.5); + if(error_status == true){ + r = 0; + } + if(shipping == true && pause == false){ + mess_timer.attach(&mess_handler, (float)user_config_para.interval); + } + after_BT = true; + after_BLE.attach(after_BL, 1); + response = '0'; +} + +void BT_true() +{ + in_BT = true; + next_state = 2; +} + +void log_count(){ + fileSystem.unmount(); + fflush(stdout); + + fileSystem.mount(&blockDevice); + FILE * log = fopen ("/fs/log.csv", "rb"); + + int ch = 0; + int lines = 0; + + while(!feof(log)){ + ch = fgetc(log); + if(ch == '\n') + { + lines++; + } + } + + log_id = lines; + + fclose(log); + fileSystem.unmount(); + + sprintf(logs_array,"%d", lines); +} + +void messdaten_count(){ + fileSystem.unmount(); + fflush(stdout); + + fileSystem.mount(&blockDevice); + FILE * messdaten = fopen ("/fs/messdaten.csv", "rb"); + + int ch = 0; + int lines = 0; + + while(!feof(messdaten)){ + ch = fgetc(messdaten); + if(ch == '\n') + { + lines++; + } + } + + fclose(messdaten); + fileSystem.unmount(); + + measure_id = lines; + + sprintf(messungen,"%d", lines); +} + +void unixtime_to_char_array(){ + tm *current; + time(&now); + current = localtime (&now); + printf ("\nDate: 20%02d-%02d-%02d %02d:%02d:%02d\r\n", current->tm_year - 100, current->tm_mon + 1, current->tm_mday, current->tm_hour, current->tm_min, current->tm_sec); + + printf("\nSekunden als Dezimal: %d\n", time(&now)); + + sprintf(seconds,"%d", time(&now)); + + printf("\nSekunden als String: %s\n", seconds); +} + +int array_to_int(int z){ + int zahl[z]; + + for(int i = 0; i < z; i++){ + zahl[i] = readValue[i]; + zahl[i] = zahl[i] - 48; + } + + unsigned long ganzes = 0; + unsigned long log = 1; + int j = z; + do{ + j--; + ganzes = ganzes + log * zahl[j]; + log = log * 10; + }while(j > 0); + + if((int)ganzes < 0){ + ganzes = 0; + } + return ganzes; +} + +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) +{ + BLE &ble = params->ble; + ble_error_t error = params->error; + + if (error != BLE_ERROR_NONE) { + return; + } + + read_id(); + + /* Setup advertising */ + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT + ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::DEVICE_ID, (uint8_t *)device_id, 5); // add name + //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet + ble.gap().setAdvertisingInterval(100); // 100ms. + + /* Add custom service */ + ble.gattServer().addService(customService); + + /* Add battery service */ + batteryServicePtr = new BatteryService(ble, batteryLevel); + + ble.gap().onDisconnection(disconnectionCallback); + ble.gap().onConnection(whenConnected); + ble.gattServer().onDataWritten(writeCharCallback); + ble.gattServer().onDataSent(onDataSent); + + /* Start advertising */ + ble.gap().startAdvertising(); +} + +void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { + BLE &ble = BLE::Instance(); + updateSensorValue(); +} + +void dataSentSend(){ + response = '0'; + connected_cancel.attach(&disconnect, user_config_para.connected); +} + +void dataSent(){ + //wait(1); + //b = 1; + //ticker.attach(sendLedCallback, 0.5); + response = '0'; + connected_cancel.attach(&disconnect, user_config_para.connected); +} + +void swap(int num1, int num2){ + int temp = messwerte[num1]; + messwerte[num1] = messwerte[num2]; + messwerte[num2] = temp; +} + +int partition(int left, int right, int pivot){ + int leftPointer = left - 1; + int rightPointer = right; + + while(true){ + while(messwerte[++leftPointer] < pivot){ + //do nothing + } + + while(rightPointer > 0 && messwerte[--rightPointer] > pivot){ + //do nothing + } + + if(leftPointer >= rightPointer){ + break; + }else{ + swap(leftPointer, rightPointer); + } + } + swap(leftPointer, right); + + return leftPointer; +} + +void quickSort(int left, int right){ + if(right - left <= 0){ + return; + }else{ + int pivot = messwerte[right]; + int partionPoint = partition(left, right, pivot); + quickSort(left, partionPoint - 1); + quickSort(partionPoint + 1, right); + } +} + +void BLE_handler() +{ + BLE &ble = BLE::Instance(); + b = 0; + ticker.attach(periodicCallback, 0.5); + advertise_cancel.attach(&BT_false, user_config_para.advertise); + clear_buffer(); + next_state = 0; + printf("\nBLE - ON\n"); + printf("\nAdvertising\n"); + + while( quit ) + { + ble.waitForEvent(); + ble.onEventsToProcess(scheduleBleEventsProcessing); + + while ( conn_state ) //Abfrage zum Verbindungsstatus + { + ble.waitForEvent(); + ble.onEventsToProcess(scheduleBleEventsProcessing); + + /* + if(readValue[0] == NULL){ + ble.onEventsToProcess(scheduleBleEventsProcessing); + } + */ + + switch ( response ) + { + /* + case 's': // Messwerte übertragen + { + connected_cancel.detach(); + clear_buffer(); + + unsigned int length = 0; + + connected_cancel.attach(&disconnect, user_config_para.connected); + while(readValue[0] == NULL || readValue[0] < 0x31|| readValue[0] > 0x39){ + clear_buffer(); + ble.waitForEvent(); + if(conn_state == 0){ + break; + } + } + connected_cancel.detach(); + if(conn_state == 1){ + int a = 0; + while(readValue[a] != NULL){ + a++; + } + int id = array_to_int(a); + clear_buffer(); + + fileSystem.mount(&blockDevice); + FILE * messdaten = fopen ("/fs/messdaten.csv", "rb"); + + fseek (messdaten, 0, SEEK_END); + length = ftell (messdaten); + fseek (messdaten, 0, SEEK_SET); + + if(id > measure_id){ + printf("\nMeasuremnt ID %d doesn't exist!\n", id); + char string[] = "Call of an inexistent measurement ID"; + write_to_sd_log_single(string); + + ble_buff[0][0] = '0'; + onDataSent(0); + }else{ + unsigned long ch = 0; + unsigned long lines = 0; + unsigned long i = 0; + unsigned long j = 1; + unsigned long c = 0; + + if(id == 1){ + fseek(messdaten, 0, SEEK_SET); + }else{ + while((id - 1) != lines){ + ch = fgetc(messdaten); + if(ch == '\n'){ + lines++; + } + i++; + } + fseek(messdaten, i, SEEK_SET); + c = i; + } + if(id == measure_id){ + while(fgetc(messdaten) != EOF){ + i++; + j++; + } + }else{ + while(fgetc(messdaten) != '\n'){ + i++; + j++; + } + } + fseek(messdaten, c, SEEK_SET); + fread (ble_buff[ble_buff_line], 1, j, messdaten); + + for(int l = 0; l < j; l++){ + onDataSent(l); + } + } + + fclose(messdaten); + fileSystem.unmount(); + fflush(stdout); + + dataSentSend(); + } + break; + } + */ + + case 's': // Messwerte übertragen + { + connected_cancel.detach(); + clear_buffer(); + + if(counter_m == 0){ + char string[] = "Command 's' (send measuremnts) received"; + write_to_sd_log_single(string); + } + + connected_cancel.attach(&disconnect, user_config_para.connected); + while(readValue[0] == NULL || readValue[0] < 0x31|| readValue[0] > 0x39){ + clear_buffer(); + ble.waitForEvent(); + if(conn_state == 0){ + break; + } + } + connected_cancel.detach(); + if(conn_state == 1){ + + if(counter_m == 0){ + char string1[] = "Sending measuremnts..."; + write_to_sd_log_single(string1); + } + + int a = 0; + while(readValue[a] != NULL){ + a++; + } + int id = array_to_int(a); + clear_buffer(); + + fileSystem.mount(&blockDevice); + FILE * messdaten = fopen ("/fs/messdaten.csv", "rb"); + + /* Daten senden */ + + if(id > measure_id){ + printf("\nMeasuremnt ID %d doesn't exist!\n", id); + char string[] = "Call of an inexistent measurement ID"; + write_to_sd_log_single(string); + + ble_buff[0][0] = '0'; + onDataSent(0); + }else{ + unsigned long ch = 0; + unsigned long lines = 0; + unsigned long i = 0; + unsigned long j = 1; + unsigned long c = 0; + + if(id == 1){ + fseek(messdaten, 0, SEEK_SET); + }else{ + while((id - 1) != lines){ + ch = fgetc(messdaten); + if(ch == '\n'){ + lines++; + } + i++; + } + fseek(messdaten, i, SEEK_SET); + c = i; + } + if(id == measure_id){ + while(fgetc(messdaten) != EOF){ + i++; + j++; + } + }else{ + int y = 0; + while(y < 10){ + i++; + j++; + ch = fgetc(messdaten); + if(ch == '\n'){ + y++; + } + if(ch == EOF){ + break; + } + } + i--; + j--; + } + fseek(messdaten, c, SEEK_SET); + fread (ble_buff[ble_buff_line], 1, j, messdaten); + + for(int l = 0; l < j; l++){ + onDataSent(l); + } + } + + fclose(messdaten); + fileSystem.unmount(); + fflush(stdout); + + counter_m += 10; + if(counter_m >= measure_id){ + char string2[] = "Sending measuremnts complete"; + write_to_sd_log_single(string2); + counter_m = 0; + dataSent(); + }else{ + dataSentSend(); + } + } + break; + } + + case 'g': //Config senden + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'g' (send config settings) received"; + write_to_sd_log_single(string); + + unsigned int length = 0; + + char string_1[] = "Sending config.."; + write_to_sd_log_single(string_1); + + fileSystem.mount(&blockDevice); + FILE * user_config = fopen ("/fs/user_config.csv", "rb"); + + fseek (user_config, 0, SEEK_END); + length = ftell (user_config); + fseek (user_config, 0, SEEK_SET); + + fread (ble_buff, 1, length, user_config); + + fclose(user_config); + fileSystem.unmount(); + + fflush(stdout); + + for(int i = 0; i < length; i++){ + onDataSent(i); + } + + char string_2[] = "Sending config complete"; + write_to_sd_log_single(string_2); + + dataSent(); + + break; + } + + case 'c': // Config auf Werkeinstellungen zurücksetzen + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'c' (reset config to standard) received"; + write_to_sd_log_single(string); + + if(load_standard_config() == 1){ + char string[] = "Config reseted to factory settings"; + write_to_sd_log_single(string); + + ble_buff[0][0] = '1'; + onDataSent(0); + }else{ + char string[] = "Failed to reset Config to factory settings"; + write_to_sd_log_single(string); + + ble_buff[0][0] = '0'; + onDataSent(0); + } + + dataSent(); + + break; + } + + case 'd': // Messwerte löschen + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'd' (delete measurement data) received"; + write_to_sd_log_single(string); + + if(delete_file_messdaten() == 1){ + char string_1[] = "Measuerements deleted succesfully"; + write_to_sd_log_single(string_1); + + ble_buff[0][0] = '1'; + onDataSent(0); + }else{ + char string_1[] = "Measuerements data delete failed"; + write_to_sd_log_single(string_1); + + ble_buff[0][0] = '0'; + onDataSent(0); + } + + dataSent(); + + break; + } + /* + case 'l': //Log-Datei übertragen + { + + connected_cancel.detach(); + clear_buffer(); + + unsigned int length = 0; + + connected_cancel.attach(&disconnect, user_config_para.connected); + while(readValue[0] == NULL || readValue[0] < 0x31|| readValue[0] > 0x39){ + clear_buffer(); + ble.waitForEvent(); + if(conn_state == 0){ + break; + } + } + connected_cancel.detach(); + if(conn_state == 1){ + int a = 0; + while(readValue[a] != NULL){ + a++; + } + int id = array_to_int(a); + clear_buffer(); + + fileSystem.mount(&blockDevice); + FILE * log = fopen ("/fs/log.csv", "rb"); + + fseek (log, 0, SEEK_END); + length = ftell (log); + fseek (log, 0, SEEK_SET); + + + + if(id > log_id){ + printf("\nLog ID %d doesn't exist!\n", id); + char string[] = "Call of an inexistent log ID"; + write_to_sd_log_single(string); + + ble_buff[0][0] = '0'; + onDataSent(0); + }else{ + unsigned long ch = 0; + unsigned long lines = 0; + unsigned long i = 0; + unsigned long j = 1; + unsigned long c = 0; + + if(id == 1){ + fseek(log, 0, SEEK_SET); + }else{ + while((id - 1) != lines){ + ch = fgetc(log); + if(ch == '\n'){ + lines++; + } + i++; + } + fseek(log, i, SEEK_SET); + c = i; + } + if(id == log_id){ + while(fgetc(log) != EOF){ + i++; + j++; + } + }else{ + while(fgetc(log) != '\n'){ + i++; + j++; + } + } + fseek(log, c, SEEK_SET); + fread (ble_buff[ble_buff_line], 1, j, log); + + for(int l = 0; l < j; l++){ + onDataSent(l); + } + } + + fclose(log); + fileSystem.unmount(); + fflush(stdout); + + dataSentSend(); + } + break; + } + */ + + case 'l': //Log-Datei übertragen + { + connected_cancel.detach(); + clear_buffer(); + + if(counter_l == 0){ + char string[] = "Command 'l' (send logs) received"; + write_to_sd_log_single(string); + } + + connected_cancel.attach(&disconnect, user_config_para.connected); + while(readValue[0] == NULL || readValue[0] < 0x31|| readValue[0] > 0x39){ + clear_buffer(); + ble.waitForEvent(); + if(conn_state == 0){ + break; + } + } + connected_cancel.detach(); + if(conn_state == 1){ + + if(counter_l == 0){ + char string1[] = "Sending logs..."; + write_to_sd_log_single(string1); + } + + int a = 0; + while(readValue[a] != NULL){ + a++; + } + int id = array_to_int(a); + clear_buffer(); + + fileSystem.mount(&blockDevice); + FILE * log = fopen ("/fs/log.csv", "rb"); + + /* Daten senden */ + + if(id > log_id){ + printf("\nLog ID %d doesn't exist!\n", id); + char string[] = "Call of an inexistent log ID"; + write_to_sd_log_single(string); + + ble_buff[0][0] = '0'; + onDataSent(0); + }else{ + unsigned long ch = 0; + unsigned long lines = 0; + unsigned long i = 0; + unsigned long j = 1; + unsigned long c = 0; + + if(id == 1){ + fseek(log, 0, SEEK_SET); + }else{ + while((id - 1) != lines){ + ch = fgetc(log); + if(ch == '\n'){ + lines++; + } + i++; + } + fseek(log, i, SEEK_SET); + c = i; + } + if(id == log_id){ + while(fgetc(log) != EOF){ + i++; + j++; + } + }else{ + int y = 0; + while(y < 5){ + i++; + j++; + ch = fgetc(log); + if(ch == '\n'){ + y++; + } + if(ch == EOF){ + break; + } + } + i--; + j--; + } + fseek(log, c, SEEK_SET); + fread (ble_buff[ble_buff_line], 1, j, log); + + for(int l = 0; l < j; l++){ + onDataSent(l); + } + } + + fclose(log); + fileSystem.unmount(); + fflush(stdout); + + counter_l += 5; + if(counter_l >= measure_id){ + char string2[] = "Sending logs complete"; + write_to_sd_log_single(string2); + counter_l = 0; + dataSent(); + }else{ + dataSentSend(); + } + } + break; + } + + case 'r': // Log Datei löschen + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'r' (delete log file) received"; + write_to_sd_log_single(string); + + if(delete_file_log() == 1){ + char string_1[] = "Log file deleted"; + write_to_sd_log_single(string_1); + + ble_buff[0][0] = '1'; + onDataSent(0); + }else{ + char string_1[] = "Log file delete failed"; + write_to_sd_log_single(string_1); + + ble_buff[0][0] = '0'; + onDataSent(0); + } + + dataSent(); + + break; + } + + case 'n': //Reset + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'n' (reboot) received"; + write_to_sd_log_single(string); + BT_false(); + wait(0.5); + reboot(); + break; + } + + case 'w': // Werkeinstellungen + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'w' (reset to factory settings) received"; + write_to_sd_log_single(string); + + if(delete_file_messdaten() && load_standard_config() && delete_file_log() == 1){ + char string_1[] = "System reseted to factory settings"; + write_to_sd_log_single(string_1); + + ble_buff[0][0] = '1'; + onDataSent(0); + + printf("\n__________________________\n\nColdchainlogger reseted \nto factory settings\n__________________________\n\n"); + }else{ + char string_1[] = "System reset to factory settings failed"; + write_to_sd_log_single(string_1); + + ble_buff[0][0] = '0'; + onDataSent(0); + } + + dataSent(); + + break; + } + + case 'a': // Datenaufnahme pausieren / stoppen + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'a' (pause / stop measurement) received"; + write_to_sd_log_single(string); + + mess_timer.detach(); + next_state = 0; + + if(pause == true){ + char string_1[] = "Measurement stopped"; + write_to_sd_log_single(string_1); + printf("\nMeasurement stopped\n"); + shipping = false; + + ble_buff[0][0] = '0'; + onDataSent(0); + }else{ + pause = true; + char string_1[] = "Measurement paused"; + write_to_sd_log_single(string_1); + printf("\nMeasurement puased\n"); + + ble_buff[0][0] = '1'; + onDataSent(0); + } + + mess_timer.detach(); + + dataSent(); + + break; + } + + case 'f': // Datenaufnahme starten / fortsetzen + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'f' (start / continue measurement) received"; + write_to_sd_log_single(string); + + if(shipping == false){ + char string_1[] = "Measurement started"; + write_to_sd_log_single(string_1); + shipping = true; + pause = false; + + if(user_config_para.wait_mode == 1){ + next_state = 3; + }else{ + next_state = 1; + } + printf("\nMeasurement started\n"); + + ble_buff[0][0] = '1'; + onDataSent(0); + }else{ + next_state = 1; + char string_1[] = "Measurement continued"; + write_to_sd_log_single(string_1); + printf("\nMeasurement continued\n"); + pause = false; + + ble_buff[0][0] = '0'; + onDataSent(0); + } + + dataSent(); + + break; + } + + case 'e': //Config erhalten + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'e' (receive and overwrite config) received"; + write_to_sd_log_single(string); + + connected_cancel.attach(&disconnect, user_config_para.connected); + printf("\nReady to receive new config settings\n"); + + while(readValue[0] == NULL){ + ble.waitForEvent(); + if(conn_state == 0){ + break; + } + } + memset(&readValue[0], 0, sizeof(readValue)); + memset(&writeValue[0], 0, sizeof(writeValue)); + while(readValue[0] == NULL){ + ble.waitForEvent(); + if(conn_state == 0){ + break; + } + } + connected_cancel.detach(); + if(conn_state == 1){ + char params[50]; + printf("\n%s\n", ble_buff[0]); + strcpy(params, ble_buff[0]); + + if(create_user_config(params) == 1){ + clear_buffer(); + + char string_1[] = "User config changed"; + write_to_sd_log_single(string_1); + printf("\n__________________________\n\nUser config changed\n__________________________\n\n"); + + ble_buff[0][0] = '1'; + onDataSent(0); + }else{ + clear_buffer(); + + char string_1[] = "Changing user config failed"; + write_to_sd_log_single(string_1); + printf("\n__________________________\n\n!!! Error, can't changing user config !!!\n__________________________\n\n"); + + ble_buff[0][0] = '0'; + onDataSent(0); + } + + dataSent(); + } + break; + } + + case 'x': // Verbindung beenden + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'x' (shut down bluetooth) received"; + write_to_sd_log_single(string); + + BT_false(); + char string_1[] = "Bluetooth shuted down"; + write_to_sd_log_single(string_1); + printf("\n\nBluetooth shuted down\n\n"); + + dataSentSend(); + clear_buffer(); + + break; + } + + case 'b': //Akku-Zustand + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'b' (send battery info) received"; + write_to_sd_log_single(string); + + updateSensorValue(); + int battery = (int)batteryLevel; + char bat[3]; + sprintf(bat,"%d", battery); + int i = 0; + while(bat[i] != NULL){ + ble_buff[0][i] = bat[i]; + onDataSent(i); + i++; + } + printf("\nBattery level: %d %%\n", battery); + + dataSent(); + + break; + } + + case 'm': //Speicher-Belegung + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'm' (send memmory info) received"; + write_to_sd_log_single(string); + + printf("\nTotal SD memory: %llu Bytes\n", blockDevice.size()); + + unsigned long long length = 0; + + //ID Größe + fileSystem.mount(&blockDevice); + FILE * id = fopen ("/fs/id.csv", "rb"); + + fseek (id, 0, SEEK_END); + length = ftell (id); + fseek (id, 0, SEEK_SET); + + fclose(id); + fileSystem.unmount(); + fflush(stdout); + + unsigned long long idn = length; + length = 0; + + //Log Größe + fileSystem.mount(&blockDevice); + FILE * log = fopen ("/fs/log.csv", "rb"); + + fseek (log, 0, SEEK_END); + length = ftell (log); + fseek (log, 0, SEEK_SET); + + fclose(log); + fileSystem.unmount(); + fflush(stdout); + + unsigned long long logs = length; + length = 0; + + //Messdaten Größe + fileSystem.mount(&blockDevice); + FILE * messdaten = fopen ("/fs/messdaten.csv", "rb"); + + fseek (messdaten, 0, SEEK_END); + length = ftell (messdaten); + fseek (messdaten, 0, SEEK_SET); + + fclose(messdaten); + fileSystem.unmount(); + fflush(stdout); + + unsigned long long mes = length; + length = 0; + + //Standard Config Größe + fileSystem.mount(&blockDevice); + FILE * stcf = fopen ("/fs/standard_config.csv", "rb"); + + fseek (stcf, 0, SEEK_END); + length = ftell (stcf); + fseek (stcf, 0, SEEK_SET); + + fclose(stcf); + fileSystem.unmount(); + fflush(stdout); + + unsigned long long stcfg = length; + length = 0; + + //User Config Größe + fileSystem.mount(&blockDevice); + FILE * uscf = fopen ("/fs/user_config.csv", "rb"); + + fseek (uscf, 0, SEEK_END); + length = ftell (uscf); + fseek (uscf, 0, SEEK_SET); + + fclose(uscf); + fileSystem.unmount(); + fflush(stdout); + + unsigned long long uscfg = length; + length = 0; + + printf("\nID size: %llu Bytes\n", idn); + printf("Logs size: %llu Bytes\n", logs); + printf("Standard config size: %llu Bytes\n", stcfg); + printf("User config size: %llu Bytes\n\n", uscfg); + + unsigned long long not_measuremnts = idn + logs + stcfg + uscfg; + unsigned long long actual_size = blockDevice.size() - not_measuremnts; + printf("Available SD memory (100 %%) = total SD memory - configs & id (%llu Bytes) - logs\n", not_measuremnts); + printf("Available SD memory (100 %%): %llu Bytes\n\n", actual_size); + + messdaten_count(); + printf("Measuremnts count: %s\n", messungen); + printf("Measurements size: %llu Bytes\n\n", mes); + + double hundert = 100; + double occupied = hundert / actual_size * mes; + printf("Occupied memory: %f %%\n", occupied); + + double free_mem = 100 - occupied; + printf("Available memory: %f %%\n", free_mem); + + char occ[3]; + char free[3]; + + if((int)occupied != 0){ + occupied = ceil( occupied * 100.0 ) / 100.0; + } + if((int)free_mem != 0){ + free_mem = ceil( free_mem * 100.0 ) / 100.0; + } + + sprintf(occ,"%d", (int)occupied); + sprintf(free,"%d", (int)free_mem); + + printf("OCC: %s\n", occ); + printf("FREE: %s\n", free); + + messdaten_count(); + + int i = 0; + while(i < messungen[i] != NULL){ + ble_buff[0][i] = messungen[i]; + onDataSent(i); + i++; + } + ble_buff[0][i] = 0x3B; + onDataSent(i); + i++; + int j = 0; + while(j < occ[j] != NULL){ + ble_buff[0][i] = occ[j]; + onDataSent(i); + i++; + j++; + } + ble_buff[0][i] = 0x3B; + onDataSent(i); + i++; + int c = 0; + while(c < free[c] != NULL){ + ble_buff[0][i] = free[c]; + onDataSent(i); + i++; + c++; + } + ble_buff[0][i] = 0x3B; + onDataSent(i); + i++; + dataSent(); + + break; + } + + case 'i': //ID auslesen + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'i' (send ID) received"; + write_to_sd_log_single(string); + + char string_1[] = "Sending device ID..."; + write_to_sd_log_single(string_1); + + printf("\nDevice ID: %s\n", device_id); + + int i = 0; + while(device_id[i] != NULL){ + ble_buff[0][i] = device_id[i]; + onDataSent(i); + i++; + } + + char string_2[] = "Sending device ID complete"; + write_to_sd_log_single(string_2); + + dataSent(); + + break; + } + + case 'z': //Hardware Module abfragen + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'z' (send connected devices) received"; + write_to_sd_log_single(string); + + char string_1[] = "Sending connected devices..."; + write_to_sd_log_single(string_1); + + printf("\nConnected devices: Temperature sensor\n"); + + char sensor[4] = {'0', '0', '1'}; + int i = 0; + while(sensor[i] != NULL){ + ble_buff[0][i] = sensor[i]; + onDataSent(i); + i++; + } + + char string_2[] = "Sending connected devices complete"; + write_to_sd_log_single(string_2); + + dataSent(); + + break; + } + + case 'v': // Zeit in Unixformat von Datenlogger abfragen + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'v' (send device time) received"; + write_to_sd_log_single(string); + + unixtime_to_char_array(); + for(int i = 0; i < sizeof(seconds); i++){ + ble_buff[0][i] = seconds[i]; + onDataSent(i); + } + + dataSent(); + + break; + } + + case 't': //Zeit in Unixformat an Datenlogger schicken + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 't' (receive and set device time) received"; + write_to_sd_log_single(string); + + connected_cancel.attach(&disconnect, user_config_para.connected); + printf("\nReady to receive new time settings\n"); + while(readValue[0] == NULL || readValue[0] < 0x31|| readValue[0] > 0x39){ + clear_buffer(); + ble.waitForEvent(); + if(conn_state == 0){ + break; + } + } + connected_cancel.detach(); + if(conn_state == 1){ + int a = 0; + while(readValue[a] != NULL){ + a++; + } + int new_time = array_to_int(a); + clear_buffer(); + + if(set_app_time(new_time) == 1){ + ble_buff[0][0] = '1'; + onDataSent(0); + }else{ + ble_buff[0][0] = '0'; + onDataSent(0); + } + + dataSent(); + } + break; + } + + case 'q': //Min- / Max- / Mittelwert schicken + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'q' (send min/max/average) received"; + write_to_sd_log_single(string); + + connected_cancel.detach(); + clear_buffer(); + + unsigned int length = 0; + int a = 0; + + char string_1[] = "Sending min/max/average.."; + write_to_sd_log_single(string_1); + + fileSystem.unmount(); + fflush(stdout); + + fileSystem.mount(&blockDevice); + FILE * messdaten = fopen ("/fs/messdaten.csv", "rb"); + + fseek (messdaten, 0, SEEK_END); + length = ftell (messdaten); + fseek (messdaten, 0, SEEK_SET); + + /* Daten senden */ + + unsigned long ch = 0; + unsigned short lines = 0; + unsigned long i = 0; + unsigned long j = 1; + unsigned long c = 0; + unsigned long e = 0; + + while(!feof(messdaten)){ + ch = fgetc(messdaten); + if(ch == '\n') + { + lines++; + } + } + + printf("\n%d Eintraege\n", lines); + + for(int a = 0; a < lines; a++) + { + messwerte[a] = *new int[8]; + } + + char temp[8]; + + fseek(messdaten, 0, SEEK_SET); + while(!feof(messdaten)){ + ch = fgetc(messdaten); + if(ch == '\n'){ + fseek(messdaten, i - 7, SEEK_SET); + fread (temp, 1, 7, messdaten); + messwerte[e] = atoi(temp); + memset(&temp[0], 0, sizeof(temp)); + e++; + }else if(ch == EOF){ + fseek(messdaten, i - 8, SEEK_SET); + fread (temp, 1, 8, messdaten); + messwerte[e] = atoi(temp); + memset(&temp[0], 0, sizeof(temp)); + } + i++; + } + + unsigned long summe = 0; + + for(int q = 1; q < e; q++){ + summe += messwerte[q]; + } + printf("\nSumme: %lu\n", summe); + + double avr = summe / lines; + + fclose(messdaten); + fileSystem.unmount(); + fflush(stdout); + + char string_2[] = "Sending min/max/average complete"; + write_to_sd_log_single(string_2); + + quickSort(1, lines); + + if((int)avr != 0){ + avr = ceil( avr * 100.0 ) / 100.0; + } + + char min[5]; + char max[5]; + char durch[5]; + + sprintf(min,"%d", messwerte[1]); + sprintf(max,"%d", messwerte[lines]); + sprintf(durch,"%d", (int)avr); + + printf("\n%s, %s, %s\n", min, max, durch); + + int k = 0; + while(k < min[k] != NULL){ + ble_buff[0][k] = min[k]; + onDataSent(k); + k++; + } + ble_buff[0][k] = 0x3B; + onDataSent(k); + k++; + int p = 0; + while(p < max[p] != NULL){ + ble_buff[0][k] = max[p]; + onDataSent(k); + k++; + p++; + } + ble_buff[0][k] = 0x3B; + onDataSent(k); + k++; + int z = 0; + while(z < durch[z] != NULL){ + ble_buff[0][k] = durch[z]; + onDataSent(k); + k++; + z++; + } + ble_buff[0][k] = 0x3B; + onDataSent(k); + k++; + + dataSent(); + + break; + } + + case 'j': //Anzahl der Messungen schicken + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'j' (send measurements count) received"; + write_to_sd_log_single(string); + + //Anzahl der Messdaten + messdaten_count(); + int i = 0; + while(messungen[i] != NULL){ + ble_buff[0][i] = messungen[i]; + onDataSent(i); + i++; + } + printf("\nMeasurements count: %s\n", messungen); + + dataSent(); + + break; + } + + case 'h': //Anzahl der Logs schicken + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'h' (send log count) received"; + write_to_sd_log_single(string); + + //Anzahl der Logs + log_count(); + int i = 0; + while(logs_array[i] != NULL){ + ble_buff[0][i] = logs_array[i]; + onDataSent(i); + i++; + } + printf("\nLogs count: %s\n", logs_array); + + dataSent(); + + break; + } + + case 'y': //Alarn AN / AUS - Anzahl der Meldungen + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'y' (Alert function and count) received"; + write_to_sd_log_single(string); + + if(user_config_para.alert == 1){ + printf("\nAlert function - ON\n"); + + printf("\nNumber of alerts: %d\n", alert_count); + char alerts[4]; + sprintf(alerts, "%d", alert_count); + int i = 0; + while(alerts[i] != NULL){ + ble_buff[0][i] = alerts[i]; + onDataSent(i); + i++; + } + }else{ + printf("\nAlert function - OFF\n"); + + ble_buff[0][0] = '0'; + onDataSent(0); + } + + dataSent(); + + break; + } + + case 'k': //Initialisierungsstatus auslesen + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'b' (send initialize status) received"; + write_to_sd_log_single(string); + + if(init_status == true){ + ble_buff[0][0] = '1'; + onDataSent(0); + + printf("\nInitialaze status: OK\n"); + }else{ + ble_buff[0][0] = '0'; + onDataSent(0); + + printf("\nInitialaze status: FAILED\n"); + } + + dataSent(); + + break; + } + + case 'p': //Mess-Status auslesen + { + connected_cancel.detach(); + clear_buffer(); + + char string[] = "Command 'p' (send measurement status) received"; + write_to_sd_log_single(string); + + if(shipping == true){ + ble_buff[0][0] = 1; + onDataSent(0); + + wait(1); + clear_buffer(); + + if(pause == false){ + ble_buff[0][0] = '1'; + onDataSent(0); + + printf("\nMeasuremt status = Launched\n"); + }else{ + ble_buff[0][0] = '0'; + onDataSent(0); + + printf("\nMeasuremt status = Paused\n"); + } + + }else{ + ble_buff[0][0] = '0'; + onDataSent(0); + + printf("\nMeasuremt status = Sttopped\n"); + } + + dataSent(); + + break; + } + + default: { + //clear_buffer(); + response = '0'; + break; + } + } + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ble_functions.h Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,25 @@ +#include "mbed.h" +#include <BLE.h> +#include "ble/BLE.h" + +void periodicCallback(void); +void writeLedCallback(void); +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *); +void updateSensorValue(); +void printMacAddress(); +void writeCharCallback(const GattWriteCallbackParams *params); +void disconnect(); +void whenConnected(const Gap::ConnectionCallbackParams_t *); +void onDataSent(unsigned count); +void gattServer_onUpdatesEnabled(GattAttribute::Handle_t handle); +void gattServer_onUpdatesDisabled(GattAttribute::Handle_t handle); +void gattServer_onConfirmationReceived(GattAttribute::Handle_t handle); +void after_BL(); +void BT_false(); +void BT_true(); +void log_count(); +void messdaten_count(); +void unixtime_to_char_array(); +int array_to_int(int z); +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params); +void BLE_handler(); \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/functions.cpp Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,968 @@ +#include "functions.h" +#include "ble_functions.h" +#include "RawSerial.h" +#include <InterruptIn.h> +#include <InterruptManager.h> +#include "bmi160.h" +#include "max32630fthr.h" +#include "max3263x.h" +#include "MAX14690.h" +#include "ADT7410.h" +//#include "Thermistor.h" +#include "Adafruit_SSD1306.h" +#include <BLE.h> +#include "ble/BLE.h" +#include "ble/Gap.h" +#include "SDBlockDevice.h" +#include "FATFileSystem.h" +#include <errno.h> +#include <stdio.h> +#include <time.h> +#include <stdint.h> + +extern char device_id[]; +extern unsigned short int measure_id; +extern unsigned short int log_id; + +extern Serial pan1326b; +extern DigitalOut bt_rst; + +extern volatile int conn_state; +extern bool in_BT; + +extern AnalogIn battery; + +extern DigitalOut r; +extern DigitalOut g; +extern DigitalOut b; +extern DigitalOut RGBs[]; + +extern MAX32630FTHR pegasus; + +extern ADT7410 myADT7410; +//extern Thermistor extruder; +extern I2C i2cm2; +extern MAX14690 max14690; + +extern InterruptIn button; + +extern FATFileSystem fileSystem; +extern SDBlockDevice blockDevice; + +extern I2C i2c; +extern int sensor_temp; +extern Adafruit_SSD1306_I2c featherOLED; + +extern volatile int quit; +extern volatile int ble_return; +extern volatile int conn_state; +extern int counter_ble; + +extern bool error_status; +extern bool BT_error; +extern bool shipping; +extern bool pause; +extern bool init_status; +extern bool after_BT; +extern bool charge; + +extern Timeout after_BLE; +extern Timeout turnoff; +extern Timeout flipper; // +extern Timeout advertise_cancel; // Zum abbrechen von BT bei keinem Verbindungsaufbau +extern Timeout connected_cancel; // Zum abbrechen von BT bei kein Befehlempfang +extern Ticker mess_timer; // ticker eventuell nur bis 30 Minuten geeignet +extern Ticker ticker; +extern time_t now; +extern time_t raw; + +extern float buffer_temp; +extern struct tm current; +extern struct tm actual; + +extern struct user_config_struct user_config_para; +extern struct tm user_config_time; + +extern Timeout done_rcv; + +extern int next_state; + +extern int unsigned long t_diff; +extern int unsigned long unix_time; + +extern int default_advertise_time; +extern int default_connected_time; + +extern int alert_count; + +void turnON(){ + max14690.writeReg(MAX14690::REG_PWR_CFG, 0x1D); + char string[] = "Coldchainlogger turned on"; + write_to_sd_log_single(string); +} + +void reboot(){ + char string[] = "Rebooting Coldchainlogger..."; + write_to_sd_log_single(string); + printf("\n__________________________\n\nRebooting Coldchainlogger\n__________________________\n\n"); + log_id -= 2; + BT_false(); + wait(0.2); + NVIC_SystemReset(); +} + +void set_time(){ + unix_time = 946684800; //946684800: 2000-01-01 00:00:00 + now = time(0); + time(&now); + set_time(unix_time); +} + +int set_app_time(unsigned long device_time){ + printf("\nDevice time: %ld\n", device_time); + if(device_time >= 946684800){ + unix_time = device_time; + now = time(0); + time(&now); + set_time(unix_time); + + tm *current; + time(&now); + current = localtime (&now); + char string[] = "Device time updated"; + write_to_sd_log_single(string); + printf ("\nDate: 20%02d-%02d-%02d %02d:%02d:%02d\r\n", current->tm_year - 100, current->tm_mon + 1, current->tm_mday, current->tm_hour, current->tm_min, current->tm_sec); + printf("\n__________________________\n\nNew device time applied\n__________________________\n\n"); + + return 1; + }else{ + printf("\n__________________________\n\nCan not applie new device time\n__________________________\n\n"); + + return -1; + } +} + +void write_to_sd_log_single(char log_string[]) +{ + fileSystem.unmount(); + fflush(stdout); + + fileSystem.mount(&blockDevice); + + char file_name[14] = "/fs/log"; + char file_type[10] = ".csv"; + char buff_file[25]; + sprintf(buff_file, "%s%s", file_name, file_type); + FILE * f = fopen(buff_file, "a"); + + log_id++; + + tm *current; + time(&now); + current = localtime (&now); + fprintf(f, "%d;20%02d-%02d-%02d;%02d:%02d:%02d;%s;\r\n", log_id, current->tm_year - 100, current->tm_mon + 1, current->tm_mday, current->tm_hour, current->tm_min, current->tm_sec, log_string); + + fclose(f); + fileSystem.unmount(); +} + +void write_to_sd_messdaten_single(float buffer_temp) +{ + fileSystem.unmount(); + fflush(stdout); + + + fileSystem.mount(&blockDevice); + + char file_name[14] = "/fs/messdaten"; + char file_type[10] = ".csv"; + char buff_file[25]; + sprintf(buff_file, "%s%s", file_name, file_type); + FILE * f = fopen(buff_file, "a"); + + int buffer_conv = (int)(100 * buffer_temp); + + measure_id++; + + tm *current; + time(&now); + current = localtime (&now); + fprintf(f, "%d;20%02d-%02d-%02d;%02d:%02d:%02d;%d;%4d;\r\n", measure_id, current->tm_year - 100, current->tm_mon + 1, current->tm_mday, current->tm_hour, current->tm_min, current->tm_sec, sensor_temp, buffer_conv); + /* + if(sensor_temp == 2){ + sensor_temp = 1; + }else{ + sensor_temp = 2; + } + */ + fclose(f); + fileSystem.unmount(); +} + +void get_save_Messpunkt(float temperatur) +{ + buffer_temp = temperatur; + + tm *current; + time(&now); + current = localtime (&now); + write_to_sd_messdaten_single(buffer_temp); + + printf ("Measurement %d saved 20%02d-%02d-%02d %02d:%02d:%02d\r\n", measure_id, current->tm_year - 100, current->tm_mon + 1, current->tm_mday, current->tm_hour, current->tm_min, current->tm_sec); +} + +float get_Messwert() +{ + float temperatur = 0; + //float temperatur_2 = 0; + if(myADT7410.getTemp()){ + temperatur = myADT7410.getTemp(); + //temperatur_2 = extruder.get_temperature(); + wait(0.3); + printf("\nTemperature Sensor mesureued: %.2f C\n", temperatur); + // printf("Temperature Thermistor mesureued: %.2f C\n\n", temperatur_2); + + if(user_config_para.alert == 1){ + if(user_config_para.minimum > temperatur){ + char string[] = "ALERT: lower threshold limit below"; + write_to_sd_log_single(string); + + printf("ALERT: lower threshold limit below (lower limit: %d C)\n", user_config_para.minimum); + }else if(user_config_para.maximum < temperatur){ + char string[] = "ALERT: upper threshold exceeded"; + write_to_sd_log_single(string); + + printf("ALERT: upper threshold exceeded (upper limit: %d C)\n", user_config_para.maximum); + } + alert_count++; + } + + get_save_Messpunkt(temperatur); + //get_save_Messpunkt(temperatur_2); + + return temperatur; + }else{ + char string[] = "Temperature sensor not detected, unable to measure"; + write_to_sd_log_single(string); + printf("\nTemperature sensor not detected, unable to measure!\n"); + + return -278.0; + } +} + +int create_file_messdaten(){ + fileSystem.unmount(); + fflush(stdout); + + int file_err = fileSystem.mount(&blockDevice); + if(file_err){ + fileSystem.unmount(); + + char string[] = "SD mount error"; + write_to_sd_log_single(string); + + printf("\n__________________________\n\n! No SD-card detected !\n__________________________\n\n"); + + return -1; + }else{ + + char file_name[14] = "/fs/messdaten"; + char file_type[10] = ".csv"; + char buff_file[25]; + sprintf(buff_file, "%s%s", file_name, file_type); + FILE * f = fopen(buff_file, "a"); + + fclose(f); + fileSystem.unmount(); + + return 1; + } +} + +int create_file_log() +{ + fileSystem.unmount(); + fflush(stdout); + + int file_err = fileSystem.mount(&blockDevice); + if(file_err){ + fileSystem.unmount(); + + char string[] = "SD mount error"; + write_to_sd_log_single(string); + + printf("\n__________________________\n\n! No SD-card detected !\n__________________________\n\n"); + + return -1; + }else{ + + char file_name[14] = "/fs/log"; + char file_type[10] = ".csv"; + char buff_file[25]; + sprintf(buff_file, "%s%s", file_name, file_type); + FILE * f = fopen(buff_file, "a"); + + fclose(f); + fileSystem.unmount(); + + return 1; + } +} + +int delete_file_messdaten(){ + fileSystem.unmount(); + fflush(stdout); + + int file_err = fileSystem.mount(&blockDevice); + if(file_err) + { + char string[] = "SD mount error"; + write_to_sd_log_single(string); + + printf("\n__________________________\n\n! No SD-card detected !\n__________________________\n\n"); + + fileSystem.unmount(); + + return -1; + }else{ + remove("/fs/messdaten.csv"); + wait(0.2); + fileSystem.unmount(); + fflush(stdout); + + char string_1[] = "Measuring data file deleted"; + write_to_sd_log_single(string_1); + + if(create_file_messdaten() == 1){ + + char string[] = "New measuring data file created"; + write_to_sd_log_single(string); + + fileSystem.unmount(); + + measure_id = 0; + + printf("\n__________________________\n\nMeasuring data deleted\n__________________________\n\n"); + return 1; + }else{ + fileSystem.unmount(); + printf("\nError while creating new file\n"); + return -1; + } + } +} + +int delete_file_log(){ + fileSystem.unmount(); + fflush(stdout); + + int file_err = fileSystem.mount(&blockDevice); + if(file_err) + { + char string[] = "SD mount error"; + write_to_sd_log_single(string); + + printf("\n__________________________\n\n! No SD-card detected !\n__________________________\n\n"); + + fileSystem.unmount(); + + return -1; + }else{ + + remove("/fs/log.csv"); + wait(0.2); + /* + char string_1[] = "Log file deleted"; + write_to_sd_log_single(string_1); + */ + if(create_file_log() == 1){ + + log_id = 0; + + char string[] = "New log data file created"; + write_to_sd_log_single(string); + + fileSystem.unmount(); + + printf("\n__________________________\n\nLog deleted\n__________________________\n\n"); + return 1; + }else{ + fileSystem.unmount(); + printf("\nError while creating new file\n"); + return -1; + } + } +} + +int file_copy(const char *src, const char *dst) +{ + fileSystem.unmount(); + fflush(stdout); + + int retval = 0; + int ch; + + int file_err = fileSystem.mount(&blockDevice); + if(file_err) + { + printf("\n__________________________\n\n! No SD-card detected !\n__________________________\n\n"); + fileSystem.unmount(); + + return -1; + }else{ + + FILE *fpsrc = fopen(src, "r"); // src file + FILE *fpdst = fopen(dst, "w"); // dest file + + while (1) { // Copy src to dest + ch = fgetc(fpsrc); // until src EOF read. + if (ch == EOF) break; + fputc(ch, fpdst); + } + fclose(fpsrc); + fclose(fpdst); + + fpdst = fopen(dst, "r"); // Reopen dest to insure + if (fpdst == NULL) { // that it was created. + retval = -1; // Return error. + } else { + fclose(fpdst); + retval = 0; // Return success. + } + return retval; + } +} + +int load_user_config() +{ + printf("\n__________________________\n\nReading the config\n__________________________\n\n"); + + fileSystem.unmount(); + fflush(stdout); + + int file_err = fileSystem.mount(&blockDevice); + if(file_err) + { + printf("\n__________________________\n\n! No SD-card detected !\n__________________________\n\n"); + fileSystem.unmount(); + + return -1; + }else{ + + FILE * conf = fopen ("/fs/user_config.csv", "rb"); + char buffer[700]; + int length; + + if (conf) + { + fseek (conf, 0, SEEK_END); + length = ftell (conf); + fseek (conf, 0, SEEK_SET); + + if (buffer) + { + fread (buffer, 1, length, conf); + } + fclose(conf); + fileSystem.unmount(); + + if (buffer) + { + char* single_word[100]; + int word_count = 0; + char delim[] = ";\r\n"; + char *ptr = strtok(buffer, delim); + + for(int j = 0; j < 100; j++) + { + single_word[j] = new char[15]; + } + + while (ptr != NULL) + { + strcpy(single_word[word_count], ptr); + ptr = strtok(NULL, delim); + word_count++; + } + + const char *params[] = {"interval", "alert", "minimum", "maximum", "wait_mode", "pre_delay", "advertise", "connected"}; + for(int k = 0; k < 100; k++) + { + for(int l = 0; l < 8; l++) + { + if(strcmp(single_word[k], params[0]) == 0){ + user_config_para.interval = atoi(single_word[k+1]); + } + else if(strcmp(single_word[k], params[1]) == 0){ + user_config_para.alert = atoi(single_word[k+1]); + } + else if(strcmp(single_word[k], params[2]) == 0){ + user_config_para.minimum = atoi(single_word[k+1]); + } + else if(strcmp(single_word[k], params[3]) == 0){ + user_config_para.maximum = atoi(single_word[k+1]); + } + else if(strcmp(single_word[k], params[4]) == 0){ + user_config_para.wait_mode = atoi(single_word[k+1]); + } + else if(strcmp(single_word[k], params[5]) == 0){ + user_config_para.pre_delay = atoi(single_word[k+1]); + } + else if(strcmp(single_word[k], params[6]) == 0){ + user_config_para.advertise = atoi(single_word[k+1]); + } + else if(strcmp(single_word[k], params[7]) == 0){ + user_config_para.connected = atoi(single_word[k+1]); + } + } + } + + //printf("\nID: #%s\n", user_config_para.id); + + //char idnummer[10]; + //sscanf(idnummer, "%d", &user_config_para.id); + + printf("\nLogger-ID: #%s\n", device_id); + if(user_config_para.interval == 1){ + printf("Mesureual interval: %d second\n", user_config_para.interval); + }else{ + printf("Mesureual interval: %d seconds\n", user_config_para.interval); + } + if(user_config_para.alert == 1){ + printf("Alert-function - ON\n"); + printf("Minimum temperature: %d C | Maximum temperature: %d C\n", user_config_para.minimum, user_config_para.maximum); + }else{ + printf("Alert-function - OFF\n"); + } + printf("BLE advertise timer: %d seconds\n", user_config_para.advertise); + printf("BLE when connected timer: %d seconds\n", user_config_para.connected); + + if(user_config_para.wait_mode == 1){ + printf("Pre-delay - ON: %d seconds\n", user_config_para.pre_delay); + }else{ + printf("Pre-delay - OFF\n"); + } + } + else{ + printf("Buffer empty: %s", buffer); + } + return 1; + } + else{ + printf("!!! Config file not found, error: %s (%d)\n", strerror(errno), -errno); + fileSystem.unmount(); + user_config_para.advertise = default_advertise_time; + user_config_para.connected = default_connected_time; + return -1; + } + } +} + +int load_standard_config(){ + fileSystem.unmount(); + fflush(stdout); + + printf("\n__________________________\n\nReseting config settings \nto default\n__________________________\n\n"); + + int file_err = fileSystem.mount(&blockDevice); + + if(file_err){ + printf("\n__________________________\n\n! No SD-card detected !\n__________________________\n\n"); + fileSystem.unmount(); + + return -1; + }else{ + remove("/fs/user_config.csv"); + wait(0.2); + + if(file_copy("/fs/standard_config.csv", "/fs/user_config.csv") == 0){ + load_user_config(); + + printf("\n__________________________\n\nConfig settings reseted\n__________________________\n\n"); + char string[] = "Standard user config file created"; + write_to_sd_log_single(string); + fileSystem.unmount(); + + return 1; + }else{ + printf("\n__________________________\n\nFailed to reset config file\n__________________________\n\n"); + char string[] = "Failed to copy standard config file"; + write_to_sd_log_single(string); + fileSystem.unmount(); + + return -1; + } + } +} + +int read_id() +{ + fileSystem.unmount(); + fflush(stdout); + + int file_err = fileSystem.mount(&blockDevice); + if(file_err) + { + printf("\n__________________________\n\n! No SD-card detected !\n__________________________\n\n"); + fileSystem.unmount(); + + return -1; + }else{ + + FILE * id = fopen ("/fs/id.csv", "rb"); + char buffer[14]; + int length; + + if (id) + { + fseek (id, 0, SEEK_END); + length = ftell (id); + fseek (id, 0, SEEK_SET); + + if (buffer) + { + fread (buffer, 1, length, id); + } + fclose(id); + fileSystem.unmount(); + + if (buffer) + { + char* single_word[10]; + int word_count = 0; + char delim[] = ";\r\n"; + char *ptr = strtok(buffer, delim); + + for(int j = 0; j < 10; j++) + { + single_word[j] = new char[10]; + } + + while (ptr != NULL) + { + strcpy(single_word[word_count], ptr); + ptr = strtok(NULL, delim); + word_count++; + } + + strcpy(device_id, single_word[1]); + return 1; + } + else{ + printf("Buffer empty: %s", buffer); + return -1; + } + } + else{ + printf("!!! Config file not found, error: %s (%d)\n", strerror(errno), -errno); + fileSystem.unmount(); + return -1; + } + } +} + +int create_user_config(char params[]){ + fileSystem.unmount(); + fflush(stdout); + + int file_err = fileSystem.mount(&blockDevice); + if(file_err){ + printf("\n__________________________\n\n! No SD-card detected !\n__________________________\n\n"); + fileSystem.unmount(); + + return -1; + }else{ + remove("/fs/user_config.csv"); + wait(0.2); + char string[] = "User config file deleted"; + write_to_sd_log_single(string); + fileSystem.unmount(); + + fileSystem.mount(&blockDevice); + char file_name[16] = "/fs/user_config"; + char file_type[10] = ".csv"; + char buff_file[27]; + sprintf(buff_file, "%s%s", file_name, file_type); + FILE * f = fopen(buff_file, "a"); + + fclose(f); + + char string_1[] = "User config file created"; + write_to_sd_log_single(string_1); + + fileSystem.unmount(); + + char *single_word[15]; + int word_count = 0; + char delim[] = ";\r\n"; + char *ptr = strtok(params, delim); + for(int j = 0; j < 9; j++) + { + single_word[j] = new char[6]; + } + + while (ptr != NULL) + { + strcpy(single_word[word_count], ptr); + ptr = strtok(NULL, delim); + word_count++; + } + printf("interval;%s;\r\nalert;%s;\r\nminimum;%s;\r\nmaximum;%s;\r\nwait_mode;%s;\r\npre_delay;%s;\r\nadvertise;%s;\r\nconnected;%s;", + single_word[0], single_word[1], single_word[2], single_word[3], single_word[4], single_word[5], single_word[6], single_word[7]); + + fileSystem.mount(&blockDevice); + FILE * fp = fopen("/fs/user_config.csv", "w"); + + fprintf(fp, "interval;%s;\r\n", single_word[0]); + fprintf(fp, "alert;%s;\r\n", single_word[1]); + fprintf(fp, "minimum;%s;\r\n", single_word[2]); + fprintf(fp, "maximum;%s;\r\n", single_word[3]); + fprintf(fp, "wait_mode;%s;\r\n", single_word[4]); + fprintf(fp, "pre_delay;%s;\r\n", single_word[5]); + fprintf(fp, "advertise;%s;\r\n", single_word[6]); + fprintf(fp, "connected;%s;\r\n", single_word[7]); + + fclose(fp); + fileSystem.unmount(); + + if(load_user_config() == 1){ + return 1; + }else{ + printf("\nFaild to read the config!\n"); + return -1; + } + } +} + +void led_blink(int led, int anzahl, int lang, int pause){ + for(int i = 0; anzahl > i; i++){ + RGBs[led] = 0; + if(lang == 1){ + wait(1.5); + }else{ + wait(0.5); + } + RGBs[led] = 1; + wait(0.5); + } + if(pause == 1){ + wait(1); + } +} + +void error_handler(int error) +{ + ticker.detach(); + g = 1; + wait(1); + + int file_err = fileSystem.mount(&blockDevice); + if(file_err){ + switch (error) { + case 10:{ //led_blink(0, 2, 1, 1); + } + break; + case 11:{ BT_error = true; //led_blink(0, 1, 1, 1); led_blink(0, 2, 1, 1); + } + break; + case 110:{ //led_blink(0, 2, 1, 1); led_blink(0, 3, 1, 1); + } + break; + case 111:{ BT_error = true; //led_blink(0, 1, 1, 1); led_blink(0, 2, 1, 1); led_blink(0, 3, 1, 1); + } + break; + } + fileSystem.unmount(); + }else{ + tm *current; + time(&now); + current = localtime (&now); + + switch (error) { + case 0:{ char string[] = "Initializing succesfull"; write_to_sd_log_single(string); led_blink(1, 1, 0, 0); + } + break; + case 1:{ char string[] = "Initializing failed: Bluetooth not detected"; write_to_sd_log_single(string); BT_error = true; //led_blink(0, 1, 1, 1); + } + break; + case 100:{ char string[] = "Initializing failed: Temperature sensor not detected"; write_to_sd_log_single(string); //led_blink(0, 3, 1, 1); + } + break; + case 101:{ char string[] = "Initializing failed: Temperature sensor and bluetooth not detected"; write_to_sd_log_single(string); BT_error = true; //led_blink(0, 1, 1, 1); led_blink(0, 3, 1, 1); + } + break; + } + fileSystem.unmount(); + } + r = 1; + g = 1; + b = 1; + wait(0.5); +} + +int check_devices() +{ + printf("\nDevices check...\n\n"); + + int bl = 1; + int sd = 10; + int temp = 100; + + //myADT7410.setConfig(ONE_SPS_MODE); // reduce sample rate to save power + //printf("Config: 0x%x\n", myADT7410.getConfig()); + /* + int count = 0; + for (int address = 0; address < 255; address += 2) { // check only for device's read address + if (!i2c.write(address, NULL, 0)) { // 0 returned is ok + printf("I2C address 0x%02X\n", address); + count++; + } + } + printf("%d devices found\n\n\n", count); + wait_ms(20); + */ + + //BLE check + bt_rst = 1; + bt_rst = 0; + wait(0.5); + BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); + ble.init(bleInitComplete); + if(ble.hasInitialized() == true){ + bl = 0; + BT_error = false; + printf("\nBLE detected\n"); + printMacAddress(); + }else{ + BT_error = true; + printf("\nBLE not detected!\n"); + } + + //SD check + int file_err = fileSystem.mount(&blockDevice); + if(file_err) + { + printf("\nSD-card not detected\n"); + BT_error = true; + } + else { + sd = 0; + create_file_log(); + create_file_messdaten(); + printf("\nSD-card detected\n"); + + fileSystem.unmount(); + + messdaten_count(); + printf("Measuremnts count: %d\n", measure_id); + log_count(); + printf("Logs count: %d\n", log_id); + } + fileSystem.unmount(); + + //Temp sensor check + myADT7410.setConfig(ONE_SPS_MODE); + if(myADT7410.getConfig() == 0x40){ + temp = 0; + printf("\nTemperature sensor detected\n"); + myADT7410.reset(); + myADT7410.setConfig(01); + }else{ + printf("\nTemperature sensor not detected\n"); + } + + //Check output + int devices = temp + sd + bl; + + if(devices != 000){ + printf("\nError in check_devices(): %03d\n", devices); + + error_handler(devices); + + return 0; + }else{ + printf("\n__________________________\n\nAll devices connected\n__________________________\n\n"); + + return 1; + } +} + +int RTC_check(){ + tm *current; + time(&now); + if ( (current = localtime (&now) ) == NULL) { + printf ("\n\n__________________________\n\n! RTC initialize failed !\n__________________________\n"); + return -1; + }else{ + set_time(); + printf ("\n\n__________________________\n\nRTC initialized\n__________________________\n"); + return 1; + } +} + +void max14690_init(){ + max14690.resetToDefaults(); + max14690.ldo2Millivolts = 3300; + max14690.ldo3Millivolts = 3300; + max14690.ldo2Mode = MAX14690::LDO_ENABLED; + max14690.ldo3Mode = MAX14690::LDO_ENABLED; + max14690.chgEn = 1; + //max14690.intEnChgStatus = 0; + //max14690.iLimCntl = MAX14690::iLimCntl_t::ILIM_1000mA; + //max14690.batReg = MAX14690::batReg_t::BAT_REG_4350mV; + max14690.intEnUSBOVP = 1; + max14690.monCfg = MAX14690::MON_BAT; //Monitoring - Multiplexer auf SYSTEM eingestellt, um Akku-wert auszulesen + //max14690.monRatio = MAX14690::MON_DIV4; + //max14690.ldo3Mode = MAX14690::SW_EN_MPC1_DSC; + if (max14690.init() == MAX14690_ERROR) { + printf("Error initializing MAX14690"); + } +} + +void initCallback(void){ + g = !g; +} + +void init(){ + r = 1; + g = 1; + b = 1; + + printf("\n\n__________________________\n\nStarting Coldchainlogger\n__________________________\n\n"); + ticker.attach(initCallback, 0.5); + + printf("Initializing MAX32630FTHR\n\n"); + pegasus.init(MAX32630FTHR::VIO_3V3); + + printf("Initializing MAX14690N\n"); + max14690_init(); + + if(RTC_check() == 1){ + if(check_devices() == 1 & read_id() == 1 & load_user_config() == 1){ + printf("\n__________________________\n\nInitializing succesfull\n__________________________\n\n"); + BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); + ticker.detach(); + g = 1; + init_status = true; + error_status = false; + error_handler(000); + }else{ + printf("\n__________________________\n\nInitializing failed\n__________________________\n\n"); + BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); + next_state = 0; + init_status = false; + error_status = true; + ticker.detach(); + g = 1; + led_blink(0, 1, 1, 1); + } + }else{ + printf("\n__________________________\n\nRTC failure\n__________________________\n\n"); + error_status = true; + init_status = false; + next_state = 0; + ticker.detach(); + g = 1; + led_blink(0, 1, 1, 1); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/functions.h Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,42 @@ +#include "mbed.h" + +void turnON(); +void reboot(); +void messdaten_count(); +void set_time(); +int set_app_time(unsigned long device_time); +void write_to_sd_log_single(char log_string[]); +void write_to_sd_messdaten_single(float buffer_temp, struct timestamp current); +void write_to_sd_messdaten_all(float *buffer_temp, struct timestamp *current); +void get_save_Messpunkt(float temperatur); +float get_Messwert(); +int create_file_messdaten(); +int create_file_log(); +int min_max_avr(); +int delete_file_messdaten(); +int delete_file_log(); +int file_copy(const char *src, const char *dst); +int load_user_config(); +int load_standard_config(); +int read_id(); +int create_user_config(char params[]); +void led_blink(int led, int anzahl, int lang, int pause); +void error_handler(int error); +int check_devices(); +void mess_handler(); +int RTC_check(); +void max14690_init(); +void initCallback(void); +void init(); + +struct user_config_struct +{ + int interval; + int alert; + int minimum; + int maximum; + int wait_mode; + int pre_delay; + int advertise; + int connected; +}; \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,286 @@ +#include "mbed.h" +#include "functions.h" +#include "ble_functions.h" + +#include "RawSerial.h" +#include "bmi160.h" +#include "max32630fthr.h" +#include "max3263x.h" +#include "MAX14690.h" +#include "ADT7410.h" +#include "Adafruit_SSD1306.h" +//#include "Thermistor.h" +//#include "ds1621.h" + +#include <BLE.h> +#include "ble/BLE.h" +#include "ble/Gap.h" + +#include "SDBlockDevice.h" +#include "FATFileSystem.h" + +#include <errno.h> +#include <stdio.h> +#include <time.h> +#include <stdint.h> +#include <InterruptIn.h> +#include <InterruptManager.h> + +//MAX32630FTHR +MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); + +//MAX14690 Wearable Power-Management Solution Driver +I2C i2cm2(I2C2_SDA, I2C2_SCL); +MAX14690 max14690(&i2cm2); + +char device_id[10]; + +int sensor_temp = 1; +unsigned short int measure_id = 0; +unsigned short int log_id = 0; + +//SD-Karte +FATFileSystem fileSystem("fs"); +SDBlockDevice blockDevice(P0_5, P0_6, P0_4, P0_7); // mosi, miso, sck, cs + +//Bluetooth-Modul PAN1326B +RawSerial pan1326b(P0_1, P0_0); //TX, RX +DigitalOut bt_rst (P1_6); //Bluetooth reset + +//I2C +I2C i2c(P3_4, P3_5); //SDA, SCL + +//Temperatursensor +ADT7410 myADT7410(P3_4, P3_5, 0x90, 100000); //SDA, SCL, Address, Hz + +//Termistor +//Thermistor extruder(AIN_1); + +//DS1621 +//#define DS1621_ADDR 0 // I2c DS1621 address is 0x00 +//DS1621 ds(&i2c, DS1621_ADDR); // Declare ds1621 throught i2c interface + +//Akku-monitoring +AnalogIn battery(AIN_0); + +//USB-monitroing +AnalogIn usb(AIN_5); + +//LEDs +DigitalOut r (P2_4); //Rote LED +DigitalOut g (P2_5); //Grüne LED +DigitalOut b (P2_6); //Blaue LED +DigitalOut RGBs[] = {r, g, b}; //LEDs Liste +/* +DigitalOut r (P5_6); //Rote LED +DigitalOut g (P5_5); //Grüne LED +DigitalOut b (P5_4); //Blaue LED +DigitalOut RGBs[] = {r, g, b}; //LEDs Liste +*/ +//Button interrupt +InterruptIn button(P2_3, PullUp); //Auf Pull Up eingestellt +//InterruptIn button(P5_3, PullUp); //BT +//InterruptIn button2(P3_3, PullUp); //BT + +//I2C OLED +Adafruit_SSD1306_I2c featherOLED(i2c); +/* +//OLED Buttons +InterruptIn aButton(P5_3, PullUp); +InterruptIn bButton(P3_3, PullUp); +InterruptIn cButton(P3_2, PullUp); +*/ +Timeout after_BLE; +Timeout led_check; //Zum Überprüfen, ob Datenlogger an ist +Timeout turnoff; //Zum Auschalten (nicht implementiert) +Timeout flipper; //Zum Bluetooth an / aus +Timeout advertise_cancel; //Zum abbrechen von BT bei keinem Verbindungsaufbau +Timeout connected_cancel; //Zum abbrechen von BT bei kein Befehlempfang +Ticker mess_timer; //ticker eventuell nur bis 30 Minuten geeignet +Ticker ticker; +Ticker charger; + +time_t now; //Zeit Funktionen +time_t raw; + +float buffer_temp; +struct tm current = {0}; //tm - Zeit im Kalenderformat +struct tm actual = {0}; + +volatile int quit = 0; //BLE Advertise Flag +volatile int conn_state = 0; //Verbindungsstatus für BLE + +bool after_BT = false; +bool in_BT = false; //Flag für BLE ist an +bool error_status = false; //Wenn es ein Fehler beim Initialisierung gab +bool BT_error = false; //Wenn es BLE oder SD Fehler ist, dann werden die Button Interrupts nicht deklariert +bool shipping = false; //Sendevorgang Status +bool pause = true; //Pause während Sendevorgang +bool init_status = false; //Erfolgreiche Initialisierung Status +bool charge = false; //Wenn DL an Stromnetz angeschlossen ist + +struct tm user_config_time; +struct user_config_struct user_config_para = {0, 0, 0, 0, 0, 0, 0}; //Liste für Config Parameter + +int next_state = 0; //State-Machine + +int unsigned long unix_time = 0; //Zeit in Unix Zeitformat + +int default_advertise_time = 300; //Default Timer für BLE advertising +int default_connected_time = 180; //Default Timer für BLE verbunden + +int alert_count = 0; //Alarme Zähler + +void turnOFF(){ + if(BT_error == false){ + in_BT = false; + BT_false(); + } + next_state = 4; +} + +void flip(){ + if(BT_error == false){ + in_BT = !in_BT; + if(in_BT == true){ + BT_true(); + }else if(in_BT == false){ + BT_false(); + } + } + turnoff.attach(&turnOFF, 4); // Button für weitere 4 Sekunden halten um Datenlogger auszuschalten +} + +void onCheck(){ + r = 1; + g = 1; +} + +void pushed() +{ + flipper.attach(&flip, 1); // Button für eine Sekunde gedrückt halten um BT zu aktivieren +} + +void released() { + if(quit == 0 && charge == false && after_BT == false){ + if(error_status == true){ + r = 0; + }else if(shipping == true && pause == false){ + r = 0; + g = 0; + }else{ + g = 0; + } + led_check.attach(&onCheck, 3); + } + flipper.detach(); + turnoff.detach(); +} + +void pre_delay(){ + for(int i = 0; user_config_para.pre_delay > i; i++){ + r = 0; + g = 0; + b = 0; + printf("\nPre-delay: %d of %d seconds\n", i + 1, user_config_para.pre_delay); + wait_ms(500); + r = 1; + g = 1; + b = 1; + wait_ms(500); + } +} + +void mess_handler(){ + next_state = 1; +} + +void chargeCheck(){ + +} + +void states(){ + switch(next_state){ + case 0: + {/* + printf("\n\nSleep\n\n"); + while ( next_state == 0 ){ + float batteryProcent = (100 / 0.271) * (battery.read() - 0.704); + if(usb.read() > 0.8){ + r = 0; + charge = true; + }else if(usb.read() < 0.2){ + r = 1; + charge = false; + } + + + }*/ + while ( next_state == 0 ){ + sleep(); + wait(0.1); + } + //wait(0.5); + break; + } + case 1: + { + get_Messwert(); + next_state = 0; + break; + } + case 2: + { + r = 1; + conn_state = 0; + quit = 1; + BLE_handler(); + break; + } + case 3: + { + pre_delay(); + next_state = 1; + mess_timer.attach(&mess_handler, (float)user_config_para.interval); // Mess-Timer attachen + break; + } + case 4: + { + b = 0; + r = 0; + if(BT_error == false){ + char string[] = "Shutting down Coldchainlogger..."; + write_to_sd_log_single(string); + } + printf("\n_______________________________\n\nShutting down Coldchainlogger\n_______________________________\n\n"); + wait(0.5); + max14690.shutdown(); + } + } +} + +int main() { + init(); + BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); + + if(BT_error == true){ + printf("\nBluetooth or SD initialize error: bluetooth can not be used\n"); + } + + __enable_irq(); + button.fall(&pushed); + button.rise(&released); + + while(true) { + /* + printf("ANALOG: %f\n", battery.read()); + float batteryProcent = (100 / 0.271) * (battery.read() - 0.704); + printf("PROCENT: %f\n", batteryProcent); + + printf("Extruder Temperature %f *C\r\n",extruder.get_temperature()); + printf("Sensor Temperature %f *C\r\n",myADT7410.getTemp()); + wait(.5); + */ + states(); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/max32630fthr.lib Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/MaximIntegrated/code/max32630fthr/#8f6e6a800f2f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#5941d1718339116cd12914238ec331c84da3d08f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Thu Jul 09 15:56:48 2020 +0000 @@ -0,0 +1,16 @@ +{ + "macros": [ + "MBED_HEAP_STATS_ENABLED=1", + "MBED_STACK_STATS_ENABLED=1", + "MBED_MEM_TRACING_ENABLED=1", + "MBED_CONF_SD_SPI_MOSI=P0_5", + "MBED_CONF_SD_SPI_MISO=P0_6", + "MBED_CONF_SD_SPI_CLK=P0_4", + "MBED_CONF_SD_SPI_CS=P0_7" + ], + "target_overrides": { + "*": { + "target.components_add": ["SD"] + } + } +} \ No newline at end of file