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.
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):climate(envi_SDA,envi_SCL),bmp(envi_SDA,envi_SCL),i2c(envi_SDA,envi_SCL),uv(i2c) 00008 { 00009 _mq_pin = mq_analog; 00010 uv.begin(VEML6070_1_T); 00011 } 00012 ENVIROMENTAL::~ENVIROMENTAL(){} 00013 00014 00015 void ENVIROMENTAL::calibrate_mq() 00016 { 00017 00018 AnalogIn analog_value(_mq_pin); 00019 float MQ7_read; 00020 float VRL; 00021 float Rs; 00022 float Ro; 00023 float RL; 00024 for(int test_cycle = 1 ; test_cycle <= 500 ; test_cycle++) //Read the analog output of the sensor for 200 times 00025 { 00026 MQ7_read = MQ7_read + analog_value.read(); //add the values for 500 00027 } 00028 MQ7_read = MQ7_read/500.0f; //Take average 00029 VRL = MQ7_read*(5.0f); //Convert analog value to voltage 00030 Rs = ((5.0f/VRL)-1) * RL; //is the formula obtained from datasheet 00031 Ro = Rs/1.7f; //RS/RO is 1.7 as we obtained from graph of datasheet 00032 printf("Ro at fresh air = %f\n\r",Ro); 00033 } 00034 00035 00036 00037 float ENVIROMENTAL::get_CO() 00038 { 00039 AnalogIn analog_value(_mq_pin); 00040 float MQ7_read; 00041 float VRL; 00042 float Rs; 00043 float ratio; 00044 float Ro; 00045 float ppm; 00046 float RL; 00047 float m =-0.661; 00048 float b =1.332; 00049 for(int test_cycle = 1 ; test_cycle <= 500 ; test_cycle++) //Read the analog output of the sensor for 200 times 00050 { 00051 MQ7_read = MQ7_read + analog_value.read(); //add the values for 500 00052 } 00053 MQ7_read = MQ7_read/500.0f; //Take average 00054 00055 VRL = MQ7_read*(5.0f); //Convert analog value to voltage 00056 Rs = ((5.0f/VRL)-1.0f) * RL; //is the formula obtained from datasheet 00057 ratio = Rs/Ro; 00058 ppm = pow(10, ((log10(ratio)-b)/m)); //use formula to calculate ppm 00059 return ppm; 00060 } 00061 00062 00063 float ENVIROMENTAL::get_temp() 00064 { 00065 float temp=0; 00066 for(int i=0;i<20;i++) 00067 { 00068 _tmp = climate.get_temperature(); 00069 temp=temp + _tmp; 00070 } 00071 00072 return temp/20; 00073 00074 } 00075 00076 00077 uint32_t ENVIROMENTAL::get_humidity(){ 00078 _tmp=0; 00079 uint32_t hum=0; 00080 for(int i=0;i<20;i++) 00081 { 00082 _tmp=climate.get_humidity(); 00083 hum = hum + _tmp; 00084 } 00085 00086 return hum/20; 00087 00088 } 00089 00090 int32_t ENVIROMENTAL::get_pressure(){ 00091 _tmp=0; 00092 int32_t pres=0; 00093 for(int i=0;i<20;i++) 00094 { 00095 _tmp=bmp.getPressure(); 00096 pres = pres + _tmp; 00097 } 00098 00099 return pres/20; 00100 00101 } 00102 00103 int ENVIROMENTAL::get_uv(){ 00104 int uvi=0; 00105 _tmp=0; 00106 for(int i=0;i<20;i++) 00107 { 00108 _tmp = uv.readUV(); 00109 uvi = uvi + _tmp; 00110 } 00111 00112 return uvi/20; 00113 00114 00115 00116 }
Generated on Wed Jul 20 2022 11:58:47 by
