wang tang / Mbed 2 deprecated DS1820_IDW01M1_finish

Dependencies:   watersenor_and_temp_code MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed

Fork of DS1820_IDW01M1 by CHANG rozen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BME280.hpp Source File

BME280.hpp

00001 #include "mbed.h"
00002 
00003 #ifndef BME280_HPP
00004 #define BME280_HPP
00005 
00006 #define BME280_size 14
00007 
00008 Serial sensor(PA_11,PA_12);
00009 Serial pc(USBTX,USBRX);
00010 int AT[3] = {0xA5,0x52,0xF7};
00011 class BME280{
00012 public:
00013     void judgement_data(char buf[]);
00014     int Temp_read();
00015 
00016 private:
00017     void receive();
00018     void send_AT();
00019     char buf[BME280_size];
00020     int Lux,Temp,Preesure,Hum,Height;
00021 
00022 };
00023 void BME280::judgement_data(char buf[]){
00024     int i=0;
00025     if(buf[i++]== 0x5A){
00026         //pc.printf("first_step\n");
00027         if(buf[i++]== 0x5A){
00028             //pc.printf("second_step\n");
00029            //i++;//go to data type
00030             if(buf[i] == 0x15){
00031                 //pc.printf("Lux type\n");
00032                 i++;//04
00033                 Lux = ((buf[i+1]<<24) | (buf[i+2]<<16) | (buf[i+3]<<8) | buf[i+4])/100;
00034                 //Lux = (0x00<<24) | (0x00<<16) | (0xFE<<8) | 0x40;//test
00035                 //pc.printf("Lux = %d\n", Lux);
00036             }else if(buf[i] == 0x45){
00037                 //pc.printf("temp type\n");
00038                 i++;//0A
00039                 Temp = ((buf[i+1]<<8)|(buf[i+2]))/100;
00040                 Preesure = ((buf[i+3]<<24)|(buf[i+4]<<16)|(buf[i+5]<<8)|(buf[i+6]))/100;
00041                 Hum = ((buf[i+7]<<8)|(buf[i+8]))/100;
00042                 Height = ((buf[i+9]<<8)|(buf[i+10]));
00043                 //pc.printf("Temp = %d\n", Temp);
00044                 //pc.printf("Preesure = %d\n", Preesure);
00045                 //pc.printf("Hum = %d\n", Hum);
00046                 //pc.printf("Height = %d\n", Height);
00047             }
00048         }
00049     }
00050 }
00051 void BME280::receive()
00052 {
00053     for(int i=0;i<BME280_size;i++){
00054         buf[i] = sensor.getc();  
00055         //pc.putc(buf[i]);
00056     }
00057     
00058 }
00059 void BME280::send_AT(){
00060     for(int i=0;i<3;i++){
00061         sensor.putc(AT[i]);
00062         //pc.putc(AT[i]);
00063     }
00064 }
00065 int BME280::Temp_read(){
00066     send_AT();
00067     //wait(0.2);
00068     receive();
00069     //wait(0.2);
00070     judgement_data(this->buf);
00071     
00072     return Temp;
00073 }
00074 
00075 #endif