BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Noob needs help with temp anywhere project

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.

Not a clue :slight_smile:

To add a second ds18 would I need another address? If so how do I find it. Everything else I can understand, I think.

Google knows.

Deleted

Look up… look way up… at my post with the code, wherein I already mention this exact issue and provide a link to the solution :stuck_out_tongue:

This whole mess has become intractable, every time I post, this happens, I wish I knew how to fix it.

Thanks Gunner. I’ll go read it again.

1 Like

Unfortunately that is the nature of a flowing, living, forum… all the information one needs is probably right there, and repeated a zillion times, but hard to pick out of the bazillion other words :stuck_out_tongue_winking_eye:

To try and keep up, I fall back on bookmarks, memory, lots of re-reading and too many sticky notes … but alas, it can still seem like a losing struggle… but keep on working the problem. eventually something will click.

1 Like

Don’t feel too bad. I was losing track of my code iterations earlier this week for a project of a similar size as yours. I finally broke down and a) started a journal for each major “technology” as well as a separate punch list document that shows dependencies, and b) signed up on github and created a repo.

When you break it down your project has 5 or 6 pieces (understand microcontroller & integrate hardware, read DHT, read DSxx, read multiple DSxx, Read DHT AND multiple DSxx, Report data (implicit but still important)). Each one of those is going to have multiple challenges. That is a reasonably large punch list for a project that, assuming your memory is no better than mine, is not easy to track in one’s head.

I don’t want to sound like a smart-ass (Or do I? :smiling_imp:) but you think wrong!
I found an excellent comment in a very, very fine written sketch about this and the use of setWaitForConversion(0); with 2 timers and 2 functions:

//============ Temperature sensor
/*
A note about about temperature readings from the DS18B20. The default resolution is 12 bits (0.0625 increments or 1/128 degrees C) and will take the sensor up to 750 ms to complete. This may seem like a problem, but it doesn't have to be (if I've done my homework correct)! startSensorConversation() tells the sensor(s) to start the temp conversation which would normally make the program halt for ~750 ms. But by setting setWaitForConversion(0) to false, the request just initiate the conversation and doesn't wait for it to finish. Total time for startSensorConversation() and getSensorData() functions is now a manageable 30-35 ms instead of 750-800! :)
	
The DS18B20 is rated for a minimum of 50.000 EEPROM Writes (but will probably do many many more). Even though it's a high number, getting a new reading once a minute equals to 1440 a day, and after just 35 days your over 50.000! So to be a bit conservative, I now only do it once every 10 minute. I'm still running getSensorData() once a minute so the data received is never older than 10 + 1 minute. That's OK for my needs.
*/

(Some spelling errors appears in the text; conversation should of course be conversion! )

Yah, I wasn’t sure… I just added that comment based on the other DHT sensors command, which does work that way… not that I ever use such old formats… Celsius all the way! Negative temperatures = Cold, Positive temperatures = Warm, simple Eh? :wink:

Hell yea! :smile: I mean, there is logic behind both Celsius and the metric system! But there is always some reactionary countries: “U.S. remains the only industrialised country that has not fully adopted the metric system as its official system of measurement”. If you could start using it in movies, TV shows, and by NFL commentators I would be happy. F - 32 / 1.8 isn’t always easy to do in the head :stuck_out_tongue_winking_eye: BTW: Anders Celsius was a :sweden:

Nuff with the off topic… I’m not sure if I would call it a general misunderstanding, but many (not just in this forum) doesn’t seem to understand that those sensors works differently. Perhaps not technically correct per se, but I think the easiest way to explain it is that:

  • DHT22 sends data to the MCU
  • MCU reads data from the DS18B20

Or MCU sends “gimme some data” to DHT22 and the DHT22 sends it back.

MCU tells DS18 to start measure temperature, do a analogue-to-digital conversion, store the value in your scratchpad memory and I’ll read it when your done. If I don’t have timing issues, I’ll sit and wait for you to finish, otherwise I’ll come back at a later time (hence the option to change setWaitForConversion()).

Good breakdown and explanation :+1:

1 Like

Thanks! I’m starting to feel like the Sensors Guy :nerd_face:

1 Like