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: mbed
main.cpp
- Committer:
- christodoulos
- Date:
- 2020-05-18
- Revision:
- 7:90753ca349c8
- Parent:
- 6:10523c9e9b68
File content as of revision 7:90753ca349c8:
#include "mbed.h"
Serial ble(PC_12, PD_2);
I2C bme(PC_1,PC_0);
int main()
{
ble.baud(9600);
int BMEaddr=0xEC; //8bit slave BMEaddress
char wHum[2];
wHum[0]=0x72; //humidity register
wHum[1]=0x1;//
char rHum[1];
bme.write(BMEaddr,wHum,2);
bme.read(BMEaddr,rHum,1);
//ble.printf("Hum: %x\n",rHum[0]);
wait(0.1);
char wPres[2];
wPres[0]=0x74;//measurement register
wPres[1]=0x54; //01010100 (kept the temperature)
char rPres[1];
bme.write(BMEaddr,wPres,2);
bme.read(BMEaddr,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(BMEaddr,wWait,2);
bme.read(BMEaddr,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(BMEaddr,wg1,1);
bme.read(BMEaddr,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(BMEaddr,wg2M,1);
bme.read(BMEaddr,rg2M,1);//get g2m
bme.write(BMEaddr,wg2L,1);
bme.read(BMEaddr,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(BMEaddr,wg3,1);
bme.read(BMEaddr,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(BMEaddr,wHR,1);
bme.read(BMEaddr,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(BMEaddr,wHV,1);
bme.read(BMEaddr,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(BMEaddr,wRes,2);
bme.read(BMEaddr,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(BMEaddr,wCG,2);
bme.read(BMEaddr,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(BMEaddr,wRG,2);
bme.read(BMEaddr,rRG,1);
//ble.printf("Run gas: %x\n",rRG[0]);
wait(0.1);
wPres[1]=0x55; //01010101 (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(BMEaddr,wPres,2); //enables measurement
bme.read(BMEaddr,rPres,1);
bme.write(BMEaddr,wGasL,1);
bme.read(BMEaddr,rGasL,1); //bits 7:6 are significant
bme.write(BMEaddr,wGasM,1);
bme.read(BMEaddr,rGasM,1); //whole byte is significant
bme.write(BMEaddr,wTempL,1);
bme.read(BMEaddr,rTempL,1);
bme.write(BMEaddr,wTempM,1);
bme.read(BMEaddr,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, Temp L: %x, Temp H: %x\n",gas, rTempL[0],rTempM[0]);
wait(0.1);
}
}