sr501/BH1750/mq-2/dht11

Dependencies:   mbed

Fork of sensors_2 by w mx

Committer:
xmwmx
Date:
Thu Oct 18 16:23:13 2018 +0000
Revision:
4:fe1e9f9c7b33
Parent:
3:31aec950f7dc
9 kinds of sensors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Dennis_Yu 0:6dca851b4779 1 #ifndef SENSORS_H
Dennis_Yu 0:6dca851b4779 2 #define SENSORS_H
Dennis_Yu 0:6dca851b4779 3
Dennis_Yu 0:6dca851b4779 4 #include "mbed.h"
xmwmx 2:cd0ea77c7d66 5
xmwmx 4:fe1e9f9c7b33 6
xmwmx 4:fe1e9f9c7b33 7
xmwmx 4:fe1e9f9c7b33 8 //==========================================
xmwmx 4:fe1e9f9c7b33 9 class sr501
xmwmx 4:fe1e9f9c7b33 10 {
xmwmx 4:fe1e9f9c7b33 11 /*****
xmwmx 4:fe1e9f9c7b33 12 * 红外热释电对象
xmwmx 4:fe1e9f9c7b33 13 * 感应输出高电平
xmwmx 4:fe1e9f9c7b33 14 *****/
xmwmx 4:fe1e9f9c7b33 15 private:
xmwmx 4:fe1e9f9c7b33 16 // DigitalIn signal;
xmwmx 4:fe1e9f9c7b33 17 bool status;
xmwmx 4:fe1e9f9c7b33 18 InterruptIn signal;
xmwmx 4:fe1e9f9c7b33 19 void triggered();
xmwmx 4:fe1e9f9c7b33 20 public:
xmwmx 4:fe1e9f9c7b33 21 sr501(PinName pSignal);
xmwmx 4:fe1e9f9c7b33 22 bool operator==(const bool &target);
xmwmx 4:fe1e9f9c7b33 23 void reset();
xmwmx 4:fe1e9f9c7b33 24 int read();
xmwmx 4:fe1e9f9c7b33 25 };
xmwmx 4:fe1e9f9c7b33 26 //=========================================
xmwmx 1:1197997ae0ea 27 //+++++++++++++++++++++BH1750+++++++++++++++++++++++++++++
xmwmx 1:1197997ae0ea 28 #define BH1750_I2CADDR 0x46
xmwmx 1:1197997ae0ea 29 #define BH1750_POWER_DOWN 0x00 // No active state
xmwmx 1:1197997ae0ea 30 #define BH1750_POWER_ON 0x01 // Wating for measurment command
xmwmx 1:1197997ae0ea 31 #define BH1750_RESET 0x07 // Reset data register value - not accepted in POWER_DOWN mode
xmwmx 1:1197997ae0ea 32 #define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
xmwmx 1:1197997ae0ea 33 #define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11 // Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
xmwmx 1:1197997ae0ea 34 #define BH1750_CONTINUOUS_LOW_RES_MODE 0x13 // Start measurement at 4lx resolution. Measurement time is approx 16ms.
xmwmx 1:1197997ae0ea 35
xmwmx 1:1197997ae0ea 36 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
xmwmx 1:1197997ae0ea 37 // Device is automatically set to Power Down after measurement.
xmwmx 1:1197997ae0ea 38 #define BH1750_ONE_TIME_HIGH_RES_MODE 0x20
xmwmx 1:1197997ae0ea 39
xmwmx 1:1197997ae0ea 40 // Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
xmwmx 1:1197997ae0ea 41 // Device is automatically set to Power Down after measurement.
xmwmx 1:1197997ae0ea 42 #define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21
xmwmx 1:1197997ae0ea 43
xmwmx 1:1197997ae0ea 44 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
xmwmx 1:1197997ae0ea 45 // Device is automatically set to Power Down after measurement.
xmwmx 1:1197997ae0ea 46 #define BH1750_ONE_TIME_LOW_RES_MODE 0x23
xmwmx 4:fe1e9f9c7b33 47 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++
xmwmx 1:1197997ae0ea 48 class BH1750
xmwmx 1:1197997ae0ea 49 {
xmwmx 1:1197997ae0ea 50
xmwmx 1:1197997ae0ea 51 /*****
xmwmx 2:cd0ea77c7d66 52 *
xmwmx 1:1197997ae0ea 53 *****/
xmwmx 1:1197997ae0ea 54 private:
xmwmx 1:1197997ae0ea 55 I2C link;
xmwmx 1:1197997ae0ea 56 char rawdata[2];
xmwmx 1:1197997ae0ea 57 bool status;
xmwmx 1:1197997ae0ea 58 public:
xmwmx 1:1197997ae0ea 59 BH1750(PinName sda,PinName scl);
xmwmx 1:1197997ae0ea 60 BH1750(PinName sda,PinName scl,char mode[]);
xmwmx 1:1197997ae0ea 61 float getlightdata();
xmwmx 1:1197997ae0ea 62 };
xmwmx 2:cd0ea77c7d66 63 //===========================================
xmwmx 2:cd0ea77c7d66 64 class mq
xmwmx 2:cd0ea77c7d66 65 {
xmwmx 2:cd0ea77c7d66 66 /*****
xmwmx 2:cd0ea77c7d66 67 *
xmwmx 2:cd0ea77c7d66 68 *****/
xmwmx 2:cd0ea77c7d66 69 private:
xmwmx 2:cd0ea77c7d66 70 bool status;
xmwmx 2:cd0ea77c7d66 71 InterruptIn signal;
xmwmx 2:cd0ea77c7d66 72 AnalogIn signallevel;
xmwmx 2:cd0ea77c7d66 73
xmwmx 2:cd0ea77c7d66 74 void triggered();
xmwmx 2:cd0ea77c7d66 75 public:
xmwmx 2:cd0ea77c7d66 76 mq(PinName dio);
xmwmx 2:cd0ea77c7d66 77 mq(PinName dio,PinName aio);
xmwmx 2:cd0ea77c7d66 78 bool operator==(const bool &target);
xmwmx 2:cd0ea77c7d66 79 float getairdata();
xmwmx 2:cd0ea77c7d66 80 void reset();
xmwmx 3:31aec950f7dc 81 int read();
xmwmx 2:cd0ea77c7d66 82 };
xmwmx 2:cd0ea77c7d66 83
xmwmx 2:cd0ea77c7d66 84 //===========================================
xmwmx 3:31aec950f7dc 85 class dht11
xmwmx 3:31aec950f7dc 86 {
xmwmx 3:31aec950f7dc 87 /*****
xmwmx 3:31aec950f7dc 88 *
xmwmx 3:31aec950f7dc 89 *****/
xmwmx 3:31aec950f7dc 90 private:
xmwmx 3:31aec950f7dc 91 Timer timer;
xmwmx 3:31aec950f7dc 92 Timer starttime;
xmwmx 3:31aec950f7dc 93 DigitalInOut datapin;
xmwmx 3:31aec950f7dc 94 float H;
xmwmx 3:31aec950f7dc 95 float T;
xmwmx 3:31aec950f7dc 96 public:
xmwmx 3:31aec950f7dc 97 dht11(PinName pin);
xmwmx 3:31aec950f7dc 98 int getdata();
xmwmx 3:31aec950f7dc 99 float gethumidity();
xmwmx 3:31aec950f7dc 100 float gettemperature();
xmwmx 3:31aec950f7dc 101
xmwmx 3:31aec950f7dc 102 };
xmwmx 4:fe1e9f9c7b33 103 //==========================================DS18B20
xmwmx 4:fe1e9f9c7b33 104 class DS18B20
xmwmx 4:fe1e9f9c7b33 105 {
xmwmx 4:fe1e9f9c7b33 106 /*****
xmwmx 4:fe1e9f9c7b33 107 *
xmwmx 4:fe1e9f9c7b33 108 *****/
xmwmx 4:fe1e9f9c7b33 109 private:
xmwmx 4:fe1e9f9c7b33 110 DigitalInOut datapin;
xmwmx 4:fe1e9f9c7b33 111 uint8_t dat;
xmwmx 4:fe1e9f9c7b33 112 float T;
xmwmx 4:fe1e9f9c7b33 113
xmwmx 4:fe1e9f9c7b33 114 int start();
xmwmx 4:fe1e9f9c7b33 115 void writebyte(uint8_t send);
xmwmx 4:fe1e9f9c7b33 116 uint8_t readByte();
xmwmx 4:fe1e9f9c7b33 117 float transfer(uint8_t h,uint8_t l);
xmwmx 4:fe1e9f9c7b33 118 public:
xmwmx 4:fe1e9f9c7b33 119 DS18B20(PinName pin);
xmwmx 4:fe1e9f9c7b33 120 int getdata();
xmwmx 4:fe1e9f9c7b33 121 float gettemperature();
xmwmx 4:fe1e9f9c7b33 122
xmwmx 4:fe1e9f9c7b33 123 };
xmwmx 4:fe1e9f9c7b33 124 //==========================================YL-38通用传感
xmwmx 4:fe1e9f9c7b33 125 class YL
xmwmx 4:fe1e9f9c7b33 126 {
xmwmx 4:fe1e9f9c7b33 127 /*****
xmwmx 4:fe1e9f9c7b33 128 *
xmwmx 4:fe1e9f9c7b33 129 *****/
xmwmx 4:fe1e9f9c7b33 130 private:
xmwmx 4:fe1e9f9c7b33 131 bool status;
xmwmx 4:fe1e9f9c7b33 132 InterruptIn signal;
xmwmx 4:fe1e9f9c7b33 133 AnalogIn signallevel;
xmwmx 4:fe1e9f9c7b33 134
xmwmx 4:fe1e9f9c7b33 135 void triggered();
xmwmx 4:fe1e9f9c7b33 136 public:
xmwmx 4:fe1e9f9c7b33 137 YL(PinName dio);
xmwmx 4:fe1e9f9c7b33 138 YL(PinName dio,PinName aio);
xmwmx 4:fe1e9f9c7b33 139 bool operator==(const bool &target);
xmwmx 4:fe1e9f9c7b33 140 float getairdata();
xmwmx 4:fe1e9f9c7b33 141 void reset();
xmwmx 4:fe1e9f9c7b33 142 int read();
xmwmx 4:fe1e9f9c7b33 143 };
xmwmx 4:fe1e9f9c7b33 144 //========================================
xmwmx 4:fe1e9f9c7b33 145 #define BMP180_ADDRESS 0x77<<1 // I2C address of BMP180, eight bit address on mbed
xmwmx 4:fe1e9f9c7b33 146 #define BMP180_WHO_AM_I 0xD0 // WHO_AM_I id of BMP180, should return 0x55
xmwmx 4:fe1e9f9c7b33 147 #define BMP180_RESET 0xE0
xmwmx 4:fe1e9f9c7b33 148 #define BMP180_CONTROL 0xF4
xmwmx 4:fe1e9f9c7b33 149 #define BMP180_OUT_MSB 0xF6
xmwmx 4:fe1e9f9c7b33 150 #define BMP180_OUT_LSB 0xF7
xmwmx 4:fe1e9f9c7b33 151 #define BMP180_OUT_XLSB 0xF8
xmwmx 4:fe1e9f9c7b33 152
xmwmx 4:fe1e9f9c7b33 153 // Set initial input parameters
xmwmx 4:fe1e9f9c7b33 154
xmwmx 4:fe1e9f9c7b33 155 enum OSS { // BMP-085 sampling rate
xmwmx 4:fe1e9f9c7b33 156 OSS_0 = 0, // 4.5 ms conversion time
xmwmx 4:fe1e9f9c7b33 157 OSS_1=1, // 7.5
xmwmx 4:fe1e9f9c7b33 158 OSS_2=2, // 13.5
xmwmx 4:fe1e9f9c7b33 159 OSS_3=3 // 25.5
xmwmx 4:fe1e9f9c7b33 160 };
xmwmx 4:fe1e9f9c7b33 161
xmwmx 4:fe1e9f9c7b33 162
xmwmx 4:fe1e9f9c7b33 163
xmwmx 4:fe1e9f9c7b33 164 class BMP180 {
xmwmx 4:fe1e9f9c7b33 165 /*****
xmwmx 4:fe1e9f9c7b33 166 *
xmwmx 4:fe1e9f9c7b33 167 *****/
xmwmx 4:fe1e9f9c7b33 168 private:
xmwmx 4:fe1e9f9c7b33 169 uint8_t OSS; // maximum pressure resolution
xmwmx 4:fe1e9f9c7b33 170
xmwmx 4:fe1e9f9c7b33 171 //Set up I2C, (SDA,SCL)
xmwmx 4:fe1e9f9c7b33 172 I2C i2c;
xmwmx 4:fe1e9f9c7b33 173
xmwmx 4:fe1e9f9c7b33 174 // These are constants used to calculate the temperature and pressure from the BMP-180 sensor
xmwmx 4:fe1e9f9c7b33 175 int16_t ac1, ac2, ac3, b1, b2, mb, mc, md, b5;
xmwmx 4:fe1e9f9c7b33 176 uint16_t ac4, ac5, ac6;
xmwmx 4:fe1e9f9c7b33 177
xmwmx 4:fe1e9f9c7b33 178 void writeByte(uint8_t address, uint8_t subAddress, uint8_t data);
xmwmx 4:fe1e9f9c7b33 179 char readByte(uint8_t address, uint8_t subAddress);
xmwmx 4:fe1e9f9c7b33 180 void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest);
xmwmx 4:fe1e9f9c7b33 181
xmwmx 4:fe1e9f9c7b33 182 // Stores all of the BMP180's calibration values into global variables
xmwmx 4:fe1e9f9c7b33 183 // Calibration values are required to calculate temp and pressure
xmwmx 4:fe1e9f9c7b33 184 // This function should be called at the beginning of the program
xmwmx 4:fe1e9f9c7b33 185 // These BMP-180 functions were adapted from Jim Lindblom of SparkFun Electronics
xmwmx 4:fe1e9f9c7b33 186 void BMP180Calibration();
xmwmx 4:fe1e9f9c7b33 187 public:
xmwmx 4:fe1e9f9c7b33 188 BMP180(PinName sda,PinName scl) ;
xmwmx 4:fe1e9f9c7b33 189 // Temperature returned will be in units of 0.1 deg C
xmwmx 4:fe1e9f9c7b33 190 long BMP180GetTemperature();
xmwmx 4:fe1e9f9c7b33 191
xmwmx 4:fe1e9f9c7b33 192 // Calculate pressure read calibration values
xmwmx 4:fe1e9f9c7b33 193 // b5 is also required so BMP180GetTemperature() must be called first.
xmwmx 4:fe1e9f9c7b33 194 // Value returned will be pressure in units of Pa.
xmwmx 4:fe1e9f9c7b33 195 long BMP180GetPressure();
xmwmx 4:fe1e9f9c7b33 196 };
xmwmx 4:fe1e9f9c7b33 197 //================================================
xmwmx 4:fe1e9f9c7b33 198 class GP2Y1010
xmwmx 4:fe1e9f9c7b33 199 {
xmwmx 4:fe1e9f9c7b33 200 /*****
xmwmx 4:fe1e9f9c7b33 201 *
xmwmx 4:fe1e9f9c7b33 202 *****/
xmwmx 4:fe1e9f9c7b33 203 private:
xmwmx 4:fe1e9f9c7b33 204 AnalogIn measurePin ;
xmwmx 4:fe1e9f9c7b33 205 DigitalOut ledPower;
xmwmx 4:fe1e9f9c7b33 206 unsigned int samplingTime ;
xmwmx 4:fe1e9f9c7b33 207 unsigned int deltaTime ;
xmwmx 4:fe1e9f9c7b33 208 unsigned int sleepTime ;
xmwmx 4:fe1e9f9c7b33 209
xmwmx 4:fe1e9f9c7b33 210 float voMeasured ;
xmwmx 4:fe1e9f9c7b33 211 float calcVoltage ;
xmwmx 4:fe1e9f9c7b33 212 float dustDensity ;
xmwmx 4:fe1e9f9c7b33 213
xmwmx 4:fe1e9f9c7b33 214 public:
xmwmx 4:fe1e9f9c7b33 215 GP2Y1010(PinName ledPower,PinName measurePin);
xmwmx 4:fe1e9f9c7b33 216 float getairdata();
xmwmx 4:fe1e9f9c7b33 217 };
xmwmx 4:fe1e9f9c7b33 218 //=============================================
Dennis_Yu 0:6dca851b4779 219 #endif