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);
} */
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?
/*
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
myHumidity.begin(); was the missing part. Thank you very much!
But new problem 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?