Projet s5 - sensors lib

Dependencies:   XBeeLib mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pH.cpp Source File

pH.cpp

00001 #include "pH.h"
00002 
00003 static I2C PH(p28,p27); // SDA / SCL
00004 static Serial pc(USBTX,USBRX);
00005 
00006 static DigitalOut led1(LED1);
00007 static DigitalOut led2(LED2);
00008 static DigitalOut led3(LED3);
00009 static DigitalOut led4(LED4);
00010 
00011 static char buf[DEFAULT_BUFFER_SIZE];
00012 static char fBuf[DEFAULT_BUFFER_SIZE];
00013 
00014 void init_PH(){
00015     PH.frequency(MAX_I2C_CLOCK);
00016     clearBuffer_PH();
00017 }
00018 
00019 void setAdresse_PH(uint8_t addresse ){
00020     PH.start();
00021     PH.write(PH_ADRESSE<<1);
00022     PH.write('I');
00023     PH.write('2');
00024     PH.write('C');
00025     PH.write(',');
00026     PH.write(addresse);
00027     PH.stop();
00028 }
00029 
00030 void calibrate_PH(){ // Calibrate the PH AC converter at 
00031     pc.printf("Clearing pH sensor calibration...");    
00032     PH.start();
00033     PH.write(PH_ADRESSE<<1);
00034     PH.write('C');
00035     PH.write('a');
00036     PH.write('l');
00037     PH.write(',');
00038     PH.write('c');
00039     PH.write('l');
00040     PH.write('e');
00041     PH.write('a');
00042     PH.write('r');
00043     PH.stop();
00044     
00045     wait_ms(300);
00046     
00047     pc.printf("pH senosr calibration cleared");    
00048     
00049     for(int i=120;i>0;i--){
00050         pc.printf("\r                                                           ");    
00051         pc.printf("\rCalibration de la sonde de pH en cours. Temps restants: %i secondes...",i);
00052         wait_ms(1000);
00053     }
00054     
00055     pc.printf("\n");    
00056     
00057     PH.start();
00058     PH.write(PH_ADRESSE<<1);
00059     PH.write('C');
00060     PH.write('a');
00061     PH.write('l');
00062     PH.write(',');
00063     PH.write('m');
00064     PH.write('i');
00065     PH.write('d');
00066     PH.write(',');
00067     PH.write('7');
00068     PH.write('.');
00069     PH.write('0');
00070     PH.write('0');
00071     PH.stop();
00072     
00073     wait_ms(1600);
00074     
00075     getCalibrationStatus_PH();
00076 }
00077 
00078 void getCalibrationStatus_PH(){
00079     PH.start();
00080     PH.write(PH_ADRESSE<<1);
00081     PH.write('C');
00082     PH.write('a');
00083     PH.write('l');
00084     PH.write(',');
00085     PH.write('?');
00086     PH.stop();
00087     
00088     wait_ms(300);
00089     PH.write(0x00);
00090     PH.read(PH_ADRESSE<<1,buf,DEFAULT_BUFFER_SIZE);
00091     
00092     for(int i=0;i<DEFAULT_BUFFER_SIZE;i++){
00093         fBuf[i]=buf[i];
00094     }
00095     for(int i=0;i<DEFAULT_BUFFER_SIZE;i++){
00096         pc.putc(buf[i]);
00097     }
00098     pc.putc('\n');
00099 }
00100 
00101 void find_PH   (){
00102     PH.start();
00103     PH.write(PH_ADRESSE<<1);
00104     PH.write('F');
00105     PH.write('i');
00106     PH.write('n');
00107     PH.write('d');
00108     PH.stop();
00109 }
00110 
00111 void status_PH(){
00112     PH.start();
00113     PH.write(PH_ADRESSE<<1);
00114     PH.write('S');
00115     PH.write('t');
00116     PH.write('a');
00117     PH.write('t');
00118     PH.write('u');
00119     PH.write('s');
00120     PH.stop();
00121     
00122     wait_ms(300);
00123     PH.write(0x00);
00124     PH.read(PH_ADRESSE<<1,buf,15);
00125     
00126     for(int i=0;i<15;i++){
00127         pc.putc(buf[i]);
00128     }
00129     pc.putc('\n');
00130 }
00131 
00132 void sleep_PH(){
00133     PH.start();
00134     PH.write(PH_ADRESSE<<1);
00135     PH.write('S');
00136     PH.write('l');
00137     PH.write('e');
00138     PH.write('e');
00139     PH.write('p');    
00140     PH.stop();
00141 }
00142 
00143 void read_PH(){
00144     
00145     PH.start();
00146     PH.write(PH_ADRESSE<<1);
00147     PH.write('R');
00148     PH.stop();
00149     
00150     wait_ms(900);
00151     PH.write(0x00);
00152     PH.read(PH_ADRESSE<<1,buf,DEFAULT_BUFFER_SIZE);
00153     
00154     int i =0;
00155     while(buf[i]!=0){
00156         pc.putc(buf[i]);
00157         i++;
00158     }
00159     pc.putc('\n');
00160     clearBuffer_PH();
00161 }
00162 
00163 float getPH(){
00164     
00165     char bufTmp[DEFAULT_BUFFER_SIZE];
00166     
00167     PH.start();
00168     PH.write(PH_ADRESSE<<1);
00169     PH.write('R');
00170     PH.stop();
00171     
00172     wait_ms(900);
00173     PH.write(0x00);
00174     PH.read(PH_ADRESSE<<1,bufTmp,DEFAULT_BUFFER_SIZE);
00175     
00176     return atof(bufTmp+1);
00177 }
00178 
00179 void read_PH(char * buffer){
00180     
00181     PH.start();
00182     PH.write(PH_ADRESSE<<1);
00183     PH.write('R');
00184     PH.stop();
00185     
00186     wait_ms(900);
00187     PH.write(0x00);
00188     PH.read(PH_ADRESSE<<1,buffer,DEFAULT_BUFFER_SIZE);
00189 }
00190 
00191 void clearBuffer_PH(){
00192     for(int i=0;i<DEFAULT_BUFFER_SIZE;i++){
00193         buf[i]=0;
00194     }
00195 }