Hola a todos:
Estoy intentando programar un esp8266-01 con un DHT22 de 3 pines. El problema es que no consigo lecturas en el monitor, aparecen nan. Probablemente alguna conexión.
Estoy usando ARDUINO UNO de pasarela.
Adjunto esquema usado (está hecho con un DHT11, pero realmente es un DHT22) y código.
Gracias de antemano cualquier ayuda
#include <DHT.h>
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
unsigned long myChannelNumber = xxxxxx;
const char * myWriteAPIKey = "xxxxxxxx";
const char* ssid = "xxxxxxx";
const char* password = "xxxxxxxxxxx";
WiFiClient client;
WiFiServer server(80);
const int DHTPin = 2;
void setup() {
Serial.begin(115200);
Serial.println("DHTxx test!");
delay(10);
dht.begin(); //Se inicia el sensor
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");
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
//Se imprimen las variables
Serial.print("Humedad: ");
Serial.print(h);
Serial.println("Temperatura: ");
Serial.println(t);
delay(200);
ThingSpeak.writeField(myChannelNumber, 1, h, myWriteAPIKey);
delay(2000);
ThingSpeak.writeField(myChannelNumber, 2, t, myWriteAPIKey);
delay(20000);
}
Sereno
December 15, 2018, 7:50pm
2
Utiliza la librería especifica para dht en un ESP.
Sereno
December 15, 2018, 8:05pm
4
Lo siento, las prisas, que borde que soy........
La librería que yo utilizo para un ESP-01 y un DHT22 es esta:
DHTesp.h
No recuerdo que problema tuve, pero con esta me funciona bien.
Sereno
December 16, 2018, 12:06pm
5
He montado un esp01 con un DHT22 con tu código y con esa librería no me deja compilarlo.
Con la librería que te he comentado y con el código que te adjunto me funciona perfectamente.
Si te sigue sin funcionar verifica que la resistencia entre el positivo del DHT22 y el pin de datos no sea demasiado baja o demasiado alta, yo tengo una puesta de 10K.
No he utilizado nada como pasarela, si no un servidor web.
#include <DHTesp.h>
#include <ESP8266WiFi.h>
DHTesp dht;
const char* ssid = "FELIZ";
const char* password = "NAVIDAD";
IPAddress ip(192, 168, 1, 205);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(8, 8, 8, 8);
WiFiClient client;
WiFiServer server(80);
void setup()
{
dht.setup(2, DHTesp::DHT22);
WiFi.config(ip, gateway, subnet, dns);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
server.begin();
}
void loop()
{
float h = dht.getHumidity();
float t = dht.getTemperature();
WiFiClient client = server.available();
if (!client)
{
return;
}
while(!client.available())
{
delay(1);
}
String request = client.readStringUntil('\r');
client.flush();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("
");
client.println("<b>DHT22:");
client.print(t);
client.println(" °C");
client.print(h);
client.print(" %\t");
client.println("</html>");
delay(1);
return;
client.stop();
}
Surbyte
December 16, 2018, 3:15pm
6
Yo uso Adafruit con su línea de sensores/librerías y funcionan todas bien sean o no comprados en Adafruit y yo no los compro.
@sereno Muchas gracias!!!! Me ha dado muchas pistas y ya está funcionando...