Hello! I’m trying to export readings from a HTU21D-F temperature and humidity sensor to a Cayenne dashboard. Pardon my ignorance, as I’m quite new to all of this.
I found a similar thread with an answer from @adam but it’s for ESP, a similar but different sensor, and there’s quite a lot of differences in library and code references. The code that apparently finally worked for the ESP is the following:
#define CAYENNE_PRINT Serial
#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.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;
HTU21D myHumidity;
void setup()
{
Serial.begin(9600);
Serial.print("Setup");
Cayenne.begin(token, ssid, password);
myHumidity.begin();
Humidity = false;
Temperature = 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);
}
The following is from the Arduino library for the sensor (which works fine by the way when I run the test, just trying to get it to Cayenne):
/***************************************************
This is an example for the HTU21D-F Humidity & Temp Sensor
Designed specifically to work with the HTU21D-F sensor from Adafruit
----> https://www.adafruit.com/products/1899
These displays use I2C to communicate, 2 pins are required to
interface
****************************************************/
#include <Wire.h>
#include "Adafruit_HTU21DF.h"
// Connect Vin to 3-5VDC
// Connect GND to ground
// Connect SCL to I2C clock pin (A5 on UNO)
// Connect SDA to I2C data pin (A4 on UNO)
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
void setup() {
Serial.begin(9600);
Serial.println("HTU21D-F test");
if (!htu.begin()) {
Serial.println("Couldn't find sensor!");
while (1);
}
}
void loop() {
Serial.print("Temp: "); Serial.print(htu.readTemperature());
Serial.print("\t\tHum: "); Serial.println(htu.readHumidity());
delay(500);
}
Any help would be GREATLY appreciated!!! Thanks in advance!