// Including the ESP8266 WiFi library
#include <ESP8266WiFi.h>
#include <DHT.h>
// karena memakai DHT 11 jadi yg lain di comment saja, kalau pakai DHT22 tinggal
uncoment yg DHT22 dan comment DHT11
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// Silahkan ganti ssid dan pasword sesuai wifi yang dipakai (bukan ssid dan pass
esp8266nya)
const char* ssid = "Mwr22";
const char* password = "Mawar2024";
// Web Server on port 80
WiFiServer server(80);
//PIN DHT Sensor yang dipakai
const int DHTPin = 2;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
// 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
[Link](115200);
delay(10);
[Link]();
// Connecting to WiFi network
[Link]();
[Link]("Connecting to ");
[Link](ssid);
[Link](ssid, password);
while ([Link]() != WL_CONNECTED) {
delay(500);
[Link](".");
}
[Link]("");
[Link]("WiFi connected");
// Starting the web server
[Link]();
[Link]("Web server running. Waiting for the ESP IP...");
delay(10000);
// Printing the ESP IP address
[Link]([Link]());
}
// runs over and over again
void loop() {
// Listenning for new clients
WiFiClient client = [Link]();
if (client) {
[Link]("New client");
// bolean to locate when the http request ends
boolean blank_line = true;
while ([Link]()) {
if ([Link]()) {
char c = [Link]();
if (c == '\n' && blank_line) {
// Sensor readings may also be up to 2 seconds 'old' (its a very slow
sensor)
float h = [Link]();
// Read temperature as Celsius (the default)
float t = [Link]();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = [Link](true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
[Link]("Failed to read from DHT sensor!");
strcpy(celsiusTemp,"Failed");
strcpy(fahrenheitTemp, "Failed");
strcpy(humidityTemp, "Failed");
}
else{
// Computes temperature values in Celsius + Fahrenheit and Humidity
float hic = [Link](t, h, false);
dtostrf(hic, 6, 2, celsiusTemp);
float hif = [Link](f, h);
dtostrf(hif, 6, 2, fahrenheitTemp);
dtostrf(h, 6, 2, humidityTemp);
// You can delete the following [Link]'s, it's just for
debugging purposes
[Link]("Humidity: ");
[Link](h);
[Link](" %\t Temperature: ");
[Link](t);
[Link](" *C ");
[Link](f);
[Link](" *F\t Heat index: ");
[Link](hic);
[Link](" *C ");
[Link](hif);
[Link](" *F");
[Link]("Humidity: ");
[Link](h);
[Link](" %\t Temperature: ");
[Link](t);
[Link](" *C ");
[Link](f);
[Link](" *F\t Heat index: ");
[Link](hic);
[Link](" *C ");
[Link](hif);
[Link](" *F");
}
[Link]("HTTP/1.1 200 OK");
[Link]("Content-Type: text/html");
[Link]("Connection: close");
[Link]();
// your actual web page that displays temperature and humidity
[Link]("<!DOCTYPE HTML>");
[Link]("<html>");
[Link]("<head></head><body><h1>ESP8266 - Temperature and
Humidity</h1><h3>Temperature in Celsius: ");
[Link](celsiusTemp);
[Link]("*C</h3><h3>Temperature in Fahrenheit: ");
[Link](fahrenheitTemp);
[Link]("*F</h3><h3>Humidity: ");
[Link](humidityTemp);
[Link]("%</h3><h3>");
[Link]("</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);
[Link]();
[Link]("Client disconnected.");
}
}