Smart Car Seat GT Design Expo Spring 2017 / Mbed 2 deprecated Smart_Car_Seat_v1

Dependencies:   Adafruit_FONA FSR SDFileSystem mbed

Fork of Smart_Car_Seat_v1 by Jea Du Kim

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "FSR.h"
00003 
00004 #include <ctype.h>
00005 #include <string>
00006 
00007 #include "SDFileSystem.h"
00008 #include "Adafruit_FONA.h"
00009 
00010 #define FONA_RST p12
00011 #define FONA_TX p13
00012 #define FONA_RX p14
00013 #define FONA_RI p11
00014 
00015 Serial pcSerial(USBTX, USBRX);
00016 
00017 DigitalOut led1(LED1);      //Fona status - on(connecting), off(disconnecting)
00018 DigitalOut led2(LED2);
00019 DigitalOut led3(LED3);      //Pressure status - on(child on seat), off(off seat)
00020 DigitalOut led4(LED4);      //Temperature status - on(Too hot), off(not hot)
00021 
00022 Adafruit_FONA fona(FONA_TX, FONA_RX, FONA_RST, FONA_RI);    //GSM
00023 
00024 SDFileSystem sd(p5, p6, p7, p8, "sd");      //SD card reader
00025 
00026 DigitalOut CtrlRelay(p16);      //Relay Controller
00027 
00028 FSR bottomFSR1(p20, 10); // Pin 17-20 is used as the AnalogIn pin and a 10k resistor is used as a voltage divider for the FSR
00029 FSR topFSR1(p19, 10);
00030 FSR topFSR2(p18, 10);
00031 FSR bottomFSR2(p17, 10);
00032 
00033 I2C i2c(p28, p27);      //I2C commands
00034 
00035 Serial bt(p9, p10);     //Bluetooth module
00036 
00037 float temp, avgTemp; //temperature in degrees C
00038 const int addr1 = 0xB4;     //Default MLX90614 address - 0x5A
00039 const int addr2 = 0x08;     //Changed another MLX90614 address to 0x04
00040 char data[3];
00041 bool danger;
00042 int onForce;
00043 
00044 float temp1,temp2,temp3,temp4,avgF;
00045 
00046 char pnum[22];
00047 char message[141]= "Emergency! Your child is still in the Smart Car Seat!";
00048 //Message to send to phone and contacts
00049 
00050 class FonaEventListener : public Adafruit_FONA::EventListener {
00051     virtual void onRing() {
00052         led1 = 1;
00053     }
00054     
00055     virtual void onNoCarrier() {
00056         led1 = 0; 
00057     }
00058 };
00059 FonaEventListener fonaEventListener;
00060 //GSM Module initalization
00061 
00062 bool onSeat(float fLevel);      //Checks if there is a child on the seat
00063 float readTemp(int addr, char* cmd);    //Reads the temperature through the I2/C
00064 void callback(void);        //Changes the stored number on the SD card
00065 
00066 int main() {
00067     pcSerial.baud(9600);        //Set the serial baudrate to 9600
00068     wait(2);                    //Wait 2 secs to initialize and settle
00069     i2c.frequency(50000);       //Set the I2C frequency to 50kHz for the MLX90614
00070     wait(2);                    //Wait another 2 secs for initalization
00071     
00072     
00073     char cmd1[1];
00074     char cmd2[1];
00075     cmd1[0] = 0x07;     //Command to read object temperature
00076     cmd2[0] = 0x06;     //Command to read ambient temperature
00077     
00078     
00079     pcSerial.printf("FONA basic test\r\n");
00080     pcSerial.printf("Initializing....(May take 3 seconds)\r\n");
00081     // See if the FONA is responding
00082     if (! fona.begin(9600)) {
00083         pcSerial.printf("Couldn't find FONA\r\n");
00084         while (1);
00085     }
00086     fona.setEventListener(&fonaEventListener);
00087     pcSerial.printf("FONA is OK\r\n");
00088     
00089     
00090     bt.attach(&callback);       //Allows the app to change the phone number
00091     
00092     
00093     //Reads the stored phone number
00094     FILE *fpr = fopen("/sd/mydir/myphone.txt", "r");
00095     if(fpr == NULL) {
00096         error("Could not open file for read\n");
00097     }
00098     fscanf(fpr,"%s", pnum); 
00099     pcSerial.printf("Phone Number: %s\n", pnum);
00100     fclose(fpr);
00101     
00102     
00103     danger = false;
00104     
00105     while (1) {
00106         if (onSeat(0.2))        //Checks if child is on the seat
00107                                 // 0.2 is very low, 0.5 for moderate touch, 0.8 for heavy pressing
00108         {   
00109             led3 = 1;
00110             
00111             temp1 = readTemp(addr1, cmd1);      //Read object temperature (C)
00112             temp2 = readTemp(addr2, cmd1);
00113             //temp3 = readTemp(addr1, cmd2);    //Read ambient temperature (C)
00114             //temp4 = readTemp(addr2, cmd2);
00115             
00116             avgTemp = (temp1+temp2)/2;          //Average value
00117             //avgTemp = (temp3+temp4)/2;
00118             printf("%5.2f\r\n",avgTemp);
00119             
00120             if ( avgTemp > 30)      //If value exceed a "bad" temperature
00121             {
00122                 led4 = 1;
00123                 CtrlRelay = 1;      //Turn on cooling system
00124                 danger = true;
00125             } else if ( avgTemp < 25)   //When it falls back down
00126             {
00127                 led4 = 0;
00128                 CtrlRelay = 0;      //Turn off cooling system
00129             }
00130             if (danger) {       //When child is in "danger"
00131                 fona.sendSMS(pnum, message);    //FONA will send message to the contact
00132                 wait(1);        //Adjust wait time after sending a message
00133             }
00134         } else {
00135             led3 = 0;
00136             led4 = 0;
00137             CtrlRelay = 0;      //When child is no longer on the seat
00138             danger = false;     //The system resets
00139         }
00140         wait(1);        //Adjust wait time after reading
00141     }
00142 }
00143 
00144 float readTemp(int addr, char* cmd) {   //read the value from the MLX90614
00145     i2c.write(addr, cmd, 1, true);
00146     i2c.read(addr, data, 3);        //Two bytes of data + 1 word of codes
00147     
00148     temp = (((float)((data[1]<<8)|data[0]))*0.02) - 273.15;
00149     return temp;
00150 }
00151 
00152 bool onSeat(float fLevel) {     //Detects child by activating enough pressure sensors
00153     onForce = 0;
00154     if  (bottomFSR1.readRaw() > fLevel) {
00155         onForce += 1;
00156     }
00157     if  (bottomFSR2.readRaw() > fLevel) {
00158         onForce += 1;
00159     }
00160     if  (topFSR1.readRaw() > fLevel) {
00161         onForce += 1;
00162     }
00163     if  (topFSR2.readRaw() > fLevel) {
00164         onForce += 1;
00165     }
00166     return onForce >= 2;
00167 }
00168 
00169 void callback(void) {       //Rewrites the phone number saved in the SD card
00170     bt.gets(pnum,11);
00171     pcSerial.printf("New number: %s\n", pnum);
00172             
00173     FILE *fp = fopen("/sd/mydir/myphone.txt", "w");
00174     if(fp == NULL) {
00175         error("Could not open file for write\n");
00176     }
00177     fprintf(fp, "%s\n", pnum);
00178     fclose(fp);
00179 }