Help with my code

I trying to use my DHT11 with a light sensor, that part is ok both of them are working well, but i dont know why my buttons are not working, I am trying to use one to control a relay but it take a very long time to change the state (normally 5~6 secs), is something wrong in my code?

//Libraries
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <IRremote.h>
#include <SimpleTimer.h>
#include <dht.h>

//Definiçoes
#define dht_dpin A1


//Pré void
dht DHT;
SimpleTimer timer;
IRsend irsend;

char auth[] = "e2a6bdbbd0a34bc7a1f6379e834fe88a";

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth);
}

void loop() {
{
   if (DHT.humidity > 95) {
    Blynk.notify("Alto risco de chuva");
  }
  if (DHT.humidity < 30) {
    Blynk.notify("Umidade do ar muito baixa");
  }

  int luz = analogRead(5);
  luz = map(luz, 0, 1023, 0, 100);
  int chk = DHT.read11(dht_dpin);
  Blynk.virtualWrite(1, String(DHT.temperature) + "*C");
  Blynk.virtualWrite(2, String(DHT.humidity) + "%");
  Blynk.virtualWrite(3, String(luz) + "%");
  delay(200);
  Blynk.run();
}

BLYNK_WRITE(2)
{
  if (param.asInt()){
    irsend.sendNEC(0xC20DF, 32); //  Source
    delay(40);
  }
}

BLYNK_WRITE(3)
{
  if (param.asInt()){
    irsend.sendNEC(0xCD02F, 32); //  Up
    delay(40);
  }
}

BLYNK_WRITE(4)
{
  if (param.asInt()){
    irsend.sendNEC(0xC30CF, 32); //  Down
    delay(40);
  }

First off, try to remove the delays. They are becoming a deadly sin really fast :wink:

I use the same stuff to control my IR signals and that works fine, just like you wrote it there.

I suggest you remove the code to display your humidity and temperate etc. and attach those to a virtual pin. In your dashboard you’ll be able to set the interval with which the virtual pin will be read. That way is much more stable than what you are doing here.

1 Like

I am an starter so can you help me? how can I remove the code, that was the only way I could make the DHT11 work.

This is the, as far as I can see, bad part which will probably mess up your connection or stability.

BLYNK_READ(5)
{
  int luz = analogRead(5);
  luz = map(luz, 0, 1023, 0, 100);
  int chk = DHT.read11(dht_dpin);
  Blynk.virtualWrite(5, String(DHT.temperature) + "*C");
}

I think this should do it. Make a virtual pin for each read-out. Do this for each value and you’re set I think.

1 Like