GSOE Webserver ESP01 DHT11
Dependencies: LCD_i2c_GSOE ESP8266WebserverGSOE
main.cpp@0:8fe7d36af056, 2014-03-17 (annotated)
- Committer:
- JENG
- Date:
- Mon Mar 17 12:35:48 2014 +0000
- Revision:
- 0:8fe7d36af056
- Child:
- 1:e6f838f99eeb
basic interface read dht11 show on clcd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JENG | 0:8fe7d36af056 | 1 | |
JENG | 0:8fe7d36af056 | 2 | #include "mbed.h" |
JENG | 0:8fe7d36af056 | 3 | |
JENG | 0:8fe7d36af056 | 4 | DigitalOut LCD_RS(D8); |
JENG | 0:8fe7d36af056 | 5 | DigitalOut LCD_EN(D9); |
JENG | 0:8fe7d36af056 | 6 | DigitalOut LCD_D4(D4); |
JENG | 0:8fe7d36af056 | 7 | DigitalOut LCD_D5(D5); |
JENG | 0:8fe7d36af056 | 8 | DigitalOut LCD_D6(D6); |
JENG | 0:8fe7d36af056 | 9 | DigitalOut LCD_D7(D7); |
JENG | 0:8fe7d36af056 | 10 | |
JENG | 0:8fe7d36af056 | 11 | |
JENG | 0:8fe7d36af056 | 12 | //######################################## |
JENG | 0:8fe7d36af056 | 13 | // LCD Library |
JENG | 0:8fe7d36af056 | 14 | //######################################## |
JENG | 0:8fe7d36af056 | 15 | |
JENG | 0:8fe7d36af056 | 16 | //send data to lcd |
JENG | 0:8fe7d36af056 | 17 | void write_lcd(uint8_t d){ |
JENG | 0:8fe7d36af056 | 18 | |
JENG | 0:8fe7d36af056 | 19 | LCD_D4 = (d & 0x10)?1:0; |
JENG | 0:8fe7d36af056 | 20 | LCD_D5 = (d & 0x20)?1:0; |
JENG | 0:8fe7d36af056 | 21 | LCD_D6 = (d & 0x40)?1:0; |
JENG | 0:8fe7d36af056 | 22 | LCD_D7 = (d & 0x80)?1:0; |
JENG | 0:8fe7d36af056 | 23 | |
JENG | 0:8fe7d36af056 | 24 | wait_us(2); |
JENG | 0:8fe7d36af056 | 25 | LCD_EN = 1; |
JENG | 0:8fe7d36af056 | 26 | wait_us(2); |
JENG | 0:8fe7d36af056 | 27 | LCD_EN = 0; |
JENG | 0:8fe7d36af056 | 28 | |
JENG | 0:8fe7d36af056 | 29 | } |
JENG | 0:8fe7d36af056 | 30 | |
JENG | 0:8fe7d36af056 | 31 | //lcd write byte |
JENG | 0:8fe7d36af056 | 32 | void lcd_write_byte(uint8_t byte){ |
JENG | 0:8fe7d36af056 | 33 | |
JENG | 0:8fe7d36af056 | 34 | write_lcd(byte); |
JENG | 0:8fe7d36af056 | 35 | write_lcd(byte << 4); |
JENG | 0:8fe7d36af056 | 36 | |
JENG | 0:8fe7d36af056 | 37 | } |
JENG | 0:8fe7d36af056 | 38 | |
JENG | 0:8fe7d36af056 | 39 | //write command |
JENG | 0:8fe7d36af056 | 40 | void lcd_putcmd(uint8_t c){ |
JENG | 0:8fe7d36af056 | 41 | |
JENG | 0:8fe7d36af056 | 42 | wait_ms(5); |
JENG | 0:8fe7d36af056 | 43 | lcd_write_byte(c); |
JENG | 0:8fe7d36af056 | 44 | |
JENG | 0:8fe7d36af056 | 45 | } |
JENG | 0:8fe7d36af056 | 46 | |
JENG | 0:8fe7d36af056 | 47 | //write one char |
JENG | 0:8fe7d36af056 | 48 | void lcd_putc(uint8_t d){ |
JENG | 0:8fe7d36af056 | 49 | |
JENG | 0:8fe7d36af056 | 50 | wait_ms(1); |
JENG | 0:8fe7d36af056 | 51 | LCD_RS = 1; |
JENG | 0:8fe7d36af056 | 52 | lcd_write_byte(d); |
JENG | 0:8fe7d36af056 | 53 | LCD_RS = 0; |
JENG | 0:8fe7d36af056 | 54 | |
JENG | 0:8fe7d36af056 | 55 | } |
JENG | 0:8fe7d36af056 | 56 | |
JENG | 0:8fe7d36af056 | 57 | |
JENG | 0:8fe7d36af056 | 58 | |
JENG | 0:8fe7d36af056 | 59 | //print string |
JENG | 0:8fe7d36af056 | 60 | void lcd_puts(char * p){ |
JENG | 0:8fe7d36af056 | 61 | |
JENG | 0:8fe7d36af056 | 62 | while(* p)lcd_putc(* p++); |
JENG | 0:8fe7d36af056 | 63 | |
JENG | 0:8fe7d36af056 | 64 | } |
JENG | 0:8fe7d36af056 | 65 | |
JENG | 0:8fe7d36af056 | 66 | //lcd initial interface |
JENG | 0:8fe7d36af056 | 67 | void lcd_initial(void){ |
JENG | 0:8fe7d36af056 | 68 | |
JENG | 0:8fe7d36af056 | 69 | uint8_t x = 3; |
JENG | 0:8fe7d36af056 | 70 | |
JENG | 0:8fe7d36af056 | 71 | LCD_RS = 0; |
JENG | 0:8fe7d36af056 | 72 | LCD_EN = 0; |
JENG | 0:8fe7d36af056 | 73 | |
JENG | 0:8fe7d36af056 | 74 | LCD_D4 = 0; |
JENG | 0:8fe7d36af056 | 75 | LCD_D5 = 0; |
JENG | 0:8fe7d36af056 | 76 | LCD_D6 = 0; |
JENG | 0:8fe7d36af056 | 77 | LCD_D7 = 0; |
JENG | 0:8fe7d36af056 | 78 | |
JENG | 0:8fe7d36af056 | 79 | wait_ms(50); |
JENG | 0:8fe7d36af056 | 80 | |
JENG | 0:8fe7d36af056 | 81 | while(x--){ |
JENG | 0:8fe7d36af056 | 82 | |
JENG | 0:8fe7d36af056 | 83 | write_lcd(0x30); |
JENG | 0:8fe7d36af056 | 84 | wait_ms(5); |
JENG | 0:8fe7d36af056 | 85 | |
JENG | 0:8fe7d36af056 | 86 | } |
JENG | 0:8fe7d36af056 | 87 | |
JENG | 0:8fe7d36af056 | 88 | write_lcd(0x20); //4bit interface |
JENG | 0:8fe7d36af056 | 89 | |
JENG | 0:8fe7d36af056 | 90 | lcd_putcmd(0x28); //function set |
JENG | 0:8fe7d36af056 | 91 | lcd_putcmd(0x08); //display off |
JENG | 0:8fe7d36af056 | 92 | lcd_putcmd(0x01); //display clear |
JENG | 0:8fe7d36af056 | 93 | lcd_putcmd(0x06); //entry mode set |
JENG | 0:8fe7d36af056 | 94 | lcd_putcmd(0x0c); //display on |
JENG | 0:8fe7d36af056 | 95 | |
JENG | 0:8fe7d36af056 | 96 | } |
JENG | 0:8fe7d36af056 | 97 | //######################################## |
JENG | 0:8fe7d36af056 | 98 | // End of LCD Library |
JENG | 0:8fe7d36af056 | 99 | //######################################## |
JENG | 0:8fe7d36af056 | 100 | |
JENG | 0:8fe7d36af056 | 101 | #define DHTLIB_OK 0 |
JENG | 0:8fe7d36af056 | 102 | #define DHTLIB_ERROR_CHECKSUM -1 |
JENG | 0:8fe7d36af056 | 103 | #define DHTLIB_ERROR_TIMEOUT -2 |
JENG | 0:8fe7d36af056 | 104 | |
JENG | 0:8fe7d36af056 | 105 | Timer tmr; |
JENG | 0:8fe7d36af056 | 106 | |
JENG | 0:8fe7d36af056 | 107 | DigitalInOut data_pin(A1); |
JENG | 0:8fe7d36af056 | 108 | |
JENG | 0:8fe7d36af056 | 109 | int humidity; |
JENG | 0:8fe7d36af056 | 110 | int temperature; |
JENG | 0:8fe7d36af056 | 111 | |
JENG | 0:8fe7d36af056 | 112 | //######################################## |
JENG | 0:8fe7d36af056 | 113 | // DHT11 Library |
JENG | 0:8fe7d36af056 | 114 | //######################################## |
JENG | 0:8fe7d36af056 | 115 | int dht_read(void){ |
JENG | 0:8fe7d36af056 | 116 | |
JENG | 0:8fe7d36af056 | 117 | // BUFFER TO RECEIVE |
JENG | 0:8fe7d36af056 | 118 | uint8_t bits[5]; |
JENG | 0:8fe7d36af056 | 119 | uint8_t cnt = 7; |
JENG | 0:8fe7d36af056 | 120 | uint8_t idx = 0; |
JENG | 0:8fe7d36af056 | 121 | |
JENG | 0:8fe7d36af056 | 122 | tmr.stop(); |
JENG | 0:8fe7d36af056 | 123 | tmr.reset(); |
JENG | 0:8fe7d36af056 | 124 | |
JENG | 0:8fe7d36af056 | 125 | // EMPTY BUFFER |
JENG | 0:8fe7d36af056 | 126 | for(int i=0; i< 5; i++) bits[i] = 0; |
JENG | 0:8fe7d36af056 | 127 | |
JENG | 0:8fe7d36af056 | 128 | // REQUEST SAMPLE |
JENG | 0:8fe7d36af056 | 129 | data_pin.output(); |
JENG | 0:8fe7d36af056 | 130 | data_pin.write(0); |
JENG | 0:8fe7d36af056 | 131 | wait_ms(18); |
JENG | 0:8fe7d36af056 | 132 | data_pin.write(1); |
JENG | 0:8fe7d36af056 | 133 | wait_us(40); |
JENG | 0:8fe7d36af056 | 134 | data_pin.input(); |
JENG | 0:8fe7d36af056 | 135 | |
JENG | 0:8fe7d36af056 | 136 | // ACKNOWLEDGE or TIMEOUT |
JENG | 0:8fe7d36af056 | 137 | unsigned int loopCnt = 10000; |
JENG | 0:8fe7d36af056 | 138 | |
JENG | 0:8fe7d36af056 | 139 | while(!data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT; |
JENG | 0:8fe7d36af056 | 140 | |
JENG | 0:8fe7d36af056 | 141 | loopCnt = 10000; |
JENG | 0:8fe7d36af056 | 142 | |
JENG | 0:8fe7d36af056 | 143 | while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT; |
JENG | 0:8fe7d36af056 | 144 | |
JENG | 0:8fe7d36af056 | 145 | // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT |
JENG | 0:8fe7d36af056 | 146 | for(int i=0; i<40; i++){ |
JENG | 0:8fe7d36af056 | 147 | |
JENG | 0:8fe7d36af056 | 148 | loopCnt = 10000; |
JENG | 0:8fe7d36af056 | 149 | |
JENG | 0:8fe7d36af056 | 150 | while(!data_pin.read())if(loopCnt-- == 0)return DHTLIB_ERROR_TIMEOUT; |
JENG | 0:8fe7d36af056 | 151 | |
JENG | 0:8fe7d36af056 | 152 | //unsigned long t = micros(); |
JENG | 0:8fe7d36af056 | 153 | tmr.start(); |
JENG | 0:8fe7d36af056 | 154 | |
JENG | 0:8fe7d36af056 | 155 | loopCnt = 10000; |
JENG | 0:8fe7d36af056 | 156 | |
JENG | 0:8fe7d36af056 | 157 | while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT; |
JENG | 0:8fe7d36af056 | 158 | |
JENG | 0:8fe7d36af056 | 159 | if(tmr.read_us() > 40) bits[idx] |= (1 << cnt); |
JENG | 0:8fe7d36af056 | 160 | |
JENG | 0:8fe7d36af056 | 161 | tmr.stop(); |
JENG | 0:8fe7d36af056 | 162 | tmr.reset(); |
JENG | 0:8fe7d36af056 | 163 | |
JENG | 0:8fe7d36af056 | 164 | if(cnt == 0){ // next byte? |
JENG | 0:8fe7d36af056 | 165 | |
JENG | 0:8fe7d36af056 | 166 | cnt = 7; // restart at MSB |
JENG | 0:8fe7d36af056 | 167 | idx++; // next byte! |
JENG | 0:8fe7d36af056 | 168 | |
JENG | 0:8fe7d36af056 | 169 | }else cnt--; |
JENG | 0:8fe7d36af056 | 170 | |
JENG | 0:8fe7d36af056 | 171 | } |
JENG | 0:8fe7d36af056 | 172 | |
JENG | 0:8fe7d36af056 | 173 | // WRITE TO RIGHT VARS |
JENG | 0:8fe7d36af056 | 174 | // as bits[1] and bits[3] are allways zero they are omitted in formulas. |
JENG | 0:8fe7d36af056 | 175 | humidity = bits[0]; |
JENG | 0:8fe7d36af056 | 176 | temperature = bits[2]; |
JENG | 0:8fe7d36af056 | 177 | |
JENG | 0:8fe7d36af056 | 178 | uint8_t sum = bits[0] + bits[2]; |
JENG | 0:8fe7d36af056 | 179 | |
JENG | 0:8fe7d36af056 | 180 | if(bits[4] != sum)return DHTLIB_ERROR_CHECKSUM; |
JENG | 0:8fe7d36af056 | 181 | |
JENG | 0:8fe7d36af056 | 182 | return DHTLIB_OK; |
JENG | 0:8fe7d36af056 | 183 | |
JENG | 0:8fe7d36af056 | 184 | } |
JENG | 0:8fe7d36af056 | 185 | |
JENG | 0:8fe7d36af056 | 186 | char buffer[17]; |
JENG | 0:8fe7d36af056 | 187 | |
JENG | 0:8fe7d36af056 | 188 | //######################################## |
JENG | 0:8fe7d36af056 | 189 | // End of DHT11 Library |
JENG | 0:8fe7d36af056 | 190 | //######################################## |
JENG | 0:8fe7d36af056 | 191 | |
JENG | 0:8fe7d36af056 | 192 | int main(void){ |
JENG | 0:8fe7d36af056 | 193 | |
JENG | 0:8fe7d36af056 | 194 | lcd_initial(); |
JENG | 0:8fe7d36af056 | 195 | |
JENG | 0:8fe7d36af056 | 196 | lcd_puts("=Nucleo - DHT11="); |
JENG | 0:8fe7d36af056 | 197 | |
JENG | 0:8fe7d36af056 | 198 | for(;;){ |
JENG | 0:8fe7d36af056 | 199 | |
JENG | 0:8fe7d36af056 | 200 | if(!dht_read()){ |
JENG | 0:8fe7d36af056 | 201 | |
JENG | 0:8fe7d36af056 | 202 | //sprintf(buffer, "Temp %2d C ", temperature); |
JENG | 0:8fe7d36af056 | 203 | //lcd_putcmd(0x80); |
JENG | 0:8fe7d36af056 | 204 | //lcd_puts(buffer); |
JENG | 0:8fe7d36af056 | 205 | sprintf(buffer, "Hum %2d%% Tmp %2dc", humidity, temperature); |
JENG | 0:8fe7d36af056 | 206 | lcd_putcmd(0xc0); |
JENG | 0:8fe7d36af056 | 207 | lcd_puts(buffer); |
JENG | 0:8fe7d36af056 | 208 | wait(0.5); |
JENG | 0:8fe7d36af056 | 209 | |
JENG | 0:8fe7d36af056 | 210 | }else{ |
JENG | 0:8fe7d36af056 | 211 | |
JENG | 0:8fe7d36af056 | 212 | lcd_putcmd(0x80); |
JENG | 0:8fe7d36af056 | 213 | lcd_puts("Sensor Error !!!"); |
JENG | 0:8fe7d36af056 | 214 | lcd_putcmd(0xc0); |
JENG | 0:8fe7d36af056 | 215 | lcd_puts(" "); |
JENG | 0:8fe7d36af056 | 216 | |
JENG | 0:8fe7d36af056 | 217 | } |
JENG | 0:8fe7d36af056 | 218 | |
JENG | 0:8fe7d36af056 | 219 | } |
JENG | 0:8fe7d36af056 | 220 | |
JENG | 0:8fe7d36af056 | 221 | } |