#if defined(ESP32)
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define DEVICE "ESP32"
#elif defined(ESP8266)
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#define DEVICE "ESP8266"
#endif
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
#include "DHT.h"
// WiFi AP SSID
#define WIFI_SSID "Wokwi-GUEST"
// WiFi password
#define WIFI_PASSWORD ""
#define DHTPIN 14 // Pin digital conectado al sensor DHT22
#define DHTTYPE DHT22 // Tipo de sensor DHT
DHT dht(DHTPIN, DHTTYPE);
#define INFLUXDB_URL "https://us-east-1-1.aws.cloud2.influxdata.com"
#define INFLUXDB_TOKEN "WdhjT5maaVcxPRk93tVqlabcj_UOYzw9n48nWZokoUOlnWT4j0nigDFXoK1yes1avxbd-doA5llN0w9izfIZWw=="
#define INFLUXDB_ORG "0925ccf91ab36478"
#define INFLUXDB_BUCKET "sesion09"
// Time zone info
#define TZ_INFO "UTC-5"
// Declare InfluxDB client instance with preconfigured InfluxCloud certificate
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
// Declare Data point
Point sensor("wifi_status");
void setup() {
Serial.begin(115200);
dht.begin();
Serial.print("DHT22 initialized!");
delay(100);
Serial.println();
// Setup wifi
WiFi.mode(WIFI_STA);
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to wifi");
while (wifiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
// Accurate time is necessary for certificate validation and writing in batches
// We use the NTP servers in your area as provided by: https://www.pool.ntp.org/zone/
// Syncing progress and the time will be printed to Serial.
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
// Check server connection
if (client.validateConnection()) {
Serial.print("Connected to InfluxDB: ");
Serial.println(client.getServerUrl());
} else {
Serial.print("InfluxDB connection failed: ");
Serial.println(client.getLastErrorMessage());
}
// Add tags to the data point
sensor.addTag("device", DEVICE);
sensor.addTag("SSID", WiFi.SSID());
}
void loop() {
// Verificar la conexión WiFi y reconectar si es necesario
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("Conexión WiFi perdida");
return; // Salir de la función loop si no hay conexión WiFi
}
// Leer la humedad y la temperatura del sensor DHT22
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // Por defecto, devuelve la temperatura en grados Celsius
// Verificar si las lecturas son válidas
/*if (isnan(humidity) || isnan(temperature)) {
Serial.println("Error al leer del sensor DHT!");
return;
}*/
// Clear fields for reusing the point. Tags will remain the same as set above.
sensor.clearFields();
// Store measured value into point
// Report RSSI of currently connected network
sensor.addField("rssi", WiFi.RSSI());
// Añadir nuevas lecturas como campos al punto de datos
sensor.addField("temperature", temperature);
sensor.addField("humidity", humidity);
// Print what are we exactly writing
Serial.print("Writing: ");
Serial.println(sensor.toLineProtocol());
// Check WiFi connection and reconnect if needed
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("Wifi connection lost");
}
// Write point
if (!client.writePoint(sensor)) {
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
}
Serial.println("Waiting 1 second");
delay(1000);
}
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1