DHT11 and EEPROM together

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #define EEPROM_ADDR   (0b10100000) // EEPROM address
00004 
00005 I2C i2c(I2C_SDA, I2C_SCL);
00006 DigitalOut myled(LED1); // Activate LED
00007 DigitalIn mybutton(USER_BUTTON); // Activate button
00008 DigitalInOut data_pin(A0); // Activate digital in
00009 Serial pc(SERIAL_TX, SERIAL_RX); // Initialize UART connection
00010 // Use a terminal program (eg. putty).
00011 Timer tmr; //initialize timer
00012 uint64_t adat; // 64 bit variable for temporary data
00013 int i;
00014 char data_write[4]; //Array for address and data
00015 char eeprom_read_address[2] = {0, 0};
00016 char data_read[2];
00017 
00018 // Function to initialize DHT11
00019 void dht_read(void)
00020 {
00021     data_pin.output(); // Set A0 as output
00022     // Initialize measurement > 18 ms low
00023     data_pin = 0;
00024     wait_ms(20);
00025     // After high and release the pin switch input mode
00026     data_pin = 1;
00027     data_pin.input();
00028     // Wait until the end of 80 us low
00029     while(!data_pin.read()) {}
00030     // Wait until end of 80 us high
00031     while(data_pin.read()) {}
00032     // 40 bit, 40 read out cycle
00033     for(i=0; i<40; i++) {
00034         adat = adat << 1; // Shift for new number
00035         tmr.stop(); // Stop timer if runs
00036         tmr.reset();  // Reset timer
00037         // Wait until pin
00038         while(!data_pin.read()) {}
00039         tmr.start();
00040         while(data_pin.read()) {}
00041         // If DHT11 HIGH longer than 40 micro seconds (hopefully 70 us)
00042         if(tmr.read_us() > 40) {
00043             // bit is 1
00044             adat++;
00045         }
00046     }
00047 }
00048 
00049 int main()
00050 {
00051     pc.printf("Log the DHT11 temperature and humidity sensor!\n\r"); //Welcome message
00052     while(1) {
00053         if (mybutton == 0) { // Button is pressed
00054             pc.printf("\n\rLogged humidity and temperature:\n\r"); //Welcome message
00055             myled = 1; // LED is ON
00056             i2c.write(EEPROM_ADDR, eeprom_read_address, 2, true);
00057             for( i=0; i < 30; i++ ) {
00058                 i2c.read(EEPROM_ADDR, data_read, 2);
00059                 pc.printf("%d %d\n\r",data_read[0], data_read[1]);
00060             }
00061             wait(3); // Wait 0.2 sec till continue.
00062         } else {
00063             myled =1;
00064             wait(0.1);
00065             adat = 0; // Resed global adat variable
00066             dht_read(); // Call the function
00067             // Save data to EEPROM
00068             data_write[2] = (adat  & 0x000000ff00000000) >> 32; // Humidity
00069             data_write[3] = (adat & 0x0000000000ff0000) >> 16 ; // Temperature
00070             i2c.write(EEPROM_ADDR, data_write, 4);
00071             myled = 0; // LED is OFF
00072             wait(1.8);
00073         }
00074     }
00075 }