DHT11 and EEPROM together
Dependencies: mbed
main.cpp@0:8253de48776b, 2016-01-13 (annotated)
- Committer:
- kaliczp
- Date:
- Wed Jan 13 12:52:35 2016 +0000
- Revision:
- 0:8253de48776b
DHT11 and I2C EEPROM together
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kaliczp | 0:8253de48776b | 1 | #include "mbed.h" |
kaliczp | 0:8253de48776b | 2 | |
kaliczp | 0:8253de48776b | 3 | #define EEPROM_ADDR (0b10100000) // EEPROM address |
kaliczp | 0:8253de48776b | 4 | |
kaliczp | 0:8253de48776b | 5 | I2C i2c(I2C_SDA, I2C_SCL); |
kaliczp | 0:8253de48776b | 6 | DigitalOut myled(LED1); // Activate LED |
kaliczp | 0:8253de48776b | 7 | DigitalIn mybutton(USER_BUTTON); // Activate button |
kaliczp | 0:8253de48776b | 8 | DigitalInOut data_pin(A0); // Activate digital in |
kaliczp | 0:8253de48776b | 9 | Serial pc(SERIAL_TX, SERIAL_RX); // Initialize UART connection |
kaliczp | 0:8253de48776b | 10 | // Use a terminal program (eg. putty). |
kaliczp | 0:8253de48776b | 11 | Timer tmr; //initialize timer |
kaliczp | 0:8253de48776b | 12 | uint64_t adat; // 64 bit variable for temporary data |
kaliczp | 0:8253de48776b | 13 | int i; |
kaliczp | 0:8253de48776b | 14 | char data_write[4]; //Array for address and data |
kaliczp | 0:8253de48776b | 15 | char eeprom_read_address[2] = {0, 0}; |
kaliczp | 0:8253de48776b | 16 | char data_read[2]; |
kaliczp | 0:8253de48776b | 17 | |
kaliczp | 0:8253de48776b | 18 | // Function to initialize DHT11 |
kaliczp | 0:8253de48776b | 19 | void dht_read(void) |
kaliczp | 0:8253de48776b | 20 | { |
kaliczp | 0:8253de48776b | 21 | data_pin.output(); // Set A0 as output |
kaliczp | 0:8253de48776b | 22 | // Initialize measurement > 18 ms low |
kaliczp | 0:8253de48776b | 23 | data_pin = 0; |
kaliczp | 0:8253de48776b | 24 | wait_ms(20); |
kaliczp | 0:8253de48776b | 25 | // After high and release the pin switch input mode |
kaliczp | 0:8253de48776b | 26 | data_pin = 1; |
kaliczp | 0:8253de48776b | 27 | data_pin.input(); |
kaliczp | 0:8253de48776b | 28 | // Wait until the end of 80 us low |
kaliczp | 0:8253de48776b | 29 | while(!data_pin.read()) {} |
kaliczp | 0:8253de48776b | 30 | // Wait until end of 80 us high |
kaliczp | 0:8253de48776b | 31 | while(data_pin.read()) {} |
kaliczp | 0:8253de48776b | 32 | // 40 bit, 40 read out cycle |
kaliczp | 0:8253de48776b | 33 | for(i=0; i<40; i++) { |
kaliczp | 0:8253de48776b | 34 | adat = adat << 1; // Shift for new number |
kaliczp | 0:8253de48776b | 35 | tmr.stop(); // Stop timer if runs |
kaliczp | 0:8253de48776b | 36 | tmr.reset(); // Reset timer |
kaliczp | 0:8253de48776b | 37 | // Wait until pin |
kaliczp | 0:8253de48776b | 38 | while(!data_pin.read()) {} |
kaliczp | 0:8253de48776b | 39 | tmr.start(); |
kaliczp | 0:8253de48776b | 40 | while(data_pin.read()) {} |
kaliczp | 0:8253de48776b | 41 | // If DHT11 HIGH longer than 40 micro seconds (hopefully 70 us) |
kaliczp | 0:8253de48776b | 42 | if(tmr.read_us() > 40) { |
kaliczp | 0:8253de48776b | 43 | // bit is 1 |
kaliczp | 0:8253de48776b | 44 | adat++; |
kaliczp | 0:8253de48776b | 45 | } |
kaliczp | 0:8253de48776b | 46 | } |
kaliczp | 0:8253de48776b | 47 | } |
kaliczp | 0:8253de48776b | 48 | |
kaliczp | 0:8253de48776b | 49 | int main() |
kaliczp | 0:8253de48776b | 50 | { |
kaliczp | 0:8253de48776b | 51 | pc.printf("Log the DHT11 temperature and humidity sensor!\n\r"); //Welcome message |
kaliczp | 0:8253de48776b | 52 | while(1) { |
kaliczp | 0:8253de48776b | 53 | if (mybutton == 0) { // Button is pressed |
kaliczp | 0:8253de48776b | 54 | pc.printf("\n\rLogged humidity and temperature:\n\r"); //Welcome message |
kaliczp | 0:8253de48776b | 55 | myled = 1; // LED is ON |
kaliczp | 0:8253de48776b | 56 | i2c.write(EEPROM_ADDR, eeprom_read_address, 2, true); |
kaliczp | 0:8253de48776b | 57 | for( i=0; i < 30; i++ ) { |
kaliczp | 0:8253de48776b | 58 | i2c.read(EEPROM_ADDR, data_read, 2); |
kaliczp | 0:8253de48776b | 59 | pc.printf("%d %d\n\r",data_read[0], data_read[1]); |
kaliczp | 0:8253de48776b | 60 | } |
kaliczp | 0:8253de48776b | 61 | wait(3); // Wait 0.2 sec till continue. |
kaliczp | 0:8253de48776b | 62 | } else { |
kaliczp | 0:8253de48776b | 63 | myled =1; |
kaliczp | 0:8253de48776b | 64 | wait(0.1); |
kaliczp | 0:8253de48776b | 65 | adat = 0; // Resed global adat variable |
kaliczp | 0:8253de48776b | 66 | dht_read(); // Call the function |
kaliczp | 0:8253de48776b | 67 | // Save data to EEPROM |
kaliczp | 0:8253de48776b | 68 | data_write[2] = (adat & 0x000000ff00000000) >> 32; // Humidity |
kaliczp | 0:8253de48776b | 69 | data_write[3] = (adat & 0x0000000000ff0000) >> 16 ; // Temperature |
kaliczp | 0:8253de48776b | 70 | i2c.write(EEPROM_ADDR, data_write, 4); |
kaliczp | 0:8253de48776b | 71 | myled = 0; // LED is OFF |
kaliczp | 0:8253de48776b | 72 | wait(1.8); |
kaliczp | 0:8253de48776b | 73 | } |
kaliczp | 0:8253de48776b | 74 | } |
kaliczp | 0:8253de48776b | 75 | } |