2.4.17

ESP 8266 DHT11 und Webserver

// SimpleDHT als Library einbinden
// Ggfs. vorher von der URL holen
// http://arduino.esp8266.com/stable/package_esp8266com_index.json
// DHT11
//GND-GND
//VCC - 3.3 V
//Data - Pin/GPIO 2 - D4

#include <SimpleDHT.h>

// Including the ESP8266 WiFi library
#include <ESP8266WiFi.h>

// Replace with your network details
const char* ssid = "SSID";
const char* password = "PASSWORD";

// Web Server on port 80
WiFiServer server(80);

// DHT Sensor
int pinDHT11 = 2;
// Initialize DHT sensor.
SimpleDHT11 dht11;

// Temporary variables
static char celsiusTemp[7];
static char fahrenheitTemp[7];
static char humidityTemp[7];

// only runs once on boot
void setup() {
  // Initializing serial port for debugging purposes
  Serial.begin(115200);
  delay(10);
 
  // Connecting to WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Starting the web server
  server.begin();
  Serial.println("Web server running. Waiting for the ESP IP...");
  delay(10000);
 
  // Printing the ESP IP address
  Serial.println(WiFi.localIP());
}

// runs over and over again
void loop()
{
  // Listenning for new clients
  WiFiClient client = server.available();
    // read without samples.
  byte temperature = 0;
  byte humidity = 0;
    if (client)
  {
    if (dht11.read(pinDHT11, &temperature, &humidity, NULL))
    {
      Serial.print("Read DHT11 failed.");
      return;
    }
    String remote_ip = client.remoteIP().toString(); // IP Adresse des Client
    
    Serial.println("Neuer Zugriff von : " + remote_ip);
    // bolean to locate when the http request ends
    boolean blank_line = true;
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
                if (c == '\n' && blank_line)
        {
          // Computes temperature values in Celsius + Fahrenheit and Humidity
          // You can delete the following Serial.print's, it's just for debugging purposes
          Serial.print((int)temperature); Serial.print(" *C, ");
          Serial.print((int)humidity); Serial.println(" %");
        }
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println("Connection: close");
        client.println();
        // your actual web page that displays temperature and humidity
        client.println("<!DOCTYPE HTML>");
        client.println("<html>");
        client.println("<head></head><body><h1>ESP8266 - Temperature and Humidity</h1><h3>Temperature in Celsius: ");
        client.println((int)temperature);
        client.println("*F</h3><h3>Humidity: ");
        client.println((int)humidity);
        client.println("%</h3><h3>");
        client.println("<hr>" + remote_ip + "<br>");
        client.println("</body></html>");    
        break;
       
        if (c == '\n')
        {
          // when starts reading a new line
          blank_line = true;
        }
        else if (c != '\r')
        {
          // when finds a character on the current line
          blank_line = false;
        }
      }
    } 
    // closing the client connection
    delay(1);
    client.stop();
    Serial.println("Client disconnected.");
    
  }  
  }

Keine Kommentare:

Kommentar veröffentlichen

Openhab und Ecoflow Max - API Anbindung

 Ich wollte die neu erworbene Powerstation in Openhab einbinden, um den aktuellen Status (Ladestand etc.) über Openhab auswerten zu können. ...