How to migrate ESP8266-AM2315 library?

Hi, in my mongoose project I use Adafruit AM2315 sensor.
I found this library and I want to use it:

What are the steps to migrate it in mongoose?
There are any other library of AM2315 for mongoose?


How to port an Arduino library.

Thank you…I follow all steps of the guide, but now i have this error:

… error: ‘class TwoWire’ has no member named ‘pins’
54 | Wire.pins(dataPin, clockPin);
| ^~~~

The esp8266-am2315 library depend of arudino-wire library, so i add the mongoose version of this library in mos.yaml

But it seems very different than original library:

the mongoose lib of arduino-wire doesn’t have pins member and a lot of other functions.

How can I resolve?

You need to read the source code of arduino-wire and change the code of ESP8266-AM2315 accordingly.

It is very worrying that the integration of such an important library is no longer maintained (the latest commit is dated 2018).
The mechanism of integrating external libraries with mongoose is too cumbersome. One of the benefits of using a framework like mongoose should be simplicity in common operations. The alternative is to have a wide range of libraries already integrated (especially the most popular !!) always updated and working.
If that doesn’t happen, I’m afraid fewer and fewer developers will choose mongoose-os for their projects.

For example I am not able to modify such a complex library, I just wanted to use it. Most likely unfortunately I will be forced to abandon Mongoose due to this problem and find other solutions.

Actually it’s very simple to modify the AM2315 arduino library to compile with Mongoose OS:

  1. Enable the i2c library in your mos.yml
  - ["i2c.enable", true]
  1. Add the scl, sda pin definitions in mos.yml if you are not using the defaults (i2c.sda_gpio=12, i2c.scl_gpio=14 for ESP8266 and i2c.sda_gpio=32, i2c.scl_gpio=33 for ESP32)
  2. Modify line 51 in AM2315_I2C.h
void begin();
  1. Modifiy line 37 in AM2315_I2C.cpp
void AM2315_I2C::begin()
  1. Comment out lines 38-39 and add a line

after them.

I don’t have this sensor, but the following code builds without problem. The file name is main.cpp

#include "mgos.h"

#include "AM2315_I2C.h"

static void timer_cb(void *arg) {
  AM2315_I2C *am2315 = static_cast<AM2315_I2C *>(arg);
  if (am2315->dataReady) {
    LOG(LL_INFO, ("temperature: %.2f, humidity: %.2f", am2315->temperature(),

extern "C" enum mgos_app_init_result mgos_app_init(void) {
  AM2315_I2C *am2315 = new AM2315_I2C();
  mgos_set_timer(10000 /* ms */, MGOS_TIMER_REPEAT, timer_cb, am2315);

Thanks for your help.
I modified the library and now the build is ok.
But after flash, dataReady is always false. While if I try it without mongoose I can read temperature and humidity…

Why I have to comment line 38 in AM2315_I2C.cpp?


I see in mos.yaml of i2c library that the default value for frequency is 100000.

@nliviu Another doubt I have is if I have to put i2c configs in mos.yaml of AM2315 library or in mos.yaml of my project that include library.

The specific settings (i2c.enable, i2c.sda_gpio, etc.) can be set in the project’s mos.yml or at runtime using mos --port <serial or ws or mqtt, etc> config-set i2.enable=true i2c.sda_gpio=12, but not in the library’s mos.yml.

I solved it by writing custom wake up method and called it before aquire data. Doing this, dataReady is true and I can read correctly temperature and humidity. I don’t know why mongoose needs manual i2c write to wake up AM2315 sensor while with an arduino sketch doesn’t need. I suspect the problem is Mongoose Wire library that is not mainaned.

static void wakeUp() {

    int retry = 0;

    struct mgos_i2c *i2c = mgos_i2c_get_global();

    if(i2c == NULL) {
        LOG(LL_ERROR, ("ERROR: I2C is NULL"));

    LOG(LL_DEBUG, ("Waking up AM2315..."));

    while(!mgos_i2c_write(i2c, AM2315_I2C_ADDR, NULL, 0, true) && retry++ < 5) {

You are talking about particulars of a specific chip.
Mongoose-OS is an event-driven framework which has a generic i2c library, if you need particulars for your chip you have to implement them on a library/whatever handling your chip, keeping that in mind.
Since you are porting an Arduino library, and afaik that is far from event-driven, there might be some quirks here and there.
Have you looked with an oscilloscope or a logic analyzer for whatever is going on there ?