Beginner ! How can i add DHT11 code in this project? Thank you!

Before creating the topic

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.


void loop()

```/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  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
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  Blynk using a LED widget on your phone!

  App project setup:
    LED widget on V3
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "e7fffa3d701e489cabf34582f3748126";
char ssid[] = "959e1a";
char pass[] = "280836872";

// Select your pin with physical button
const int btnPin = 5;

WidgetLED led3(V3);
WidgetLED led4(V4);

BlynkTimer timer;

// V3 LED Widget represents the physical button state
boolean btnState = false;
void buttonLedWidget()
{
  // Read button
  boolean isPressed = (digitalRead(btnPin) == LOW);

  // If state has changed...
  if (isPressed != btnState) {
    if (isPressed) {
      led3.on();
       led4.off();
    } else {
      led3.off();
       led4.on();
    }
    btnState = isPressed;
  }
}

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

  Blynk.begin(auth, ssid, pass);

  // Setup physical button pin (active low)
  pinMode(btnPin, INPUT_PULLUP);

  timer.setInterval(500L, buttonLedWidget);
}

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

I would recommend pulling up the DHT Blynk example in another window and paste in the DHT lines of code till it works… thats how i put projects together when im not sure how the code works.

/*************************************************************


Blynk using a LED widget on your phone!

App project setup:
LED widget on V3
*************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

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

har auth[] = “e7fffa3d701e489cabf34582f3748126”;
char ssid[] = “959e1a”;
char pass[] = “280836872”;
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

const int btnPin = 5;

WidgetLED led3(V3);
WidgetLED led4(V4);

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

Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}

boolean btnState = false;
void buttonLedWidget()
{
boolean isPressed = (digitalRead(btnPin) == LOW);

if (isPressed != btnState) {
if (isPressed) {
led3.on();
led4.off();
} else {
led3.off();
led4.on();
}
btnState = isPressed;
}
}

void setup()
{

Serial.begin(9600);

Blynk.begin(auth, ssid, pass);
pinMode(btnPin, INPUT_PULLUP);
dht.begin();
timer.setInterval(500L, buttonLedWidget);
}

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

I try this one… was successfully verify but didn’t work .

Check this example:

https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=More%2FDHT11

You’ve created (or added) a function called sendSensor, but it’s never being called. You need to add another timer to void setup similar to the one you already have:

timer.setInterval(500L, buttonLedWidget);

The DHT11 is quite slow, so don’t call your sendSensor more frequently than every 5 seconds (5000 milliseconds).

You also have a typo here, you’ve deleted the ‘c’ from char.

har auth[] = “e7fffa3d701e489cabf34582f3748126”;

It’s also not a great idea to share your Auth code when you’re using the Blynk cloud server, as it allows others to write values to your app. You can regenerate a new Auth code from within your app.

Pete.

Thanks Pete for the quick reply i will check this after i will be back home.

plz guide me also how i add dht11 ccode in python

you forgot to put :

timer.setInterval(10000L, sendSensor);

???
what is that bad code ?

has @psoro said

this is the right code

https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=More%2FDHT11

Thank you Alexis! everything working fine now .

Now is working as intended.
Thank you for your help Pete.

1 Like

sounds good :wink:

@Blynk_Coeur sir can you plz guide me and remove my errors thanks

I can’t because your code is in Python language, not #C
why don’t use that code :
https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=More%2FDHT11