Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BOX_1
ENVIROMENTAL.cpp
00001 #include "ENVIROMENTAL.h" 00002 00003 00004 //#define Ro 28 //Change this value with the value from Calibrate and uncomment 00005 00006 00007 ENVIROMENTAL::ENVIROMENTAL (PinName envi_SDA, PinName envi_SCL,PinName mq_analog,I2C &i2c):climate(envi_SDA,envi_SCL),bmp(envi_SDA,envi_SCL),uv(i2c),lux(envi_SDA,envi_SCL) 00008 { 00009 00010 _mq_pin = mq_analog; 00011 uv.begin(VEML6070_1_T); 00012 } 00013 ENVIROMENTAL::~ENVIROMENTAL(){} 00014 00015 00016 void ENVIROMENTAL::calibrate_mq() 00017 { 00018 00019 AnalogIn analog_value(_mq_pin); 00020 float MQ7_read; 00021 float VRL; 00022 float Rs; 00023 float Ro; 00024 float RL; 00025 for(int test_cycle = 1 ; test_cycle <= 500 ; test_cycle++) //Read the analog output of the sensor for 200 times 00026 { 00027 MQ7_read = MQ7_read + analog_value.read(); //add the values for 500 00028 } 00029 MQ7_read = MQ7_read/500.0f; //Take average 00030 VRL = MQ7_read*(5.0f); //Convert analog value to voltage 00031 Rs = ((5.0f/VRL)-1) * RL; //is the formula obtained from datasheet 00032 Ro = Rs/1.7f; //RS/RO is 1.7 as we obtained from graph of datasheet 00033 printf("Ro at fresh air = %f\n\r",Ro); 00034 } 00035 00036 00037 00038 float ENVIROMENTAL::get_CO() 00039 { 00040 AnalogIn analog_value(_mq_pin); 00041 float MQ7_read; 00042 float VRL; 00043 float Rs; 00044 float ratio; 00045 float Ro=28; 00046 float ppm; 00047 float RL=10; 00048 float m =-0.661; 00049 float b =1.332; 00050 for(int test_cycle = 1 ; test_cycle <= 500 ; test_cycle++) //Read the analog output of the sensor for 200 times 00051 { 00052 MQ7_read = MQ7_read + analog_value.read(); //add the values for 500 00053 } 00054 MQ7_read = MQ7_read/500.0f; //Take average 00055 00056 VRL = MQ7_read*(5.0f); //Convert analog value to voltage 00057 Rs = ((5.0f/VRL)-1.0f) * RL; //is the formula obtained from datasheet 00058 ratio = Rs/Ro; 00059 ppm = pow(10, ((log10(ratio)-b)/m)); //use formula to calculate ppm 00060 return ppm; 00061 } 00062 00063 00064 float ENVIROMENTAL::get_temp() 00065 { 00066 float temp=0; 00067 for(int i=0;i<20;i++) 00068 { 00069 _tmp = climate.get_temperature(); 00070 temp=temp + _tmp; 00071 } 00072 00073 return temp/20; 00074 00075 } 00076 00077 00078 uint32_t ENVIROMENTAL::get_humidity(){ 00079 _tmp=0; 00080 uint32_t hum=0; 00081 for(int i=0;i<20;i++) 00082 { 00083 _tmp=climate.get_humidity(); 00084 hum = hum + _tmp; 00085 } 00086 00087 return hum/20; 00088 00089 } 00090 00091 int32_t ENVIROMENTAL::get_pressure(){ 00092 _tmp=0; 00093 int32_t pres=0; 00094 for(int i=0;i<20;i++) 00095 { 00096 _tmp=bmp.getPressure(); 00097 pres = pres + _tmp; 00098 } 00099 00100 return pres/20; 00101 00102 } 00103 00104 int ENVIROMENTAL::get_uv(){ 00105 int uvi=0; 00106 _tmp=0; 00107 for(int i=0;i<20;i++) 00108 { 00109 _tmp = uv.readUV(); 00110 uvi = uvi +_tmp; 00111 } 00112 00113 return uvi/20; 00114 00115 00116 00117 } 00118 00119 00120 int ENVIROMENTAL::get_lumen(){ 00121 int lumen=0; 00122 _tmp=0; 00123 for(int i=0;i<20;i++) 00124 { 00125 _tmp = lux.getLuminosity(2); 00126 lumen = lumen + _tmp; 00127 } 00128 00129 return lumen/20; 00130 } 00131 00132 int ENVIROMENTAL::get_infrared(){ 00133 int lumen=0; 00134 _tmp=0; 00135 for(int i=0;i<20;i++) 00136 { 00137 _tmp = lux.getLuminosity(1); 00138 lumen = lumen + _tmp; 00139 } 00140 00141 return lumen/20; 00142 }
Generated on Sat Jul 16 2022 00:45:31 by
 1.7.2