Dht22 and ds18b20 together?

Hi,
I am a complete novice in programming. Please help me with writing code.
I want to program NODEMCU (wifi) to BLYNK smartphone handled simultaneously DHT22 (room temp) and DS18B20 (water temperature in the aquarium). I can not cope with the code. Someone could do this for me?

I have code that works with DHT22:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 12 //pin gpio 12 in sensor
#define DHTTYPE DHT22   // DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MyToken";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "SSID", "password"); //insert here your SSID and password
 
  // Setup a function to be called every second
  timer.setInterval(1000L, sendUptime);
}

void sendUptime()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
   //Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  Blynk.virtualWrite(10, t); // virtual pin 
  Blynk.virtualWrite(11, h); // virtual pin 
}

void loop()
{
  Blynk.run();
  timer.run();
}

but how can I add DS18B20?

Anybody help me?

Just use the same principle as with DHT.

  • Find a library
  • Read the sensor
  • Push the data in the timer loop with Virtual Write

Pavel,
I think, you’re right, but I don’t know how to add this.
I found code to ds18b20:

#include <SimpleTimer.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2          // This is the ESP8266 pin (WeMos D1 Mini in my case)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

char auth[] = "3f087525778645f1...";
char ssid[] = "mrsmob";
char pass[] = "01223334";

SimpleTimer timer;

int roomTemperature;            // Room temperature in F

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  while (Blynk.connect() == false) {
    // Wait until connected
  }

  sensors.begin();
  sensors.setResolution(10);              // More on resolution: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/

  timer.setInterval(2000L, sendTemps);    // Temperature sensor "polling" interval.
}

void loop()
{
  Blynk.run();
  timer.run();
}

void sendTemps()
{
  sensors.requestTemperatures();                  // Polls the sensors.
  roomTemperature = sensors.getTempCByIndex(0);   // Stores temperature. Change to getTempCByIndex(0) for celcius.
  Blynk.virtualWrite(1, roomTemperature);         // Send temperature to Blynk app virtual pin 1.
}

How to connect this 2 codes?
BTW On this code (ds18b20) i have respond -127*C on BLYNK application. What is wrong?
Resistor 4k7, 3V VCC

Dude, I hope you understand that nobody will write a code for you, right :wink: ? Which leads us to the next plan:

  1. Write a code that will print out the readings from your sensor to serial port. Make sure that you know how to read data. No Blynk, just you, sensor and microcontroller.

  2. Understand the code you are using. Make sure you can “read every line” and understand what it does. Go through every example from Blynk Example Sketches. Specifically learn about timers.

  3. Combine your knowledge and make it work together for your case.

The whole process should not take longer than 30-60 mins. But you will know 90% of Blynk.

Also, there are some great recommendations for beginners

Why not? It’s a simple request. I know, it is not right place to dispute about it.
I do not know how to program in C. I will learn this, but it will take some time. I would like to shorten it and build a device, somebody help me life, so my simple request.
Thank you for your interest in my problem and explanations. Have fun!

PS Last question: Value -127*C from DS18B20 it is a straight or difficult to solve the problem?

Personally I consider this community not being a code fabric, rather than a knowledge exchange place and troubleshooting destination

You spent 0 time on research which is clearly a warning sign

This is what google says:
https://www.google.com/search?q=blynk+ds18b20&oq=blynk+ds18b20&aqs=chrome..69i57j69i60l2j69i61.2696j0j4&sourceid=chrome&ie=UTF-8

This is what search on community shows
http://community.blynk.cc/search?q=ds18b20

Have you at least tried before asking for help? :confused:

1 Like

Brilliant answer :confused:.
Sad that so easily you give people the motivation and incentive. I understand that this is your kingdom and you make the rules. You can do it. Think, however, sometimes wider than the prospect of their own skills and standardized assessments. Who knows? Maybe then instead of several thousands of users there will be dozens of times more?
This project deserves more, and when I met him I found that it is also for people like me.
Good luck to you, and I look for solutions elsewhere, according to your advice.

@mstaniek in fairness to the Blynk team they have built an awesome product but they can’t be expected to teach us all how to code.

There are much better places to ask coding questions but Blynkers will try to help you where we can.

You mention “-127 on Blynk” but this is nothing to do with Blynk. -127 is the default reading you get from a DS18B20 when you haven’t set it up correctly.

So the way to proceed is to get the sensor working without Blynk and then Blynkify it. The internet has hundreds if not thousands of pages on the DS18B20.

