DFRobot SHT20 I2C temperature/humidity sensor interfacing with a Photon

I am writing code on the WebIDE to connect a DFRobot SHT20 I2C to a Photon. The hardware connection (to D1 and D0) is obvious. The manufacturers of this probe supply code containing #include “DFRobot_SHT20.h” but unfortunately DFRobot_SHT20 is not in the WebIDE Library Manager. I have downloaded it from Github but my question is: how do I include it in my code on the WebIDE? And I suppose this is an example of a more general question of how to create an Include on the WebIDE from some external firmware?
I should be most grateful for suggestions.

The DFRobot_SHT20 library is in the Particle Community Libraries as just SHT20. I have not tested it, but it exists. Add the SHT20 library in the Web IDE, but still include DFRobot_SHT20.h in the source.

Very many thanks, Rick.

You see anything obviously wrong with code below where I included SHT20


Yes, remove this line

#include <sht20.h>

and replace it with:

#include "DFRobot_SHT20.h"

Thanks Rick but how do I do this? From your first reply DFRobot_SHT20 is not available as such in the community library. As I anticipated, merely typing

#include "DFRobot_SHT20.h"


#include <sht20.h>

does not do it (see below).

J. W. Evans

Professor of the Graduate School
P. Malozemoff Professor Emeritus
Dept. of Materials Science and Engineering
University of California
Berkeley, CA 94720, USA
'Phone = (510) 813-5877

Create a exta tab in you project and name it

#include "DFRobot_SHT20.h"

If that does not work then use

<include "DFRobot_SHT20.h>

Fill the new page you created with the code that’s on the DFRobot_SHT20.h page.

The #include <DFRobot_SHT20.h> is fine.
The compiler complains about that line


You cannot have a file name as an instruction.

BTW, when you are using Web IDE, you can always post a SHARE THIS REVISION link to allow others to test your exact code with all the libraries you have imported.

Also when in doubt try going with the sample(s) bundled with any given library to see how it should be done :wink:

(however in this example you may want to remove the #include <Wire.h> line as it’s not needed in the Particle framework)

Many thanks. Here is a link to a verified code that I crafted from the sample code in Community Libraries. Note that the sample code (and my modification) contain the line to which you objected.

Nope, I don’t think they do.
I objected to the line 14 which reads


The other line in the image (line 13) is commented out and hence not impacting the build.
However, even if it was not commented, it would still be wrong as it contains a trailing .h which the working line in your current code does not.

Otherwise, if you mean the `#include <Wire.h>’ line, then I didn’t really object to it but noted that it was not needed - I didn’t say it was forbidden.

Moving on to a new potential issue - which may explain the YET_MORE_FRUSTRATION title of your project.
Your code is running two Particle.publish() calls with only one second of delay before the next two. This will eventually break the rate limit of one publish per second (with an optional burst of 4 followed by 4 seconds of “cool down”).

I’d suggest you unify the two publishes into one and hence avoid hitting the limit

  char msg[64];
  snprintf(msg, sizeof(msg), "%.2f °C, %.1f %%", temp, hum);
  Particle.publish("envData", msg, PRIVATE);

Additionally we usually suggest to avoid the use of String as it may lead to heap fragmentation and instability of your code when running for long times and also abstain from using delay() as it also impacts responsiveness of your code when dealing with external user input (e.g. button presses or remote Particle.function() calls).

Very many thanks to all who helped get this sorted. My modified code, based on the example found in the Community Libraries, now appears to be working fine.

1 Like