Noob needs help with temp anywhere project

This is another new experience for me and I need help again.
The above code compiled after your suggestions but after I uploaded it nothing happened, esp did not go online.
The new dht22 was connected
Sketch uploaded
Power restored
Nothing

To check:
I reloaded the original two wire ds18 sketch leaving the dht 22 connected and the original sketch runs well online.
What direction will the next step be in figuring out what is wrong?

UPDATE I’ve made some changes and will upload it tomorrow to see if it works.
Thanks,
Dave


/**************************************************************
 * IoT Multiple Temperature Monitor with Blynk
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 * 
 * Multiple OneWire Sensor: DS18B20
 * Developed by Marcelo Rovai - 25 August 2017
 **************************************************************/

/* ESP & Blynk */
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
char auth[] = " ";

/* WiFi credentials */
char ssid[] = "";
char pass[] = "";

/* TIMER */
BlynkTimer timer;

/* DS18B20 Sensor */
#include <OneWire.h>
#include<DallasTemperature.h> 
#define ONE_WIRE_BUS 2 // DS18B20 on arduino pin2 corresponds to D4 on physical board
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float temp_0;
float temp_1;

/* DHT22 Sensor */
#include <SPI.h>
#include <DHT.h>
#define DHTPIN 12    //D4 on board)
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);

void setup() 
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  sensors.begin();
  sensors. setResolution(10);
  timer.setInterval(3000, readTemp);
  Serial.println(" ");
  Serial.println("Testing Dual Sensor data");
}

/***************************************************
 * Send Sensor data to Blynk
 **************************************************/
void readTemp()
{
sensors.requestTemperatures();
temp_0 = sensors.getTempFByIndex(0);
temp_1 = sensors.getTempFByIndex(1);
sensors.requestTemperatures();
float floatTempF = sensors.getTempFByIndex(0);
char t_buffer[15];
dtostrf (floatTempF, 8, 9, t_buffer);
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Temp_0: ");
Serial.print(temp_0);
Serial.print(" oF . Temp_1: ");
Serial.print(temp_1);
Serial.println(" 0F");

Blynk.virtualWrite(10, t_buffer); // Freezer temp
Blynk.virtualWrite(11, t_buffer); // Refrigerator temp
Blynk.virtualWrite(12, t); //DHT22 Temperature vp12
Blynk.virtualWrite(13, h); //DHT22 Humidity vp13
}
void loop() 
{

 Blynk.run();
 timer.run();
}

This is the all new code, the previous would not go online at all.
What I am trying to do is to add a DHT22 to my existing 2 wire DS18B20 project.
This code goes online but the refrigerator temp virtual pin 11 reads the same as the freezer temp virtual pin 10 and nothing reads at all from the DHT22
Does anyone see where I messed up?
As always, Thanks, Dave

You are reading the same sensor twice. Both are Index(0) :yum:

Not really sure what you are trying to do here… :face_with_raised_eyebrow: t_buffer is the value from floatTempF from Index(0) which are sent to both virtualWrite 10 and 11 = same string. If you reuse the values from temp_0 and temp_1 you don’t have to call requestTemperatures() again, avoiding some delay from the temperature conversation.

