
Augur rain MASTER
Dependencies: mbed
Fork of 1A_PROJECT_DIGITAL by
Revision 0:ede9cc7e508b, committed 2015-12-08
- Comitter:
- gamezajad
- Date:
- Tue Dec 08 19:25:47 2015 +0000
- Commit message:
- 1A
Changed in this revision
diff -r 000000000000 -r ede9cc7e508b DHT22.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHT22.cpp Tue Dec 08 19:25:47 2015 +0000 @@ -0,0 +1,190 @@ + +#include "DHT22.h" +#define DHT22_DATA_BIT_COUNT 41 + +DHT22::DHT22(PinName Data) { + + _data = Data; // Set Data Pin + _lastReadTime = time(NULL); + _lastHumidity = 0; + _lastTemperature = DHT22_ERROR_VALUE; +} + +DHT22::~DHT22() { +} + +DHT22_ERROR DHT22::readData() { + int i, j, retryCount; + int currentTemperature=0; + int currentHumidity=0; + unsigned int checkSum = 0, csPart1, csPart2, csPart3, csPart4; + unsigned int bitTimes[DHT22_DATA_BIT_COUNT]; + DHT22_ERROR err = DHT_ERROR_NONE; + time_t currentTime = time(NULL); + + DigitalInOut DATA(_data); + + for (i = 0; i < DHT22_DATA_BIT_COUNT; i++) { + bitTimes[i] = 0; + } + + if (int(currentTime - _lastReadTime) < 2) { + err = DHT_ERROR_TOO_QUICK; + } + retryCount = 0; + + do { + if (retryCount > 125) { + printf("DHT22 Bus busy!"); + err = DHT_BUS_HUNG; + } + retryCount ++; + wait_us(2); + } while (DATA==0); // exit on DHT22 return 'High' Signal within 250us + + // Send the activate pulse + // Step 1: MCU send out start signal to DHT22 and DHT22 send + // response signal to MCU. + // If always signal high-voltage-level, it means DHT22 is not + // working properly, please check the electrical connection status. + // + DATA.output(); // set pin to output data + DATA = 0; // MCU send out start signal to DHT22 + wait_ms(18); // 18 ms wait (spec: at least 1ms) + DATA = 1; // MCU pull up + wait_us(40); + DATA.input(); // set pin to receive data + // Find the start of the ACK Pulse + retryCount = 0; + do { + if (retryCount > 40) { // (Spec is 20-40 us high) + printf("DHT22 not responding!"); + err = DHT_ERROR_NOT_PRESENT; + } + retryCount++; + wait_us(1); + } while (DATA==1); // Exit on DHT22 pull low within 40us + if (err != DHT_ERROR_NONE) { + // initialisation failed + return err; + } + wait_us(80); // DHT pull up ready to transmit data + + /* + if (DATA == 0) { + printf("DHT22 not ready!"); + err = DHT_ERROR_ACK_TOO_LONG; + return err; + } + */ + + // Reading the 5 byte data stream + // Step 2: DHT22 send data to MCU + // Start bit -> low volage within 50us (actually could be anything from 35-75us) + // 0 -> high volage within 26-28us (actually could be 10-40us) + // 1 -> high volage within 70us (actually could be 60-85us) + // See http://www.sparkfun.com/products/10167#comment-4f118d6e757b7f536e000000 + + + for (i = 0; i < 5; i++) { + for (j = 0; j < 8; j++) { + // Instead of relying on the data sheet, just wait while the RHT03 pin is low + retryCount = 0; + do { + if (retryCount > 75) { + printf("DHT22 timeout waiting for data!"); + err = DHT_ERROR_DATA_TIMEOUT; + } + retryCount++; + wait_us(1); + } while (DATA == 0); + // We now wait for 40us + wait_us(40); + if (DATA == 1) { + // If pin is still high, bit value is a 1 + bitTimes[i*8+j] = 1; + } else { + // The bit value is a 0 + bitTimes[i*8+j] = 0; + } + int count = 0; + while (DATA == 1 && count < 100) { + wait_us(1); // Delay for 1 microsecond + count++; + } + } + } + // Re-init DHT22 pin + DATA.output(); + DATA = 1; + + // Now bitTimes have the actual bits + // that were needed to find the end of each data bit + // Note: the bits are offset by one from the data sheet, not sure why + currentHumidity = 0; + currentTemperature = 0; + checkSum = 0; + // First 16 bits is Humidity + for (i=0; i<16; i++) { + //printf("bit %d: %d ", i, bitTimes[i+1]); + if (bitTimes[i+1] > 0) { + currentHumidity |= ( 1 << (15-i)); + } + } + + // Second 16 bits is Temperature + for (i=0; i<16; i ++) { + //printf("bit %d: %d ", i+16, bitTimes[i+17]); + if (bitTimes[i+17] > 0) { + currentTemperature |= (1 <<(15-i)); + } + } + + // Last 8 bit is Checksum + for (i=0; i<8; i++) { + //printf("bit %d: %d ", i+32, bitTimes[i+33]); + if (bitTimes[i+33] > 0) { + checkSum |= (1 << (7-i)); + } + } + + _lastHumidity = (float(currentHumidity) / 10.0); + + // if first bit of currentTemperature is 1, it is negative value. + if ((currentTemperature & 0x8000)==0x8000) { + _lastTemperature = (float(currentTemperature & 0x7FFF) / 10.0) * -1.0; + } else { + _lastTemperature = float(currentTemperature) / 10.0; + } + + // Calculate Check Sum + csPart1 = currentHumidity >> 8; + csPart2 = currentHumidity & 0xFF; + csPart3 = currentTemperature >> 8; + csPart4 = currentTemperature & 0xFF; + + if (checkSum == ((csPart1 + csPart2 + csPart3 + csPart4) & 0xFF)) { + _lastReadTime = currentTime; + //printf("OK-->Temperature :: %f , Humidity :: %f\r\n", _lastTemperature, _lastHumidity); + err = DHT_ERROR_NONE; + printf("\n"); + + } + else { + //printf("DHT22 Checksum error!\n"); + //printf("Calculate check sum is %d\n",(csPart1 + csPart2 + csPart3 + csPart4) & 0xFF); + //printf("Reading check sum is %d\n",checkSum); + err = DHT_ERROR_CHECKSUM; + printf("\n"); + } + + return err; +} + +float DHT22::getTemperatureC() { + return _lastTemperature; +} + +float DHT22::getHumidity() { + return _lastHumidity; +} \ No newline at end of file
diff -r 000000000000 -r ede9cc7e508b DHT22.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DHT22.h Tue Dec 08 19:25:47 2015 +0000 @@ -0,0 +1,84 @@ +/* mbed DHT22 Library + * Copyright (c) 2011, sford, http://mbed.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documnetation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MBED_DHT22_H +#define MBED_DHT22_H + +#include "mbed.h" + +/** + * Currently supports DHT22 (SparkFun Electronics) + * Humidity and Temperature Sensor + * + * Features: + * > 3.3-6V Input + * > 1-1.5mA measuring current + * > 40-50uA standby current + * > Humidity from 0-100% RH + * > -40 - 80 degrees C temperature range + * > +-2% RH accuracy + * > +-0.5 degrees C + * + * Usages + * #include "DHT22.h" + * + * DTH22 dth22(PinName sda, PinName scl, int addr); // pin + * + * int main() { + * err=dth22.readData(); + * if (err==0) { + printf("Temperature is %f.2 C\n",dht22.getTemperature()); + printf("Humidity is %f.2 \%\n",dht22.getHumidity()); + } + * + * + */ + +#define DHT22_ERROR_VALUE -99.5 + +typedef enum { + DHT_ERROR_NONE = 0, + DHT_BUS_HUNG, + DHT_ERROR_NOT_PRESENT, + DHT_ERROR_ACK_TOO_LONG, + DHT_ERROR_SYNC_TIMEOUT, + DHT_ERROR_DATA_TIMEOUT, + DHT_ERROR_CHECKSUM, + DHT_ERROR_TOO_QUICK +} DHT22_ERROR; + +class DHT22 { +private: + time_t _lastReadTime; + PinName _data; + float _lastHumidity; + float _lastTemperature; +public: + DHT22(PinName Data); + ~DHT22(); + DHT22_ERROR readData(void); + float getHumidity(); + float getTemperatureC(); + void clockReset(); +}; + +#endif /*_DHT22_H_*/
diff -r 000000000000 -r ede9cc7e508b DS1307.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS1307.cpp Tue Dec 08 19:25:47 2015 +0000 @@ -0,0 +1,78 @@ + +#define DS1307_ADDR 0xD0 +#include "DS1307.h" + +DS1307::DS1307(PinName sda,PinName scl):rtc(sda,scl),display(D7, D8){ + col=1; + display.clear(); +} +void DS1307::displays(){ + + if(col){display.setColon(1);col=0;} + else {display.setColon(0);col=1;} + int sec,mins,hour,day,date,month,year; + + gettime(&sec,&mins,&hour,&day,&date,&month,&year); + + display.write(0, hour / 10); + display.write(1, hour % 10); + display.write(2, mins / 10); + display.write(3, mins % 10); + wait(1); +} +void DS1307::settime(int sec,int mins,int hour,int day,int date,int month,int year){ + char cmd[2]; + cmd[0] = 0x00; + cmd[1] = dectobcd(sec); + rtc.write(DS1307_ADDR, cmd, 2); + cmd[0] = 0x01; + cmd[1] = dectobcd(mins); + rtc.write(DS1307_ADDR, cmd, 2); + cmd[0] = 0x02; + cmd[1] = dectobcd(hour); + rtc.write(DS1307_ADDR, cmd, 2); + cmd[0] = 0x03; + cmd[1] = dectobcd(day); + rtc.write(DS1307_ADDR, cmd, 2); + cmd[0] = 0x04; + cmd[1] = dectobcd(date); + rtc.write(DS1307_ADDR, cmd, 2); + cmd[0] = 0x05; + cmd[1] = dectobcd(month); + rtc.write(DS1307_ADDR, cmd, 2); + cmd[0] = 0x06; + cmd[1] = dectobcd(year); + rtc.write(DS1307_ADDR, cmd, 2); +} +void DS1307::gettime(int *sec,int *mins,int *hour,int *day,int *date,int *month,int *year){ + char cmd = 0; + char v[7]; + for(int x=0;x<=6;x++){ + rtc.write(DS1307_ADDR, &cmd, 1); + rtc.read(DS1307_ADDR, &v[x], 1); + cmd++; + } + *sec=bcdtodec(v[0]); + *mins=bcdtodec(v[1]); + *hour=bcdtodec(v[2]); + *day=bcdtodec(v[3]); + *date=bcdtodec(v[4]); + *month=bcdtodec(v[5]); + *year=bcdtodec(v[6]); +} +int DS1307::bcdtodec( int bcd) { + int low = 0; + int high = 0; + + high = bcd / 16; + low = bcd - (high * 16); + return ((high * 10) + low); +} +int DS1307::dectobcd( int dec) { + int low = 0; + int high = 0; + + high = dec / 10; // this gives the high nibble value + low = dec - (high * 10); // this gives the lower nibble value + return ((high *16) + low); // this is the final bcd value but in interger format +} \ No newline at end of file
diff -r 000000000000 -r ede9cc7e508b DS1307.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS1307.h Tue Dec 08 19:25:47 2015 +0000 @@ -0,0 +1,15 @@ +#include "mbed.h" +#include "DigitDisplay.h" +class DS1307{ + public: + DS1307(PinName,PinName); + int bcdtodec(int); + int dectobcd(int); + void gettime(int *sec,int *mins,int *hour,int *day,int *date,int *month,int *year); + void settime(int sec,int mins,int hour,int day,int date,int month,int year); + void displays(); + private: + I2C rtc; + DigitDisplay display; + bool col; +};
diff -r 000000000000 -r ede9cc7e508b DigitDisplay.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DigitDisplay.cpp Tue Dec 08 19:25:47 2015 +0000 @@ -0,0 +1,317 @@ +/* The library of Grove - 4 Digit Display + * + * \author Yihui Xiong + * \date 2014/2/8 + * + * The MIT License (MIT) + * + * Copyright (c) 2014 Seeed Technology Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "DigitDisplay.h" + +#define ADDR_AUTO 0x40 +#define ADDR_FIXED 0x44 + +#define POSITION_COLON 1 + +#define DIGIT_UNKOWN 0x08 +#define DIGIT_NULL 0x00 +#define DIGIT_MINUS 0x40 + +const uint8_t DIGIT_TABLE[] = {0x3f, 0x06, 0x5b, 0x4f, + 0x66, 0x6d, 0x7d, 0x07, + 0x7f, 0x6f, 0x77, 0x7c, + 0x39, 0x5e, 0x79, 0x71 + }; //0~9,A,b,C,d,E,F + + +inline uint8_t conv(uint8_t n) +{ + uint8_t segments; + + if (n <= sizeof(DIGIT_TABLE)) { + segments = DIGIT_TABLE[n]; + }else if (n == 0xFF) { + segments = DIGIT_NULL; + } else { + segments = DIGIT_UNKOWN; + } + + return segments; +} + +DigitDisplay::DigitDisplay(PinName clk, PinName dio) : _clk(clk), _dio(dio) +{ + _dio.output(); + _dio = 1; + _clk = 1; + + _brightness = 2; + _colon = false; + _off = true; + + for (uint8_t i = 0; i < sizeof(_content); i++) { + _content[i] = DIGIT_NULL; + } +} + +void DigitDisplay::on() +{ + start(); + send(0x88 | _brightness); + stop(); +} + +void DigitDisplay::off() +{ + start(); + send(0x80); + stop(); +} + +void DigitDisplay::setBrightness(uint8_t brightness) +{ + if (brightness > 7) { + brightness = 7; + } + + _brightness = brightness; + + start(); + send(0x88 | _brightness); + stop(); +} + +void DigitDisplay::setColon(bool enable) +{ + if (_colon != enable) { + _colon = enable; + + if (enable) { + _content[POSITION_COLON] |= 0x80; + } else { + _content[POSITION_COLON] &= 0x7F; + } + + writeRaw(POSITION_COLON, _content[POSITION_COLON]); + } +} + +void DigitDisplay::write(int16_t n) +{ + uint8_t negative = 0; + + if (n < 0) { + negative = 1; + n = (-n) % 1000; + } else { + n = n % 10000; + } + + int8_t i = 3; + do { + uint8_t r = n % 10; + _content[i] = conv(r); + i--; + n = n / 10; + } while (n != 0); + + if (negative) { + _content[i] = DIGIT_MINUS; + i--; + } + + for (int8_t j = 0; j <= i; j++) { + _content[j] = DIGIT_NULL; + } + + if (_colon) { + _content[POSITION_COLON] |= 0x80; + } + + writeRaw(_content); +} + +void DigitDisplay::write(uint8_t numbers[]) +{ + for (uint8_t i = 0; i < 4; i++) { + _content[i] = conv(numbers[i]); + } + + if (_colon) { + _content[POSITION_COLON] |= 0x80; + } + + start(); + send(ADDR_AUTO); + stop(); + start(); + send(0xC0); + for (uint8_t i = 0; i < 4; i++) { + send(_content[i]); + } + stop(); + + if (_off) { + _off = 0; + start(); + send(0x88 | _brightness); + stop(); + } +} + +void DigitDisplay::write(uint8_t position, uint8_t number) +{ + if (position >= 4) { + return; + } + + uint8_t segments = conv(number); + + if ((position == POSITION_COLON) && _colon) { + segments |= 0x80; + } + + _content[position] = segments; + + start(); + send(ADDR_FIXED); + stop(); + start(); + send(0xC0 | position); + send(segments); + stop(); + + if (_off) { + _off = 0; + start(); + send(0x88 | _brightness); + stop(); + } +} + +void DigitDisplay::writeRaw(uint8_t segments[]) +{ + for (uint8_t i = 0; i < 4; i++) { + _content[i] = segments[i]; + } + + start(); + send(ADDR_AUTO); + stop(); + start(); + send(0xC0); + for (uint8_t i = 0; i < 4; i++) { + send(segments[i]); + } + stop(); + + if (_off) { + _off = 0; + start(); + send(0x88 | _brightness); + stop(); + } +} + +void DigitDisplay::writeRaw(uint8_t position, uint8_t segments) +{ + if (position >= 4) { + return; + } + + _content[position] = segments; + + start(); + send(ADDR_FIXED); + stop(); + start(); + send(0xC0 | position); + send(segments); + stop(); + + if (_off) { + _off = 0; + start(); + send(0x88 | _brightness); + stop(); + } +} + +void DigitDisplay::clear() +{ + for (uint8_t i = 0; i < 4; i++) { + _content[i] = DIGIT_NULL; + } + _colon = false; + + writeRaw(0, DIGIT_NULL); + writeRaw(1, DIGIT_NULL); + writeRaw(2, DIGIT_NULL); + writeRaw(3, DIGIT_NULL); +} + +void DigitDisplay::start() +{ + _clk = 1; + _dio = 1; + _dio = 0; + _clk = 0; +} + +bool DigitDisplay::send(uint8_t data) +{ + for (uint8_t i = 0; i < 8; i++) { + _clk = 0; + _dio = data & 1; + data >>= 1; + _clk = 1; + } + + // check ack + _clk = 0; + _dio = 1; + _clk = 1; + _dio.input(); + + uint16_t count = 0; + while (_dio) { + count++; + if (count >= 200) { + _dio.output(); + return false; + } + } + + _dio.output(); + return true; +} + +void DigitDisplay::stop() +{ + _clk = 0; + _dio = 0; + _clk = 1; + _dio = 1; +} + +
diff -r 000000000000 -r ede9cc7e508b DigitDisplay.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DigitDisplay.h Tue Dec 08 19:25:47 2015 +0000 @@ -0,0 +1,96 @@ +/* The library of Grove - 4 Digit Display + * + * \author Yihui Xiong + * \date 2014/2/8 + * + * The MIT License (MIT) + * + * Copyright (c) 2014 Seeed Technology Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef __DIGIT_DISPLAY__ +#define __DIGIT_DISPLAY__ + +#include "mbed.h" + +class DigitDisplay +{ +public: + DigitDisplay(PinName clk, PinName dio); + + /** + * Display a decimal number, A number larger than 10000 or smaller than -1000 will be truncated, MSB will be lost. + */ + void write(int16_t number); + + /** + * Display four numbers + * \param numbers 0 - 0xF: 0 - 9, A, b, C, d, E, F + * 0x10 - 0xFE: '_'(unknown) + * 0xFF: ' '(null) + */ + void write(uint8_t numbers[]); + + /** + * Display a number in a specific position + * \param position 0 - 3, left to right + * \param number 0 - 0xF: 0 - 9, A, b, C, d, E, F + * 0x10 - 0xFE: '_' + * 0xFF: ' ' + */ + void write(uint8_t position, uint8_t number); + + void writeRaw(uint8_t position, uint8_t segments); + void writeRaw(uint8_t segments[]); + + void clear(); + void on(); + void off(); + + /** + * Set the brightness, level 0 to level 7 + */ + void setBrightness(uint8_t brightness); + + /** + * Enable/Disable the colon + */ + void setColon(bool enable); + + DigitDisplay& operator= (int16_t number) { + write(number); + return *this; + } + +private: + void start(); + bool send(uint8_t data); + void stop(); + + DigitalOut _clk; + DigitalInOut _dio; + bool _off; + bool _colon; + uint8_t _brightness; + uint8_t _content[4]; +}; + +#endif // __DIGIT_DISPLAY__
diff -r 000000000000 -r ede9cc7e508b main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Dec 08 19:25:47 2015 +0000 @@ -0,0 +1,219 @@ +#include "mbed.h" +#include "DHT22.h" +#include "DS1307.h" +#include "DigitDisplay.h" + +//+ADDR:98d3:31:504589 +DS1307 rtc(I2C_SDA,I2C_SCL); +Serial Master_to_Slave(PA_11,PA_12 ); +DigitDisplay display(D7,D8); +AnalogIn lightsensor(A3); +Ticker flipper; + +DigitalOut LEDOUT1(D2); +DigitalOut LEDOUT2(D3); +DigitalOut LEDOUT3(D4); +DigitalOut LEDOUT4(D5); +DigitalOut buzzer(D6); +DHT22 hum_temp(D9); + +void Sunny(); +void DHT22(); +void lightLDR(); +void realtime(); +void Augur_Rain(); +void Senddata(); +void printf_data(); + +int test_hum=3,test_light = 3500; +int rain_hum_bf=0,rain_hum_af=0,Sum=0,rain_drop=0,Send_light=0; +float Analog,temp,hum; +float Max_temp,Max_hum,Max_light; +int sec=00,mins=8,hour=23,day=01,date=8,month=12,year=15,done=0; +int sec_new,mins_new,hour_new,day_new,date_new,month_new,year_new; + +void Sunny() +{ + DHT22();//วัดอุหภูมินในอากาศกับความชื้น + lightLDR();//ค่าแสงที่ได + Augur_Rain();//พยากรณ์ฝน + printf_data();//ออกมาทาง PC + LEDOUT4=1; + //printf("%d\n",hour_new); + if(hour_new >= 6 && hour_new <= 18 ) { + LEDOUT2=1; + LEDOUT3=0; //สถานะของ LED + + //DHT22();//วัดอุหภูมินในอากาศกับความชื้น + //lightLDR();//ค่าแสงที่ได + //Augur_Rain();//พยากรณ์ฝน + //printf_data();//ออกมาทาง PC + //Senddata(); + + + + + } else { + + //printf("Sleeping guy \n"); + LEDOUT3=1; + LEDOUT2=0; + } + + +} + +void DHT22() +{ + + if(hum_temp.readData() == DHT_ERROR_NONE) done=1; + { + temp = hum_temp.getTemperatureC(); + hum = hum_temp.getHumidity(); + + + } + + if(temp > Max_temp) { + Max_temp=temp; + } + if(hum > Max_hum) { + Max_hum=hum; + } + if(hour_new > 18) { + Max_temp=0; + Max_hum=0; + } + +} +void lightLDR() +{ + Analog=lightsensor.read(); + Analog=Analog*5000; + + if(Analog < 700 ) { + Send_light=1; //มีแสงง + } else { + Send_light=0;//ไม่มีแสงหรือมีแสงน้อย + } + + + if(Analog > Max_light) { + Max_light=Analog; + } + if(hour_new > 17) { + Max_light=0; + } + + + +} +void Augur_Rain() +{ + + if(mins_new == 5) { + rain_hum_af=hum; + //rain_hum_af=78; + } + if(mins_new == 10) { + rain_hum_bf=hum; + } + if(mins_new == 15) { + rain_hum_af=hum; + } + if(mins_new == 20) { + rain_hum_bf=hum; + } + if(mins_new == 25) { + rain_hum_af=hum; + } + if(mins_new == 30) { + rain_hum_bf=hum; + } + if(mins_new == 35) { + rain_hum_af=hum; + } + if(mins_new == 40) { + rain_hum_bf=hum; + } + if(mins_new == 45) { + rain_hum_af=hum; + } + if(mins_new == 50) { + rain_hum_bf=hum; + } + if(mins_new == 55) { + rain_hum_af=hum; + } + if(mins_new == 0) { + rain_hum_bf=hum; + rain_hum_af=0; + //rain_hum_bf=75; + + } + + Sum=rain_hum_bf - rain_hum_af;//ค่าที่ได้ออกมาถ้าห่างกัน2-3ผมอาจจะตกตามที่วัดจากของจิง + + //test_hum=3,test_light = 3500; + //Sum=test_hum; + //Analog=test_light; + + if(((Sum >=-5 && Sum <= -3) || (Sum >= 3 && Sum <= 5)) && (Analog > 1500 && Analog <= 5000) ) { + rain_drop=1;//อีกประมาณ5-30นาทีฝนตก + LEDOUT1=1; + } else { + rain_drop=0;//ฝนไม่ตก + LEDOUT1=0; + } + if(hour_new > 18) { + //Sum=0; + } +} +void Senddata() +{ + + Master_to_Slave.printf("%.2d%.2d%.1f%.1f%.1d%.1d\n",hour_new,mins_new,temp,hum,Send_light,rain_drop); + +} +void printf_data() +{ + printf("time :: %d:%d \n",hour_new,mins_new); + printf("Temperature :: %f C \n",temp); + printf("Humidity :: %f g/m3\n",hum); + printf("Light by LDR :: %f\n",Analog); + //printf("Max_Temperature :: %f C \n",Max_temp); + // printf("Max_Humidity :: %f g/m3\n",Max_hum); + // printf("Max_Light :: %f\n",Max_light); + printf("light :: %d \n",Send_light); + printf("Sum :: %d \n",Sum); + printf("Rain_drop :: %d \n",rain_drop); + printf("Sendata M TO SL :: %.2d%.2d%.1f%.1f%.1d%.1d\n\n",hour_new,mins_new,temp,hum,Send_light,rain_drop); +} +void realtime() +{ + rtc.gettime(&sec_new,&mins_new,&hour_new,&day_new,&date_new,&month_new,&year_new); + rtc.displays(); + +} +void flip() +{ + //if(hour_new >= 6 && hour_new <= 18 ) + //{Senddata();} + Senddata(); +} + +int main() +{ + + rtc.settime(sec,mins,hour,day,date,month,year); + flipper.attach(&flip, 10.0); + + while(1) { + + + realtime(); + Sunny(); + + + } +}
diff -r 000000000000 -r ede9cc7e508b mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Dec 08 19:25:47 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68 \ No newline at end of file