DHT 22+Rain sensor+NodeMCU V3

(I’m from Spain, so my english isn’t perfect, sorry)
Hi,
I have been workin in a project with DHT 22+Rain sensor+NodeMCU V3, but i have some problems.
-I don’t know how i can put the temperature in celsius
-I don’t know how i can put the humidity in percentage
-How I can invert the logic of the rain sensor in code?
I have been taking parts of diferents codes and I’m making this.
If somebody have any recomendation for de code, or an implementation of other sensor or actuator, I will thank.

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

#include <DHT.h>
#define v2 2
#define SENSOR 2
#define DHTTYPE DHT22
#define DHTPIN  14

DHT dht(DHTPIN, DHTTYPE, 11);

char ssid[] = "";
char wifiPassword[] = "a4";

// Cayenne authentication info.
char username[] = "b";
char password[] = "422a";
char clientID[] = "d4adce";

unsigned long lastMillis = 0;

void setup() {
  Serial.begin(115200);
  dht.begin();
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pinMode(SENSOR, INPUT);
}

long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 1.0;

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

  
CAYENNE_OUT_DEFAULT()
{
  float newTemp = dht.readTemperature();
  temp = newTemp;
  Serial.print("New temperature:");
  Serial.println(String(temp).c_str());
  Cayenne.virtualWrite(0, String(temp).c_str()); // channel 0
  
  float newHum = dht.readHumidity();
  hum = newHum;
  Serial.print("New humidity:");
  Serial.println(String(hum).c_str());
  Cayenne.virtualWrite(1, String(hum).c_str()); // channel 1

}
CAYENNE_OUT(v2)
{
  // Read data from the sensor and send it to the virtual channel here.
  // For example, to send a digital value you can use the following:
  int value = digitalRead(SENSOR);
  Cayenne.virtualWrite(V2, value, TYPE_DIGITAL_SENSOR, UNIT_DIGITAL);
}

if you can share the code you are using then we can help you.

oh, sorry.

have a look at this topic Data types for Cayenne MQTT API
first, remove all the widgets from the dashboard, make the changes with correct data types in your code and upload it. once done it will automatically create green temporary widgets.add them by clicking on +
if you still have trouble making the changes in your code, let me know.

can you make a quick example with temperature for example, please.
Thanck you.

Because here where i can put the DHT sensor?
Pass temperature value 25.2 in Celsius to MQTT Channel 2
virtualWrite(2, 25.2, "temp", "c")

from your code:

CAYENNE_OUT(0)
{
float newTemp = dht.readTemperature();
temp = newTemp;
Cayenne.virtualWrite(0, temp, "temp", "c")
}

Thank you, I appreciate it. I will try it tonight and I say something.
Good job.

1 Like