Is this really correct dtostrf (floatTempF, 8, 9, t_buffer)? String length of 8 with 9 decimals when `sensors.setResolution(10); itself only returns 2 decimals? I’m not 100% sure how dtostrf works but it looks strange :smile:

I’m using this code to get 2 decimals from my sensors:

float temp = DS18B20.getTempCByIndex(0);
float roundedValue = ceilf(temp * 100) / 100;
1 Like

Thank you Distans for replying.
Yes, I see that typing error re:0, I’ll fix that and report what that does for the situation.
I don’t know much about this, and have been trying to learn as I go to complete this project, I’ve been working on it for months off and on.
What I did was to take my working sketch for two DS18B20’s on a esp 8266 and try to combine it with a working sketch posted by another that has one ds18b20 and one dht22.
Trying to figure out how to mesh these to get 3 temps in different places plus humidity
Fridge, Freezer, Ambient temp, Amb. Hum,
I know nothing about the t_buffer and dtostrf myself except that that poster had it in his code and it worked. That poster is like me in experience, Costas actually wrote it for him. His code was posted in the noob section regarding getting a dht and a ds18 working together. I could find it if it would help to see it,
No one I know outside of this forum knows anything about this, so I am completely dependent. It’s a good thing I bought lots of energy b/c I have burned through a lot trying to come up with this, I’ll buy more though just to help Blynk.

Thanks Distans, I made the fix from 0 to 1 unfortunately there is no change. Freez and Fridg reading same and nothing at all from dht22.
Returned to original sketch, both ds18’s read properly. Dht22 still connected but not included in origal sketch.
The link I was talking about previously:Dht22 and ds18b20 together?
Decimals are irrelevant to my purposes, whole numbers are all I need. I was guessing those lines had something to do with sensors waiting in line to give readings. Maybe Costas knows what it is.
DRAT
Ideas? Life jacket?, Something to put me out of my misery?
Dave

I don’t have any DHT22 myself so I can’t tell if your code is correct or not, but if you search the forum there is a lot of working examples you could use as reference. According to Adafruit, not all sensors have an internal pull-up resistor so you might need to add one. Also, they say that not all works with just 3.3V, so try feeding it with 5V instead (Don’t forget common ground!!)

My experience from this forum is that temp sensors can cause (too) long delays which mess up connectivity. Set the timer to 10-20 sec until everything works as it should.

Split the readTemp() function into two. One for the DS18 and one for the DHT22 and make separate timers. Comment out one of the timers, and get the other timer + function to work first, then the other. When both functions are working you could merge them together and skip one timer if you want. Personally I would keep them separated since it’s different hardware, pins and libraries.

That’s how I would :facepunch: (attack) the problem :slight_smile:

O…K. that all totally overloaded my brain. I have to go one thing at a time so first I’ll take some code for a dht22 and just run it, that should prove that the hardware and voltages are appropriate. I’ll be back with results.

No no, your not overloading anything, you’re on the right track! Just break it up in smaller pieces and:

I’m slowed down here, I had another board I was going to use but it turned out to be defective. The reason is that my project is in the cold basement and to work on code on that board means I have to cart my laptop down there and deal with a bunch of wires etc.

I thought just having another one up here in the livingroom near the woodstove would be easier on me (I’m 67 and arthritic). So I’m waiting on another board to continue.
The version 3 boards seem to have far reaching problems concerning memory, documented on youtube, wish I knew that before, but the seller refunded so that’s o.k.

I haven’t given up…yet

The DHT22 is an affordable and decent sensor but is known to be slow. The most common libraries were developed for the UNO and such. They can have timing problems when used with faster processors. This is important because there are numerous libraries around and they may need adjustment to work with an ESP 8266. I can help you get this working but anyone trying to help can benefit from knowing what DHT library you are using. You will need to make progress at understanding the code (at least superficially at first) you are pasting in or will make very slow process. When you add (ie #include ) a library, there will need to be an initialization call to it where you usually pass set up information to the library. You need to tell your library what pin you are using AND what “name” you will use when you want it to do some work for you.
This arduino dht library is easily adjusted to work with the ESP 8266 and I can provide you with a modified version, but would recommend that you look over the complete web page. Some of the code may be unintelligible to you now but the english language discussion should make some sense and the sketch and library files. The author, Rob Tillaart states that the maximum read rate for the DHT22 is 2 second.
You have one sensor working. I agree with distans and would even go further – you should first get a sketch you understand working with the DHT22 (as you have with the other sensor) and then get that sensor talking to Blynk. Then you can add in the other working sensor routines.

Your copied code above from Dec 7 tries to call routines to read sensors that have not been defined! You will need code for a proceedure to read the DHT 22 and code for a routine to read the DS18. THis Code:

 void readTemp()
{
sensors.requestTemperatures();
temp_0 = sensors.getTempFByIndex(0);
temp_1 = sensors.getTempFByIndex(1);
sensors.requestTemperatures();    

starts by calling a sensor object to request Temperatures() plural, no less. This may be part of the DS18 Library, but it has nothing to do with the DHT22 library. You likely copied it from an application that used multiple DS18 sensors, not a mixture of different sensors. The libraries handle some of the heavy lifting for your hardware interfacing, but each type of hardware will usually (almost always) have it’s own library and its own way of putting it to work ie the code you do need to interact with it. Don’t get stressed – you need to build your complicated app from manageable pieces.

  • Get a simple temperature reading sketch working for each sensor individually, no blink, no wifi, just use the serial monitor

  • Get a sketch running that reads both sensors and prints out the temperature

  • Integrate Blink and add Blink related code to reconnect when connection drops

  • Set up your notifications

You already have learned a fair bit and have the notifications and Blynk interface pretty well sorted! Just need to back up a bit and get the two sensors reading.

[You can open the dht.h (or DHT.h) library file with Word Pad and read the comments to see which library you have or let us know where you downloaded it]

By the way, I am 72 so age is not a barrier - - it just takes a while for all this to make sense. Cheers. James

Here is a screenshot of my Dual DHT22 Blynk app monitoring two areas of my house. You can see the dehumidifier cycling on and off etc

Thanks for your detailed reply, the line sensors. request was in a sketch that revolved around combining a dht22 and ds18 published by another blynker. I haven’t run his code yet due to board problems. All I know is he said it worked. That’s where the dostrf bits came from and t-buffer. I’ll be back, waiting on slow boat from China.

As I read through that post I really felt for the guy, I get it that this forum is about Blynk and not code, but a lot of new people come here for help because it was the snazzy GUI that brought us.

I’m happy that this particular guy came back after Costas got him going to post the code for the rest of us, that was kind, and we all could use some kindness, but I fear that the Blynk community may never see him again.

Perhaps the solution might be a “Code Corner” division of the “noob” section where there might be folks who have the inclination for “hand-holding” help with these problems. I say the more the merrier, invite everyone to play with Blynk, only success and good feelings will follow.

I’ll work on trying to get the dht info you requested.
Dave
here’s the only dht file I found doing a search:

 //
//    FILE: dht.h
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.14
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
//     URL: http://arduino.cc/playground/Main/DHTLib
//
// HISTORY:
// see dht.cpp file
//

#ifndef dht_h
#define dht_h

// #if ARDUINO < 100
// #include <WProgram.h>
// #else
// #include <Arduino.h>
// #endif
#include "c_types.h"

#define DHT_LIB_VERSION "0.1.14"

#define DHTLIB_OK                0
#define DHTLIB_ERROR_CHECKSUM   -1
#define DHTLIB_ERROR_TIMEOUT    -2
#define DHTLIB_INVALID_VALUE    -999

#define DHTLIB_DHT11_WAKEUP     18
#define DHTLIB_DHT_WAKEUP       1
#define DHTLIB_DHT_UNI_WAKEUP   18

#define DHT_DEBUG

// max timeout is 100 usec.
// For a 16 Mhz proc 100 usec is 1600 clock cycles
// loops using DHTLIB_TIMEOUT use at least 4 clock cycli
// so 100 us takes max 400 loops
// so by dividing F_CPU by 40000 we "fail" as fast as possible
// ESP8266 uses delay_us get 1us time
#define DHTLIB_TIMEOUT (100)

// Platform specific I/O definitions

#define DIRECT_READ(pin)         (0x1 & GPIO_INPUT_GET(GPIO_ID_PIN(pin_num[pin])))
#define DIRECT_MODE_INPUT(pin)   GPIO_DIS_OUTPUT(pin_num[pin])
#define DIRECT_MODE_OUTPUT(pin)
#define DIRECT_WRITE_LOW(pin)    (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 0))
#define DIRECT_WRITE_HIGH(pin)   (GPIO_OUTPUT_SET(GPIO_ID_PIN(pin_num[pin]), 1))

// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int dht_read_universal(uint8_t pin);
int dht_read11(uint8_t pin);
int dht_read(uint8_t pin);

int dht_read21(uint8_t pin);
int dht_read22(uint8_t pin);
int dht_read33(uint8_t pin);
int dht_read44(uint8_t pin);

double dht_getHumidity(void);
double dht_getTemperature(void);

#endif
//
// END OF FILE
//

I believe it came from a dht master file,

1 Like

@David_Vinch I had to format that code for proper forum viewing… sorry, but yet another thing for you to learn :stuck_out_tongue: It never ends does it :smiley:

What I don’t understand is why you posted it? Just use the existing Blynified DHT11 code example in the Sketch Builder… it works and is simple. (And works for DHT22 with a basic code change)

I agree with this statement… And developer/user discussions about this topic resulted in the creation of the Help Center and Sketch Builder.

That would be a potentially lonely corner full of “help” posts and crying emojis…

There is already countless websites and tutorials that do this… and this forum also has many members that take voluntary time and effort to accommodate new members. But the primary purpose of this forum is to teach and promote Blynk, it’s process, commands and uses, not the innumerable ways of programming… even the whole Internet doesn’t seem big enough for that :slight_smile:

So, back to the basics… load in that Blynk supplied DHT11 example and get it to work with your DHT22, then dig into it so that you KNOW HOW it works… that is the key, not just finding someone else’s ‘functional for them’ code and wondering why it doesn’t work.

Once your other parts come in… well, one thing at a time :wink:

DrJFM asked for it, I didn’t realize it was code like a sketch.

I see.

OK, so this topic is long and a bit convoluted… can you summarize what you want to do and what equipment you have to do it?

What I am trying to do is to add a DHT22 to my existing 2 wire DS18B20 NodeMcu project.

I have working code for 2 ds18’s and I believe I have working code for one dht22 and one ds18 combined.

So my idea is to weld them together.

David and Gunner,

The request for the library stems from the fact that without the proper timeout setting for the faster ESP 8266, the reading of the device fails. The code below is from the Arduino.cc link I posted to the original version of the library. David has a version that has a hard coded setting for the timeout and it is too short for the ESP 8266. I have used UNO, ESP 8266 and ESP 32 with the DHT 22 and the timeout must be set up properly for each. For the ESP 8266, a timeout of 1200 works.
[ this is more of a count of machine instructions than reading a clock or microsecond function. Faster processors need more clock cycles to give the same timeout delay}

// max timeout is 100 usec.
// For a 16 Mhz proc 100 usec is 1600 clock cycles
// loops using DHTLIB_TIMEOUT use at least 4 clock cycli
// so 100 us takes max 400 loops
// so by dividing F_CPU by 40000 we "fail" as fast as possible
#define DHTLIB_TIMEOUT (F_CPU/40000)

David’s library has a hard coded value of 100. This may be fine for an Arduino Uno, but this value should be set to 1200 for the ESP 8266. ie it should read:
#define DHTLIB_TIMEOUT (1200)
David – make this change with whatever program you used to view the dht.h file and save the modified program making sure you use the option to save as plain text. Notepad doesn’t show good line breaks so Word Pad is easier to read and edit, but you must save the file as plain text.

You will need this library change to get the Blynk example code to run. Gunner’s suggestion to just use example code from here is good – but you may be well served by just getting the DHT to read with no wifi, no blynk first – and understanding, more or less, how the code accomplishes this.
I have copied the example code from the Arduino link below for you.

    //
//    FILE: dht_test.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.07
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
//     URL: http://arduino.cc/playground/Main/DHTLib
//
// Released to the public domain
//

#include <dht.h>

dht DHT;

#define DHT11_PIN 4
#define DHT21_PIN 5
#define DHT22_PIN 6

void setup()
{
  Serial.begin(115200);
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
}

void loop()
{
  // READ DATA
  Serial.print("DHT22, \t");
  int chk = DHT.read22(DHT22_PIN);
  switch (chk)
  {
    case DHTLIB_OK:  
		Serial.print("OK,\t"); 
		break;
    case DHTLIB_ERROR_CHECKSUM: 
		Serial.print("Checksum error,\t"); 
		break;
    case DHTLIB_ERROR_TIMEOUT: 
		Serial.print("Time out error,\t"); 
		break;
    default: 
		Serial.print("Unknown error,\t"); 
		break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);

  delay(1000);


    // READ DATA
  Serial.print("DHT21, \t");
  chk = DHT.read21(DHT21_PIN);
  switch (chk)
  {
    case DHTLIB_OK:  
		Serial.print("OK,\t"); 
		break;
    case DHTLIB_ERROR_CHECKSUM: 
		Serial.print("Checksum error,\t"); 
		break;
    case DHTLIB_ERROR_TIMEOUT: 
		Serial.print("Time out error,\t"); 
		break;
    default: 
		Serial.print("Unknown error,\t"); 
		break;
  }
  // DISPLAY DATA
  Serial.print(DHT.humidity, 1);
  Serial.print(",\t");
  Serial.println(DHT.temperature, 1);

  delay(1000);

  // READ DATA
  Serial.print("DHT11, \t");
  chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK:  
		Serial.print("OK,\t"); 
		break;
    case DHTLIB_ERROR_CHECKSUM: 
		Serial.print("Checksum error,\t"); 
		break;
    case DHTLIB_ERROR_TIMEOUT: 
		Serial.print("Time out error,\t"); 
		break;
    default: 
		Serial.print("Unknown error,\t"); 
		break;
  }
 // DISPLAY DATA
  Serial.print(DHT.humidity,1);
  Serial.print(",\t");
  Serial.println(DHT.temperature,1);

  delay(1000);
}
//
// END OF FILE

Cheers

O.K. Guys please don’t get mad. This PM I received a new board. I wanted to make sure it wasn’t junk like the last one so I went ahead and loaded a sketch I found for just a DHT22 on esp8266 Blynk enabled.
It works, I have proper reading temp and humidity here in the living room 80.6f 18.2rh. (woodstove is running, its 14f outside)

So this means the following are good to go:
1.board
2.dht22 sensor and library
3.sketch
4. Blynk interface

Now I have to wait for the slow parts to arrive, but hey! it’s progress.
I’ll get back to basics when the parts arrive.

It all makes me wonder about that “bad” board, it errored on loading with a mem fail. I got this same with this new board b/c I forgot to change the port from 10 to 8. Now the fail board was plugged into the same usb socket and I checked it with node-mcu-flasher because that program shows the port used by the board plugged in and it said port 10. It’s got me scratching my head a little. I hate to toss out something when it was my mistake all along.
It’s more fun when you’re winning.
Dave

@DrJFM I had to format your code for this forum as well :stuck_out_tongue_winking_eye:

@David_Vinch Glad you are seeing some positive results.

Here is a simple example I put together using a single DHT22 and a single DS18B20 both running on a ESP-01 with OTA suport.

Hopefully you can see that there is really nothing fancy about it… no need to hack into libraries or otherwise strange manipulations… You do need to know the specific ID of the DS18B20 sensor, but that can be determined with another simple code (here) … or otherwise red using the index method.

And yes, this can also be coded to read multiples of either sensors (additional sensor designations for the DS18B20, and additional GPIO pins for the DHT22).

#include <ESP8266WiFi.h>  // For Blynk
#include <BlynkSimpleEsp8266.h>  // For Blynk

#include <ESP8266mDNS.h>  // For OTA
#include <WiFiUdp.h>  // For OTA
#include <ArduinoOTA.h>  // For OTA

char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
char IP[] = "xxx.xxx.xxx.xxx";

BlynkTimer timer;

// DHT22 Sensor setup
#include <DHT.h>
#define DHTPIN 0
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float h;
float t;

// DS18B20 Sensor setup
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float DS18B20Temp1;
DeviceAddress DS18B20Thermometer1 = { 0x28, 0xFF, 0xCD, 0x13, 0x23, 0x17, 0x04, 0xA5 };



void setup()
{
  DS18B20.begin();
  DS18B20.setResolution(DS18B20Thermometer1, 10);
  DS18B20.setWaitForConversion(false); // Or (true) for Fahrenheit... I think?

  Blynk.begin(auth, ssid, pass, IP, 8442);

  timer.setInterval(1000L, UpTime);
  timer.setInterval(3000L, DS18B20TempSensor);
  timer.setInterval(6000L, DHT22TempHumSensor);

  ArduinoOTA.setHostname("ESP-01 Test");  // For OTA
  ArduinoOTA.begin();  // For OTA
}



void DS18B20TempSensor()  // DS18B20 sensor reading
{
  DS18B20.requestTemperatures();
  delay(250);
  DS18B20Temp1 = DS18B20.getTempC(DS18B20Thermometer1);
  Blynk.virtualWrite(V3, DS18B20Temp1);
}



void DHT22TempHumSensor()  // DHT22 sensor reading
{
  h = dht.readHumidity();
  t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  Blynk.virtualWrite(V4, h);
  Blynk.virtualWrite(V5, t);
}



void UpTime()
{
  Blynk.virtualWrite(V0, millis() / 1000);
}



void loop()
{
  Blynk.run();
  timer.run();
  ArduinoOTA.handle();  // For OTA
}

image

Just for completeness, can you let me know what library you are using and, if similar to the Tillaart one I cited, what the timeout code in dht.h looks like. Thanks.

David, good luck going forward. You should likely start a new thread if you hit any further snags. Note that Gunner uses separate functions to talk to each sensor. Each sensor has also been initalized – ie a call to the library for that sensor to allow you to access the library. The two lines that initialize the sensor objects are

DHT dht(DHTPIN, DHTTYPE);

for the DHT22. Then you use the lowercase dht calls to access the sensor eg _h = dht.readHumidity(); line

DallasTemperature DS18B20(&oneWire);

for the Dall Temperature sensor. Then you use DS18B20 calls to access this sensor.
eg. DS18B20.requestTemperatures(); line

Most libraries you use will have such a setup call in the early parts of your sketch, before setup.

Good luck.