Firmware

Dependencies:   BMI160 ADT7410 Thermistor DS1621 max32630fthr Adafruit_FeatherOLED

Committer:
condato_mbed
Date:
Thu Jul 09 16:04:25 2020 +0000
Revision:
2:b802b30cc7d2
Parent:
0:556294574340
coldchainlogger

Who changed what in which revision?

UserRevisionLine numberNew contents of line
condato_mbed 0:556294574340 1 #include "mbed.h"
condato_mbed 0:556294574340 2 #include "functions.h"
condato_mbed 0:556294574340 3 #include "ble_functions.h"
condato_mbed 0:556294574340 4
condato_mbed 0:556294574340 5 #include "RawSerial.h"
condato_mbed 0:556294574340 6 #include "bmi160.h"
condato_mbed 0:556294574340 7 #include "max32630fthr.h"
condato_mbed 0:556294574340 8 #include "max3263x.h"
condato_mbed 0:556294574340 9 #include "MAX14690.h"
condato_mbed 0:556294574340 10 #include "ADT7410.h"
condato_mbed 0:556294574340 11 #include "Adafruit_SSD1306.h"
condato_mbed 0:556294574340 12 //#include "Thermistor.h"
condato_mbed 0:556294574340 13 //#include "ds1621.h"
condato_mbed 0:556294574340 14
condato_mbed 0:556294574340 15 #include <BLE.h>
condato_mbed 0:556294574340 16 #include "ble/BLE.h"
condato_mbed 0:556294574340 17 #include "ble/Gap.h"
condato_mbed 0:556294574340 18
condato_mbed 0:556294574340 19 #include "SDBlockDevice.h"
condato_mbed 0:556294574340 20 #include "FATFileSystem.h"
condato_mbed 0:556294574340 21
condato_mbed 0:556294574340 22 #include <errno.h>
condato_mbed 0:556294574340 23 #include <stdio.h>
condato_mbed 0:556294574340 24 #include <time.h>
condato_mbed 0:556294574340 25 #include <stdint.h>
condato_mbed 0:556294574340 26 #include <InterruptIn.h>
condato_mbed 0:556294574340 27 #include <InterruptManager.h>
condato_mbed 0:556294574340 28
condato_mbed 0:556294574340 29 //MAX32630FTHR
condato_mbed 0:556294574340 30 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
condato_mbed 0:556294574340 31
condato_mbed 0:556294574340 32 //MAX14690 Wearable Power-Management Solution Driver
condato_mbed 0:556294574340 33 I2C i2cm2(I2C2_SDA, I2C2_SCL);
condato_mbed 0:556294574340 34 MAX14690 max14690(&i2cm2);
condato_mbed 0:556294574340 35
condato_mbed 0:556294574340 36 char device_id[10];
condato_mbed 0:556294574340 37
condato_mbed 0:556294574340 38 int sensor_temp = 1;
condato_mbed 0:556294574340 39 unsigned short int measure_id = 0;
condato_mbed 0:556294574340 40 unsigned short int log_id = 0;
condato_mbed 0:556294574340 41
condato_mbed 0:556294574340 42 //SD-Karte
condato_mbed 0:556294574340 43 FATFileSystem fileSystem("fs");
condato_mbed 0:556294574340 44 SDBlockDevice blockDevice(P0_5, P0_6, P0_4, P0_7); // mosi, miso, sck, cs
condato_mbed 0:556294574340 45
condato_mbed 0:556294574340 46 //Bluetooth-Modul PAN1326B
condato_mbed 0:556294574340 47 RawSerial pan1326b(P0_1, P0_0); //TX, RX
condato_mbed 0:556294574340 48 DigitalOut bt_rst (P1_6); //Bluetooth reset
condato_mbed 0:556294574340 49
condato_mbed 0:556294574340 50 //I2C
condato_mbed 0:556294574340 51 I2C i2c(P3_4, P3_5); //SDA, SCL
condato_mbed 0:556294574340 52
condato_mbed 0:556294574340 53 //Temperatursensor
condato_mbed 0:556294574340 54 ADT7410 myADT7410(P3_4, P3_5, 0x90, 100000); //SDA, SCL, Address, Hz
condato_mbed 0:556294574340 55
condato_mbed 0:556294574340 56 //Termistor
condato_mbed 0:556294574340 57 //Thermistor extruder(AIN_1);
condato_mbed 0:556294574340 58
condato_mbed 0:556294574340 59 //DS1621
condato_mbed 0:556294574340 60 //#define DS1621_ADDR 0 // I2c DS1621 address is 0x00
condato_mbed 0:556294574340 61 //DS1621 ds(&i2c, DS1621_ADDR); // Declare ds1621 throught i2c interface
condato_mbed 0:556294574340 62
condato_mbed 0:556294574340 63 //Akku-monitoring
condato_mbed 0:556294574340 64 AnalogIn battery(AIN_0);
condato_mbed 0:556294574340 65
condato_mbed 0:556294574340 66 //USB-monitroing
condato_mbed 0:556294574340 67 AnalogIn usb(AIN_5);
condato_mbed 0:556294574340 68
condato_mbed 0:556294574340 69 //LEDs
condato_mbed 0:556294574340 70 DigitalOut r (P2_4); //Rote LED
condato_mbed 0:556294574340 71 DigitalOut g (P2_5); //Grüne LED
condato_mbed 0:556294574340 72 DigitalOut b (P2_6); //Blaue LED
condato_mbed 0:556294574340 73 DigitalOut RGBs[] = {r, g, b}; //LEDs Liste
condato_mbed 0:556294574340 74 /*
condato_mbed 0:556294574340 75 DigitalOut r (P5_6); //Rote LED
condato_mbed 0:556294574340 76 DigitalOut g (P5_5); //Grüne LED
condato_mbed 0:556294574340 77 DigitalOut b (P5_4); //Blaue LED
condato_mbed 0:556294574340 78 DigitalOut RGBs[] = {r, g, b}; //LEDs Liste
condato_mbed 0:556294574340 79 */
condato_mbed 0:556294574340 80 //Button interrupt
condato_mbed 0:556294574340 81 InterruptIn button(P2_3, PullUp); //Auf Pull Up eingestellt
condato_mbed 0:556294574340 82 //InterruptIn button(P5_3, PullUp); //BT
condato_mbed 0:556294574340 83 //InterruptIn button2(P3_3, PullUp); //BT
condato_mbed 0:556294574340 84
condato_mbed 0:556294574340 85 //I2C OLED
condato_mbed 0:556294574340 86 Adafruit_SSD1306_I2c featherOLED(i2c);
condato_mbed 0:556294574340 87 /*
condato_mbed 0:556294574340 88 //OLED Buttons
condato_mbed 0:556294574340 89 InterruptIn aButton(P5_3, PullUp);
condato_mbed 0:556294574340 90 InterruptIn bButton(P3_3, PullUp);
condato_mbed 0:556294574340 91 InterruptIn cButton(P3_2, PullUp);
condato_mbed 0:556294574340 92 */
condato_mbed 0:556294574340 93 Timeout after_BLE;
condato_mbed 0:556294574340 94 Timeout led_check; //Zum Überprüfen, ob Datenlogger an ist
condato_mbed 0:556294574340 95 Timeout turnoff; //Zum Auschalten (nicht implementiert)
condato_mbed 0:556294574340 96 Timeout flipper; //Zum Bluetooth an / aus
condato_mbed 0:556294574340 97 Timeout advertise_cancel; //Zum abbrechen von BT bei keinem Verbindungsaufbau
condato_mbed 0:556294574340 98 Timeout connected_cancel; //Zum abbrechen von BT bei kein Befehlempfang
condato_mbed 0:556294574340 99 Ticker mess_timer; //ticker eventuell nur bis 30 Minuten geeignet
condato_mbed 0:556294574340 100 Ticker ticker;
condato_mbed 0:556294574340 101 Ticker charger;
condato_mbed 0:556294574340 102
condato_mbed 0:556294574340 103 time_t now; //Zeit Funktionen
condato_mbed 0:556294574340 104 time_t raw;
condato_mbed 0:556294574340 105
condato_mbed 0:556294574340 106 float buffer_temp;
condato_mbed 0:556294574340 107 struct tm current = {0}; //tm - Zeit im Kalenderformat
condato_mbed 0:556294574340 108 struct tm actual = {0};
condato_mbed 0:556294574340 109
condato_mbed 0:556294574340 110 volatile int quit = 0; //BLE Advertise Flag
condato_mbed 0:556294574340 111 volatile int conn_state = 0; //Verbindungsstatus für BLE
condato_mbed 0:556294574340 112
condato_mbed 0:556294574340 113 bool after_BT = false;
condato_mbed 0:556294574340 114 bool in_BT = false; //Flag für BLE ist an
condato_mbed 0:556294574340 115 bool error_status = false; //Wenn es ein Fehler beim Initialisierung gab
condato_mbed 0:556294574340 116 bool BT_error = false; //Wenn es BLE oder SD Fehler ist, dann werden die Button Interrupts nicht deklariert
condato_mbed 0:556294574340 117 bool shipping = false; //Sendevorgang Status
condato_mbed 0:556294574340 118 bool pause = true; //Pause während Sendevorgang
condato_mbed 0:556294574340 119 bool init_status = false; //Erfolgreiche Initialisierung Status
condato_mbed 0:556294574340 120 bool charge = false; //Wenn DL an Stromnetz angeschlossen ist
condato_mbed 0:556294574340 121
condato_mbed 0:556294574340 122 struct tm user_config_time;
condato_mbed 0:556294574340 123 struct user_config_struct user_config_para = {0, 0, 0, 0, 0, 0, 0}; //Liste für Config Parameter
condato_mbed 0:556294574340 124
condato_mbed 0:556294574340 125 int next_state = 0; //State-Machine
condato_mbed 0:556294574340 126
condato_mbed 0:556294574340 127 int unsigned long unix_time = 0; //Zeit in Unix Zeitformat
condato_mbed 0:556294574340 128
condato_mbed 0:556294574340 129 int default_advertise_time = 300; //Default Timer für BLE advertising
condato_mbed 0:556294574340 130 int default_connected_time = 180; //Default Timer für BLE verbunden
condato_mbed 0:556294574340 131
condato_mbed 0:556294574340 132 int alert_count = 0; //Alarme Zähler
condato_mbed 0:556294574340 133
condato_mbed 0:556294574340 134 void turnOFF(){
condato_mbed 0:556294574340 135 if(BT_error == false){
condato_mbed 0:556294574340 136 in_BT = false;
condato_mbed 0:556294574340 137 BT_false();
condato_mbed 0:556294574340 138 }
condato_mbed 0:556294574340 139 next_state = 4;
condato_mbed 0:556294574340 140 }
condato_mbed 0:556294574340 141
condato_mbed 0:556294574340 142 void flip(){
condato_mbed 0:556294574340 143 if(BT_error == false){
condato_mbed 0:556294574340 144 in_BT = !in_BT;
condato_mbed 0:556294574340 145 if(in_BT == true){
condato_mbed 0:556294574340 146 BT_true();
condato_mbed 0:556294574340 147 }else if(in_BT == false){
condato_mbed 0:556294574340 148 BT_false();
condato_mbed 0:556294574340 149 }
condato_mbed 0:556294574340 150 }
condato_mbed 0:556294574340 151 turnoff.attach(&turnOFF, 4); // Button für weitere 4 Sekunden halten um Datenlogger auszuschalten
condato_mbed 0:556294574340 152 }
condato_mbed 0:556294574340 153
condato_mbed 0:556294574340 154 void onCheck(){
condato_mbed 0:556294574340 155 r = 1;
condato_mbed 0:556294574340 156 g = 1;
condato_mbed 0:556294574340 157 }
condato_mbed 0:556294574340 158
condato_mbed 0:556294574340 159 void pushed()
condato_mbed 0:556294574340 160 {
condato_mbed 0:556294574340 161 flipper.attach(&flip, 1); // Button für eine Sekunde gedrückt halten um BT zu aktivieren
condato_mbed 0:556294574340 162 }
condato_mbed 0:556294574340 163
condato_mbed 0:556294574340 164 void released() {
condato_mbed 0:556294574340 165 if(quit == 0 && charge == false && after_BT == false){
condato_mbed 0:556294574340 166 if(error_status == true){
condato_mbed 0:556294574340 167 r = 0;
condato_mbed 0:556294574340 168 }else if(shipping == true && pause == false){
condato_mbed 0:556294574340 169 r = 0;
condato_mbed 0:556294574340 170 g = 0;
condato_mbed 0:556294574340 171 }else{
condato_mbed 0:556294574340 172 g = 0;
condato_mbed 0:556294574340 173 }
condato_mbed 0:556294574340 174 led_check.attach(&onCheck, 3);
condato_mbed 0:556294574340 175 }
condato_mbed 0:556294574340 176 flipper.detach();
condato_mbed 0:556294574340 177 turnoff.detach();
condato_mbed 0:556294574340 178 }
condato_mbed 0:556294574340 179
condato_mbed 0:556294574340 180 void pre_delay(){
condato_mbed 0:556294574340 181 for(int i = 0; user_config_para.pre_delay > i; i++){
condato_mbed 0:556294574340 182 r = 0;
condato_mbed 0:556294574340 183 g = 0;
condato_mbed 0:556294574340 184 b = 0;
condato_mbed 0:556294574340 185 printf("\nPre-delay: %d of %d seconds\n", i + 1, user_config_para.pre_delay);
condato_mbed 0:556294574340 186 wait_ms(500);
condato_mbed 0:556294574340 187 r = 1;
condato_mbed 0:556294574340 188 g = 1;
condato_mbed 0:556294574340 189 b = 1;
condato_mbed 0:556294574340 190 wait_ms(500);
condato_mbed 0:556294574340 191 }
condato_mbed 0:556294574340 192 }
condato_mbed 0:556294574340 193
condato_mbed 0:556294574340 194 void mess_handler(){
condato_mbed 0:556294574340 195 next_state = 1;
condato_mbed 0:556294574340 196 }
condato_mbed 0:556294574340 197
condato_mbed 0:556294574340 198 void chargeCheck(){
condato_mbed 0:556294574340 199
condato_mbed 0:556294574340 200 }
condato_mbed 0:556294574340 201
condato_mbed 0:556294574340 202 void states(){
condato_mbed 0:556294574340 203 switch(next_state){
condato_mbed 0:556294574340 204 case 0:
condato_mbed 0:556294574340 205 {/*
condato_mbed 0:556294574340 206 printf("\n\nSleep\n\n");
condato_mbed 0:556294574340 207 while ( next_state == 0 ){
condato_mbed 0:556294574340 208 float batteryProcent = (100 / 0.271) * (battery.read() - 0.704);
condato_mbed 0:556294574340 209 if(usb.read() > 0.8){
condato_mbed 0:556294574340 210 r = 0;
condato_mbed 0:556294574340 211 charge = true;
condato_mbed 0:556294574340 212 }else if(usb.read() < 0.2){
condato_mbed 0:556294574340 213 r = 1;
condato_mbed 0:556294574340 214 charge = false;
condato_mbed 0:556294574340 215 }
condato_mbed 0:556294574340 216
condato_mbed 0:556294574340 217
condato_mbed 0:556294574340 218 }*/
condato_mbed 0:556294574340 219 while ( next_state == 0 ){
condato_mbed 0:556294574340 220 sleep();
condato_mbed 0:556294574340 221 wait(0.1);
condato_mbed 0:556294574340 222 }
condato_mbed 0:556294574340 223 //wait(0.5);
condato_mbed 0:556294574340 224 break;
condato_mbed 0:556294574340 225 }
condato_mbed 0:556294574340 226 case 1:
condato_mbed 0:556294574340 227 {
condato_mbed 0:556294574340 228 get_Messwert();
condato_mbed 0:556294574340 229 next_state = 0;
condato_mbed 0:556294574340 230 break;
condato_mbed 0:556294574340 231 }
condato_mbed 0:556294574340 232 case 2:
condato_mbed 0:556294574340 233 {
condato_mbed 0:556294574340 234 r = 1;
condato_mbed 0:556294574340 235 conn_state = 0;
condato_mbed 0:556294574340 236 quit = 1;
condato_mbed 0:556294574340 237 BLE_handler();
condato_mbed 0:556294574340 238 break;
condato_mbed 0:556294574340 239 }
condato_mbed 0:556294574340 240 case 3:
condato_mbed 0:556294574340 241 {
condato_mbed 0:556294574340 242 pre_delay();
condato_mbed 0:556294574340 243 next_state = 1;
condato_mbed 0:556294574340 244 mess_timer.attach(&mess_handler, (float)user_config_para.interval); // Mess-Timer attachen
condato_mbed 0:556294574340 245 break;
condato_mbed 0:556294574340 246 }
condato_mbed 0:556294574340 247 case 4:
condato_mbed 0:556294574340 248 {
condato_mbed 0:556294574340 249 b = 0;
condato_mbed 0:556294574340 250 r = 0;
condato_mbed 0:556294574340 251 if(BT_error == false){
condato_mbed 0:556294574340 252 char string[] = "Shutting down Coldchainlogger...";
condato_mbed 0:556294574340 253 write_to_sd_log_single(string);
condato_mbed 0:556294574340 254 }
condato_mbed 0:556294574340 255 printf("\n_______________________________\n\nShutting down Coldchainlogger\n_______________________________\n\n");
condato_mbed 0:556294574340 256 wait(0.5);
condato_mbed 0:556294574340 257 max14690.shutdown();
condato_mbed 0:556294574340 258 }
condato_mbed 0:556294574340 259 }
condato_mbed 0:556294574340 260 }
condato_mbed 0:556294574340 261
condato_mbed 0:556294574340 262 int main() {
condato_mbed 0:556294574340 263 init();
condato_mbed 0:556294574340 264 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
condato_mbed 0:556294574340 265
condato_mbed 0:556294574340 266 if(BT_error == true){
condato_mbed 0:556294574340 267 printf("\nBluetooth or SD initialize error: bluetooth can not be used\n");
condato_mbed 0:556294574340 268 }
condato_mbed 0:556294574340 269
condato_mbed 0:556294574340 270 __enable_irq();
condato_mbed 0:556294574340 271 button.fall(&pushed);
condato_mbed 0:556294574340 272 button.rise(&released);
condato_mbed 0:556294574340 273
condato_mbed 0:556294574340 274 while(true) {
condato_mbed 0:556294574340 275 /*
condato_mbed 0:556294574340 276 printf("ANALOG: %f\n", battery.read());
condato_mbed 0:556294574340 277 float batteryProcent = (100 / 0.271) * (battery.read() - 0.704);
condato_mbed 0:556294574340 278 printf("PROCENT: %f\n", batteryProcent);
condato_mbed 0:556294574340 279
condato_mbed 0:556294574340 280 printf("Extruder Temperature %f *C\r\n",extruder.get_temperature());
condato_mbed 0:556294574340 281 printf("Sensor Temperature %f *C\r\n",myADT7410.getTemp());
condato_mbed 0:556294574340 282 wait(.5);
condato_mbed 0:556294574340 283 */
condato_mbed 0:556294574340 284 states();
condato_mbed 0:556294574340 285 }
condato_mbed 0:556294574340 286 }