ECE56810 Project 2 - Runner's Monitor

Dependencies:   mbed rohm-bh1790glc-driver mbed-http FXAS21000 FXOS8700Q ESP8266 RegisterWriter

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "hcsr04.h"
00003 #include "ESP8266.h"
00004 #include "math.h"
00005 #include "FXOS8700Q.h"
00006 #include "FXAS21000.h"
00007 #include "RegisterWriter/RegisterWriter/rohm_hal2.h"
00008 #include "rohm-bh1790glc-driver/bh1790glc_registers.h"
00009 #include "rohm-bh1790glc-driver/bh1790glc.h"
00010 
00011 #define CloudIP "184.106.153.149"           //Raw IP Address of ThingSpeak Cloud Server
00012 #define SSID "SSID"
00013 #define Password "Password"
00014 
00015 HCSR04 usensor1(D10,D11);                   //ECHO Pin=D11, TRIG Pin=D10
00016 Serial pc(USBTX,USBRX);                     //Serial Communication with PC
00017 ESP8266 wifi(PTC17, PTC16, 115200);         //Tx Pin:PTC17; Rx Pin:PTC17; Baud rate:115200
00018 
00019 I2C i2c(PTE25, PTE24);
00020 FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);    // Configured for the FRDM-K64F with onboard sensors
00021 FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR1);
00022 FXAS21000 gyro(D14,D15);
00023 
00024 I2C i2chm(I2C_SDA, I2C_SCL);
00025 RegisterWriter i2c_rw(i2chm);
00026 BH1790GLC bh1790glc(i2c_rw);
00027 
00028 DigitalOut buzzer(D7);
00029 
00030 int on = 1, off = 0; 
00031 
00032 void wifi_send(void);               //Connect and Push Data Channel to Cloud Server
00033 void US_Sensor(void);               //Take ultrasonic measurement and send to cloud
00034 void Acc_Mag_Gyro(void);            //Take measurements from acceleromter, magnetometer, and gyroscope and send to cloud
00035 void Heart_Monitor(void);              
00036 
00037 int num = 0;
00038 char snd[255],rcv[1000];                    //snd: send command to ESP8266, rcv: receive response from ESP8266
00039     
00040 //Ultrasonic sensor variable
00041 int distance1 = 100;
00042     
00043 //acc, mag, and gryo variables
00044 float gyro_data[3];
00045 motion_data_units_t acc_data, mag_data;
00046 motion_data_counts_t acc_raw, mag_raw;
00047 float faX, faY, faZ, fmX, fmY, fmZ, tmp_float;
00048 int16_t raX, raY, raZ, rmX, rmY, rmZ, tmp_int;
00049 
00050 //heart monitor variables
00051 bool interval_elapsed;
00052 Ticker ticker;
00053 uint16_t data[2];
00054 
00055 void timer_isr()
00056 {
00057     interval_elapsed = true;
00058 }
00059 
00060 int main()
00061 {
00062     buzzer = off;
00063     pc.baud(115200);                        //Baud Rate of 115200 for Tera Term
00064     
00065     acc.enable();
00066     mag.enable();
00067 
00068     pc.baud(115200);
00069     i2c.frequency(400000);
00070     
00071     int error;
00072 
00073     wait(0.1);
00074 
00075     do
00076     { //until init OK.
00077         error = bh1790glc.set_default_on();
00078         wait_ms(500);
00079         wait_ms(200);
00080     }
00081     while (error);
00082     
00083     bh1790glc.set_default_on();
00084     
00085     interval_elapsed = false;
00086     ticker.attach(&timer_isr, 0.03125);                 //32Hz
00087     
00088     pc.printf("Initial Setup\r\n");
00089     wifi.SetMode(1);                        //Set ESP mode to 1
00090     wifi.RcvReply(rcv, 1000);               //Receive a response from ESP
00091     pc.printf("%s\r", rcv);
00092 
00093     pc.printf("Connecting to WiFi\r\n");    //AP Setup Initialization
00094     wifi.Join(SSID, Password);              //Put your Wifi SSID followed by Password
00095     wifi.RcvReply(rcv, 1000);
00096     pc.printf("%s\n", rcv);
00097     wait(2);
00098     
00099     wifi.GetIP(rcv);                        //Obtains an IP address from the AP
00100     
00101     while (1) 
00102     {
00103         wifi_send();
00104         
00105         wait(1.0f);
00106     }
00107 }
00108 
00109 void wifi_send(void)
00110 {
00111     while(num<1000000000000)
00112     {
00113         num=num+1;
00114         pc.printf("\nSyncing to Thingspeak #: %d\n\r", num);
00115         
00116         US_Sensor();
00117         
00118         Acc_Mag_Gyro();     
00119         
00120         Heart_Monitor();
00121     }
00122 }
00123 
00124 void US_Sensor(void)
00125 {
00126     //Ultrasound Sensor (HC-SR04) #1 Initialization
00127     usensor1.start();
00128     wait_ms(100);
00129         
00130     //Calculating Distance Percentage Remaining for Sensor # 1
00131     distance1 = usensor1.get_dist_cm();
00132         
00133     //Sending Data to the Cloud Server via ESP8266 WiFi Module
00134     strcpy(snd,"AT+CIPMUX=0\n\r");        //AT+CIPMUX: Enabling Single Channel Mode
00135     wifi.SendCMD(snd);
00136     wait(0.1);
00137     wifi.RcvReply(rcv, 1000);
00138     wait(0.1);
00139         
00140     //Establish TCP connection w/ Cloud Server
00141     sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",CloudIP);
00142     wait(0.1);
00143     wifi.RcvReply(rcv, 1000);
00144     wait(0.1);
00145         
00146     //Set length of the data that will be sent
00147     strcpy(snd,"AT+CIPSEND=100\n\r");
00148     wifi.SendCMD(snd);
00149     pc.printf("%s\r", rcv);
00150     wait(0.1);
00151     wifi.RcvReply(rcv, 1000);
00152     pc.printf("%s\r", rcv);
00153     wait(0.1);
00154         
00155     //Pushing the data acquired from HC-SR04 Ultrasonic Sensor to Cloud Server via API
00156     sprintf(snd,"\rhttps://api.thingspeak.com/update?api_key=MR7SIH7MSC5KIIH0&field1=%d\r", distance1);
00157     printf("Distance from Object: %dcm\n\r", distance1);
00158     
00159     
00160     
00161     if(distance1 < 6)
00162     {
00163         buzzer = on;
00164         wait(.3);
00165         buzzer = off;
00166         wait(0.3);
00167         buzzer = on;
00168         wait(0.3);
00169         buzzer = off;
00170         wait(0.3);
00171         buzzer = on;
00172         wait(0.3);
00173         buzzer = off;
00174     }
00175     
00176     wifi.SendCMD(snd);
00177     pc.printf("%s\r",snd);
00178     wait(0.1);
00179     wifi.RcvReply(rcv, 1000);
00180     pc.printf("%s\r", rcv);    
00181 }
00182 
00183 void Acc_Mag_Gyro(void)
00184 {
00185     //counts based results
00186     acc.getAxis(acc_raw);
00187     mag.getAxis(mag_raw);
00188     acc.getX(raX);
00189     acc.getY(raY);
00190     acc.getZ(raZ);
00191     mag.getX(rmX);
00192     mag.getY(rmY);
00193     mag.getZ(rmZ);
00194             
00195     //unit based results
00196     acc.getAxis(acc_data);
00197     mag.getAxis(mag_data);
00198     acc.getX(faX);
00199     acc.getY(faY);
00200     acc.getZ(faZ);
00201     mag.getX(fmX);
00202     mag.getY(fmY);
00203     mag.getZ(fmZ);
00204     pc.printf("Acellerometer: X=%1.4fY=%1.4fZ=%1.4f\n\r", acc.getX(faX),acc.getY(faY),acc.getZ(faZ));
00205     pc.printf("Magnetometer: X=%4.1fY=%4.1fZ=%4.1f\n\r", mag.getX(fmX),mag.getY(fmY), mag.getZ(fmZ));
00206     gyro.ReadXYZ(gyro_data);
00207     pc.printf("Gyro: X=%6.2f Y=%6.2f Z=%6.2f\n\r", gyro_data[0],gyro_data[1], gyro_data[2]);
00208     wait(0.5f);
00209             
00210     //Sending Data to the Cloud Server via ESP8266 WiFi Module
00211     strcpy(snd,"AT+CIPMUX=0\n\r");        //AT+CIPMUX: Enabling Single Channel Mode
00212     wifi.SendCMD(snd);
00213     wait(0.1);
00214     wifi.RcvReply(rcv, 1000);
00215     wait(0.1);
00216         
00217     //Establish TCP connection w/ Cloud Server
00218     sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",CloudIP);
00219     wait(0.1);
00220     wifi.RcvReply(rcv, 1000);
00221     wait(0.1);
00222         
00223     //Set length of the data that will be sent
00224     strcpy(snd,"AT+CIPSEND=100\n\r");
00225     wifi.SendCMD(snd);
00226     pc.printf("%s\r", rcv);
00227     wait(0.1);
00228     wifi.RcvReply(rcv, 1000);
00229     pc.printf("%s\r", rcv);
00230     wait(0.1);
00231         
00232     //Pushing the data acquired from Acc to Cloud Server via API
00233     //Sends only X data for simplicity of testing purposes
00234     sprintf(snd,"\rhttps://api.thingspeak.com/update?api_key=MR7SIH7MSC5KIIH0&field2=%f\r", acc.getX(faX));
00235     printf("Current X Acceleration: %f\n\r", acc.getX(faX));
00236     wifi.SendCMD(snd);
00237     pc.printf("%s\r",snd);
00238     wait(0.1);
00239     wifi.RcvReply(rcv, 1000);
00240     pc.printf("%s\r", rcv);   
00241     
00242     //Pushing the data acquired from Mag to Cloud Server via API
00243     //Sends only X data for simplicity of testing purposes
00244     sprintf(snd,"\rhttps://api.thingspeak.com/update?api_key=MR7SIH7MSC5KIIH0&field3=%f\r", mag.getX(fmX));
00245     printf("Current X Position: %f\n\r", mag.getX(fmX));
00246     wifi.SendCMD(snd);
00247     pc.printf("%s\r",snd);
00248     wait(0.1);
00249     wifi.RcvReply(rcv, 1000);
00250     pc.printf("%s\r", rcv); 
00251     
00252     //Pushing the data acquired from Gyro to Cloud Server via API
00253     //Sends only X data for simplicity of testing purposes
00254     sprintf(snd,"\rhttps://api.thingspeak.com/update?api_key=MR7SIH7MSC5KIIH0&field4=%f\r", gyro_data[0]);
00255     printf("Current X Orientation: %f\n\r", gyro_data[0]);
00256     wifi.SendCMD(snd);
00257     pc.printf("%s\r",snd);
00258     wait(0.1);
00259     wifi.RcvReply(rcv, 1000);
00260     pc.printf("%s\r", rcv);  
00261 }
00262 
00263 void Heart_Monitor(void)
00264 {
00265     int i = 0;
00266     int sum = 0;
00267     int count = 0;
00268     float average = 0;
00269     int error;
00270     
00271     wait_ms(500);
00272     
00273     while(i < 10)
00274         { 
00275             if (interval_elapsed)       //Wait until ISR
00276             {                         
00277                 error = bh1790glc.getresults(data);
00278                 
00279                 if (!error) 
00280                 {                          
00281                     //get data, print to serial, update flags
00282                     pc.printf("%d, \t%d\n\r", data[1], data[0]);                
00283                     interval_elapsed = false;
00284                     i++;
00285                     sum += data[1];
00286                     count++;
00287                 }   
00288             }
00289         }
00290         
00291         average = sum/10;
00292         
00293     if(average > 3500)
00294     {
00295         buzzer = on;
00296         wait(.1);
00297         buzzer = off;
00298         wait(0.1);
00299         buzzer = on;
00300         wait(0.1);
00301         buzzer = off;
00302         wait(0.1);
00303         buzzer = on;
00304         wait(.1);
00305         buzzer = off;
00306         wait(0.1);
00307         buzzer = on;
00308         wait(0.1);
00309         buzzer = off;
00310         wait(0.1);
00311         buzzer = on;
00312         wait(.1);
00313         buzzer = off;
00314         wait(0.1);
00315         buzzer = on;
00316         wait(0.1);
00317         buzzer = off;
00318     }
00319         
00320     //Sending Data to the Cloud Server via ESP8266 WiFi Module
00321     strcpy(snd,"AT+CIPMUX=0\n\r");        //AT+CIPMUX: Enabling Single Channel Mode
00322     wifi.SendCMD(snd);
00323     wait(0.1);
00324     wifi.RcvReply(rcv, 1000);
00325     wait(0.1);
00326         
00327     //Establish TCP connection w/ Cloud Server
00328     sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",CloudIP);
00329     wait(0.1);
00330     wifi.RcvReply(rcv, 1000);
00331     wait(0.1);
00332         
00333     //Set length of the data that will be sent
00334     strcpy(snd,"AT+CIPSEND=100\n\r");
00335     wifi.SendCMD(snd);
00336     pc.printf("%s\r", rcv);
00337     wait(0.1);
00338     wifi.RcvReply(rcv, 1000);
00339     pc.printf("%s\r", rcv);
00340     wait(0.1);
00341         
00342     //Pushing the data acquired from heart monitor to Cloud Server via API
00343     //Sends only X data for simplicity of testing purposes
00344     sprintf(snd,"\rhttps://api.thingspeak.com/update?api_key=MR7SIH7MSC5KIIH0&field5=%f\r", average);
00345     printf("Current average heart rate: %f\n\r", average);
00346     wifi.SendCMD(snd);
00347     pc.printf("%s\r",snd);
00348     wait(0.1);
00349     wifi.RcvReply(rcv, 1000);
00350     pc.printf("%s\r", rcv);
00351 }