SEDO subject project
Dependencies: ds3231 mbed-rtos mbed DHT
Diff: main.cpp
- Revision:
- 7:8065e844e99a
- Parent:
- 6:dff022cf4d99
- Child:
- 9:5e9c4277151d
diff -r dff022cf4d99 -r 8065e844e99a main.cpp --- a/main.cpp Wed Apr 26 18:24:48 2017 +0000 +++ b/main.cpp Sat Apr 29 15:10:34 2017 +0000 @@ -7,90 +7,108 @@ #define I2C_SDA D14 #define I2C_SCL D15 -#define DHT_PIN D2 -#define LCD_PIN1 D8 -#define LCD_PIN2 D13 -#define LCD_PIN3 D9 -#define LCD_PIN4 D4 -#define LCD_PIN5 D5 -#define LCD_PIN6 D6 -#define LCD_PIN7 D7 +#define DHT_EN_PIN D7 +#define DHT_PIN D6 #define M_DIR_PIN D4 #define M_SPE_PIN D5 +void setupRTC(); void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member); void get_user_input(char* message, uint8_t min, uint8_t max, bool* member); -void setupRTC(Ds3231 rtc, time_t* epoch_time, ds3231_cntl_stat_t rtc_control_status, ds3231_time_t rtc_time, ds3231_calendar_t rtc_calendar); - +int readDHT(float* temperature, float* humidity, float* dewPoint); +int measure(time_t* ts, float* temperature, float* humidity, float* dewPoint); +int setMotor(int direction, double speed); +void wakeupFromRTC(void); Ds3231 rtc(D14, D15); -PwmOut m_speed(M_SPE_PIN); -DigitalOut m_direction(M_DIR_PIN); +PwmOut m_speed(D4); +DigitalOut m_direction(D5); +DHT DHTSensor(D6, DHT11); +DigitalOut DHTEnable(D7); -DHT sensor(D2, DHT22); +InterruptIn iRTC(D8); int main(void) { - //rtc variables - time_t epoch_time; + int measureError = 0; + float temperature = 0.0, humidity = 0.0, dewPoint = 0.0; + int motorDirection = 0; + double motorSpeed = 0.0; + char buffer[32]; + time_t ts; + + //setupRTC(); + + iRTC.rise(wakeupFromRTC); + + while(true) { + //printf("%c[2J", ESC); //clear screen + sleep(); + ts = rtc.get_epoch(); + measureError = measure(&ts,&temperature,&humidity,&dewPoint); + + if(!measureError) { + printf("\nTime as seconds since January 1, 1970 = %d\n", ts); + printf("Temperature in Celcius: %4.2f\n", temperature); + printf("Humidity is %4.2f, Dewpoint: %4.2f\n", humidity, dewPoint); + setMotor(motorDirection, motorSpeed); + } + wait(1); + }//loop +} +int measure(time_t* ts, float* temperature, float* humidity, float* dewPoint) +{ + int DHTError = 0; + *ts = rtc.get_epoch(); + if(*ts<=0) + return 1; + DHTError = readDHT( temperature, humidity, dewPoint); + if(DHTError) { + printf("DHTError: %d\n", DHTError); + return 2; + } + return 0; + +} +int readDHT(float* temperature, float* humidity, float* dewPoint) +{ + int error = 0; + DHTEnable = 1; + wait(2); + error = DHTSensor.readData(); + if (0 == error) { + *temperature = DHTSensor.ReadTemperature(CELCIUS); + *humidity = DHTSensor.ReadHumidity(); + *dewPoint = DHTSensor.CalcdewPoint(*temperature, *humidity); + } + DHTEnable=0; + return error; +} + +int setMotor(int direction, double speed) +{ + if(speed<0 or speed>255) + return 1; + else { + m_speed = speed; + if(direction) + m_direction=1; + else + m_direction=0; + } + return 0; +} + +void setupRTC() +{ + //default, use bit masks in ds3231.h for desired operation ds3231_cntl_stat_t rtc_control_status = {0,0}; ds3231_time_t rtc_time; ds3231_calendar_t rtc_calendar; - //setupRTC(rtc, &epoch_time, rtc_control_status, rtc_time, rtc_calendar); - - char buffer[32]; - for(;;) { - printf("%c[2J", ESC); //clear screen - printf("%c[H", ESC); //move cursor to Home - - //new epoch time fx - epoch_time = rtc.get_epoch(); - - printf("\nTime as seconds since January 1, 1970 = %d\n", epoch_time); - - printf("\nTime as a basic string = %s", ctime(&epoch_time)); - - strftime(buffer, 32, "%I:%M %p\n", localtime(&epoch_time)); - printf("\nTime as a custom formatted string = %s", buffer); - - while(m_speed < 1.0) { - m_speed = m_speed + 0.01; - wait(0.1); - if(m_speed >= 1.0) { - } - } - m_speed = 0; - m_direction = !m_direction; - - - int error = 0; - float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; - - error = sensor.readData(); - if (0 == error) { - c = sensor.ReadTemperature(CELCIUS); - f = sensor.ReadTemperature(FARENHEIT); - k = sensor.ReadTemperature(KELVIN); - h = sensor.ReadHumidity(); - dp = sensor.CalcdewPoint(c, h); - dpf = sensor.CalcdewPointFast(c, h); - printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f); - printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf); - } else { - printf("Error: %d\n", error); - } - wait(5.0); - }//loop -} - - -void setupRTC(Ds3231 rtc, time_t* epoch_time, ds3231_cntl_stat_t rtc_control_status, ds3231_time_t rtc_time, ds3231_calendar_t rtc_calendar) -{ - rtc.set_cntl_stat_reg(rtc_control_status); //get day from user @@ -201,5 +219,5 @@ } } while((*(member) < min) || (*(member) > max)); } +void wakeupFromRTC(void){} -