Skip to content

Instantly share code, notes, and snippets.

@kumekay

kumekay/bme280.rb

Last active Mar 17, 2018
Embed
What would you like to do?
mruby + esp32 + thingspeak @ wroc_love.rb 2018
include ESP32
API_KEY = "****"
SSID = "****"
PASS = ""****"
i2c = I2C.new(I2C::PORT0, scl: 22, sda: 21).init(I2C::MASTER)
bme280 = SENSOR::BME280.new(i2c, 0x76)
bme280.init
puts "Getting ready to start wifi"
wifi = ESP32::WiFi.new
wifi.on_connected do |ip|
puts "Connected: #{ip}"
soc = TCPSocket.open("api.thingspeak.com", 80)
msg = "HEAD /update?field1=#{bme280.pressure}&field2=#{bme280.temperature}&field3=#{bme280.humidity}&api_key=#{API_KEY} HTTP/1.1\r\nHost: api.thingspeak.com\r\n\r\n"
msg.split("\r\n").each do |e|
puts ">>> #{e}"
end
soc.send(msg, 0)
loop do
buf = soc.recv(128, 0)
break if buf.length == 0
print buf
end
# Deep sleep for half a minute
ESP32::System.deep_sleep_for(30 * 1000000)
end
wifi.on_disconnected do
puts "Disconnected"
end
puts "Connecting to wifi"
wifi.connect(SSID, PASS)
while true do
mem = ESP32::System.available_memory() / 1000
puts "Free heap: #{mem}K"
ESP32::System.delay(10 * 1000)
end
MRuby::Build.new do |conf|
toolchain :gcc
[conf.cc, conf.objc, conf.asm].each do |cc|
cc.command = 'gcc'
cc.flags = [%w(-g -std=gnu99 -O3 -Wall -Werror-implicit-function-declaration -Wdeclaration-after-statement -Wwrite-strings)]
end
[conf.cxx].each do |cxx|
cxx.command = 'g++'
cxx.flags = [%w(-g -O3 -Wall -Werror-implicit-function-declaration)]
end
conf.linker do |linker|
linker.command = 'gcc'
linker.flags = [%w()]
linker.libraries = %w(m)
linker.library_paths = []
end
conf.archiver do |archiver|
archiver.command = "ar"
end
conf.gembox 'default'
end
MRuby::CrossBuild.new('esp32') do |conf|
toolchain :gcc
conf.cc do |cc|
cc.include_paths << ENV["COMPONENT_INCLUDES"].split(' ')
cc.flags << '-Wno-maybe-uninitialized'
cc.flags.collect! { |x| x.gsub('-MP', '') }
cc.defines << %w(MRB_HEAP_PAGE_SIZE=64)
cc.defines << %w(MRB_USE_IV_SEGLIST)
cc.defines << %w(KHASH_DEFAULT_SIZE=8)
cc.defines << %w(MRB_STR_BUF_MIN_SIZE=20)
cc.defines << %w(MRB_GC_STRESS)
cc.defines << %w(ESP_PLATFORM)
end
conf.cxx do |cxx|
cxx.include_paths = conf.cc.include_paths.dup
cxx.flags.collect! { |x| x.gsub('-MP', '') }
cxx.defines = conf.cc.defines.dup
end
conf.bins = []
conf.build_mrbtest_lib_only
conf.disable_cxx_exception
conf.gem :core => "mruby-print"
conf.gem :core => "mruby-compiler"
conf.gem :core => "mruby-toplevel-ext"
conf.gem :core => "mruby-struct"
conf.gem :github => 'iij/mruby-pack'
conf.gem :github => "mruby-esp32/mruby-esp32-i2c"
conf.gem :github => 'icm7216/mruby-esp32-i2c-bme280'
conf.gem :github => "mruby-esp32/mruby-esp32-system"
conf.gem :github => "mruby-esp32/mruby-esp32-wifi"
conf.gem :github => 'mruby-esp32/mruby-socket', :branch => 'esp32'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment