Go Down

Topic: Dht22 on gpio2 of an Esp8266  (Read 497 times) previous topic - next topic

Marciokoko

Is it possible to use a Dht22 on gpio2 of an Esp8266?  I'm getting -3276.70 for temp and nan for humidity:

Code: [Select]

include <ESP8266WiFi.h>
#include "DHT.h"
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

const char* ssid     = "myssid";
const char* password = "mypwd";
const char* host = "myserver.com";
unsigned long previousMillis = 0;        // will store last time was updated
const long interval = 180000;
char tempString[20];
char humString[20];

void setup() {
  Serial.begin(115200);
  delay(100);
 
  // We start by connecting to a 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"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
 
int value = 0;

void start_test () {
  //For DHT22 Grove Pro
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  dtostrf(t, 4, 2, tempString);  //convert flat to char 
  dtostrf(h, 4, 2, humString);  //convert flat to char
}

void loop() {
  ///////////INSERTED millis-sample code
    unsigned long currentMillis = millis();
  if (currentMillis - previousMillis <= interval) { //when time elapsed is LESS than interval of 30 mins   
  } else { //if deltaTimeElapsed is GREATER than target interval, sample and post...
    previousMillis = currentMillis; //sets previous-0 to current-3600
    start_test(); //sample
  }
  delay(5000);
  ++value;
 
  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  // We now create a URI for the request
 String url = String("/emoncms/input/post.json?apikey=mykey&node=fortnite&json={\"t\":") + tempString + ",\"h\":" + humString + "}";
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(500);
 
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
}

Juraj

Is it possible to use a Dht22 on gpio2 of an Esp8266?  I'm getting -3276.70 for temp and nan for humidity:
esp8266 dev boards have pull-up on io 2 because it is one of the boot configuration pins. it could interfere the communication with DHT if this pull-up is strong
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

GolamMostafa

I tested my dht11 at GPIO2(D2)-pin of NodeMCU 1.0 (ESP-12E) Module. The sketch is uploaded and the sensor is working.

wvmarle

GPIO2 works fine for that sensor. Been there done that. The internal pull-up is normally used for boot (it is enabled by default during boot), and it's actually weaker than the pull-up required for the DHT22, so you still need an external 10k resistor to make it work reliably.

.
I tested my dht11 at GPIO2(D2)-pin of NodeMCU 1.0 (ESP-12E) Module. The sketch is uploaded and the sensor is working.
D2 on NodeMCU is GPIO4

GPIO2 is D4.

There's no obvious relationship between the D numbers and the GPIO numbers on NodeMCU.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

GolamMostafa

#4
Oct 20, 2018, 05:33 pm Last Edit: Oct 20, 2018, 05:34 pm by GolamMostafa
D2 on NodeMCU is GPIO4

GPIO2 is D4.
You are correct; sorry, for the typo mistakes.

Go Up