ESP8266 Temperature/Humidity Monitor with HTU21d

I tried to copy this project of @adam http://community.mydevices.com/t/battery-powered-esp8266-temperature-humidity-monitor-with-dht11/1202 to use it with HTU21d (SparkFun) sensor, which is quite more accurate. But until now no i had no luck, as you ca see in the original thread.

Maybe anyone has already used HTU21d with Cayenne or has ESP (nodeMCU) and an HTU21d to copy and test my setup.

Sketch (you can find all edits i made to the original sketch):

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
// #define DHTTYPE DHT11
// #define DHTPIN  14

#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"
// #include <DHT.h>
#include <Wire.h>
#include <SparkFunHTU21D.h>

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "token";
// Your network name and password.
char ssid[] = "ssid name";
char password[] = "password";
//Variables for DHT11 values
//float h, t, hif;
float h, t;
bool Humidity = false;
bool Temperature = false;
//bool HeatIndex = false;

//DHT dht(DHTPIN, DHTTYPE);
HTU21D myHumidity;

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

  Humidity = false;
  Temperature = false;
  //HeatIndex = false;
}

void loop()
{
  //Run Cayenne Functions
  Cayenne.run();
  delay(100);
  //Check if we got all values we wanted and sleep for 10 minutes if we did
  // if (Humidity && Temperature && HeatIndex){
    if (Humidity && Temperature){
    Serial.println("Got all values - sleeping");
    delay(100);
    ESP.deepSleep(60000000, WAKE_RF_DEFAULT);
    delay(100);
  }
}

CAYENNE_OUT(V0){
  Serial.println("Entered Humidity");

  //Check if read failed and try until success
  do {
    //Read humidity (percent)
    // h = dht.readHumidity();
    // h = myHumidity.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("Entered Temperature");
  
  //Check if read failed and try until success
  do {
    //Read temperature as Fahrenheit
    // t = dht.readTemperature(true);
     t = myHumidity.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);
}

/*CAYENNE_OUT(V2){
  Serial.println("Entered Heat Index");

  //Check if read failed and try until success
  do {
    //Read humidity (percent)
    h = dht.readHumidity();
    //Read temperature as Fahrenheit
    t = dht.readTemperature(true);
    //Calculate Heat Index as Fahrenheit
    hif = dht.computeHeatIndex(t, h);
  } while  (isnan(t) || isnan(h));

  Serial.print("Heat Index: ");
  Serial.println(hif);

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

  //Write to Cayenne Dashboard
  Cayenne.virtualWrite(V2, hif);
} */

hi @pearl3 ,

Just want you to know that I edited out your Cayenne token, SSID name, and Password, just since this is a public forum and I want to be safe. What sort of response are you seeing in your serial monitor?

1 Like

Thanks for this :wink:

[2263] Connected to WiFi
[2263] My IP: 192.168.200.68
[5065] Connecting to arduino.mydevices.com:8442
[5421] Ready (ping: 100ms).

Exception (28):
epc1=0x402040dd epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: cont 
sp: 3ffef6c0 end: 3ffefab0 offset: 01a0

>>>stack>>>
3ffef860:  3ffee1b0 3ffee870 3ffeea90 402040d9  
3ffef870:  00007276 3ffef9f4 00000000 4020418b  
3ffef880:  00000005 00000000 00000020 40202977  
3ffef890:  3ffe8fb5 40105047 3ffec9f0 3ffeea90  
3ffef8a0:  40102a7d 3ffec9f0 3ffee790 40204b04  
3ffef8b0:  00007fff 007c2eec 3ffed480 40102c0c  
3ffef8c0:  3ffe983c 00000000 00000000 6800312e  
3ffef8d0:  00000000 007c2eec 4010304a 00000100  
3ffef8e0:  7fffffff 3ffe983c 3ffe983c 00000001  
3ffef8f0:  00000001 4e00646c 3120766f 30322030  
3ffef900:  32203631 007c2eec 00002200 4000050c  
3ffef910:  3ffef9f3 00000000 0ccccccc 00000009  
3ffef920:  00000019 00000001 00000000 3ffef9f5  
3ffef930:  00000000 3ffe8cdc 00000001 3ffef9f0  
3ffef940:  00007276 3ffef9f4 3ffee790 4020101c  
3ffef950:  3ffef9f4 00000000 00000000 40223271  
3ffef960:  3fff0b7c 3fffc6fc 00000001 3ffef9f0  
3ffef970:  00007276 3ffef9f4 00000000 40202c40  
3ffef980:  3ffef9f0 3fff0b7c 3fff0a34 40203c81  
3ffef990:  3ffef900 3ffee7c4 00000030 40203ce2  
3ffef9a0:  00002000 3ffee7c4 3ffeea90 3ffef9f3  
3ffef9b0:  3ffee790 00000004 40204a8c 3ffeea90  
3ffef9c0:  00000005 3ffef9f0 3ffee790 3ffef9f0  
3ffef9d0:  00000004 00000001 3ffee790 40202eb6  
3ffef9e0:  00000000 3ffee7b0 3ffee790 40202d18  
3ffef9f0:  30007276 31303100 00373732 fe003939  
3ffefa00:  04000414 00002000 3ffeea90 3ffee7b0  
3ffefa10:  00000004 3ffef9f0 40204a8c 3ffeea90  
3ffefa20:  3ffe84a0 000020fa 3ffee790 00000001  
3ffefa30:  00000000 3ffee7b0 3ffee790 402030a9  
3ffefa40:  4010599c 0033168e 3ffee870 00000000  
3ffefa50:  3ffee1b0 3ffee870 3ffeea90 3ffee870  
3ffefa60:  3ffee7b0 3ffeea88 40204a8c 3ffeea90  
3ffefa70:  4020151e 00000064 00000064 3ffeea88  
3ffefa80:  3fffdad0 00000000 3ffeea80 402030e7  
3ffefa90:  3fffdad0 00000000 3ffeea80 40204ad8  
3ffefaa0:  feefeffe feefeffe 3ffeea90 40100718  
<<<stack<<<
Ć¼!)Ć„Ć¾1Ā¤1Ć¹Ć¾Setup[261] Connecting to networxSAVE2
[2264] Connected to WiFi
[2264] My IP: 192.168.200.68
[5066] Connecting to arduino.mydevices.com:8442
1 Like

