How to migrate ESP8266-AM2315 library?

#1

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?

Thanks

#2

How to port an Arduino library.

#3

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?

#4

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

#5

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.

#6

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
config_schema:
  - ["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
Wire.begin();

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);
  am2315->acquireData();
  if (am2315->dataReady) {
    LOG(LL_INFO, ("temperature: %.2f, humidity: %.2f", am2315->temperature(),
                  am2315->humidity()));
  }
}

extern "C" enum mgos_app_init_result mgos_app_init(void) {
  AM2315_I2C *am2315 = new AM2315_I2C();
  am2315->begin();
  
  mgos_set_timer(10000 /* ms */, MGOS_TIMER_REPEAT, timer_cb, am2315);
  return MGOS_APP_INIT_SUCCESS;
}
#7

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…

#8

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

Wire.setClock(400000L);

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.
Thanks

#9

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.