SEDO subject project
Dependencies: ds3231 mbed-rtos mbed DHT
temp_hum_sensor.cpp@9:5e9c4277151d, 2017-05-08 (annotated)
- Committer:
- ValenSalLop
- Date:
- Mon May 08 07:20:54 2017 +0000
- Revision:
- 9:5e9c4277151d
- Child:
- 10:f14f47225091
RTOS used
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ValenSalLop | 9:5e9c4277151d | 1 | /* |
ValenSalLop | 9:5e9c4277151d | 2 | * Temperature & hummidity sensor operations |
ValenSalLop | 9:5e9c4277151d | 3 | */ |
ValenSalLop | 9:5e9c4277151d | 4 | #include "mbed.h" |
ValenSalLop | 9:5e9c4277151d | 5 | #include "rtos.h" |
ValenSalLop | 9:5e9c4277151d | 6 | |
ValenSalLop | 9:5e9c4277151d | 7 | #include "main.h" |
ValenSalLop | 9:5e9c4277151d | 8 | #include "temp_hum_sensor.h" |
ValenSalLop | 9:5e9c4277151d | 9 | #include "DHT.h" |
ValenSalLop | 9:5e9c4277151d | 10 | |
ValenSalLop | 9:5e9c4277151d | 11 | static DHT DHTSensor(D6, DHT11); |
ValenSalLop | 9:5e9c4277151d | 12 | static DigitalOut DHTEnable(D7); |
ValenSalLop | 9:5e9c4277151d | 13 | static float tempCurr, humCurr, dewPCurr; |
ValenSalLop | 9:5e9c4277151d | 14 | uint32_t temp_hum_init(void const *args) |
ValenSalLop | 9:5e9c4277151d | 15 | { |
ValenSalLop | 9:5e9c4277151d | 16 | |
ValenSalLop | 9:5e9c4277151d | 17 | return 1; |
ValenSalLop | 9:5e9c4277151d | 18 | } |
ValenSalLop | 9:5e9c4277151d | 19 | void temp_hum_thread(void const *args) |
ValenSalLop | 9:5e9c4277151d | 20 | { |
ValenSalLop | 9:5e9c4277151d | 21 | //DEBUG |
ValenSalLop | 9:5e9c4277151d | 22 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 23 | pc.printf("SENSOR: thread init\n"); |
ValenSalLop | 9:5e9c4277151d | 24 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 25 | int8_t error = 0; |
ValenSalLop | 9:5e9c4277151d | 26 | int8_t attempts=0; |
ValenSalLop | 9:5e9c4277151d | 27 | while(true) { |
ValenSalLop | 9:5e9c4277151d | 28 | Thread::signal_wait(0); |
ValenSalLop | 9:5e9c4277151d | 29 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 30 | pc.printf("SENSOR: loop\n"); |
ValenSalLop | 9:5e9c4277151d | 31 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 32 | error = -1; |
ValenSalLop | 9:5e9c4277151d | 33 | attempts=0; |
ValenSalLop | 9:5e9c4277151d | 34 | while(error!=0 && attempts<MAX_READ_ATTEMPTS_TEMP_HUM && sensors_running == true) { |
ValenSalLop | 9:5e9c4277151d | 35 | error = DHTread(); |
ValenSalLop | 9:5e9c4277151d | 36 | if(error==0) { |
ValenSalLop | 9:5e9c4277151d | 37 | mutexData.lock(); |
ValenSalLop | 9:5e9c4277151d | 38 | data.temperature = tempCurr; |
ValenSalLop | 9:5e9c4277151d | 39 | data.humidity = humCurr; |
ValenSalLop | 9:5e9c4277151d | 40 | data.dewPoint = dewPCurr; |
ValenSalLop | 9:5e9c4277151d | 41 | data.DHTError = 0; |
ValenSalLop | 9:5e9c4277151d | 42 | mutexData.unlock(); |
ValenSalLop | 9:5e9c4277151d | 43 | } else { |
ValenSalLop | 9:5e9c4277151d | 44 | mutexData.lock(); |
ValenSalLop | 9:5e9c4277151d | 45 | data.DHTError = error; |
ValenSalLop | 9:5e9c4277151d | 46 | mutexData.unlock(); |
ValenSalLop | 9:5e9c4277151d | 47 | attempts++; |
ValenSalLop | 9:5e9c4277151d | 48 | Thread::wait(3000); |
ValenSalLop | 9:5e9c4277151d | 49 | /* //DEBUG |
ValenSalLop | 9:5e9c4277151d | 50 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 51 | printf("DHTError: %d\n", DHTError); |
ValenSalLop | 9:5e9c4277151d | 52 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 53 | */ |
ValenSalLop | 9:5e9c4277151d | 54 | } |
ValenSalLop | 9:5e9c4277151d | 55 | // DEBUG |
ValenSalLop | 9:5e9c4277151d | 56 | mutexPCComm.lock(); |
ValenSalLop | 9:5e9c4277151d | 57 | printf("DHTError: attempt %d\n", attempts); |
ValenSalLop | 9:5e9c4277151d | 58 | mutexPCComm.unlock(); |
ValenSalLop | 9:5e9c4277151d | 59 | } // while read attempts |
ValenSalLop | 9:5e9c4277151d | 60 | } // main thread while |
ValenSalLop | 9:5e9c4277151d | 61 | } // thread function |
ValenSalLop | 9:5e9c4277151d | 62 | |
ValenSalLop | 9:5e9c4277151d | 63 | int DHTread(void) |
ValenSalLop | 9:5e9c4277151d | 64 | { |
ValenSalLop | 9:5e9c4277151d | 65 | int error = 0; |
ValenSalLop | 9:5e9c4277151d | 66 | DHTEnable = 1; |
ValenSalLop | 9:5e9c4277151d | 67 | Thread::wait(1000); |
ValenSalLop | 9:5e9c4277151d | 68 | error = DHTSensor.readData(); |
ValenSalLop | 9:5e9c4277151d | 69 | if (error == 0) { |
ValenSalLop | 9:5e9c4277151d | 70 | tempCurr = DHTSensor.ReadTemperature(CELCIUS); |
ValenSalLop | 9:5e9c4277151d | 71 | humCurr = DHTSensor.ReadHumidity(); |
ValenSalLop | 9:5e9c4277151d | 72 | dewPCurr = DHTSensor.CalcdewPoint(tempCurr, humCurr); |
ValenSalLop | 9:5e9c4277151d | 73 | } |
ValenSalLop | 9:5e9c4277151d | 74 | DHTEnable=0; |
ValenSalLop | 9:5e9c4277151d | 75 | return error; |
ValenSalLop | 9:5e9c4277151d | 76 | } |