Need help with Temperature and Humidity Sensor

I a working on a project for a class I'm taking and I am supposed to use a guide online to make a temperature and Humidity sensor. After buying all of the right parts and putting it together and running the program, nothing happens. I've tried to email the creator of the guide, but I didn't get a response. I've made sure that everything is connected right, and I'm not sure what else to do. I think there must be a problem with the code, so I attached it below. Could someone please read through it and see if anything is messed up. I'm not very familiar with all of this.

I'm also using a DHT11 temperature and humidity sensor and a Diymall .96in i2c 128x64 oled

Thanks,
Cls413

Call_Thingspeak.ino (859 Bytes)

Humidor.ino (2.9 KB)

oled_print.ino (586 Bytes)

read_dht_sensor.ino (585 Bytes)

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Hi,

Can you tell us your electronics, programming, Arduino, hardware experience?

Have you looked in the examples of the Arduino IDE, in the DHT11 examples.
That is if you have loaded the DHT11 library.

Your "read-dht 'sensor code is for the DHT22 and shows no library.

Just concentrate on the DHT11 for a start and use the example in the IDE.
Forget the rest until you get this part working.

Then do another code for another part etc etc.

Thanks. Tom.. :slight_smile:

Sorry about the code being in the wrong format. I also meant to say DHT22 instead of DHT11.
Here is the first page of code:

// Huzzah esp8266 = adafruit huzzah esp8266
// espcomm upload failed error = disconnect wire to rst pin

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

// oled display
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET LED_BUILTIN //4
Adafruit_SSD1306 display(OLED_RESET);

const char* ssid = "YOUR-SSID";
const char* password = "YOUR-PASSWORD";

// ThinkSpeak
const char* host = "api.thingspeak.com";
const char* THINGSPEAK_API_KEY = "DISAW554WKSXT0ZU";  // Get this from your Channel tab: API Keys.

// DHT Settings
#define DHTPIN 2
#define DHTTYPE DHT22
const boolean IS_METRIC = false;
const boolean call_thinkSpeak_api = true;

// Time to sleep (in seconds):
const int sleepTimeS = 3600; // hourly

// OLED Display Settings
const int I2C_DISPLAY_ADDRESS = 0x3c;
const int SDA_PIN = 4; // yellow is d1
const int SDC_PIN = 5; // green is d2

// Initialize the temperature/ humidity sensor
DHT dht(DHTPIN, DHTTYPE);

// pins
const int LED = 14;

// ———————————————————— //
void setup() {
Serial.begin(9600);
delay(10);

Serial.println("—->>>>>>> setup loop started <<<<<<<——");

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

}

// ———————————————————— //
void loop() {

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;
}

// wait a bit for sensor to pull room values
delay(3000);
int humidity = 0;
int temperature = 0;
read_dht_sensor(humidity, temperature);
Serial.println(humidity);
Serial.println(temperature);

// OLED display info
displayHumidorInfo(humidity, temperature);

call_thingspeak(temperature, humidity, client);

pinMode(LED, OUTPUT);
// Light up Led for 3 seconds
digitalWrite(LED, HIGH);
delay(30000);

// Clear the buffer.
display.clearDisplay();
display.display();

// Sleep
Serial.println("ESP8266 in sleep mode");
ESP.deepSleep(sleepTimeS * 1000000);

// THIS WILL NOT BE EXECUTED – WAKE WILL GO STRAIGHT TO SETUP //
Serial.println("ESP8266 in sleep mode — done —");

}

// ———————————————————— //
void displayHumidorInfo(float humidity, float temperature) {
Serial.println("—– oled displayHumidorInfo —–");

String msg1 = "MY HUMIDOR";
String msg2 = "HUMIDITY: " + String( (int) humidity ) + " %";
String msg3 = "TEMPERATURE: " + String((int) temperature) + " " + (char)247 + "F" ;
String msg4 = "";

printDisplay( msg1, msg2, msg3, msg4);
return;

}

This is the second page:

void call_thingspeak(int temperature, int humidity, WiFiClient client) {

if(temperature > 1000){
Serial.println("temperature error");
Serial.println(temperature);
return;
}

// We now create a URI for the request
String url = "/update?api_key=";
url += THINGSPEAK_API_KEY;
url += "&field1=";
url += String(temperature);
url += "&field2=";
url += String(humidity);

Serial.print("Requesting URL: ");
Serial.println(url);

// This will send the request to the server
if (call_thinkSpeak_api) {
client.print(String("GET ") + url + " HTTP/1.1rn" +
"Host: " + host + "rn" +
"Connection: closernrn");

delay(10);
while (!client.available()) {
delay(100);
Serial.print(".");
}
// 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);
}
}
return;
}

This is the third page:

void printDisplay(String msg1, String msg2, String msg3, String msg4) {
// set and clear
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Clear the buffer.
display.clearDisplay();
display.display();

// row 1 – yellow
display.setTextSize(1);
display.setTextColor(WHITE); // fails without this but is yellow then blue
display.setCursor(0, 0);
display.println(msg1);
//display.display(); // push it to oled

if (msg2 != "") {
display.println(msg2);
}
if (msg3 != "") {
display.println(msg3);
}
if (msg4 != "") {
display.println(msg4);
}
//
display.display(); // push it to oled
return;
}

And finally, the fourth page:

void read_dht_sensor(int &humidity, int &temperature) {

Serial.println("—– reading sensor —-");

// read values from the sensor — dh22 is float dh11 is int
Serial.println(dht.readHumidity());
Serial.println(dht.readTemperature());
humidity = dht.readHumidity();
temperature = dht.readTemperature(!IS_METRIC);

if ( isnan(humidity) or humidity == 2147483647 ) {
Serial.println("---- bad reading----");
humidity=0;
temperature=0;
return;
}

Serial.print("humidity: ");
Serial.println(humidity);
delay(20);
Serial.print("temperature: ");
Serial.println(temperature);

return;
}

Hi,
Have you established that you can get readings from your DHT22?

If not write some code, or use an example from the Arduino IDE, to get that working first.
Use the Serial Monitor to read the results, forget about all the other stuff, just have the DHT22 connected.

Just copying a big block of code is not going to help you to debug if you know little about programming.

Thanks.. Tom.. :slight_smile:

Ok, so I got the DHT sensor to show temp and Humidity levels on the serial monitor. I tried running the original code again and nothing shows up on the serial monitor.

// read values from the sensor -- dh22 is float dh11 is int
Serial.println(dht.readHumidity());
Serial.println(dht.readTemperature());
humidity = dht.readHumidity();
temperature = dht.readTemperature(!IS_METRIC);

try
putting the values into variables,
then print the variable ???
Serial.Print(humidity):
after you get the values ?

I tried adding the Serial.print(Humidity) like you said after that code, but that didn't help. When I opn up the serial monitor and try to restart the ESP, I get this message.

ets Jan 8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 1384, room 16
tail 8
chksum 0xef
csum 0xef
csum err
ets_main.c

I think it has something to do with it not booting up right, but I'm not sure.

Is that the first message you see, after resetting the esp? Do you not even see "--->>>>>>> setup loop started <<<<<<<----" (which is the first serial message the setup() code sends)?

yeah I don't see any of that.

Can you upload the "blink" sketch? Does that work OK?