P.Santillan
Published

ESP8266 NodeMCU Temperature and Humidity on Cayenne Platform

Using DHT11 sensor connected to nodeMCU to read Humidity and Temperature using Cayenne mydevices platform

BeginnerFull instructions provided2 hours250
ESP8266 NodeMCU Temperature and Humidity on Cayenne Platform

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Cayenne
myDevices Cayenne

Story

Read more

Schematics

Nodemcu DHT11

Code

Nodemcu cayenne code

Arduino
/*
Cayenne DHT11 Sensor Project
*/

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#define DHTPIN 0          // D3

// Uncomment whatever type you're using! In this project we used DHT11
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

#include <CayenneMQTTESP8266.h>
#include <DHT.h>

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

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "dfsfdfdfs";
char password[] = "sdsdsd";
char clientID[] = "dsdsdsd";

DHT dht(DHTPIN, DHTTYPE);

//Variables for DHT11 values
float h;
float t;
bool humidity = false;
bool Temperature = false;


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

  humidity = false;
  Temperature = false;
}

void loop()
{
	//Run Cayenne Functions
  Cayenne.loop();
}

CAYENNE_OUT(V0){
  //Serial.println("humidity");

  //Check if read failed and try until success
  do {
    //Read humidity (percent)
    h = dht.readHumidity();

    delay(1000);
  } while  (isnan(h));

  Serial.print("humidity: ");
  Serial.println(h);

  //Set Humidity to true so we know when to sleep
  humidity = true;

  //Write to Cayenne Dashboard`
  Cayenne.virtualWrite(V0, h);
}

CAYENNE_OUT(V1){
  //Serial.println("temperature");
  
  //Check if read failed and try until success
  do {
    //Read temperature as Celsius
   
    t = dht.readTemperature();
    

    delay(1000);
  } while  (isnan(t));

  Serial.print("temperature: ");
  Serial.println(t);

  //Set Temperature to true so we know when to sleep
  //Temperature = true;

  //Write to Cayenne Dashboard
  Cayenne.virtualWrite(V1, t);
}

Credits

P.Santillan

P.Santillan

2 projects • 0 followers
Electrical Engineer

Comments