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

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

Hello, looks like there are issues with the characters or the
CayenneESP8266Shield.h is not copied to the cayenne libraries folder…

1 Like

can you give me libary folder CayenneESP8266Shield ?
thankyou

It is already attached to the first topics of this post…

1 Like

i did add it to arduino. but it is

not work

have you disconnected the RX and TX pins of arduino during the download of
the code?

1 Like

no. i have just verified in arduino.

you have to disconnect the RX and TX pins to make the download.

1 Like

how can i do it. can you help me. please

@vuthemanh0507,

Can you confirm that the board you selected in the Arduino IDE is the Uno board and not the ESP. Also, make sure you have the comport selected for the Uno board. Looks like COM5 isn’t it.

Then as @cassio.lucass states, you need to make sure the RX line on your Uno is disconnected from the ESP shield otherwise you have your PC and the ESP fighting over who gets to talk to your Uno.

Next if all that works and your Uno programs, but you are not connected to Cayenne, please confirm that your ESP is programmed with AT firmware and that it is a newer version.

Cheers,

Craig

Is it possible to apply your techniques to the raspberry pi. Specifically, I would like to use the HC-SR04 with the pi and cayenne in a garage door application. I am not aware that cayenne has opened up their interface to allow user added code.

Thanks

@richard hello, of course you can, I dont have the raspberry yet but should arrive soon, all you need to do is to make the logic for the HC-SR04 inside the micro-controller and send the result value to cayenne. But, why do you want to use the raspberry as it is so powerful for such a small application? you could use an arduino nano… or similar…

Do you think it would be difficult to add on top of this masterpiece a LCD displaying the reading of the sensor? Imagine you lose WiFi and need to know the distance from a container that is sealed, simply as a backup.

you can add a LCD Double Temperature, rain, humidity on LCD with ESP8266.

but this wont work, as cayenne does not have a offline feature. when wifi is lost the code keeps looping for re-connection and no other code is run.

I see. Then I guess a solution would be to add an Arduino with a second sensor. That would also serve as redundancy safety check for the wifi sensor readings.

Of course you can, especially if you have a Uno and have the I2C or SPI interface free.

Network does not need to be active to show results of your sensors. I like this little baby.

https://learn.sparkfun.com/tutorials/micro-oled-breakout-hookup-guide

Cheers,

Craig