0% found this document useful (0 votes)
3 views2 pages

dht sensor

This document contains an Arduino sketch for an ESP8266 microcontroller that reads temperature and humidity data from a DHT11 sensor. It connects to a specified Wi-Fi network and sends the sensor data to a server via an HTTP GET request. Additionally, it controls an LED based on the server's response to the data sent.

Uploaded by

rocketraccoon123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

dht sensor

This document contains an Arduino sketch for an ESP8266 microcontroller that reads temperature and humidity data from a DHT11 sensor. It connects to a specified Wi-Fi network and sends the sensor data to a server via an HTTP GET request. Additionally, it controls an LED based on the server's response to the data sent.

Uploaded by

rocketraccoon123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <ESP8266WiFi.

h>
#include <ESP8266HTTPClient.h>
#include "DHT.h"

#define DHTPIN D3
#define DHTTYPE DHT11
// Replace with your network credentials
const char* ssid = "IITH-Guest-PWD-IITH@2024";
const char* password = "IITH@2024";
int ir= D1;
int led = D0;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println(F("DHTxx test!"));

dht.begin();
pinMode(ir,INPUT);
pinMode(led, OUTPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
delay(2000);
int value = digitalRead(ir);
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("temp and humid--");
Serial.print(h);
Serial.print("---");
Serial.println(t);
if (WiFi.status() == WL_CONNECTED) {
// Create a WiFi client object
WiFiClient wifiClient;
// Make HTTP GET request
HTTPClient http;
const char* baseUrl ="http://172.16.66.40:3000/setTempHumidity?temp=";
String url = String(baseUrl) + t ;
url = url + "&humidity="+ h;

http.begin(wifiClient, url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.print("HTTP response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println("Response payload:");
Serial.println(payload);
if(payload == "0"){
digitalWrite(led,HIGH);
}
if(payload == "1"){
digitalWrite(led,LOW);
}
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
Serial.print("Error message: ");
Serial.println(http.errorToString(httpResponseCode).c_str());
}
http.end(); // Close HTTP connection
}
delay(5000);
}

You might also like