AND / Mbed 2 deprecated Sensor_BME680_lowLevel

Dependencies:   mbed

Revision:
6:10523c9e9b68
Parent:
5:0c6401d671c6
Child:
7:90753ca349c8
--- a/main.cpp	Tue Oct 23 12:08:10 2018 +0200
+++ b/main.cpp	Fri May 01 14:30:25 2020 +0000
@@ -1,34 +1,190 @@
-/* mbed Example Program
- * Copyright (c) 2006-2014 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include "mbed.h"
+ #include "mbed.h"
+
+Serial ble(PC_12, PD_2);
+I2C bme(PC_1,PC_0);
+
+int main()
+{
+    ble.baud(9600);
+//    char rId[1]; //id read
+    int addr=0xEC; //8bit slave address
+
+    char wHum[2];
+    wHum[0]=0x72; //humidity register
+    wHum[1]=0x1;//
+    char rHum[1];
 
-int main() {
-    set_time(1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
+    bme.write(addr,wHum,2);
+    bme.read(addr,rHum,1);
+    ble.printf("Hum: %x\n",rHum[0]);
+    wait(0.1);
+    
+//    char wTemp[2]; 
+//    wTemp[0]=0x74;//measurement register
+//    wTemp[1]=0x40; //01000000
+//    char rTemp[1];
+//    
+//    bme.write(addr,wTemp,2);
+//    bme.read(addr,rTemp,1);
+//    ble.printf("%x\n",rTemp[0]);
+//    wait(0.1);
+    
+    char wPres[2]; 
+    wPres[0]=0x74;//measurement register
+    wPres[1]=0x54; //01010100 (kept the temperature)
+    char rPres[1];
+    
+    bme.write(addr,wPres,2);
+    bme.read(addr,rPres,1);
+    ble.printf("Temp and pres: %x\n",rPres[0]);
+    wait(0.1);
+    
+    char wWait[2]; 
+    wWait[0]=0x64;//gas wait 0 register
+    wWait[1]=0x59; //100 ms
+    char rWait[1];
+    
+    bme.write(addr,wWait,2);
+    bme.read(addr,rWait,1);
+    ble.printf("Wait: %x\n",rWait[0]);
+    wait(0.1);
+    
+    //GET HEAT SETTING PARAMETERS
+    char wg1[1];
+    wg1[0]=0xED;
+    char rg1[1];
+    char wg2M[1];
+    char wg2L[1];
+    char rg2M[1];
+    char rg2L[1];
+    wg2M[0]=0xEC;
+    wg2L[0]=0xEB;
+    char wg3[1];
+    wg3[0]=0xEE;
+    char rg3[1];
+    char wHR[1];
+    wHR[0]=0x02;
+    char rHR[1];
+    char wHV[1];
+    wHV[0]=0x00;
+    char rHV[1];
+    
+    bme.write(addr,wg1,1);
+    bme.read(addr,rg1,1);//get g1
+    int g1=rg1[0];
+    ble.printf("G1 hex: %x, G1 int: %d\n",rg1[0], g1);
+    wait(0.1);
+    
+    bme.write(addr,wg2M,1);
+    bme.read(addr,rg2M,1);//get g2m
+    
+    bme.write(addr,wg2L,1);
+    bme.read(addr,rg2L,1);//get g2l
+    ble.printf("G2M hex: %x, G2L hex: %x\n",rg2M[0], rg2L[0]);
+    int g2=((rg2M[0]<<8)+rg2L[0]);
+    ble.printf("G2 hex: %x, G2 int: %d\n",g2, g2);
+    wait(0.1);
+    
+    bme.write(addr,wg3,1);
+    bme.read(addr,rg3,1);//get g3
+    int g3=rg3[0];
+    ble.printf("G3 hex: %x, G3 int: %d\n",rg3[0],g3);
+    wait(0.1);
+    
+    bme.write(addr,wHR,1);
+    bme.read(addr,rHR,1);//get heating range
+    int HR=((rHR[0]<<2)>>6);
+    ble.printf("HR hex: %x, HR int: %d\n",rHR[0], HR);
+    wait(0.1);
+    
+    bme.write(addr,wHV,1);
+    bme.read(addr,rHV,1);//get g3
+    int HV=rHV[0];
+    ble.printf("HV hex: %x, HV int: %d\n",rHV[0], HV);
+    wait(0.1);
+    
+    double var1=(g1/16.0)+49.0;
+    double var2= ((g2 / 32768.0) * 0.0005) + 0.00235;
+    double var3=g3 / 1024.0;
+    double var4 = var1 * (1.0 + (var2 *3000)); //target temp 300c
+    double var5= var4 + (var3 * 24.0);//ambient 24c
+    uint8_t heat=(3.4 * ((var5 * (4.0 / (4.0 + HR)) * (1.0/(1.0 +(HV * 0.002)))) - 25));
+    ble.printf("Re heat: %d\n",heat);
+    wait(0.1);
+    
+    //END OF HEAT SETTING PARAMETERS
+    
+    char wRes[2]; 
+    wRes[0]=0x5A;//gas wait 0 heat setting
+    wRes[1]=0x59; //???
+    char rRes[1];
+    
+    bme.write(addr,wRes,2);
+    bme.read(addr,rRes,1);
+    ble.printf("Heater resistance: %x\n",rRes[0]);
+    wait(0.1);
+    
+    char wCG[2];  
+    wCG[0]=0x71;//control gas register
+    wCG[1]=0x0; 
+    char rCG[1];
+    
+    bme.write(addr,wCG,2);
+    bme.read(addr,rCG,1);
+    ble.printf("Control gas: %x\n",rCG[0]);
+    wait(0.1);
+    
+    char wRG[2];  
+    wRG[0]=0x71;//control gas register
+    wRG[1]=0x10; 
+    char rRG[1];
+    
+    bme.write(addr,wRG,2);
+    bme.read(addr,rRG,1);
+    ble.printf("Run gas: %x\n",rRG[0]);
+    wait(0.1);
+    
+    
 
-    while (true) {
-        time_t seconds = time(NULL);
-        
-        printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)seconds);
+
+
+    wPres[1]=0x55; //01010100 (kept the temperature and prssure, enabled measurement )
+    
+    ///GET GAS
+    char wGasL[1];
+    wGasL[0]=0x2B;
+    char wGasM[1];
+    wGasM[0]=0x2A;
+    char rGasL[1];
+    char rGasM[1];
+    
+    //GET TEMP
+    char wTempL[1];
+    char wTempM[1];
+    wTempL[0]=0x23;
+    wTempM[0]=0x22; 
+    char rTempL[1];
+    char rTempM[1];
+    
+    while(1) {
+        bme.write(addr,wPres,2); //enables measurement
+        bme.read(addr,rPres,1);
         
-        printf("Time as a basic string = %s", ctime(&seconds));
+        bme.write(addr,wGasL,1);
+        bme.read(addr,rGasL,1); //bits 7:6 are significant
+        bme.write(addr,wGasM,1);
+        bme.read(addr,rGasM,1); //whole byte is significant
+        
+        bme.write(addr,wTempL,1);
+        bme.read(addr,rTempL,1);
+        bme.write(addr,wTempM,1);
+        bme.read(addr,rTempM,1);
+        
+        int temp =(rTempM[0]<<8)+rTempL[0];
+        int gas =(rGasM[0]<<2)+(rGasL[0]>>6); //make space on the most of 2 bits to add only the first two bits from the least
+        
+        ble.printf("Gas: %i, Tem: %i\n",gas, temp);
+        wait(0.1);
+    }
 
-        char buffer[32];
-        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
-        printf("Time as a custom formatted string = %s", buffer);
-        
-        wait(1);
-    }
 }