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

arduino_sensor_V2_ Bloc de notas

This document is an Arduino sketch for an ESP32 that reads temperature and humidity data from a DHT22 sensor. It connects to a specified WiFi network and sends the sensor data to a server via HTTP GET requests every three minutes. The code includes error handling for sensor readings and HTTP requests.

Uploaded by

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

arduino_sensor_V2_ Bloc de notas

This document is an Arduino sketch for an ESP32 that reads temperature and humidity data from a DHT22 sensor. It connects to a specified WiFi network and sends the sensor data to a server via HTTP GET requests every three minutes. The code includes error handling for sensor readings and HTTP requests.

Uploaded by

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

#include <DHT.

h>
#include <DHT_U.h>
#include <WiFi.h>
#include <HTTPClient.h>

#define DHTTYPE DHT22 //Definimos el modelo del sensor DHT22


#define const int DHTPIN 4 // Se define el pin D4 del ESP32 para conectar

const char WIFI_SSID[] = "Ceypabasa";


const char WIFI_PASSWORD[] = "C3yp4b4s4W";
String HOST_NAME = "http://192.168.1.123"; // change to your PC's IP address
String PATH_NAME = "/add.php";
String queryString;

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
Serial.begin(9600);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("Conectando");
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.print("Conectado a la red WiFi con direccion IP: ");
Serial.println(WiFi.localIP());

dht.begin();
}

void loop()
{
delay (2000);

float h = dht.readHumidity();
float t = dht.readTemperature();

if (isnan(h) || isnan(t))
{
Serial.println("No se pudo leer el sensor DHT!");
return;
}

//Cambiar el id del sensor


queryString = "?id=3&tem=" +String(t) + "&hum=" + String(h);

//Se imprimen las variables


Serial.print("Send http: ");
Serial.println(HOST_NAME + PATH_NAME + queryString);
HTTPClient http;
http.begin(HOST_NAME + PATH_NAME + queryString); //HTTP
int httpCode = http.GET();

// httpCode will be negative on error


if(httpCode > 0) {
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
} else

{
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n",
http.errorToString(httpCode).c_str());
}
http.end();
delay(180000); // WAIT Thre MINUTES BEFORE SENDING AGAIN
}

You might also like