Skip to content

icm7216/mruby-esp32-i2c-bme280

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mruby-esp32-i2c-bme280

BME280 (I2C) library for mruby-esp32.

This library is a for the BOSCH BME280 digital humidity, pressure and temperature sensor running on the mruby-esp32.

Installation

Add the line below to your esp32_build_config.rb:

  conf.gem :github => 'icm7216/mruby-esp32-i2c-bme280'

Usage

Example of measurement humidity, pressure and temperature by bme280 sensor.

I2C Wiring

   BME280       NodeMCU-32S(ESP32)
      VCC ----> 3v3  (3.3V)
      GND ----> GND
      SCL ----> GPIO 22
      SDA ----> GPIO 21
      CSB xxxx
      SD0 ----> VCC  (I2C Bus address = 0x77)

BME280 I2C Bus address is 0x76, if you connect from SDO to GND.

i2c = I2C.new(I2C::PORT0, scl: 22, sda: 21).init(I2C::MASTER)
bme280 = SENSOR::BME280.new(i2c, 0x76).init

BME280 I2C Bus address is 0x77, if you connect from SDO to VCC. (If the address is omitted, it is set to 0x77.)

i2c = I2C.new(I2C::PORT0, scl: 22, sda: 21).init(I2C::MASTER)
bme280 = SENSOR::BME280.new(i2c).init

In advance, you will need to add several mrbgems to esp32_build_config.rb

  conf.gem :core => "mruby-sprintf"
  conf.gem :core => "mruby-toplevel-ext"
  conf.gem :core => "mruby-struct"
  conf.gem :github => "mruby-esp32/mruby-esp32-i2c"
  conf.gem :github => "mruby-esp32/mruby-esp32-system"
  conf.gem :github => "icm7216/mruby-esp32-i2c-ssd1306"
  conf.gem :github => "iij/mruby-pack"

code

Example 1: single-Shot measurement (for connect test)

Output to the serial console.

include ESP32

i2c = I2C.new(I2C::PORT0, scl: 22, sda: 21).init(I2C::MASTER)
bme280 = SENSOR::BME280.new(i2c)
bme280.init
System.delay(500)

puts "temp: #{'%.2f' % bme280.temperature}"
puts "hum:  #{'%.2f' % bme280.humidity}"
puts "press:#{'%.2f' % bme280.pressure}"

Example 2: repeated measurement with loop

Output to the SSD1306 OLED display.

png

include ESP32

i2c = I2C.new(I2C::PORT0, scl: 22, sda: 21).init(I2C::MASTER)
bme280 = SENSOR::BME280.new(i2c).init
oled = OLED::SSD1306.new(i2c).init.clear
System.delay(500)

def view_data(oled, bme280)
  temperature = "#{'%.2f' % bme280.temperature}"
  humidity =    "#{'%.2f' % bme280.humidity}"
  pressure =    "#{'%.2f' % bme280.pressure}"

  oled.clear
  oled.fontsize = 1
  oled.text(102,  0, " o")
  oled.fontsize = 2
  oled.text(118,  0, "C")
  oled.text(118, 21, "%")
  oled.text(104, 42, "hPa")
  oled.text(62,  0, temperature)
  oled.text(62, 21, humidity)
  oled.text(46, 42, pressure)
  oled.display
end

loop do
  view_data(oled, bme280)
  GC.start  
  System.delay(1000) 
end

using library

Many thanks!

License

MIT

About

BME280 (I2C) library for mruby-esp32

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published