The use of the ESP8266 in the world of IoT

User avatar
By mallickarjuna
#84501 Dear All,

I am using NodeMCU in Arduino IDE

The code works well with Onewire and ds18b20 temprature sensor

I am trying to modify a code I received to add the DTH21 to connect to Cayenne MQTT

Can someone help me put it rightly? I am a novice in coding anything.

Code: Select all#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneMQTTESP8266.h>
#include <ESP8266WiFi.h>

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>


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

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "xxxxxxx";
char password[] = "xxxxxxxxxxx";
char clientID[] = "xxxxx";

#define SENSOR_PIN D3 // Digital pin connected to the Sump Temp sensor
#define VIRTUAL_CHANNEL 1

#define DHTPIN D2 // D2 // Digital pin connected to the DTH sensor
#define VIRTUAL_CHANNEL 2
#define DHTTYPE DHT21  // DHT21 (AM2301)

OneWire oneWire(SENSOR_PIN);
DallasTemperature sensors(&oneWire);

DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

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


  // Print humidity sensor details
  sensor_t sensor;
  //dht.temperature().getSensor(&sensor);
 
  dht.humidity().getSensor(&sensor);
   // Set delay between sensor readings based on sensor details.
  delayMS = sensor.min_delay / 1000;
}

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

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL)

{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Channel.
  Cayenne.celsiusWrite(VIRTUAL_CHANNEL, sensors.getTempCByIndex(0));
  // To send the temperature in Fahrenheit use the corresponding code below.
  //Cayenne.fahrenheitWrite(VIRTUAL_CHANNEL, sensors.getTempFByIndex(0));

// Send the command to get Humidity
//
// This command writes the Humidity in percentage to the Virtual Channel.
   
}