Connection of DHT22, UNO through USB serial

Hi All.

The problem is that i can’t get the temp from DHT22 to show in Blynk widget.
I CAN get the data from serial monitor when i use the DHTsensor scetch (i can see the info in serial monitor)
But besides this scetch, i also need to upload usbserial scetch (cause UNO connected to PC through usb) and Pushdata scetch (cause Blynk needs to gather the info from virtual pin).
I think the problem is that i can’t combine all three scetches in one.

Does anyone understand what am i trying to explain? If so, please try to tell me what i’m doign wrong.

thanks!

@jimmy quite permissible to combine as many sketches as you like. PushData only has 3 or 4 lines that you need when you have the parts from the other sketches.

USBserial can be a bit of a pain as it involves manually adjusting secondary files for Serial port names etc. Think you should perhaps work on this, tell us what you have done, including all hardware and software used and what problems you have.

We can then show you how to add the bits for DHT22 etc.

@costas The total i’ve got in Blynk for now is like this:

  1. when i use usbserial scetch and run BAT file on my PC, i can easily blink LEDs which i connected to breadboard. I mean the Button widget in Blynk app replies on my taps and turns led on and off. It means that i connected Blynk, UNO and usbserial right.
  2. Then, i connected all things on breadboard to make DHT22 works. Uploaded scetch for DHT22 into UNO, and serial monitor in Arduino IDE shows me the temp and humidity, It means that the connection was right.
  3. But when i’am trying to see the temp and humidity data from DHT22 on my Blynk, i fail. As i understand, the problem is that i always need to upload new scetch to UNO - to check the data from DHT, to establish usbserial connection, and to pushdata - these are three different scteches. How to combine them? It’s a tough task for me, since i am in this only for couple of days.

thanks for any possible help

You are well on your way if 1 and 2 are working except no DHT22 data showing in Blynk.

Paste in the 2 sketches USBSerial and DHT22 (format with the </> button so we can read them) and we will go through the combination process and fixing the DHT22 data (and wrap up with any PushData bits you need).

@jimmy we also have ready example for DHT - https://github.com/blynkkk/blynk-library/blob/master/examples/More/DHT11/DHT11.ino all you have to do is just to change it to USB.

@dmitriy what exactly should i change to USB in there? would you be so kind to figure it out to me?

@costas the scetch of usbserial is here:

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

void setup()
{
  // Debug console
  DebugSerial.begin(9600);

  // Blynk will work through Serial
  Serial.begin(9600);
  Blynk.begin(auth, Serial);
}

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

the second one is here:

`#include "DHT.h"

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

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

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  Serial.print(hif);
  Serial.println(" *F");
}

Can you edit the last post.

Highlight all of the first sketch and then press </>, then highlight all of second sketch and press </> again.
You beat me to it. I’ll take a look now.

Are you new to coding?

1 Like

just done it.

and yes, I am very new to coding in any way

I’m going to assume you are quite new to coding.

So one setup() and one loop() and unique variables when you join the 2 sketches.
If 1 sketch states use V1 and a second states use V1 then you are going to have to change the second to V2 etc.

Save the “perfect” first sketch as something like “Combined”.
Paste in the required library and definitions from sketch 2 into Combined alongside the libraries and definitions you already have in the sketch i.e.

#include "DHT.h"
#define DHTPIN 2     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);

With ioT server based sketches we don’t have the loop() as you have it for the DHT but we might get away with it for now. Later you will use SimpleTimer and call functions at your chosen intervals.

Copy these 2 lines into the Combined setup()

  Serial.println("DHTxx test!");
  dht.begin();

Paste in the DHT loop() but ensuring you just have one Blynk.run(). I notice you don’t have one in the DHT sketch.

Show me what you have at this stage and we will take it from there.

I cannot get what should i do with it…

seems like i have to learn more about scetches and coding…

isn’t it possible for you to make one combined scetch? Of course i understand that i should make at least small part by myself, but i am too new for it, although i am 31 already

OK I will knock up a combined sketch using your 2 and the official DHT example but ensure you study it when I post it. Give me a few minutes.

Whilst I am doing that add 2 Value displays to your project, one on V5 and one on V6.

seems like i just made a workable one )))

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 * This example shows how value can be pushed from Arduino to
 * the Blynk App.
 *
 * WARNING :
 * For this example you'll need SimpleTimer library:
 *   https://github.com/jfturcot/SimpleTimer
 * and Adafruit DHT sensor library:
 *   https://github.com/adafruit/DHT-sensor-library
 *
 * App project setup:
 *   Value Display widget attached to V5
 *   Value Display widget attached to V6
 *
 **************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <SoftwareSerial.h>
#include <SimpleTimer.h>
#include <DHT.h>

SoftwareSerial DebugSerial(2, 3); // RX, TX
#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "0b9e95231fbe47b897e6529ff14ac4af";

#define DHTPIN 2          // What digital pin we're connected to

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

DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{
  DebugSerial.begin(9600);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, Serial);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

The only thing left is to add Celcius sign to temp and special sign of humidity, cause now i can only see the numbers

Just posting my combination of the 3 and then I will compare with yours:

/*
 * App project setup:
 *   Value Display widget attached to V5 labelled humidity
 *   Value Display widget attached to V6 labelled temperature
 **************************************************************/

//#define BLYNK_PRINT Serial      // not needed
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX
#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleStream.h>
//#include <SPI.h>                  // not needed
//#include <Ethernet.h>             // not needed
//#include <BlynkSimpleEthernet.h>  // not needed
#include <SimpleTimer.h> 
#include <DHT.h>

char auth[] = "YourAuthToken";

#define DHTPIN 2          // What digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321

DHT dht(DHTPIN, DHTTYPE);

SimpleTimer timer;



// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // You can send any value at any time but don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}
void setup()
{
  // Debug console
  DebugSerial.begin(9600);
  Serial.begin(9600);
  Blynk.begin(auth, Serial);
  dht.begin();
  timer.setInterval(2000L, sendSensor); // Setup a function to be called every 2 seconds
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}
1 Like

Comparing yours with mine you will see they are VERY similar.

thanks very much! First day with Blynk and so huge step towards smarthome (IMHO)

thanks again!

1 Like