A simple WIP that logs data from a Grove sensor, and can send and receive information over USB and SMS.

Dependencies:   DHT DS_1337 SDFileSystem USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GroveDht22.cpp Source File

GroveDht22.cpp

00001 #include "GroveDht22.h"
00002 #include "measurementhandler.h"
00003 
00004 #define GROVE_NUM_RETRIES 10        // number of retries for reading sensor
00005 
00006 DigitalOut grovePwr(P1_3);          // if anything else is interfaced to uart/adc/i2c connectors, this will have to change, as they share this enable line
00007 
00008 GroveDht22::GroveDht22(MeasurementHandler *_measure, MyTimers * _timer) : AbstractHandler(_timer), m_measure(_measure)
00009 {
00010     // initialise class variables
00011     mode            = dht_StartTurnOff;
00012     _lastCelcius    = 0.0f;
00013     _lastHumidity   = 0.0f;
00014     _lastDewpoint   = 0.0f;
00015     _lastError      = ERROR_NONE;
00016     _retries        = 0;
00017     _newInfo        = 0;
00018 
00019     m_sensor = new DHT(P1_14,SEN51035P);        // Use the SEN51035P sensor
00020 }
00021 
00022 GroveDht22::~GroveDht22()
00023 {
00024     delete m_sensor;
00025 }
00026 
00027 void GroveDht22::run()
00028 {
00029     switch(mode)
00030     {
00031     case dht_StartTurnOff:
00032         powerOn(false);
00033         m_timer->SetTimer(MyTimers::tmr_GroveMeasure, 1000);     // leave off for 1 second
00034         mode = dht_StartTurnOffWait;
00035         break;
00036 
00037     case dht_StartTurnOffWait:
00038         if (!m_timer->GetTimer(MyTimers::tmr_GroveMeasure))      // wait until timer has elapsed
00039             mode = dht_StartTurnOn;
00040         // else come back here
00041         break;
00042 
00043     case dht_StartTurnOn:
00044         powerOn(true);
00045         m_timer->SetTimer(MyTimers::tmr_GroveMeasure, 1000);     // wait one second for sensor to settle
00046         mode = dht_StartTurnOnWait;
00047         break;
00048 
00049     case dht_StartTurnOnWait:
00050         if (!m_timer->GetTimer(MyTimers::tmr_GroveMeasure))      // wait until timer has elapsed
00051             mode = dht_TakeMeasurement;
00052         // else come back here
00053         break;
00054 
00055     case dht_TakeMeasurement:
00056         _lastError = (eError)m_sensor->readData();     // take the measurement (todo: see if nonblocking is available)
00057         if (_lastError == ERROR_NONE)
00058         {
00059             _retries = 0;                   // reset retries as measurement was successful
00060             _lastCelcius = m_sensor->ReadTemperature(CELCIUS);
00061             _lastHumidity = m_sensor->ReadHumidity();
00062             _lastDewpoint = m_sensor->CalcdewPoint(_lastCelcius, _lastHumidity);
00063             m_timer->SetTimer(MyTimers::tmr_GroveMeasure, 3000); // wait three seconds
00064             
00065             // add the date time
00066             time_t _time = time(NULL); // get the seconds since dawn of time
00067             Dht22Result data = {_time, _lastCelcius, _lastHumidity, _lastDewpoint};
00068             m_measure->setRequest(MeasurementHandler::measreq_DhtResult, (void*)&data);
00069             mode = dht_WaitMeasurement;
00070         }
00071         else
00072         {
00073             _retries++;     // there was an error. See if we have reached critical retries
00074             if (_retries >= GROVE_NUM_RETRIES)
00075             {
00076                 _retries = 0;
00077                 mode = dht_StartTurnOff; // restart the sensor
00078             }
00079             else
00080             {
00081                 // haven't reach critical retries yet, so try again, after waiting
00082                 m_timer->SetTimer(MyTimers::tmr_GroveMeasure, 3000); // wait three seconds
00083                 mode = dht_WaitMeasurement;
00084             }
00085             int sendData = (int)_lastError; // just to make sure nothing funny happens with the enum
00086             m_measure->setRequest(MeasurementHandler::measreq_DhtError, (int*)&sendData);
00087         }
00088         _newInfo = 1;
00089         break;
00090 
00091     case dht_WaitMeasurement:
00092         if (!m_timer->GetTimer(MyTimers::tmr_GroveMeasure))  // if timer elapsed
00093             mode = dht_TakeMeasurement;
00094         // else come back here
00095         break;
00096 
00097     }
00098 }
00099 
00100 void GroveDht22::setRequest(int request, void *data)
00101 {
00102     // no requests (yet)
00103 }
00104 
00105 // check if there is new information and reset the flag once the check occurs
00106 unsigned char GroveDht22::newInfo()
00107 {
00108     unsigned char retval = _newInfo;
00109     _newInfo = 0;
00110     return retval;
00111 }
00112 
00113 // turn the enable pin for the peripheral plugins on the Arch GPRS v2
00114 // it is active low and acts on Q1
00115 void GroveDht22::powerOn(unsigned char ON)
00116 {
00117     if (ON)
00118         grovePwr = 0;   // active low, will turn power on
00119     else
00120         grovePwr = 1;   // will turn power off
00121 }