Program očitava temperaturu i vlažnost zraka u prostoriji pomoću senzora DHT11. Očitane vrijednosti ispisuje na računalu u programu Tera Term. Za vlažnost zraka manju od 60% svijetli zelena LED, a za vlažnost zraka veću od 60% pali se crvena LED. Pritiskom na tipkalo prekida se rad programa na 5sekundi. Odbrojavanje se prikazuje na 7-segmentnom displey-u. Za to vrijeme ugašene su sve LED. Nakon isteka 5 sekundi program nastavlja sa radom tamo di je prethodno stao. Žuta LED predstavlja ticker i blinka svake 0,2 sekunde i time pokazuje da se program izvršava.
dht11.h@0:d2e26e6fa11b, 2021-04-06 (annotated)
- Committer:
- MarioBlazevic
- Date:
- Tue Apr 06 12:12:07 2021 +0000
- Revision:
- 0:d2e26e6fa11b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MarioBlazevic | 0:d2e26e6fa11b | 1 | #ifndef DHT11_H |
MarioBlazevic | 0:d2e26e6fa11b | 2 | #define DHT11_H |
MarioBlazevic | 0:d2e26e6fa11b | 3 | |
MarioBlazevic | 0:d2e26e6fa11b | 4 | #include "mbed.h" |
MarioBlazevic | 0:d2e26e6fa11b | 5 | |
MarioBlazevic | 0:d2e26e6fa11b | 6 | #define DHTLIB_OK 0 |
MarioBlazevic | 0:d2e26e6fa11b | 7 | #define DHTLIB_ERROR_CHECKSUM -1 |
MarioBlazevic | 0:d2e26e6fa11b | 8 | #define DHTLIB_ERROR_TIMEOUT -2 |
MarioBlazevic | 0:d2e26e6fa11b | 9 | |
MarioBlazevic | 0:d2e26e6fa11b | 10 | /** Class for the DHT11 sensor. |
MarioBlazevic | 0:d2e26e6fa11b | 11 | * |
MarioBlazevic | 0:d2e26e6fa11b | 12 | * Example: |
MarioBlazevic | 0:d2e26e6fa11b | 13 | * @code |
MarioBlazevic | 0:d2e26e6fa11b | 14 | * #include "mbed.h" |
MarioBlazevic | 0:d2e26e6fa11b | 15 | * #include "Dht11.h" |
MarioBlazevic | 0:d2e26e6fa11b | 16 | * |
MarioBlazevic | 0:d2e26e6fa11b | 17 | * Serial pc(USBTX, USBRX); |
MarioBlazevic | 0:d2e26e6fa11b | 18 | * Dht11 sensor(PTD7); |
MarioBlazevic | 0:d2e26e6fa11b | 19 | * |
MarioBlazevic | 0:d2e26e6fa11b | 20 | * int main() { |
MarioBlazevic | 0:d2e26e6fa11b | 21 | * sensor.read(); |
MarioBlazevic | 0:d2e26e6fa11b | 22 | * pc.printf("T: %f, H: %d\r\n", sensor.getFahrenheit(), sensor.getHumidity()); |
MarioBlazevic | 0:d2e26e6fa11b | 23 | * } |
MarioBlazevic | 0:d2e26e6fa11b | 24 | * @endcode |
MarioBlazevic | 0:d2e26e6fa11b | 25 | */ |
MarioBlazevic | 0:d2e26e6fa11b | 26 | class Dht11 |
MarioBlazevic | 0:d2e26e6fa11b | 27 | { |
MarioBlazevic | 0:d2e26e6fa11b | 28 | public: |
MarioBlazevic | 0:d2e26e6fa11b | 29 | /** Construct the sensor object. |
MarioBlazevic | 0:d2e26e6fa11b | 30 | * |
MarioBlazevic | 0:d2e26e6fa11b | 31 | * @param pin PinName for the sensor pin. |
MarioBlazevic | 0:d2e26e6fa11b | 32 | */ |
MarioBlazevic | 0:d2e26e6fa11b | 33 | Dht11(PinName const &p); |
MarioBlazevic | 0:d2e26e6fa11b | 34 | |
MarioBlazevic | 0:d2e26e6fa11b | 35 | /** Update the humidity and temp from the sensor. |
MarioBlazevic | 0:d2e26e6fa11b | 36 | * |
MarioBlazevic | 0:d2e26e6fa11b | 37 | * @returns |
MarioBlazevic | 0:d2e26e6fa11b | 38 | * 0 on success, otherwise error. |
MarioBlazevic | 0:d2e26e6fa11b | 39 | */ |
MarioBlazevic | 0:d2e26e6fa11b | 40 | int read(); |
MarioBlazevic | 0:d2e26e6fa11b | 41 | |
MarioBlazevic | 0:d2e26e6fa11b | 42 | /** Get the temp(f) from the saved object. |
MarioBlazevic | 0:d2e26e6fa11b | 43 | * |
MarioBlazevic | 0:d2e26e6fa11b | 44 | * @returns |
MarioBlazevic | 0:d2e26e6fa11b | 45 | * Fahrenheit float |
MarioBlazevic | 0:d2e26e6fa11b | 46 | */ |
MarioBlazevic | 0:d2e26e6fa11b | 47 | float getFahrenheit(); |
MarioBlazevic | 0:d2e26e6fa11b | 48 | |
MarioBlazevic | 0:d2e26e6fa11b | 49 | /** Get the temp(c) from the saved object. |
MarioBlazevic | 0:d2e26e6fa11b | 50 | * |
MarioBlazevic | 0:d2e26e6fa11b | 51 | * @returns |
MarioBlazevic | 0:d2e26e6fa11b | 52 | * Celsius int |
MarioBlazevic | 0:d2e26e6fa11b | 53 | */ |
MarioBlazevic | 0:d2e26e6fa11b | 54 | int getCelsius(); |
MarioBlazevic | 0:d2e26e6fa11b | 55 | |
MarioBlazevic | 0:d2e26e6fa11b | 56 | /** Get the humidity from the saved object. |
MarioBlazevic | 0:d2e26e6fa11b | 57 | * |
MarioBlazevic | 0:d2e26e6fa11b | 58 | * @returns |
MarioBlazevic | 0:d2e26e6fa11b | 59 | * Humidity percent int |
MarioBlazevic | 0:d2e26e6fa11b | 60 | */ |
MarioBlazevic | 0:d2e26e6fa11b | 61 | int getHumidity(); |
MarioBlazevic | 0:d2e26e6fa11b | 62 | |
MarioBlazevic | 0:d2e26e6fa11b | 63 | private: |
MarioBlazevic | 0:d2e26e6fa11b | 64 | /// percentage of humidity |
MarioBlazevic | 0:d2e26e6fa11b | 65 | int _humidity; |
MarioBlazevic | 0:d2e26e6fa11b | 66 | /// celsius |
MarioBlazevic | 0:d2e26e6fa11b | 67 | int _temperature; |
MarioBlazevic | 0:d2e26e6fa11b | 68 | /// pin to read the sensor info on |
MarioBlazevic | 0:d2e26e6fa11b | 69 | DigitalInOut _pin; |
MarioBlazevic | 0:d2e26e6fa11b | 70 | /// times startup (must settle for at least a second) |
MarioBlazevic | 0:d2e26e6fa11b | 71 | Timer _timer; |
MarioBlazevic | 0:d2e26e6fa11b | 72 | }; |
MarioBlazevic | 0:d2e26e6fa11b | 73 | |
MarioBlazevic | 0:d2e26e6fa11b | 74 | #endif |