-->
Page 1 of 2

ESP8266-12 and four DHT22's. Which GPIO pins are ok to use?

PostPosted: Wed Jul 22, 2015 7:39 am
by jetblackstar
Ok, So I've a Data logging project where the ESP8266-12 is quite simply sitting there squawking Temperatures and humidity for four DHT22 sensors back to my internal webserver, every ten minutes. Written using the Arduino IDE.

I had used GPIO 13,12,14 and 16 as GPIO. (As labelled on the ESP8266 itself)
I'd set them all to INPUT, initialised the DHT library using the .begin() function.

I had two problems.
Firstly the sensor on GPIO 13 randomly begins giving me nan values for a while and then comes back and works again on its own (i.e. without powercycling).

Secondly, GPIO14 used to work when I first began testing, and now it doesn't. After trying a pullup 10k resistor, pulldown, using internal pullup and down, tweaking the second DHT parameter up and down (is it a clock speed setting?) no luck. Checked the wires continuity, that it was getting 3.3v. Even replaced the sensor with a fresh one.

Eventually tried it on pin2 (GPIO2) and it starts working.
Is 14 just not usable for this kind of data/sensor?

I know most of the GPIO pins have alternate special uses for all sorts, but I thought/hoped most of them could be reperposed to BASIC GPIO, with the exception of 0 and 15.
Is the dropout on GPIO13 likely related to the same kind of issue?

Many thanks for your help, this community rocks!

Arduino sketch code below for reference:
Code: Select all// Datalogging ESP8266 -12 sketch

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

#define DHTPIN1 13    // Sensor 1
#define DHTPIN2 12    // Sensor 2
#define DHTPIN3 2     // Sensor 3
#define DHTPIN4 16    // Sensor 4

// Type of DHT sensor
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
//
// DAT: 15 needed as were running on ESP8266 and 3.3v auto seems to screw up
DHT dht1(DHTPIN1, DHTTYPE, 15);
DHT dht2(DHTPIN2, DHTTYPE, 15);
DHT dht3(DHTPIN3, DHTTYPE, 15);
DHT dht4(DHTPIN4, DHTTYPE, 15);

const char* ssid     = "Attic";
const char* password = "wanderonintotexel";

const char* host = "10.0.0.30";
const char* port = "1010";


void setup() {
  // Init Serial for when we are bench testing.
  Serial.begin(115200);
  Serial.println("Hay temperatures!");

  // Set all required pins to input just in case
  pinMode(DHTPIN1, INPUT_PULLUP);
  pinMode(DHTPIN2, INPUT_PULLUP);
  pinMode(DHTPIN3, INPUT_PULLUP);
  pinMode(DHTPIN4, INPUT_PULLUP);
 
  connectToWifi();

  // Initialise all dht sensors
  dht1.begin();
  dht2.begin();
  dht3.begin();
  dht4.begin();
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
 
  // Read temperature as Celsius (the default)
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  delay(200);
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
  delay(200);
  float h3 = dht3.readHumidity();
  float t3 = dht3.readTemperature();
  delay(200);
  float h4 = dht4.readHumidity();
  float t4 = dht4.readTemperature();

  // Check if any reads failed and exit early (to try again).
  // DAT: Removed as we want to transmit "nan" if we have a problem,
  // let the webserver sort it out
  //  if (isnan(h1) || isnan(t1) ) {
  //    Serial.println("Failed to read from DHT sensor!");
  //    return;
  //  }

  // Debug temps to serial
  printTemp(h1, t1);
  printTemp(h2, t2);
  printTemp(h3, t3);
  printTemp(h4, t4);

  Serial.println(WiFi.localIP());
  Serial.println("------");

  sendToServer(t1, h1, t2, h2, t3, h3, t4, h4);

  // Wait ten minutes between measurements.
  delay(600000);
}

void printTemp(float h, float t) {
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.println("");
}

void connectToWifi() {
  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());
}

void sendToServer(
  float t1, float h1,
  float t2, float h2,
  float t3, float h3,
  float t4, float h4) {
    Serial.print("connecting to ");
    Serial.println(host);
   
    // Use WiFiClient class to create TCP connections
    WiFiClient client;
    const int httpPort = 1010;
    if (!client.connect(host, httpPort)) {
      Serial.println("connection failed");
      return;
    }
   
    // We now create a URI for the request
    String url = "/gpio/hay";
    url += "?t1=";
    url += t1;
    url += "&h1=";
    url += h1;

    url += "&t2=";
    url += t2;
    url += "&h2=";
    url += h2;

    url += "&t3=";
    url += t3;
    url += "&h3=";
    url += h3;

    url += "&t4=";
    url += t4;
    url += "&h4=";
    url += h4;
   
   
    Serial.print("Requesting URL: ");
    Serial.println(url);
   
    // This will send the request to the server
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n\r\n");
    delay(10);
   
    // Read all the lines of the reply from server and print them to Serial
    while(client.available()){
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }
   
    Serial.println();
    Serial.println("closing connection");
}

Re: ESP8266-12 and four DHT22's. Which GPIO pins are ok to u

PostPosted: Wed Jul 22, 2015 5:30 pm
by tytower
I have just one DHT22 connected to GPIO2 . It does the same thing ,starts sending nan and no temp. I have always assumed it was stray capacitance as I do not have it soldered in , rather its on a separate small breadboard joined by cheap breadboard wires. I give it a wiggle, it gets going again immediately and this is regularly over the last two months . I am putting another together and will solder them to the ESP so I'll know then . Waiting on another DHT22.

So what I suspect is the processor is working at 80Mhz - any capacitance on the junctions of wires causes interference in the signals at that frequency so are yours soldered or just jumper wires?
Most set our read speed at 15 , or I do too . Maybe try altering that if you want to play and see if it changes in any way . I sprayed my breadboards with Inox as they are outside in the weather too. That seemed to help

Re: ESP8266-12 and four DHT22's. Which GPIO pins are ok to u

PostPosted: Thu Jul 23, 2015 6:09 pm
by jetblackstar
Initially they were plugged into sockets. But I had far more dropouts which I'd attributed to loose or bad connection. These sensors are being shoved into a stack of hay for monitoring heat and humidity buildup. Easy to knock one out of place.
I soldered them all in place and reinforced the join with hot glue. Became much more reliable but left me with the problems I've described.

They are on quite a long cable, 5-6 metres at its longest, with each sensor equally placed apart. (Cat 5e using two cores for common voltage in parralell and six remaining for sensors, of which I'm using four)

But it's not the furthest away that had trouble, it's the second closest. And now I've seen that very occasionally the the third closest is giving nan once, then carrying on.
Is there any way to alter the circuit to clear down the potential capacitance you mentioned?

Perhaps I need to look for more professional sensors. Had hoped the dht22 vs dht11 was enough of a step up, any recommendations?

Re: ESP8266-12 and four DHT22's. Which GPIO pins are ok to u

PostPosted: Thu Jul 23, 2015 8:00 pm
by m33600
Excellent project of yours! I will try it on my board.
To deal with DHT's you ll have to be very very permissive. In you case you have plenty 10 minutes to acquire the correct data. Why not dive into the " loose connection" principle, i,e, make it in a way it doesn t have to be reliable. Make slow readings, many times, retry, retry, filter "nan" internally in the software, until getting all data... the next 10 minute event will eventually be completely populated with healthy data!