Arduino MKR wifi 1010 and DHT22 sensor

Does anyone have a working sketch using an Arduino MKR wifi 1010 and DHT22 sensor? I’m just trying to update my Cayenne Dashboard with Humidity and Temperature from this one sensor but I cannot find a working example out there. Needless to say I’m very new to this and any help would be much appreciated.

Thank you,

Mark

for MKR1010 use this https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/Connections/MKR1010/MKR1010.ino and the add the DHT code into it https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/Connections/MKR1010/MKR1010.ino

Thank you, but I have tried that and I cannot find a DHT22 piece of code that will actually show up in the Cayenne dashboard.

sorry, my bad. here is the code for dht https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/CommunitySubmitted/DHT/DHT.ino

This is my code, I’m sure it’s very wrong as I’m not a coder but trying to learn. I can get the MKR1010 to connect to wifi but no sensor data is ever sent to Cayenne. Can anyone fix my errors?
-mark FYI the [ ] are showing up as squares for some reason

#include <SPI.h>
#include <CayenneMQTTMKR1010.h>
#include <WiFiNINA.h>
#include <DHT.h>

//Constants
#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
// --SENSOR–
#define DHTPIN 2 // what pin we’re connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)

#define TEMPERATURE_VIRTUAL_CHANNEL 1
#define HUMIDITY_VIRTUAL_CHANNEL 2
#define SECRET_SSID “***”
#define SECRET_PASS “***”

DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor.

// WiFi network info.
char ssid = SECRET_SSID;
char pass = SECRET_PASS;
int status = WL_IDLE_STATUS;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “***”;
char password = “***”;
char clientID = “***”;

void setup() {
Serial.begin(9600);
//Cayenne.begin(username, password, clientID);

while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println(“Communication with WiFi module failed!”);
// don’t continue
while (true);
}

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 5 seconds for connection:
delay(5000);
}
// you’re connected now, so print out the data:
Serial.print(“You’re connected to the network”);
printCurrentNet();
printWifiData();
}

void loop()
{
Cayenne.loop();
Cayenne.begin(username, password, clientID);
}

// This function is called at intervals to send temperature sensor data to Cayenne in Celsius.
CAYENNE_OUT(TEMPERATURE_VIRTUAL_CHANNEL)
{
Cayenne.virtualWrite(TEMPERATURE_VIRTUAL_CHANNEL, dht.readTemperature(), “temp”, “c”);
}

// This function is called at intervals to send humidity sensor data to Cayenne.
CAYENNE_OUT(HUMIDITY_VIRTUAL_CHANNEL)
{
Cayenne.virtualWrite(HUMIDITY_VIRTUAL_CHANNEL, dht.readHumidity(), “rel_hum”, “p”);
}

void printWifiData() {
// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print(“IP Address: “);
Serial.println(ip);
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print(“MAC address: “);
Serial.print(mac[5], HEX);
Serial.print(”:”);
Serial.print(mac[4], HEX);
Serial.print(”:”);
Serial.print(mac[3], HEX);
Serial.print(":");
Serial.print(mac[2], HEX);
Serial.print(":");
Serial.print(mac[1], HEX);
Serial.print(":");
Serial.println(mac[0], HEX);
}
void printCurrentNet() {
// print the SSID of the network you’re attached to:
Serial.print(“SSID: “);
Serial.println(WiFi.SSID());
// print the MAC address of the router you’re attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print(“BSSID: “);
Serial.print(bssid[5], HEX);
Serial.print(”:”);
Serial.print(bssid[4], HEX);
Serial.print(”:”);
Serial.print(bssid[3], HEX);
Serial.print(":");
Serial.print(bssid[2], HEX);
Serial.print(":");
Serial.print(bssid[1], HEX);
Serial.print(":");
Serial.println(bssid[0], HEX);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print(“Encryption Type:”);
Serial.println(encryption, HEX);
Serial.println();
}

just combining the two code is not a difficult task at all.

/*
  This example shows how to connect to Cayenne using an Arduino MKR1010 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.
  Steps:
  1. Install the Arduino SAMD Boards from the Arduino IDE Boards Manager if you have not already done so.
  2. Install the WiFiNINA library.
  3. Select the MKR1010 board from the Arduino IDE Tools menu.
  4. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  5. Set the network name and password.
  6. Compile and upload the sketch.
  7. 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_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTMKR1010.h>
#include <DHT.h>

#define DHTPIN 2

#define DHTTYPE DHT22

#define TEMPERATURE_VIRTUAL_CHANNEL 1
#define HUMIDITY_VIRTUAL_CHANNEL 2

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

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

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

}

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

// This function is called at intervals to send temperature sensor data to Cayenne in Celsius.
CAYENNE_OUT(TEMPERATURE_VIRTUAL_CHANNEL)
{
  Cayenne.virtualWrite(TEMPERATURE_VIRTUAL_CHANNEL, dht.readTemperature(), "temp", "c");
}

// This function is called at intervals to send humidity sensor data to Cayenne.
CAYENNE_OUT(HUMIDITY_VIRTUAL_CHANNEL)
{
  Cayenne.virtualWrite(HUMIDITY_VIRTUAL_CHANNEL, dht.readHumidity(), "rel_hum", "p");
}