Arduino connected to RPi3, temp/humidity sensor HTU21D-F

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!

Bump, anyone? Any resources at all would be appreciated, I’m new and trying to learn c but I don’t think this situation is too difficult…

Are you using an ESP too? If not what device are you using?

Sorry, I put it in topic but should have specified and been more clear… It’s an Arduino Uno connected via USB to RPi3.

If your code works OK, but you are only trying to push data to Cayenne, just make 2 custom widgets with temperature and humidity data, with Virtual Pins 0 and 1 (or something you like) and just write the data to the dashboard with this functions (notice you have to put cayenne libraries and cayenne.run… if you don’t know how to do this tell me and I would explain you more deeply):

CAYENNE_OUT(VIRTUAL_PIN_0){ 
Cayenne.virtualWrite(VIRTUAL_PIN_0, htu.readTemperature()); 
}

CAYENNE_OUT(VIRTUAL_PIN_1){ 
Cayenne.virtualWrite(VIRTUAL_PIN_1, htu.readHumidity()); 
}

Ok, so you have an Arduino connected to an RPi via USB and the RPi is running the software to send the data to Cayenne?

So honestly this is where I’m showing my ignorance. I have both units, and the sensor can actually hook up to either. I just don’t know what would be the easiest route to get it to the Cayenne dashboard, via the Arduino or the RPi? Again pardon my ignorance, I’m new to all of this.

I’ve installed the sensor drivers with it hooked up directly to the pi (a basic python script), and I’ve uploaded an Arduino sketch that also works fine. The question is what’s the easiest, most reliable, and hopefully code-free method to get the data to a dashboard?

Thanks so much for your time in responding!

To export the data to cayenne, you have to use virtual pins, which you are using in the code:

Cayenne.virtualWrite(VIRTUAL_PIN_1, htu.readHumidity());

here i put the code i made, and it´s what you need i think, i am using an arduino uno and raspberry PI 3b too, but using a diferent humidty sensor, the DHT22

#include "DHT.h"
#include <CayenneSerial.h>

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "j79moerll3";

#define DHTPIN 5     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

//Pines Virtuales
#define Temperatura V0
#define Humedad V1

unsigned long actual, anterior=0;

void setup()
{
  //Baud rate can be specified by calling Cayenne.begin(token, 9600);
  Cayenne.begin(token);
    dht.begin();
}

void loop()
{
  Cayenne.run();
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  actual=millis();
  if (actual-anterior>2000)
  {
  Cayenne.virtualWrite(Humedad, h);
  Cayenne.virtualWrite(Temperatura, t);
  anterior=actual;
  }
}

So, this code reads temperature and humidity from the sensor and sends it to cayenne server, but to see that info you have to set a widget to show the data that is sent from the arduino, selecting the digital pin when making the configuration of the wiget.
Hope i helped, i´ll be here if you have more questions

The easiest way is using the RPI only, or the arduino only, is not needed to use both. In my case i connect the arduino to the RPI because it´s all i have, i need to use the PWM of the arduino, and the wifi of the RPI, i could use only the arduino if i had a wifi shield.

saulalonsopalazuelos, thanks for the input!

That’s actually where I’m hung up. I appreciate the code, but the sensor I have isn’t a DHT sensor so the libraries and definitions at the top of the code aren’t relevant to this sensor.

As far as hooking up to the RPi directly, I’ve decided to try that, so I disconnected the sensor completely from the Arduino Uno and hooked it up to the Pi, but I don’t know where to go from there. I’ve installed Node.js fine, and I’m running the Blynk client from the RPi, but in the Blynk app I don’t see any data nor do I know how to connect it. Do I need to be running any other script or driver or something? Is there a tutorial that can help? The sensor is connected to the 3v3, ground, SCA, and SCL pins (4 pins total).

Thanks again so much for everyone’s help, hopefully I can get this up and running soon with your help!!!

So it looks like I might need to use the following documentation:

https://mydevices.com/cayenne/docs/#bring-your-own-thing-api-using-c-code-examples-connect-board-to-cayenne

Do you guys think this is the best route to go (MQTT with c code from a working c program for this sensor)? If so, then I guess the next step is for me to hunt down some pi-compatible c code for the HTU21D-F sensor…

Edit: Found a github source with the c code for this sensor on the Pi: GitHub - bbx10/htu21dflib: htu21df temperature and humidity sensor support for Raspberry Pi … so I just need to incorporate this code into MQTT as shown in the link above, then add it to Blynk once the client is running as per the guide?

