Skip to content

Instantly share code, notes, and snippets.

@Ajak58a
Created January 15, 2024 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ajak58a/854ef7692c3c9cac68478686f9fd8a06 to your computer and use it in GitHub Desktop.
Save Ajak58a/854ef7692c3c9cac68478686f9fd8a06 to your computer and use it in GitHub Desktop.
#include "DHT.h"
#include <ESP8266WiFi.h>
#define DHTPIN 5 // esp8266 D2 pin map as 4 in Arduino IDE
#define DHTTYPE DHT11 // there are multiple kinds of DHT sensors
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_NETWORK_PASSWORD";
WiFiServer server(80);
int m;
float mp;
float h;
float t;
void setup() {
Serial.begin(9600);
Serial.setTimeout(2000);
// Connecting to wifi.
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected with WiFi.");
// Starting the Web Server
server.begin();
Serial.println("Web server started.");
// This is the IP To Connect
Serial.print("This is the IP to connect: ");
Serial.print("http://");
Serial.println(WiFi.localIP());
// Wait for serial to initialize.
while(!Serial) { }
dht.begin();
Serial.println("Device Started");
Serial.println("-------------------------------------");
Serial.println("Running DHT!");
Serial.println("-------------------------------------");
}
void loop() {
int m = analogRead(A0);
mp = ( 100 - ( (m/1024.00) * 100 ) );
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
WiFiClient client = server.available();
if (!client) {
delay(100);
return;
}
Serial.print("New client: ");
Serial.println(client.remoteIP());
// Wait until the client sends data.
while(!client.available()){ delay(1); }
// Read the information sent by the client.
String req = client.readStringUntil('\r');
Serial.println(req);
// Make the client's request.
if (req.indexOf("read") != -1){
delay(10);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // Importante.
client.print("Humidity: ");
client.print(h);
client.println(" %");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
client.print("Temperature: ");
client.print(t);
client.println(" C");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" C");
client.print("Moisture: ");
client.print(mp);
client.println(" %");
Serial.print("Mositure: ");
Serial.print(mp);
Serial.print(" %\t");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment