watersensor and temp
Dependencies: MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed watersenor_and_temp_code
Fork of IDW01M1-MQTT by
BME280/BME280.hpp@0:80c7778c82b8, 2017-10-31 (annotated)
- Committer:
- rozendhyan@gmail.com
- Date:
- Tue Oct 31 18:41:30 2017 +0800
- Revision:
- 0:80c7778c82b8
- Child:
- 3:30d9b8d9adc9
My commit message
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rozendhyan@gmail.com | 0:80c7778c82b8 | 1 | #include "mbed.h" |
rozendhyan@gmail.com | 0:80c7778c82b8 | 2 | |
rozendhyan@gmail.com | 0:80c7778c82b8 | 3 | #ifndef BME280_HPP |
rozendhyan@gmail.com | 0:80c7778c82b8 | 4 | #define BME280_HPP |
rozendhyan@gmail.com | 0:80c7778c82b8 | 5 | |
rozendhyan@gmail.com | 0:80c7778c82b8 | 6 | #define BME280_size 14 |
rozendhyan@gmail.com | 0:80c7778c82b8 | 7 | |
rozendhyan@gmail.com | 0:80c7778c82b8 | 8 | Serial sensor(PA_11,PA_12); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 9 | Serial pc(USBTX,USBRX); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 10 | int AT[3] = {0xA5,0x52,0xF7}; |
rozendhyan@gmail.com | 0:80c7778c82b8 | 11 | class BME280{ |
rozendhyan@gmail.com | 0:80c7778c82b8 | 12 | public: |
rozendhyan@gmail.com | 0:80c7778c82b8 | 13 | void judgement_data(char buf[]); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 14 | int Temp_read(); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 15 | |
rozendhyan@gmail.com | 0:80c7778c82b8 | 16 | private: |
rozendhyan@gmail.com | 0:80c7778c82b8 | 17 | void receive(); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 18 | void send_AT(); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 19 | char buf[BME280_size]; |
rozendhyan@gmail.com | 0:80c7778c82b8 | 20 | int Lux,Temp,Preesure,Hum,Height; |
rozendhyan@gmail.com | 0:80c7778c82b8 | 21 | |
rozendhyan@gmail.com | 0:80c7778c82b8 | 22 | }; |
rozendhyan@gmail.com | 0:80c7778c82b8 | 23 | void BME280::judgement_data(char buf[]){ |
rozendhyan@gmail.com | 0:80c7778c82b8 | 24 | int i=0; |
rozendhyan@gmail.com | 0:80c7778c82b8 | 25 | if(buf[i++]== 0x5A){ |
rozendhyan@gmail.com | 0:80c7778c82b8 | 26 | //pc.printf("first_step\n"); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 27 | if(buf[i++]== 0x5A){ |
rozendhyan@gmail.com | 0:80c7778c82b8 | 28 | //pc.printf("second_step\n"); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 29 | //i++;//go to data type |
rozendhyan@gmail.com | 0:80c7778c82b8 | 30 | if(buf[i] == 0x15){ |
rozendhyan@gmail.com | 0:80c7778c82b8 | 31 | //pc.printf("Lux type\n"); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 32 | i++;//04 |
rozendhyan@gmail.com | 0:80c7778c82b8 | 33 | Lux = ((buf[i+1]<<24) | (buf[i+2]<<16) | (buf[i+3]<<8) | buf[i+4])/100; |
rozendhyan@gmail.com | 0:80c7778c82b8 | 34 | //Lux = (0x00<<24) | (0x00<<16) | (0xFE<<8) | 0x40;//test |
rozendhyan@gmail.com | 0:80c7778c82b8 | 35 | //pc.printf("Lux = %d\n", Lux); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 36 | }else if(buf[i] == 0x45){ |
rozendhyan@gmail.com | 0:80c7778c82b8 | 37 | //pc.printf("temp type\n"); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 38 | i++;//0A |
rozendhyan@gmail.com | 0:80c7778c82b8 | 39 | Temp = ((buf[i+1]<<8)|(buf[i+2]))/100; |
rozendhyan@gmail.com | 0:80c7778c82b8 | 40 | Preesure = ((buf[i+3]<<24)|(buf[i+4]<<16)|(buf[i+5]<<8)|(buf[i+6]))/100; |
rozendhyan@gmail.com | 0:80c7778c82b8 | 41 | Hum = ((buf[i+7]<<8)|(buf[i+8]))/100; |
rozendhyan@gmail.com | 0:80c7778c82b8 | 42 | Height = ((buf[i+9]<<8)|(buf[i+10])); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 43 | //pc.printf("Temp = %d\n", Temp); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 44 | //pc.printf("Preesure = %d\n", Preesure); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 45 | //pc.printf("Hum = %d\n", Hum); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 46 | //pc.printf("Height = %d\n", Height); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 47 | } |
rozendhyan@gmail.com | 0:80c7778c82b8 | 48 | } |
rozendhyan@gmail.com | 0:80c7778c82b8 | 49 | } |
rozendhyan@gmail.com | 0:80c7778c82b8 | 50 | } |
rozendhyan@gmail.com | 0:80c7778c82b8 | 51 | void BME280::receive(){ |
rozendhyan@gmail.com | 0:80c7778c82b8 | 52 | for(int i=0;i<BME280_size;i++){ |
rozendhyan@gmail.com | 0:80c7778c82b8 | 53 | buf[i] = sensor.getc(); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 54 | //pc.putc(buf[i]); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 55 | } |
rozendhyan@gmail.com | 0:80c7778c82b8 | 56 | |
rozendhyan@gmail.com | 0:80c7778c82b8 | 57 | } |
rozendhyan@gmail.com | 0:80c7778c82b8 | 58 | void BME280::send_AT(){ |
rozendhyan@gmail.com | 0:80c7778c82b8 | 59 | for(int i=0;i<3;i++){ |
rozendhyan@gmail.com | 0:80c7778c82b8 | 60 | sensor.putc(AT[i]); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 61 | //pc.putc(AT[i]); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 62 | } |
rozendhyan@gmail.com | 0:80c7778c82b8 | 63 | } |
rozendhyan@gmail.com | 0:80c7778c82b8 | 64 | int BME280::Temp_read(){ |
rozendhyan@gmail.com | 0:80c7778c82b8 | 65 | send_AT(); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 66 | //wait(0.2); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 67 | receive(); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 68 | //wait(0.2); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 69 | judgement_data(this->buf); |
rozendhyan@gmail.com | 0:80c7778c82b8 | 70 | |
rozendhyan@gmail.com | 0:80c7778c82b8 | 71 | return Temp; |
rozendhyan@gmail.com | 0:80c7778c82b8 | 72 | } |
rozendhyan@gmail.com | 0:80c7778c82b8 | 73 | |
rozendhyan@gmail.com | 0:80c7778c82b8 | 74 | #endif |