Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By hiddenbit
#40867 Hey guys, I already read this forum for a long time, but just registered in cause of not getting my project working.

I have an ESP-201 attached to an external power supply and directly connect with a DHT22 on PIN 2. My software is very simple: (1) connecting the WLAN (2) read the DHT22 values (3) post them to my server (4) go to deep sleep (1) .. (2) ..

My project starts, connects the WLAN and reads the current values. After that it goes to deep sleep for a few seconds and restarts successful and establishs the WLAN connection, but the sensor values are NAN. If I restart the ESP8266 "hard", the first values reading from the sensor are valid again, but fails the second time after deep sleep. The procedure is repeatable.
:roll:
Have you guys any ideas about that?

Code: Select all#include <ESP8266WiFi.h>
extern "C" {
  #include "user_interface.h"
}
#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
//define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE, 15);

const char* ssid     = "WLAN";
const char* password = "1234567890xyz";


const char* host = "192.168.180.12";
const char* thingspeak_key = "API_KEY";

void turnOff(int pin) {
  pinMode(pin, OUTPUT);
  digitalWrite(pin, 1);
}

void setup() {
  Serial.begin(115200);

  // disable all output to save power
  turnOff(0);
  turnOff(2);
  turnOff(4);
  turnOff(5);
  turnOff(12);
  turnOff(13);
  turnOff(14);
  turnOff(15);

  dht.begin();
  delay(10);
 

  // 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 loop() {
  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;
  }

  String temp = String(dht.readTemperature());
  String humidity = String(dht.readHumidity());
  String url = "/update?key=";
  url += thingspeak_key;
  url += "&field1=";
  url += temp;
  url += "&field2=";
  url += humidity;
 
  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. going to sleep...");
  delay(1000);
  // go to deepsleep for few minutes
  system_deep_sleep_set_option(0);
  system_deep_sleep(0.5 * 60 * 1000000);
}
User avatar
By chaeplin
#41225 Could you try this lib ?
https://github.com/chaeplin/PietteTech_DHT-8266

https://tzapu.com/minimalist-battery-po ... re-logger/
also check this : https://github.com/tzapu/DeepSleepDHT22 ... pDHT22.ino

Since you are using deepsleep, following is minimal.
```
#include "PietteTech_DHT.h"

// system defines
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN 2 // Digital pin for communications
#define DHT_SAMPLE_INTERVAL 2000

//
float t, h;

//declaration
void dht_wrapper(); // must be declared before the lib initialization

// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);

// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper() {
DHT.isrCallback();
}

void setup()
{
acquireresult = DHT.acquireAndWait(1000);
if ( acquireresult == 0 ) {
t = DHT.getCelsius();
h = DHT.getHumidity();
} else {
t = h = 0;
}
}
```