1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LOLIN ESP32 OLED ボードの MicroPython で 温度、湿度、気圧の測定

Posted at

清水@ロボット部 (日本Androidの会 秋葉原支部)です。

ロボット部 Googleグループ
https://groups.google.com/forum/?hl=ja#!forum/robot-android-group-japan-akb


WEMOS社 LOLIN32 ESP32 OLED ボード上の MicroPython で、 温度、湿度、気圧センサの値を、OLED(organic light-emitting diode)に表示してみました。

センサー

GAOHOUGY-BME280 温度、湿度、気圧センサ 1.8-5V
image.png
このセンサは小型で見た目が良い。測定精度が高いところも良いです。

温度:-40〜+ 85℃
湿度:0〜100%
圧力:300-1100hPa
温度:0.01℃
湿度:0.008%
圧力:0.18Pa
温度:+ 1℃
湿度:+ 3%
圧力:±1Pa

センサーテストプログラム (コンソール出力)

testBME280.py
# BME280(Temperature,Humidity,Pressure) Sensor
# MicroPython on Lolin ESP32 OLED 128x64 16x6(0..50)

import machine, time
import bme280

i2cBME280 = machine.I2C(scl=machine.Pin(0), sda=machine.Pin(2))
bme = bme280.BME280(i2c=i2cBME280)
for i in range(3):
    print(bme.values)
    time.sleep(1)
コンソール出力 (温度、湿度、気圧)
('14.65C', '1010.97hPa', '35.86%')
('14.45C', '1011.02hPa', '37.08%')
('14.45C', '1010.95hPa', '36.91%')

プログラム(OLED出力)実行結果

右端にセンサーがあります。中央にあるディスプレイの表示は上から順に次の値を表示しています。

  • 温度 ℃
  • 湿度 %
  • 気圧 hPa
  • 時刻
  • 日付

image.png

プログラム(OLED出力)

mainBME280.py
# BME280(Temperature,Humidity,Pressure) Sensor
# MicroPython on Lolin ESP32 OLED 128x64 16x6(0..50)

ssid = "777"; password =  "7777777777"
ntp_host = "ntp.nict.jp"
# (datetime.date(2000, 1, 1) - datetime.date(1900, 1, 1)).days * 24 * 60 * 60
NTP_DELTA = 3155673600

import machine, time, ssd1306, bme280
import network, socket, struct


i2cBME280 = machine.I2C(scl=machine.Pin(0), sda=machine.Pin(2))
bme = bme280.BME280(i2c=i2cBME280)

i2cSSD1306 = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5))
oled = ssd1306.SSD1306_I2C(128, 64, i2cSSD1306)

oled.fill(0)
#         '1234567890123456'  0_temperature,1_humidity,2_pressure,3_time,4_date
oled.text('BME280 on Python', 0, 50)

station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False: pass
#oled.text(station.ifconfig()[0], 0, 0,1)
#oled.show()

NTP_QUERY = bytearray(48)
NTP_QUERY[0] = 0x1b
addr = socket.getaddrinfo(ntp_host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
s.close()
val = struct.unpack("!I", msg[40:44])[0]
ntpSec = val - NTP_DELTA  + (9*60*60)
deltaSec = ntpSec - time.time() 
saveVal = [""] * 6

def f2(i): return '{:0>2}'.format(i)    
def disp(pos, stringVal):   # pos:0..5
    global saveVal
    oled.text(saveVal[pos], 0, pos * 10, 0)
    saveVal[pos] = stringVal
    oled.text(saveVal[pos], 0, pos * 10)

while True:

    sensorStringTPH=bme.values
    nowSec = time.time() + deltaSec
    now = time.localtime(nowSec)
    
    disp(0, sensorStringTPH[0])
    disp(1, sensorStringTPH[2])
    disp(2, sensorStringTPH[1])
    disp(3, '%s:%s:%s' % (f2(now[3]),f2(now[4]),f2(now[5])))
    disp(4, '%s/%s/%s' % (str(now[0]),f2(now[1]),f2(now[2])))

    oled.show()
    time.sleep(1)

参考

Driver for the Bosch BME280 for use with MicroPython on ESP8266 boards
https://github.com/catdog2/mpy_bme280_esp8266

LOLIN ESP32 OLED ボードの MicroPython で NTP時刻表示
https://qiita.com/tshimizu8/items/a7c189612a9ba0a5284b

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?