Creating MH-Z19 Co2 Sensor library in my local

If you are asking a question, please follow this template:

  1. My goal is: to create and port an mhz19 co2 sensor library and use it in my mongoose app.
  2. My actions are: I followed the steps below
  1. The result I see is: I got and error when I build from the mongoose app.

  2. My expectation & question is: I thought creating a library is easy but it’s hard and confusing but I’m still working on this and I want to learn how you guys made some. My question is how can I make this work? what are things that I need to put in my code. my knowledge in C/C++ is not really deep I only know the basics. Any tips would be greatly appreciated. :slight_smile:

If it’s work, I want to contribute it also in mongoose os

Please copy/paste code or from the console log. It’s difficult to read the screenshots, but I see basic C/C++ programming errors.

1 Like
/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/mhz19/src/mgos_mhz19.cpp:4:37: error: 'serial' has not been declared
    4 | void mgos_mhz19_begin(MHZ19 *mhz19, serial) {
      |                                     ^~~~~~
/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/mhz19/src/mgos_mhz19.cpp: In function 'void mgos_mhz19_begin(MHZ19*, int)':
/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/mhz19/src/mgos_mhz19.cpp:5:34: error: return-statement with a value, in function returning 'void' [-fpermissive]
    5 |     if (mhz19 == nullptr) return 0;
      |                                  ^
/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/mhz19/src/mgos_mhz19.cpp:6:25: error: 'serial' was not declared in this scope
    6 |     return mhz19->begin(serial)
      |                         ^~~~~~
/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/mhz19/src/mgos_mhz19.cpp:6:31: error: return-statement with a value, in function returning 'void' [-fpermissive]
    6 |     return mhz19->begin(serial)
      |                               ^
/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/mhz19/src/mgos_mhz19.cpp:6:32: error: expected ';' before '}' token
    6 |     return mhz19->begin(serial)
      |                                ^
      |                                ;
    7 | }
      | ~                               
In file included from /data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/mhz19/src/mgos_mhz19.cpp:1:
/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/mhz19/src/mgos_mhz19.cpp: In function 'void mgos_mhz19_autoCalibration(MHZ19*, bool)':
/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/mhz19/include/mgos_mhz19.h:12:30: error: return-statement with a value, in function returning 'void' [-fpermissive]
   12 | #define MGOS_MHZ19_RES_FAIL -10000
      |                              ^~~~~
/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/mhz19/src/mgos_mhz19.cpp:19:33: note: in expansion of macro 'MGOS_MHZ19_RES_FAIL'
   19 |     if(mhz19 == nullptr) return MGOS_MHZ19_RES_FAIL;
      |                                 ^~~~~~~~~~~~~~~~~~~
/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/modules/mongoose-os/platforms/esp8266/Makefile.build:383: recipe for target '/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/build/objs/mgos_mhz19.cpp.o' failed
make: *** [/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/build/objs/mgos_mhz19.cpp.o] Error 1
make: *** Waiting for unfinished jobs....
make: Leaving directory '/app'
Error: exit status 2

sorry it’s very long

Here’s my codes…

  • deps\mhz19\include\mgos_mhz19.h
/*
   * Arduino MH-Z19 library API wrapper
*/

#include "MHZ19.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define MGOS_MHZ19_RES_FAIL -10000

int mgos_mhz19_getCO2Raw(MHZ19 *mhz19);

#ifdef __cplusplus
}
#endif /* __cplusplus*/
  • deps\mhz19\src\mgos_mhz19.c
#include <stdbool.h>
// NOTE: library init function must be called mgos_LIBNAME_init()
bool mgos_mhz19_init(void) {
  return true;
}
  • deps\mhz19\src\mgos_mhz19.cpp
#include "mgos_mhz19.h"

void mgos_mhz19_begin(MHZ19 *mhz19, serial) {
    if (mhz19 == nullptr) return 0;
    return mhz19->begin(serial)
}

int mgos_mhz19_getCO2Raw(MHZ19 *mhz19) {
    if (mhz19 == nullptr) return MGOS_MHZ19_RES_FAIL;

    return mhz19->getCO2Raw();
}

byte mgos_mhz19_getPWMStatus(MHZ19) {
    return 0;
}

void mgos_mhz19_autoCalibration(MHZ19 *mhz19, bool isON) {
    if(mhz19 == nullptr) return MGOS_MHZ19_RES_FAIL;

    return mhz19->autoCalibration(isON);
}

you can check the github library of the mhz19 sensor that I’m using here: https://github.com/WifWaf/MH-Z19

I’ve edited your posts to be more readable and removed the irrelevant parts. Please use triple backticks to enclose your code/log.

As I said in a previous post, the errors are basic C/C++ programming errors.

E.g:

/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/mhz19/src/mgos_mhz19.cpp: In function 'void mgos_mhz19_begin(MHZ19*, int)':
/data/fwbuild-volumes/2.18.0/apps/zip-iot-virus-index-nodemcu/esp8266/build_contexts/build_ctx_003116900/deps/mhz19/src/mgos_mhz19.cpp:5:34: error: return-statement with a value, in function returning 'void' [-fpermissive]
    5 |     if (mhz19 == nullptr) return 0;
      |                                  ^

You return 0; in a function which is declared to have no return value. The compiler gives you the hint.

This has nothing to do with Mongoose OS, but with C/C++.

1 Like