DHT11

ESP8266 ESP-01 & DHT11 Sensor
Humidity (%)      : 44.00
Temperature (°C)  : 29.00
Temperature (°F)  : 84.20
tNow              : 4292


http://192.168.16.17/

A DHT Temperature and Humidity Sensor, $11 for 5 pieces (on 2017.07.03), soldered to an ESP8266 ESP-01, with code to connect to an access point, and on request, show the data shown above (with a 30 second refresh) in a browser. Pretty handy if you just want to provide 5V with a USB cable to the programmer (or 3.3V using wires). http://www.elec-cafe.com/ shows all the magic on how to do the temperature and humidity part.



// http://www.elec-cafe.com/temperature-sensor-on-the-web-with-esp8266-and-ds18b20/
// 07.03: reset:'!' and info:'?'
#include <ESP8266WiFi.h> #include <DHT11.h> #define VERSION "2017.07.03 ESP-01 DHT"
int counter;
unsigned int tNow;
unsigned int tPrevious;

WiFiServer server( 80 );
DHT11 dht11( PIN );
void setup( ) {
  Serial.begin( 115200 );
  delay( 10 );
  Serial.println( VERSION );

  WiFi.mode( WIFI_STA );
  Serial.println( );
  Serial.print( "Connecting: " );
  Serial.println( ssid );

  WiFi.begin( ssid, password );

  while ( WiFi.status( ) != WL_CONNECTED ) {
    delay( 500 );
    Serial.print( "." );
  } // while not connected

  Serial.println( "" );  Serial.println( "WiFi ready!" );  // Start the server  server.begin( );  Serial.println( "Server ready!" );  counter = 0;  tNow = millis( );  tPrevious = tNow; } // setup( )


void info( ) {  Serial.println( );  Serial.println( VERSION );  Serial.print( "SSID: " );  Serial.println( ssid );  Serial.print( "IP Address: " );  Serial.println( WiFi.localIP( ) ); } // info


void
loop( ) {  tNow = millis( );
 if ( tNow - tPrevious > DELAYTIME ) {    tPrevious = tPrevious + DELAYTIME;
    // Serial.println( "all the needed info" );
  } // if time to print on Serial port

 WiFiClient client = server.available( );  client.println( "HTTP/1.1 200 OK" );  client.println( "Content-Type: text/html" );  client.println( "Connection: close" );  // the connection will be closed after completion of the response  client.println( "Refresh: 30" );        // refresh the page automatically every 30 sec  client.println( );  client.println( "<!DOCTYPE html>" );  client.println( "<html xmlns='http://www.w3.org/1999/xhtml'>" );  client.println( "<head>\n<meta charset='UTF-8'>" );  client.println( "<title>ESP8266 Temperature & Humidity</title>" );  client.println( "</head>\n<body>" );  client.println( VERSION );
  // client.println all your information here...
  client.print( "</body>\n</html>" );
  delay( DHT11_RETRY_DELAY ); // delay for reread

  if ( Serial.available( ) > 0 ) {
    int inByte = Serial.read( );
    if ( inByte == '!' ) ESP.reset( );   // reset
    if ( inByte == '?' ) info( );
  } // if Serial.available( )
} // loop( )
Comments