How to run bme280 sensor driver in nodemcu using Javascript

#1
  1. My goal is: to get the temperature data from bme280 sensor.
  2. My actions are: I’ve already added the bme280 library in my mos.yml file
    “- origin: https://github.com/mongoose-os-libs/bme280” and load the “load(‘api_bme280.js’);” I’ve also done mos build and flash. Here’s my actual code:
load('api_timer.js');
load('api_math.js');
load('api_bme280.js');

let myBME = BME280Data.create();

Timer.set(10000 /* milliseconds */ , Timer.REPEAT, function () {
  print('Temperature: ', myBME.readTemperature());
}, null);
  1. The result I see is:
[Nov  9 13:04:48.013]   at init.js:31
[Nov  9 13:04:48.013] MJS callback error: calling non-callable
[Nov  9 13:04:58.013]   at init.js:31
[Nov  9 13:04:58.013] MJS callback error: calling non-callable
  1. My expectation & question is: I want to get temperature data and other data like press and hum, but I’m not sure if my code is correct.
#2

BME280Data is a structure which contains the result of BME280.readAll(data) where data is a BME280Data object.

If you want to read the temperature

load('api_timer.js');
load('api_bme280.js');

// https://github.com/mongoose-os-libs/bme280/blob/4f17b96aa8153c5eb07583dcd0ee669fc4234134/mjs_fs/api_bme280.js#L59-L66
let myBME = BME280.createI2C(0x76); // Have to check the datasheeet of your sensor. Might be 0x76 or 0x77

Timer.set(10000, Timer.REPEAT, function () {
  print('Temperature: ', myBME.readTemp()); // https://github.com/mongoose-os-libs/bme280/blob/4f17b96aa8153c5eb07583dcd0ee669fc4234134/mjs_fs/api_bme280.js#L93-L98
}, null);

To read temperature, humidity and pressure:

load('api_timer.js');
load('api_bme280.js');

// https://github.com/mongoose-os-libs/bme280/blob/4f17b96aa8153c5eb07583dcd0ee669fc4234134/mjs_fs/api_bme280.js#L59-L66
let myBME = BME280.createI2C(0x76);

let myBMEData=BME280Data.create(); // create the data object

Timer.set(10000 , Timer.REPEAT, function () {
  myBME.readAll(myBMEData); // https://github.com/mongoose-os-libs/bme280/blob/4f17b96aa8153c5eb07583dcd0ee669fc4234134/mjs_fs/api_bme280.js#L86-L91
  print('Temperature:', myBMEData.temp(), 'Humidity:', myBMEData.humid(), 'Pressure:', myBMEData.press());
}, null);
1 Like
#3

I try all the code that you provided, the first one is showing Temperature: -128 and the second code show like this:
[Nov 9 18:15:25.979] mgos_bme280.c:184 BMP/BME280 device not found - -2
[Nov 9 18:15:26.014] mgos_mongoose.c:66 New heap free LWM: 26528
[Nov 9 18:15:26.018] mgos_ota_core.c:1329 UID: 0307171199375ef5, license: none
[Nov 9 18:15:36.033] Temperature: 0 Humidity: 0 Pressure: 0
[Nov 9 18:15:46.032] Temperature: 0 Humidity: 0 Pressure: 0

heres: my mos.yml config:

config_schema:
  - ["mqtt.server", "iot.eclipse.org:1883"]
  - ["i2c.enable", true]
  - ["i2c1.sda_gpio", 4]
  - ["i2c1.scl_gpio", 5]


tags:
  - js

filesystem:
  - fs

libs:
  - origin: https://github.com/mongoose-os-libs/boards
  - origin: https://github.com/mongoose-os-libs/js-demo-bundle
  - origin: https://github.com/mongoose-os-libs/rpc-service-config
  - origin: https://github.com/mongoose-os-libs/rpc-service-fs
  - origin: https://github.com/mongoose-os-libs/rpc-uart
  - origin: https://github.com/mongoose-os-libs/wifi
  - origin: https://github.com/mongoose-os-libs/dht
  - origin: https://github.com/mongoose-os-libs/mjs
  - origin: https://github.com/mongoose-os-libs/bme280 
  - origin: https://github.com/mongoose-os-libs/i2c
#4

The default I2C configuration for esp8266 is

        - ["i2c.sda_gpio", "i", 12, {title: "GPIO to use for SDA"}]
        - ["i2c.scl_gpio", "i", 14, {title: "GPIO to use for SCL"}]

You defined the gpios for the i2c1 channel.
If you want to use GPIO4 andGPIO5:

        - ["i2c.sda_gpio", 4]
        - ["i2c.scl_gpio", 5]

Also check your device’s I2C address.

1 Like
#5

I’m so confused now :< i’ts still show the same error:

[Nov 9 18:49:19.057] mgos_bme280.c:184 BMP/BME280 device not found - -2
[Nov 9 18:49:19.074] mgos_mongoose.c:66 New heap free LWM: 32496
[Nov 9 18:49:19.078] mgos_ota_core.c:1329 UID: 0307171199375ef5, license: none
[Nov 9 18:49:29.072] Temperature: -128
[Nov 9 18:49:39.072] Temperature: -128

my init.js code:

load(‘api_timer.js’);
load(‘api_bme280.js’);

let myBME = BME280.createI2C(0x76); // Have to check the datasheeet of your sensor. Might be 0x76 or 0x77

Timer.set(10000, Timer.REPEAT, function () {
print('Temperature: ', myBME.readTemp());
}, null);

mos yml:

config_schema:

  • [“mqtt.server”, “iot.eclipse.org:1883”]
  • [“i2c.enable”, false]
  • [“i2c.sda_gpio”, 4]
  • [“i2c.scl_gpio”, 5]

tags:

  • js

filesystem:

  • fs

libs:

I connected my sensor sck and sd1 to gpio5 and gpio4 pin

#6

Finally It works now! I was able to get the data:
[Nov 9 19:18:04.760] Temperature: 23.250000 Humidity: 63.491211 Pressure: 100297

Thank you very much help sir!!