IONOTS

Reading Temperature and Humidity From NodeMCU in LAN

In this project, you will learn how to Measure temperature, Humidity with DHT11 sensor on NodeMCU and Reading this data on any PC within a LAN.

Hi! In this project let us see how to configure NodeMCUto read Temperature and Humidity from a DHT11 sensor and forward it to a user or client on a request

Requirements:

Arduino IDE with an esp8266 board community.
Pc with putty software installed.
NodeMCU.
DHT11 sensor
connecting wires/cables

Construction:

Process:

Here we connect the DHT11 sensor to NodeMCU. This NodeMCU is connected to our wifi network and starts its server listening on port 80.   When a client is connected to the network and sends some data, immediately the data from the digital sensor DHT11  is taken and if the command or request from a client is TEMP/HUMI/GET then it sends the data to the client.

CODE:

 

#include <ESP8266WiFi.h> 
#include "DHT.h" // including the library of DHT11 temperature and humidity sensor
#define DHTTYPE DHT11 // DHT 11

#define dht_dpin D3
DHT dht(dht_dpin, DHTTYPE);

const char* ssid = "Your WiFI network name ";
const char* password = "ypur WiFi network password"; 
int ledPin = 13; // GPIO13
WiFiServer server(80); 
void setup() {
dht.begin();
Serial.begin(115200); 
delay(10); 
// Connect to WiFi network
Serial.println();
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");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print(WiFi.localIP()); 
} 
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
//while connected keep listining to packets 
if (client) { 
// Wait until the client sends some data
Serial.println("new client");
while (client.connected()) {
while(!client.available()){
delay(1);
} 
float h = dht.readHumidity();
float t = dht.readTemperature();
// Read the first line of the request
String request = client.readStringUntil('#');
Serial.println(request);
client.flush(); 
// Match the request

if (request.indexOf("TEMP") >= 0) 
{
client.print("temperature is - ");
client.println(t);
}
else if (request.indexOf("HUMI") >= 0) // indenOf returns -1 if there is no such word in request string
{
client.print("humidity is - ");
client.println(h);
}
else if (request.indexOf("GET") >= 0)
{
client.print("temperature/humidity is");
client.print(t)
client.print('/')
client.println(h)
}
}
}
}

 

–ABHIJITH

Share this post

Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on whatsapp