ESP8266 NodeMCU and DHT22 & BH1750

New to this but can get my ESP8266 NodeMCU to connect to Cayenne and show the automatic test values, but trying to connect any actual sensors is an exercise in futility and many errors.

So, instead of both sensors (I can get them working along to the serial monitor, but not to IoT Cayenne), I’ll try starting with the DHT22, but still many issues.

This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.

The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

1. If you have not already installed the ESP8266 Board Package install it using the instructions here:
2. Select your ESP8266 board from the Tools menu.
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
4. Set the network name and password.
5. Compile and upload the sketch.
6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <DHT.h>
#define DHTPIN 6     // Digital pin connected to the DHT22 sensor
#define DHTTYPE DHT22   // DHT22 (AM2302)

// WiFi network info.
char ssid[] = "vodafoneAAZGBF";
char wifiPassword[] = "af9YsnfFzcYcRkaz";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = ":-)";
char password[] = "blablabla";
char clientID[] = ":-)";

void setup() {
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);

void loop() {
	int chk =;
	float temp = DHT.temperature;
	float hum = DHT.humidity;
	Cayenne.virtualWrite(1, temp, TYPE_TEMPERATURE, UNIT_Celsius)
	Cayenne.virtualWrite(2, hum, TYPE_HUMIDITY, UNIT_Percent)}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
	// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
	Cayenne.virtualWrite(0, millis());
	// Some examples of other functions you can use to send data.
	//Cayenne.celsiusWrite(1, 22.0);
	//Cayenne.luxWrite(2, 700);
	//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
	CAYENNE_LOG("Channel %u, value %s",, getValue.asString());
	//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");

Here are my error codes, and even with ChatGPT I’m not understanding what is wrong. Should be simple right? No.

Arduino: 1.8.19 (Windows 10), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, Flash, Disabled (new aborts on oom), Disabled, All SSL ciphers (most compatible), 32KB cache + 32KB IRAM (balanced), Use pgm_read macros for IRAM/PROGMEM, 4MB (FS:2MB OTA:~1019KB), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

C:\Users\Aaron\AppData\Local\Temp\arduino_modified_sketch_766695\ESP8266.ino: In function 'void setup()':

ESP8266:36:7: error: invalid use of non-static member function 'void DHT::begin(uint8_t)'

   36 |   dht.begin;}

      |   ~~~~^~~~~

In file included from C:\Users\Aaron\AppData\Local\Temp\arduino_modified_sketch_766695\ESP8266.ino:18:

C:\Users\Aaron\Documents\Arduino\libraries\DHT_sensor_library/DHT.h:66:8: note: declared here

   66 |   void begin(uint8_t usec = 55);

      |        ^~~~~

C:\Users\Aaron\AppData\Local\Temp\arduino_modified_sketch_766695\ESP8266.ino: In function 'void loop()':

ESP8266:40:15: error: expected primary-expression before '.' token

   40 |  int chk =;

      |               ^

ESP8266:41:18: error: expected primary-expression before '.' token

   41 |  float temp = DHT.temperature;

      |                  ^

ESP8266:42:17: error: expected primary-expression before '.' token

   42 |  float hum = DHT.humidity;

      |                 ^

ESP8266:43:50: error: 'UNIT_Celsius' was not declared in this scope

   43 |  Cayenne.virtualWrite(1, temp, TYPE_TEMPERATURE, UNIT_Celsius)

      |                                                  ^~~~~~~~~~~~

exit status 1

invalid use of non-static member function 'void DHT::begin(uint8_t)'

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

here is the code you can use for DHT Cayenne-MQTT-Arduino/DHT.ino at master · myDevicesIoT/Cayenne-MQTT-Arduino · GitHub