Can you post the code that works with this sensor outside of Cayenne? With that we can try to mesh the two together.

This is the HTU sample code:

/* 
 HTU21D Humidity Sensor Example Code
 By: Nathan Seidle
 SparkFun Electronics
 Date: September 15th, 2013
 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
 
 Uses the HTU21D library to display the current humidity and temperature
 
 Open serial monitor at 9600 baud to see readings. Errors 998 if not sensor is detected. Error 999 if CRC is bad.
  
 Hardware Connections (Breakoutboard to Arduino):
 -VCC = 3.3V
 -GND = GND
 -SDA = A4 (use inline 330 ohm resistor if your board is 5V)
 -SCL = A5 (use inline 330 ohm resistor if your board is 5V)

 */

#include <Wire.h>
#include "SparkFunHTU21D.h"

//Create an instance of the object
HTU21D myHumidity;

void setup()
{
  Serial.begin(9600);
  Serial.println("HTU21D Example!");

  myHumidity.begin();
}

void loop()
{
  float humd = myHumidity.readHumidity();
  float temp = myHumidity.readTemperature();

  Serial.print("Time:");
  Serial.print(millis());
  Serial.print(" Temperature:");
  Serial.print(temp, 1);
  Serial.print("C");
  Serial.print(" Humidity:");
  Serial.print(humd, 1);
  Serial.print("%");

  Serial.println();
  delay(1000);
}

Give the code below a try. Unfortunately I donā€™t have this sensor so I canā€™t test it. The major part I found missing was in the setup function myHumidity.begin();

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
// #define DHTTYPE DHT11
// #define DHTPIN  14

#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"
// #include <DHT.h>
#include <Wire.h>
#include "SparkFunHTU21D.h"

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "token";
// Your network name and password.
char ssid[] = "ssid name";
char password[] = "password";
//Variables for DHT11 values
//float h, t, hif;
float h, t;
bool Humidity = false;
bool Temperature = false;
//bool HeatIndex = false;

//DHT dht(DHTPIN, DHTTYPE);
HTU21D myHumidity;

void setup()
{
  Serial.begin(9600);
  Serial.print("Setup");
  Cayenne.begin(token, ssid, password);
  myHumidity.begin();

  Humidity = false;
  Temperature = false;
  //HeatIndex = false;
}

void loop()
{
  //Run Cayenne Functions
  Cayenne.run();
  delay(100);
  //Check if we got all values we wanted and sleep for 10 minutes if we did
  // if (Humidity && Temperature && HeatIndex){
    if (Humidity && Temperature){
    Serial.println("Got all values - sleeping");
    delay(100);
    ESP.deepSleep(60000000, WAKE_RF_DEFAULT);
    delay(100);
  }
}

CAYENNE_OUT(V0){
  Serial.println("Entered Humidity");

  float humd = myHumidity.readHumidity();

  Serial.print("Humidity: ");
  Serial.println(humd);

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

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

CAYENNE_OUT(V1){
  Serial.println("Entered Temperature");
  
  float temp = myHumidity.readTemperature();

  Serial.print("Temperature: ");
  Serial.println(temp);

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

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

Perfect! Sometimes it is hard to see the wood for the trees :wink:
myHumidity.begin(); was the missing part. Thank you very much!

But new problem :wink: ESP will not wakeup after sleep. It goes to sleep with ā€œGot all values - sleepingā€ and red LED comes up, but then no wake up. I tried with different sleeptimes.
I read a bit and it seems GPIO16 to RST should fix it. But not really, then ESP will do a reset and reconnect when it should go to sleep.
Do you connect GPIO16 to RST?

Yes, I have pin 16 connected to reset

Good morning,

I am a Newbie to this sort of thing. I have a Pi 2 and a HTU21D. Is there a guide that would walk me through my first attempt?

Not sure what pins to use GND and 3.3v are obvious Pin 6 for GND and pin 1 for 3.3v. its the data pins Iā€™m not sure about.

Thanks

Here are a couple resources for you

https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=76688