Using SHT20 sensor with ESP32 on MongooseOS

I am wanting to use the SHT20 sensor for my ESP32. I have previously only used it with Particle Photon using the library DFRobot_SHT20.h. But I have understood that this library is for Arduino specifically, so how can I use the SHT20 sensor with Mongoose OS? Is there another library? (When searching the list of libraries for Mongoose OS, I couldn’t find anything for this sensor, excepting perhaps i2c(??). I should add that I am new to Mongoose OS.

AFAIK, there is no Mongoose OS SHT20 library. However, there is one for SHT31

I don’t know whether SHT31 is compatible with SHT20, but the Arduino SHT31 library should be easily ported to Mongoose OS

Thanks, I have now bought the SHT31 instead and have tried with the library you mentioned. Something is not working though. I have followed the example code here, but this didn’t work I’m afraid. I also tried changing the I2C address to 0x44 or 0x45 (as it says in the specs of the SHT31-D, instead of the default 0x40 in the example code), as well as changing power source to 5V instead of 3.3V from ESP32. None of this has worked though.

Could it be that I need to add resistors to SDA and SCL? I am using the ESP32 Huzzah.

How did you connect the I2C bus to the sensor? Mongoose OS uses SDA=32, SCL=33 by default.

Add the rpc-service-i2c library and run mos --port <port> call I2C.Scan. It will return the addresses of the I2C devices found on the bus.

Make sure you enabled I2C (- ["i2c.enable", true] in the config_schema: section of mos.yml).

Thanks! I also just noticed that the pins seemed to be 32 and 33 by default and I changed the cfg to SCL: 22 and SDA: 23, which is what seems to be the I2C pins of the ESP32 Huzzah.

This didn’t solve it, so using rpc-service-i2c sounds like a great idea! Didn’t get it working though…I added this to my libs in mos.yml:
- origin:
But I seem to be importing it incorrectly in main.c or something:
#include "rpc_service_i2c"
Where do I need to add the call to I2C.Scan?

(The I2C is enabled in the config_schema.)

You don’t need to add any #include for rpc-service-i2c in main.c.
You just run

mos call I2C.Scan

from your terminal.

Great, thanks! I got the address [68] from the scan. Sadly, it doesn’t seem to solve my problem. After editing my code to s_sht31=mgos_sht31_create(i2c, 0x68); , I still get the error:

mgos_sht31.c:109 Failed to create SHT31 at I2C 0x68

The result from $ mos config-get i2c:
“debug”: true,
“enable”: true,
“freq”: 100000,
“scl_gpio”: 22,
“sda_gpio”: 23,
“unit_no”: 0

I also tried: $ mos call I2C.Read '{"addr": 68, "len": 2}'
with result:

Using port /dev/ttyUSB0
Error: /build/mos-TvZafi/mos-2.17.0+18c1939~bionic0/go/src/ remote error 503: I2C read failed
/build/mos-TvZafi/mos-2.17.0+18c1939~bionic0/go/src/ call failed

Update: I tried again but with a different ESP32, namely a Wroom-32D and it worked! I didn’t get an address from scanning for I2C addresses this time (only empty brackets), but tried with 0x44, which was apparently correct and finally gave me some temperature data.

I don’t know what was wrong with my setup with the ESP32 Huzzah though, so if anyone has any ideas I’d still be very grateful for a solution there.

The address returned by I2C.Scan is decimal. So 68=0x44.

i2c.debug=true must be used in conjunction with debug.level=3 in order to issue log messages.

Maybe the Huzzah needs I2C pull-up resistors?

Ah, right! Thanks, should have realised it was decimal…

Yes, will have to try with pull-up resistors!

You finally found out why it doesn’t work on an ESP32 wroom 32?

I don’t really think “it does not work” but no one put an oscilloscope to see what was actually going on. Did you read “pull-up resistors” in the thread ? Well, i2c an pull-up resistors are synonims.