Using DHT11 + HC-SR04 Ultrasonic sensor and ESP8266 as Wifi

Hello, you should choose the arduino type which you are using and then wifi
shield.

Yes, I get this"Waiting for board to connectā€¦" But it does not connect me

Yes because you need to follow this topic since the beginning where I show
the code, libraries and electrical connectionsā€¦ Then you need to adapt
the code for your wifi network, password and the auth code.

Yes, I also did it :frowning: . But I just try to use a simpler code to learn how to connect

Copy and paste my code completely and compile. Test my code and then if it
works then you modify it to your needs.
My code works, so make sure it will work on your project even if you donā€™t
have the sensorsā€¦

1 Like

I found the error, I had badly connected a pin on the ESP8266, thanks for the patience :slight_smile:

So it works now?

yeah :slight_smile: thanks!!!

Awesome.

1 Like

Hi @bestes, Iā€™m trying to turn ON/OFF an air conditioner through cayenne, and thatā€™s working fine.
My problem is when I try to increase or decrease the temperatura.
I donā€™t know if cayenne doesnā€™t support to many signals or if the problem is my code, but when I put three signals to be sent the platform starts to go on and off after few seconds.
I didnā€™t find anything here in teh community about it, do you know what can be?

Personally, I donā€™t like to rely on a network connection and some computer in the cloud somewhere making decisions on when to turn on and off heaters, etc. I want that logic local. Telling the device its setpoint, sending out values, and triggering alarms and alarm delivery are great for the cloud and Cayenne does a great job.

We just canā€™t depend on 100% up time. Even only a 0.01% downtime with heaters, etc., could cause your house to burn down. So always do decisions locally or at least build in redundant failsafes. Cayenne has some pretty solid infrastructure and we now have active bugfixes, but generally my argument is still sound. In the controls industry, if we donā€™t see a watchdog pulse (communications are good) after so many minutes, we shut it down.

You can look in the projects area for my smoker project for some ideas on how to do local decision making - itā€™s a start at least :slight_smile:

Cheers,

Craig

Very good Craig. I agree.

I understood your point. I am not setting Cayenne to do anything alone.
What Iā€™m trying to do is allow an user changes the temperature if his wants, using the cayenne platform. Basically, I am making an air conditioning remote control.
However, after upload the code to arduino, the ESP is not able to connect with cayenne. That occurs only when I put more than two signals in the code. I am using the IRemote library.
I donā€™t know if the problem is that the signals are being sending at the same time ( if is this, I donā€™t know how to fix it) or if the problem is some ESP limitation during the transmission. Any idea of what can be the problem?

Hello Barbara, I had the same problem when developing the code of the topic
by adding the ultrasonic and dht sensor. Sometimes it was not able to
connect to cayenne but this is because of the code structure, I donā€™t know
the real reason but looks like you only can add the lines at the correct
places of the code so it will not disturb the cayenne loop.

Interesting. The question now is How do I do that?
Because I donā€™t know which places are correct. I would like to understant how cayenne loop works, but I donā€™t find any doc.

Can you try to use my code as reference? You will see where I have
customized itā€¦

Yes, Iā€™ll try!
thanks!

Post your code so we can take a look at it.

Cheers,

Craig

Here it is:

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
//#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "********";
char ssid[] = "******";
char password[] = "*****";

//#include <CayenneESP8266Shield.h>
//#include "DHT.h"
//#include <IRremote.h>

IRsend irsend;

// Set ESP8266 Serial object
//#define EspSerial Serial

ESP8266 wifi(EspSerial);


//#define DHTPIN A1 // pino que estamos conectado
//#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);

//#define VIRTUAL_PIN0 V0 //PINO DA LUMINOSIDADE
//#define VIRTUAL_PIN1 V1 //PINO DA TEMPERATURA
//#define VIRTUAL_PIN2 V2 //PINO DA UMIDADE
//#define VIRTUAL_PIN3 V4 



int vPin1 = 0;
int temp;
int setpoint = 22;
int temperatura = 0;
int power=1;
int khz = 38;
int ligado=0;
int setpoint_atual;




void setup()
{
  EspSerial.begin(115200);
   dht.begin();
  Cayenne.begin(token, wifi, ssid, password);
  
}

CAYENNE_IN(V3){
  vPin1 = getValue.asInt();
  temp = vPin1/1023;
}


