ESP8266 MQTT Virtual Pins+DHT22

When selecting a “Channel” is that a virtual channel or the digital/analog pin on the actual device?
so for example in the code below…

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

CAYENNE_OUT(V1)
{
float t = dht.readTemperature();
Cayenne.virtualWrite(V1, t); //virtual pin
}

CAYENNE_OUT(V2)
{
float h = dht.readHumidity();
Cayenne.virtualWrite(V2, h); //virtual pin
}

void loop() {
Cayenne.loop();

…I would have this configured in MyDevices…


As you can see I’m not getting any data from the DHT22 sensor…

Thanks for your help.

Benny

Can you try adding in a print statement to verify A. it’s getting in the read functions and B. it’s getting a valid reading?

Hi @adam,

Sorry about the late reply mate, have been enjoying to much beer and whisky…

I have changed the code so it outputs to serial and to virtual pins. The serial output is spot on, but still no data in MyDevices. Here is the code…

#include “CayenneMQTTESP8266.h”;
#include “CayenneDefines.h”
#include “SimpleTimer.h”
#include “DHT.h”
#define CAYANNE_DEBUG
#define CAYANNE_PRINT Serial
#define DHTPIN 5
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
float h;
float t;
char ssid = “sdsdsdsds”;
char wifiPassword = “sdsdsdsdsd”;
char username = “345345345”;
char password = “345345345345”;
char clientID = “34534534534”;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
dht.begin();
}
void loop() {
Cayenne.loop();
h = dht.readHumidity();
t = dht.readTemperature();
Serial.print("Humidity: “);
Serial.print(h);
Serial.print(”%, Temperature: ");
Serial.print(t);
Serial.println(“C”);
Cayenne.virtualWrite(V0, t);
Cayenne.virtualWrite(V1, h);
}

It output to the serial so I know the wiring and the sensor works…

Here is the dashboard…

I’m at a bit of a loss as to what I’ve done wrong or missing.

Hopefully someone can tell me what simple error I have made!

Cheers

Ben:roll_eyes:

Remove both your widgets off your dashboard and try this. May need to specify the types on your virtual pins. The widgets will pop up automatically or at least they should. Let me know if it works

#include “CayenneMQTTESP8266.h”;
#include “CayenneDefines.h”
#include “SimpleTimer.h”
#include “DHT.h”
#define CAYANNE_DEBUG
#define CAYANNE_PRINT Serial
#define DHTPIN 5
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
float h;
float t;
char ssid[] = “sdsdsdsds”;
char wifiPassword[] = “sdsdsdsdsd”;
char username[] = “345345345”;
char password[] = “345345345345”;
char clientID[] = “34534534534”;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
dht.begin();
}
void loop() {
Cayenne.loop();
h = dht.readHumidity();
t = dht.readTemperature();
Serial.print("Humidity: “);
Serial.print(h);
Serial.print(”%, Temperature: ");
Serial.print(t);
Serial.println(“C”);
Cayenne.virtualWrite(0, t, "temp", "f");
Cayenne.virtualWrite(1, h, "rel_hum", "p");
}

Hi @vapor83

I was just stuffing around and changed the channel from 0 to V0 and data appeared! So I removed the widgets and it automatically populated them. Thanks mate.

Can you add a button for a relay automatically?

Cheers
Ben

The “actuator” or something that sends a command to the micro controller won’t auto populate. You need to add the widget yourself. You also need to add a function in your code that handles the actuator input. You can run code in the “in” function on channel 22 that executes when the actuator is clicked.

CAYENNE_IN(22)
{
	sleepButton = getValue.asInt();

	if (sleepButton == 1)
	{
		run code here();
	}

	else
	{
		run alternate code here();
	}
}