SEDO subject project

Dependencies:   ds3231 mbed-rtos mbed DHT

Committer:
ValenSalLop
Date:
Mon May 08 18:30:12 2017 +0000
Revision:
11:a9f41d6489c9
Parent:
10:f14f47225091
XBee integration

Who changed what in which revision?

UserRevisionLine numberNew 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 11:a9f41d6489c9 11 static DHT DHTSensor(D3, 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 10:f14f47225091 49 //DEBUG
ValenSalLop 9:5e9c4277151d 50 mutexPCComm.lock();
ValenSalLop 10:f14f47225091 51 printf("DHTError: %d\n", error);
ValenSalLop 9:5e9c4277151d 52 mutexPCComm.unlock();
ValenSalLop 10:f14f47225091 53
ValenSalLop 9:5e9c4277151d 54 }
ValenSalLop 10:f14f47225091 55 /*
ValenSalLop 9:5e9c4277151d 56 // DEBUG
ValenSalLop 9:5e9c4277151d 57 mutexPCComm.lock();
ValenSalLop 10:f14f47225091 58 printf("DHTError: attempt %d %d %d %4.2f\n", attempts, error,sensors_running,tempCurr);
ValenSalLop 9:5e9c4277151d 59 mutexPCComm.unlock();
ValenSalLop 10:f14f47225091 60 */
ValenSalLop 9:5e9c4277151d 61 } // while read attempts
ValenSalLop 9:5e9c4277151d 62 } // main thread while
ValenSalLop 9:5e9c4277151d 63 } // thread function
ValenSalLop 9:5e9c4277151d 64
ValenSalLop 9:5e9c4277151d 65 int DHTread(void)
ValenSalLop 9:5e9c4277151d 66 {
ValenSalLop 9:5e9c4277151d 67 int error = 0;
ValenSalLop 9:5e9c4277151d 68 DHTEnable = 1;
ValenSalLop 9:5e9c4277151d 69 Thread::wait(1000);
ValenSalLop 9:5e9c4277151d 70 error = DHTSensor.readData();
ValenSalLop 9:5e9c4277151d 71 if (error == 0) {
ValenSalLop 9:5e9c4277151d 72 tempCurr = DHTSensor.ReadTemperature(CELCIUS);
ValenSalLop 9:5e9c4277151d 73 humCurr = DHTSensor.ReadHumidity();
ValenSalLop 9:5e9c4277151d 74 dewPCurr = DHTSensor.CalcdewPoint(tempCurr, humCurr);
ValenSalLop 9:5e9c4277151d 75 }
ValenSalLop 9:5e9c4277151d 76 DHTEnable=0;
ValenSalLop 9:5e9c4277151d 77 return error;
ValenSalLop 9:5e9c4277151d 78 }