First of all you have to download the Cayenne app for android or iphone, because the app will set up the raspberry, and automatically install the cayenne libraries, for more infomation and detailed process watch this video Getting a Raspberry Pi Ready for Cayenne - YouTube, after doing that you would be able to add the sensor from the cayenne dashboard.

After doing the app installation you can use arduino virtual pins to communicate with cayenne, just change the code i gave to you to make it work with your sensor. This is the way using the arduino, but if you want to use the rpi, the method changes, i have only used virtual pins on arduino.

Yes, indeed, thanks for the reply. Actually I’ve already installed Cayenne on Android, and I’ve connected both the Pi and Arduino successully to the app. The challenge, however, is that my sensor is not on the dashboard so there’s no way to add it.

To me it definitely appears that I’ll need to do the custom sensor via MQTT above from my last post, but just to confirm if anyone with more experience on the Pi with custom sensors can chime in, is this the best/easiest route?

If you have your Arduino connected to the dashboard the easiest way to get values from your sensor is through Arduino. With your Arduino you can use a virtual channel and send it back that way. Basically just mix my sketch with your sketch. Take out my bits that read the DHT sensor and add in yours to read the HTU.

So, I’ve been out of town for the past week, but I just got back and gave it another shot. Unfortunately, I’m not having success, but I think I’m close!!!

So first of all even with the Serial USB Cayenne test, it seems that the connection to Cayenne keeps dropping in and out. It shows up as connected on the dashboard while running cayenne-ser.sh for a while, but inevitably after 10 seconds or so it disconnects, only to reconnect again and repeat the process. I also added the Raspberry Pi device to my Cayenne dashboard, and that seems to remain mostly connected, though very occasionally there are brief disconnect periods. I don’t know if this is related.

So in light of your recommendation, @adam , and to try to get the sensor working on the dashboard, I changed the code to be as simple as possible. As a reminder, the following is the simple code that WORKS with the sensor:

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

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(10000);
}

Using the following sketch, the Arduino is indeed detected on my Cayenne dashboard, and I’ve made a custom widget to read from virtual pin 4, but there is no data being received:

#include "CayenneSerial.h"
#include <Wire.h>
#include "Adafruit_HTU21DF.h"

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "TOKENINFOHERE";

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

void setup()
{
  // Serial.begin(9600);
  Cayenne.begin(token);
}

void loop()
{
  Cayenne.run();
  delay(100);
}

CAYENNE_OUT(V4){
Cayenne.virtualWrite(V4, htu.readTemperature());
}

CAYENNE_OUT(V5){
Cayenne.virtualWrite(V5, htu.readHumidity());
}

I think I’m really close to getting this, hopefully there’s just a small tweak I need to get it working! Help, anyone?? (Thanks again in advance!!)

Maybe it’s you didn’t write htu.begin(); in the setup

Thanks for the response! That could have very well been an issue - I added it, compiled, and uploaded the sketch with no problem. Unfortunately, although the Arduino still says online on my Cayenne dashboard, I’m still not getting any readings from Virtual Pin 4. Any other ideas, perhaps?

Try to send 255 to cayenne dashboard instead of the temperature, is for see if is the htu or the connection to cayenne.

IT WORKED!!! It wasn’t sending because I added an errant serial print in there from last night.

For anyone seeing this in the future or who had any issues, download the HTU21D-F sensor files from Adafruit’s github, test sensor to make sure it’s working, then use the following sketch for reporting temperature on virtual pin 4 (converted to degrees F here, just remove the math for C) and humidity on virtual pin 5. Big thanks to @Neoxelox and @adam for helping out here!!!

#include "CayenneSerial.h"
#include <Wire.h>
#include "Adafruit_HTU21DF.h"

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "ENTER_TOKEN_HERE";


Adafruit_HTU21DF htu = Adafruit_HTU21DF();

void setup()
{
  // Serial.begin(9600);
  Cayenne.begin(token);
  htu.begin();
}

void loop()
{
  Cayenne.run();
  delay(100);
}


CAYENNE_OUT(V4){
Cayenne.virtualWrite(V4, (htu.readTemperature() * 1.8 + 32));
}

CAYENNE_OUT(V5){
Cayenne.virtualWrite(V5, htu.readHumidity());
}
2 Likes