CAYENNE_IN(V5){

  int setpointAtual=temp;
  int setpointAnterior=22;
  unsigned long tempoAnterior = 0;
  unsigned long tempoIntervalo = 120000;
  
  int enviou=0;
  int cont1 = setpointAnterior - setpointAtual;
  int cont2 = setpointAtual - setpointAnterior;
  
  power = getValue.asInt();
  if(ligado == 0){
         if((power == 1) || (temperatura > setpoint)){
        ligarAr();
        ligado=1;
           if((millis-tempoAnterior) > tempoIntervalo && ligado == 1 ){
                   if((setpointAtual < setpointAnterior) && (enviou < cont1 )){
                    diminuirTemperatura();
                    enviou++;
                    delay(5000);
                   }else if ((setpointAtual > setpointAnterior)&& (enviou<cont2)){
                    aumentarTemperatura();
                    enviou++;
                    delay(5000);
                   }
                   setpointAnterior = setpointAtual;
                   tempoAnterior = millis();
                    }
                    if((enviou==cont1)||(enviou==cont2)){
                      enviou = 0;
                    }
      }
 }else if((power == 0)&&(ligado == 1)){
    desligarAr();
  ligado=0;
}
}


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

//SENSOR LDR

CAYENNE_OUT(VIRTUAL_PIN0)
{
int sensorPin = A0; // select the input pin for the LDR sensor
int sensorValue = 0; // variable to store the value coming from the sensor
// sensorValue = analogRead(sensorPin);
//  Cayenne.virtualWrite(V0, sensorValue);
}

//SENSOR DHT11 - TEMPERATURA

CAYENNE_OUT(VIRTUAL_PIN1)
{
  temperatura = dht.readTemperature();
 Cayenne.virtualWrite(V1, temperatura);
}

 
//SENSOR DHT11 - UMIDADE

CAYENNE_OUT(VIRTUAL_PIN2)
{
 float umidade = dht.readHumidity();
  Cayenne.virtualWrite(V2, umidade);
}

//Watching setpoint changing

CAYENNE_OUT(VIRTUAL_PIN3){
  Cayenne.virtualWrite(V4, setpoint);
}



void ligarAr(){
  unsigned int irSignal[] = {3184, 9680, 620, 1448, 592, 428, 592, 428, 592, 428, 592, 1444, 596, 428, 592, 428, 592, 424, 596, 424, 596, 428, 616, 428, 592, 428, 592, 428, 612, 408, 612, 1428, 616, 404, 616, 428, 592, 428, 596, 1444, 592, 1448, 612, 408, 616, 1448, 584, 436, 612, 408, 616, 1448, 508, 512, 528, 492, 616, 1432, 612};
  irsend.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), khz); //Note the approach used to automatically calculate the size of the array.
}

void desligarAr(){
  unsigned int irSignal[] = {3196, 9692, 620, 1444, 596, 424, 596, 424, 620, 404, 616, 1448, 592, 428, 616, 428, 592, 428, 592, 1448, 616, 1452, 612, 404, 612, 416, 612, 428, 592, 428, 612, 412, 616, 404, 612, 408, 616, 424, 584, 436, 592, 428, 592, 428, 592, 1448, 580, 444, 616, 1444, 584, 436, 592, 432, 616, 428, 592, 1448, 592, 44512, 108};
  irsend.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), khz); //Note the approach used to automatically calculate the size of the array.
 }

 void aumentarTemperatura(){
 unsigned int irSignal[] = {108, 64520, 3180, 9632, 584, 1456, 592, 436, 608, 428, 564, 456, 564, 1476, 620, 408, 584, 436, 584, 452, 588, 432, 508, 512, 504, 516, 528, 492, 588, 1452, 612, 408, 584, 444, 584, 452, 568, 456, 564, 1472, 592, 1456, 584, 1476, 584, 432, 592, 1476, 588, 436, 612, 408, 584, 456, 564, 456, 592, 1452, 584, 1480, 616};
  irsend.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), khz); //Note the approach used to automatically calculate the size of the array.
 }

  void diminuirTemperatura(){
 unsigned int irSignal[] = {3200, 9672, 608, 1452, 612, 408, 584, 436, 584, 436, 532, 1508, 532, 488, 532, 488, 532, 488, 608, 412, 552, 468, 552, 468, 608, 436, 612, 1424, 616, 408, 532, 488, 636, 384, 552, 492, 584, 1456, 612, 1452, 612, 408, 612, 408, 552, 1488, 532, 488, 532, 512, 616, 404, 616, 404, 616, 1424, 640, 404, 616, 1016, 108, 51864, 120};
 irsend.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), khz); //Note the approach used to automatically calculate the size of the array.
 }

Hello cassio.lucass
Why i did use this like you said but it is still not working
It show me the problem like this