Connect a DHT11 sensor

I am not able to get any humitity data back from my DHT11 sensor. It seems to be necessary to call a “CayenneVirualWrite” function, but I have not found any documentation about that function and how to use it. I essentially have a variable with the humidity percentage and would like to pass that variable up to the Cayenne Dashboard. Does anybody have a code example for that?

Hey @mike2506red, welcome to the Cayenne Community.

You’ll likely be interested in this post which shows how to build VirtualWrite() statements for devices connected via our Bring Your Own Thing (MQTT) API: Data types for Cayenne MQTT API

In short, if you have a variable myHum with a humidity percentage, you can pass it to Cayenne with a statement like:

virtualWrite(13, myHum, "rel_hum", "p")

This passes the value of myHum to MQTT channel 13, and specifies the Data Type as humidity and the units as percentage. You should be able to see on the chart in that post where I got those last two values from.

Once run, this statement should auto-create a widget on your dashboard with myHum’s humidity value, looking similar to this:

humMQTT

1 Like

Thanks, however, I am using an Arduino Uno with W5100. Is the syntax the same? And are “rel_hum” and “p” keywords for Cayenne or can I put any arbitrary string between the quotes?

If you’re using the non-MQTT Arduino connectivity, the process is a bit different. In that case widgets aren’t autocreated on your dashboard, you’d instead first create a widget through Add New > Device/Widget > Custom Widgets and then specify an I/O of “Virtual”, select a Virtual Pin number, and a data type/unit through the web UI. Then in your sketch code you’d just need a statement like:

Cayenne.virtualWrite(V13, myHum)

(where V13 is the virtual pin selected on the web dashboard)

There is no need to specify data and unit type this way since you’ve already done so on the web dashboard. Ultimately you have a choice here as we have Arduino libraries for both connectivity types, so having an Arduino/W5100 doesn’t preclude you from using the way I originally described.

https://github.com/myDevicesIoT/Cayenne-Arduino-Library

To answer your other question, rel_hum and p (and all the other items in that linked table) are keywords in the sense that they help the widget auto-creation process select the correct widget and facilitate data conversion in some cases (for example, if you pass a Celsius value to Cayenne and want us to be able to convert it to F within the web UI, you have to specifically use temp and c so we recognize it for conversion.)

But if you just want a text label on a generic Value widget, you can pass whatever you want here:

zoinks

This widget was created with client.virtualWrite(14, myValue, "zoinks", "hello!")

“zoinks” was discarded, our system doesn’t recognize it so it just falls back to a generic value widget, and then “hello!” is applied as a text label for units. It will display but you won’t see any conversion options since its an unknown unit.

2 Likes

Thanks. This solved the problem! Also your link to the MQTT API was very helpful!

2 Likes