There are already quite a few pages on this site on how you turn a regular Arduino sketch into a Blynk sketch but you are not at this stage yet. When your sensor can give you proper readings without Blynk do ask if you need help using the sensor with Blynk.

2 Likes

Wow, either you are being intentionally obtuse (for nefarious reasons) or you are probably not old enough to be playing with electronic devices…

Either way, you could find the answers you seek if you actually try to look for then…

Here are the concrete answer! Thanks Costas!

You can laugh at me and despise me but whatever!
Below a solution by which I meant that it worked… after 98 attempts trial and error (more errors of course)?

Circuit:

  • NODEMCU
  • DHT22 -> D6 (GPIO12); DATA-VCC resistor 10k
  • DS18B20 -> D4 (GPIO2); DATA-VCC resistor 4k7

and the “secret and magic” code:

#define BLYNK_PRINT Serial  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SPI.h>
#include <DHT.h>    		
#define ONE_WIRE_BUS 2		//Data wire plugged to pin GPIO2 (DS18B20 sensor)
#define DHTPIN 12 			//Data wire plugged to pin GPIO12(DHT22 sensor)
#define DHTTYPE DHT22   	//DHT 22 Change this if you have a DHT11
DHT dht(DHTPIN, DHTTYPE);

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
char auth[] = "SecretBlynkToken"; //Put your Auth Token in the Blynk App here

SimpleTimer timer;

void setup()
{
 Serial.begin(9600);
 Blynk.begin(auth, "SSID", "password"); //insert here your SSID and password
 sensors.begin();
 sensors.setResolution(10); 	//More on resolution: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/
 timer.setInterval(3000, readTemp); 	//Data reading interval
}

void readTemp()
{
 sensors.requestTemperatures();
 float floatTempC = sensors.getTempCByIndex(0); //Stores temperature. Change to getTempFByIndex(0) for F.
 char t_buffer[15];
 dtostrf(floatTempC, 8, 9, t_buffer);
 float h = dht.readHumidity();
 float t = dht.readTemperature();
 Blynk.virtualWrite(5, t_buffer);	//DS18W20 temperature virtual pin V5
 Blynk.virtualWrite(10, t); 		//DHT22 temperature virtual pin V10
 Blynk.virtualWrite(11, h); 		//DHT22 humidity virtual pinV11
}
void loop()
{
 Blynk.run();
 timer.run();
}

Now my fish in the aquarium will be safely monitored during my absence.
That’s all folks!

4 Likes

I just purchased a car… but the dealer will not teach me how to drive it :frowning: Can someone here teach me… right away?? I have Blynk on my phone and my phone will be with me in the car, so that counts as valid justification right??

But in all seriousness @mstaniek, glad you figured out the solution (and thanks for posting code… that is helpful to some like me). People here will help (after you show that you have researched and tried yourself first), we are just not going to do it ALL for you.

Next time please read something like Tutorial: How To Ask Good Questions On Technical And Scientific Forums before blaming the founders for not holding your hand just right.

Rant over… I clearly have too much time on my hands… should spend it goooogleing more :wink:

I sympathize with the OP,
I have come late to this in life and find it incredibly confusing to try to understand what to do. When there is no-one to talk to or show stuff to in person the task of learning this variant of C ( which I can’t figure out either ) is like climbing a mountain without a guide.

I thank the world every day for Costas and his willingness to get right down and help.

Is there anywhere to go where someone could get step-by-step 101 intro training, if there is I haven’t been able to find it so far. I’ve been all though the docs section and googled extensively and feel pretty stupid for not being able to get a handle on this, but I sure appreciate Blynk and IOT.
A dummies guide for Blynk!

First, please take note of time date stamps on topics… this is a old post.

Pick the topic, and there is probably some form of tutorial out there for it… but it’s effectiveness is entirely subjective and dependent on one’s learning style and aptitudes. For Blynk, we have Documentation, Help Center and Sketch Builder (And not all of those were even availed when I started - about a year ago). The forum and other talented users help… but some rely too much on “fix it for me” hand holding.

I, for one, really need handholding almost every step of the way (even when younger - undiagnosed learning disability traits and all that)… But reality dictates differently, so I instead hold my own hand and start looking for examples that I almost understand, then while “applying” them (if able) I take them apart for further trial and error type study.

Those that are willing to learn, do, and many become beneficial to others in turn…those that are NOT willing, eventually leave.

PS… Don’t have one yet… But feel free to create the “Need Help” topics as you go along. One just needs to simply show initiative and effort, and many users will jump in and help :wink: